Star Citizen Wiki Modul:Clothing
Wir laden dich herzlich auf den Star Citizen Wiki Discord Server ein! Du kannst uns auch auf unserem neuen YouTube-Chanel finden!

Modul:Clothing

From Star Citizen Wiki

Modulabhängigkeiten

Modulinfo

Dieses Modul setzt die Vorlage:Kleidung um. Anweisungen zur Verwendung findest du auf der Vorlagenseite.


local translation = mw.getCurrentFrame():callParserFunction{name='#translation', args="1"}
local Clothing = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local objectData = mw.loadData( 'Module:Clothing/Data' )

-- Extensions
local common = require( 'Module:Common' )
local armor = require( 'Module:CharArmor' )
local TNT = require( 'Module:TNT' )


--- Add manual smw data
function methodtable.addManual( t )
    if t.frameArgs == nil then
        return
    end

    local setObj = {
        [ 'Name' ]                                = t.frameArgs.Name                                    or nil,
        [ 'Beschreibung' ]                        = t.frameArgs.Beschreibung                            or nil,
        [ 'Hersteller' ]                          = t.frameArgs[ 'Hersteller' ]                         or nil,
        [ 'Typ' ]                                 = t.frameArgs[ 'Typ' ]                                or nil,
        [ 'Ist Basisversion' ]                    = t.frameArgs[ 'Ist Basisversion' ]                   or nil,
        [ 'Basisversion UUID' ]                   = t.frameArgs[ 'Basisversion UUID' ]                  or nil,
        [ 'SP' ]                                  = t.frameArgs[ 'SP' ]                                 or nil,
        [ 'Temperaturresistenz Minimal' ]         = t.frameArgs[ 'Temperaturresistenz Minimal' ]        or nil,
        [ 'Temperaturresistenz Maximal' ]         = t.frameArgs[ 'Temperaturresistenz Maximal' ]        or nil,
        [ 'Spielversion' ]                        = t.frameArgs[ 'Spielversion' ]                       or nil,
    }

    mw.smw.set( setObj )
end


--- Request Api Data
--- Using current page name
--- @return table
function methodtable.getApiDataForCurrentPage( t )
    local name = t.frameArgs[ 'uuid' ] or t.frameArgs[ 'name' ] or require('Module:Localized'):getMainTitle()

    local json = mw.text.jsonDecode( mw.ext.Apiunto.get_raw( 'char/clothing/' .. name, {
        locale = '',
        include = {
            'shops.items',
        },
    } ) )

    common.checkApiResponse( json, true, false )

    t.apiData = json[ 'data' ]

    return t.apiData
end


--- Request the first price from smw
--- @param name string
--- @return table
function methodtable.getPrice( t, name )
    local query = {
        '[[Name::' .. name .. ']][[Kaufbar::1]]',
        '?Preis#-p0=price',
        'mainlabel=-',
        'limit=1'
    }

    return mw.smw.ask( query )
end


--- Queries the SMW Store
--- @return table
function methodtable.getSmwData( t )
    -- Cache multiple calls
    if t.smwData ~= nil then
        return t.smwData
    end

    -- name from args or current page
    local queryName = t.frameArgs[ 'name' ] or require( 'Module:Localized' ):getMainTitle()

    local data = mw.smw.ask( {
        '[[' .. queryName .. ']][[Hersteller::+]]',
        '?#-=page',
        '?Name#-=name',
        '?Hersteller#-=manufacturer',
        '?Beschreibung=description', '+lang=' .. common.getLocaleForPage(),
        '?SP=sp',
        '?Typ#-=type', '+lang=' .. common.getLocaleForPage(),
        '?Typ#-=type_de', '+lang=de',
        '?Temperaturresistenz#-n=tempresist',
        '?Länge=length',
        '?Breite=width',
        '?Höhe=height',
        '?Volumen=volume',
        '?Spielversion#-=version',
        'mainlabel=-'
    } )

    if data == nil or data[ 1 ] == nil then
        -- error( 'Seite "' .. queryName .. '" besitzt keine semantischen Daten.', 0 )
        return TNT.format( 'I18n/Module:Clothing', 'msg_smw_loading' )
    end

    t.smwData = data[ 1 ]

    t.smwData.price = t:getPrice( queryName )
    if t.smwData.price ~= nil then
        t.smwData.price = t.smwData.price[ 1 ].price
    end

    return t.smwData
end


--- Semantic Properties
--- @return table SMW Result
function methodtable.setSemanticProperties( t )
    -- Api Error, don't set anything
    if t.apiData == nil or mw.title.getCurrentTitle().isSubpage then
        return
    end

    local manufacturerName = t.apiData.manufacturer or nil
    if manufacturerName ~= nil then
        manufacturerName = mw.ustring.gsub( manufacturerName, '%[PH%]', '' )
    end

    if manufacturerName == '@LOC_PLACEHOLDER' or manufacturerName == '@LOC PLACEHOLDER' or manufacturerName == 'Unknown Manufacturer' then
        manufacturerName = 'Unbekannter Hersteller'
    end

    local setObj = {
        [ 'UUID' ]              = t.apiData.uuid        or nil,
        [ 'Name' ]              = t.apiData.name        or nil,
        [ 'Beschreibung' ]      = common.mapTranslation( t.apiData.description or nil ),
        [ 'Hersteller' ]        = manufacturerName,
        [ 'SP' ]                = armor.formatCarryingCapacity( t.apiData.carrying_capacity ),
        [ 'Typ' ]               = common.mapTranslation( Clothing.translateType( t.apiData.clothing_type or nil ) ),
        [ 'Temperaturresistenz' ] = {
            armor.formatTemperature( t.apiData.resistances.temperature.min ),
            armor.formatTemperature( t.apiData.resistances.temperature.max ),
        },
        [ 'Länge' ]        = common.formatNum( t.apiData.volume.length or nil, nil ),
        [ 'Breite' ]       = common.formatNum( t.apiData.volume.width or nil, nil ),
        [ 'Höhe' ]         = common.formatNum( t.apiData.volume.height or nil, nil ),
        [ 'Volumen' ]      = common.formatNum( t.apiData.volume.volume or nil, nil ),
        [ 'Spielversion' ] = t.apiData.version or nil,
    }

    if type( t.apiData.base_model ) == 'table' then
        setObj[ 'Ist Basisversion' ] = 0
        setObj[ 'Basisversion UUID' ] = t.apiData.base_model.uuid
        setObj[ 'Basisversion' ] = t.apiData.base_model.name
    else
    	setObj['Ist Basisversion'] = 1
    end

    local result = mw.smw.set( setObj )

    local commodity = require( 'Module:Commodity' ):new()
    commodity:addShopData( t.apiData )

    return result
end

--- Entrypoint for {{#seo:}}
function methodtable.setSeoData( t )
    if t.currentFrame == nil then
        error( 'No frame set. Call "setFrame" first.', 0 )
    end

    local data = t:getSmwData()

    if nil == data.manufacturer then
        -- Faulty SMW data, don't call #seo
        return
    end

	local manufacturer = require( 'Module:Hersteller' )

    local code = manufacturer.getCodeFromName( data.manufacturer )
    if code == data.manufacturer then
    	code = ''
	end

    require( 'Module:SEO' ).set(
        TNT.format( 'I18n/Module:Clothing', 'seo_section' ),
        data.page,
        table.concat({
            data.name,
            data.manufacturer,
            t.currentFrame:preprocess( '{{SITENAME}}' )
        }, ' - '),
        'replace',
        {
            data.name,
            data.type,
            data.manufacturer,
            code,
        },
        nil,
        t.frameArgs[ 'image' ],
        data.page
    )
end

--- Creates the infobox
function methodtable.getInfoBox( t )
    local data = t:getSmwData()

	if type( data ) == 'string' then
		return string.format( '<p class="hatnote">%s</p>', data )
	end

	local manufacturer = require( 'Module:Hersteller' )

    if nil == data.manufacturer then
        data.manufacturer = TNT.format( 'I18n/Module:Clothing', 'unknown_manufacturer' )
    end

    local code = manufacturer.getCodeFromName( data.manufacturer )
    if code == data.manufacturer then
    	code = ''
	else
		code = string.format( ' (%s)', code )
	end

    table.insert( t.categories, '[[Category:' .. data.manufacturer .. translation .. '|' .. data.name .. ' ]]' )

    if data.type_de ~= nil and data.type_de ~= 'Unbekannter Typ' then
        table.insert( t.categories, string.format('[[Kategorie:%s|%s]]', data.type_de .. translation, data.name ) )
    else
    	table.insert( t.categories, string.format('[[Kategorie:Kleidung' .. translation .. '|%s]]', data.name ) )
    end

    -- Set Title
    common.setDisplayTitle( t.currentFrame, data.name )

    local box = require( 'Module:Infobox' ).create( {
        bodyClass = 'floatright',
        allowReplace = false,
        removeEmpty = true,
        emptyString = '-',
        placeholderImage = 'Platzhalter Rüstung.webp',
    } )

	local nameNormalized, _ = mw.ustring.gsub( data.name, "[^%w-]", ' ' )
	nameNormalized, _ = mw.ustring.gsub( nameNormalized, "%s+", ' ' )

    box:addImage( common.getImage( {
        t.frameArgs[ 'image' ],
        nameNormalized .. '.jpg',
    } ), {
        [ 'alternativtext' ] = data.name,
        'rahmenlos',
        '600px'
    } )

    local source = t.currentFrame:extensionTag{
        name = 'ref',
        content = TNT.format( 'I18n/Module:Clothing', 'msg_version_info', data.version, '[https://github.com/StarCitizenWiki/scunpacked-data GitHub]' )
    }

    box:addTitle( data.name )

    box:addRow(
        TNT.format( 'I18n/Module:Clothing', 'lbl_manufacturer' ),
        '[[' .. data.manufacturer .. ']]' .. code .. source,
        nil,
        'col2'
    )

    local tempMinRes, tempMaxRes = Clothing.temperatureResistanceToText( data.tempresist )

    local clothingType = data.type
    if clothingType ~= nil then
        clothingType = string.format('[[:Kategorie:%s|%s]]', data.type_de .. translation, clothingType)
    end

    box:addRow( TNT.format( 'I18n/Module:Clothing', 'lbl_type' ), clothingType or '-', nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:Clothing', 'lbl_price' ), data.price or TNT.format( 'I18n/Module:Clothing', 'msg_cant_buy' ), nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:Clothing', 'lbl_carrying_capacity' ), data.sp or '-', nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:Clothing', 'lbl_weight' ), data.volume or '-', nil, 'col2' )

       :addHeader( TNT.format( 'I18n/Module:Clothing', 'lbl_temp_resistance' ) )
       :addRow( TNT.format( 'I18n/Module:Clothing', 'lbl_temp_min' ), tempMinRes or '-', nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:Clothing', 'lbl_temp_max' ), tempMaxRes or '-', nil, 'col2' )

    if t.frameArgs ~= nil then
        box:addRowsFromArgs( t.frameArgs, '!' )
    end

    return tostring( box )
end


--- Set the frame and load args
--- @param frame table
function methodtable.setFrame( t, frame )
    t.currentFrame = frame
    t.frameArgs = require( 'Module:Arguments' ).getArgs( frame )
end


--- Get categories
function methodtable.getCategories( t )
    return tostring( table.concat( t.categories ) )
end


--- Save Api Data to SMW store
function methodtable.saveApiData( t )
    if t.currentFrame == nil then
        error( 'No frame set. Call "setFrame" first.', 0 )
    end

    local data = t:getApiDataForCurrentPage()

    t:setSemanticProperties()

    return data
end


--[[

Clothing Methods

]]--

--- @param type string
--- @return table
function Clothing.translateType( type )
    return {
        de_DE = objectData.typeTranslations[ type ] or type,
        en_EN = type,
    }
end


--- Returns temperature resistances as strings containing °C
--- @param resTable table containing an arbitrary amount of temperatures
--- @return string, string
function Clothing.temperatureResistanceToText( resTable )
    local tempMinRes = math.huge
    local tempMaxRes = -math.huge

    if resTable ~= nil and type( resTable ) == 'table' then
        for _, res in pairs( resTable ) do
            res = common.toNumber( res, 0 )

            if res < tempMinRes then
                tempMinRes = res
            end

            if res > tempMaxRes then
                tempMaxRes = res
            end
        end
    end

    if tempMinRes == math.huge then
        tempMinRes = '-'
    else
        tempMinRes = tempMinRes .. '°C'
    end

    if tempMaxRes == -math.huge then
        tempMaxRes = '-'
    else
        tempMaxRes = tempMaxRes .. '°C'
    end

    return tempMinRes, tempMaxRes
end


--- Template entry
function Clothing.main( frame )
    local instance = Clothing:new()
    instance:setFrame( frame )

	if not mw.title.getCurrentTitle().isSubpage then
	    if instance.frameArgs[ 'Manuell' ] ~= nil then
	        instance:addManual()
	    else
	        instance:saveApiData()
	    end
    end

    instance:setSeoData()

    return tostring( instance:getInfoBox() ) .. instance:getCategories()
end


--- New Instance
function Clothing.new( self, name )
    local instance = {
        categories = {},
        frameArgs = {
            [ 'name' ] = name
        }
    }

    setmetatable( instance, metatable )

    return instance
end

return Clothing
Cookies help us deliver our services. By using our services, you agree to our use of cookies.