Modul:WeaponAttachment: Unterschied zwischen den Versionen

Aus Steel Beasts Wiki
sc>FoXFTW
KKeine Bearbeitungszusammenfassung
 
K (1 Version importiert)
 

Aktuelle Version vom 3. September 2023, 08:02 Uhr

Die Dokumentation für dieses Modul kann unter Modul:WeaponAttachment/Doku erstellt werden

local WeaponAttachment = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

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

-- Extensions
local common = require( 'Module:Common' )
local api = require( 'Module:Common/Api' )
local localized = require( 'Module:Localized' )
local TNT = require( 'Module:Translate' ):new()
local log = require( 'Module:Log' )
local item = require( 'Module:Item' )
local commodity = require( 'Module:Commodity' )

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

    local setObj = {
        [ 'Name' ]                                = self.frameArgs.Name                                    or nil,
        [ 'Beschreibung' ]                        = self.frameArgs.Beschreibung                            or nil,
        [ 'Größe' ]                               = self.frameArgs[ 'Größe' ]                              or nil,
        [ 'Hersteller' ]                          = self.frameArgs[ 'Hersteller' ]                         or nil,
        [ 'Position' ]                            = self.frameArgs[ 'Position' ]                        or nil,
        [ 'Spielversion' ]                        = self.frameArgs[ 'Spielversion' ]                       or nil,
    }

    mw.smw.set( setObj )
end

--- Request Api Data
--- Using current subpage name
--- @return table
function methodtable.getApiDataForCurrentPage( self )
    local name = self.frameArgs[ 'uuid' ] or self.frameArgs[ 'name' ] or mw.title.getCurrentTitle().text

    local json = mw.text.jsonDecode( mw.ext.Apiunto.get_raw( 'v2/weapon-attachments/' .. name, {
        include = {
        	'shops.items',
        },
    } ) )

    api.checkResponseStructure( json, true, false )

    self.apiData = json[ 'data' ]
    self.apiData = api.makeAccessSafe( self.apiData )

    return self.apiData
end


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

    -- name from args or current page
    local queryName = self.frameArgs[ 'name' ] or localized.getMainTitle()

    local data = mw.smw.ask( {
        '[[Name::' .. queryName .. ']][[Hersteller::+]]',
        '?#-=page',
        '?Name#-',
        '?Größe#-',
        '?Hersteller#-',
        '?Beschreibung', '+lang=' .. common.getLocaleForPage(),
        '?Kapazität#-=capacity',
        '?Vergrößerungsfaktor#-=magnification',
        '?Typ=type', '+lang=' .. common.getLocaleForPage(),
        '?Typ=type_de', '+lang=de',
        '?Position=position', '+lang=' .. common.getLocaleForPage(),
        '?Position=position_de', '+lang=de',
        '?Länge',
        '?Breite',
        '?Höhe',
        '?Volumen',
        '?Spielversion#-=version',
        'mainlabel=-'
    } )

    if data == nil or data[ 1 ] == nil then
        return TNT.format( 'Module:Common/i18n.json', 'msg_smw_loading' )
    end

    self.smwData = data[ 1 ]

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

	-- TODO: FIX!
	if type( self.smwData.Hersteller ) == 'table' then
		self.smwData.Hersteller = self.smwData.Hersteller[ 1 ]
	end

    return self.smwData
end

--- Base Properties that are shared across all Vehicles
--- @return table SMW Result
function methodtable.setSemanticProperties( self )
    -- Api Error, don't set anything
    if self.apiData == nil then
        return
    end

	local attachType = (self.apiData.iron_sight and self.apiData.iron_sight.type) or
					   (self.apiData.barrel_attach and self.apiData.barrel_attach.type) or 
					   (self.apiData.personal_weapon_magazine and self.apiData.personal_weapon_magazine.type) or 
					   self.apiData.sub_type or nil

    local item = item:new( '', self.apiData )

    local setObj = item:makeSmwObject()

    setObj[ 'Typ' ]                 = api.mapTranslation( WeaponAttachment.translateType( attachType ) )
    setObj[ 'Position' ]            = api.mapTranslation( WeaponAttachment.translatePosition( self.apiData.position or nil ) )
    setObj[ 'Vergrößerungsfaktor' ] = self.apiData:get( 'iron_sight.magnification' )
    setObj[ 'Kapazität' ] = self.apiData:get( 'personal_weapon_magazine.max_ammo_count' )

    local result = mw.smw.set( setObj )

    commodity:new():addShopData( self.apiData )

    return result
end

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

    local data = self:getSmwData()

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

    require( 'Module:SEO' ).set(
        TNT.format( 'Module:WeaponAttachment/i18n.json', 'seo_section' ),
        data.page,
        table.concat({
            data.Name,
            data.Hersteller,
            self.currentFrame:preprocess( '{{SITENAME}}' )
        }, ' - '),
        'replace',
        {
            data.Name,
            data.type or '',
            data.Hersteller,
            require( 'Module:Hersteller' ).getCodeFromName( data.Hersteller ),
            TNT.format( 'Module:WeaponAttachment/i18n.json', 'seo_section' ),
        },
        nil,
        self.frameArgs[ 'image' ],
        data.page
    )
end

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

	if type( data ) == 'string' then
		return log.info( data )
	end

    if nil == data.Hersteller then
        data.Hersteller = 'Unbekannter Hersteller'
    end

    -- Set Title
    common.setDisplayTitle( self.currentFrame, data.Name )

    local box = require( 'Module:InfoboxNeue' ):new( {
        removeEmpty = true,
        emptyString = '-',
        placeholderImage = 'Platzhalter Rüstung.webp',
    } )

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

    box:renderImage( common.getImage( {
        self.frameArgs[ 'image' ],
        nameNormalized .. '.jpg',
    } ) )

    box:renderHeader( {
    	title = data.Name,
    	subtitle = '[[' .. data.Hersteller .. ']]' .. api.getVersionInfoRef( data.version, self.lang ),
    } )

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

	table.insert( section, box:renderItem( TNT.format( 'Module:Common/i18n.json', 'lbl_type' ), attachType or '-' ) )
	table.insert( section, box:renderItem( TNT.format( 'Module:WeaponAttachment/i18n.json', 'lbl_position' ), data.position or '-' ) )
	table.insert( section, box:renderItem( TNT.format( 'Module:Common/i18n.json', 'lbl_price' ), data.price or TNT.format( 'Module:Common/i18n.json', 'txt_cant_buy' ) ) )
	table.insert( section, box:renderItem( TNT.format( 'Module:Common/i18n.json', 'lbl_weight' ), data[ 'Volumen' ] or '-' ) )

	if data.capacity ~= nil then
		table.insert( section, box:renderItem( TNT.format( 'Module:WeaponAttachment/i18n.json', 'lbl_capacity' ), data.capacity or '-' ) )
	end

	if data.magnification ~= nil then
		table.insert( section, box:renderItem( TNT.format( 'Module:WeaponAttachment/i18n.json', 'lbl_magnification' ), data.magnification or '-' ) )
	end
	
	box:renderSection( {
		col = 2,
		content = section,
	} )

    return tostring( box )
end


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


--- Sets the main categories for this object
function methodtable.setCategories( self, smwData )
	if smwData.Hersteller == nil then
		table.insert( self.categories, '[[Category:Seiten mit Skriptfehlern]]' )
		return
	end

	if self.lang == 'de' then
	    table.insert( self.categories, '[[Category:' .. smwData.Hersteller .. '|' .. smwData.Name .. ' ]]' )
	
	    if smwData.type_de ~= nil then
	        table.insert( self.categories, string.format('[[Kategorie:%s|%s]]', smwData.type_de, smwData.Name ) )
	    else
	    	table.insert( self.categories, string.format('[[Kategorie:Waffenbefestigung|%s]]', smwData.Name ) )
	    end
    else
    	table.insert( self.categories, '[[Category:' .. smwData.Name ..'|' .. smwData.Name .. ' ]]' )
    end
end


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


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

    local data = self:getApiDataForCurrentPage()

    self:setSemanticProperties()

    return data
end


--[[

Attachment Methods

]]--

--- @param class string
--- @return table
function WeaponAttachment.translateType( class )
    if objectData.attachmentTypeTranslations[ class ] == nil then
        return {
            de_DE = class,
            en_EN = class
        }
    end

    return objectData.attachmentTypeTranslations[ class ]
end

--- @param type string
--- @return table
function WeaponAttachment.translatePosition( type )
    return {
        de_DE = objectData.attachmentPositionTranslations[ type ] or type,
        en_EN = type
    }
end

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

	if not localized.isLanguagePage() then
	    if instance.frameArgs[ 'Manuell' ] ~= nil then
	        instance:addManual()
	    else
	        instance:saveApiData()
	    end
    end

    instance:setSeoData()
    instance:setCategories( instance:getSmwData() )

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

--- New Instance
function WeaponAttachment.new( self, name )
    local instance = {
        categories = {},
        frameArgs = {
            [ 'name' ] = name
        },
        lang = require( 'Module:Template translation' )._getLanguageSubpage( mw.title.getCurrentTitle() )
    }

    setmetatable( instance, metatable )

    return instance
end

return WeaponAttachment