Modul:Missile

Aus Steel Beasts Wiki
Version vom 3. September 2023, 08:02 Uhr von Dr.Thodt (Diskussion | Beiträge) (1 Version importiert)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

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

require( 'strict' )

local Missile = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

-- Extensions
local infobox = require( 'Module:InfoboxNeue' )
local manufacturer = require( 'Module:Hersteller' )
local common = require( 'Module:Common' )
local api = require( 'Module:Common/Api' )
local TNT = require( 'Module:Translate' ):new()
local log = require( 'Module:Log' )
local item = require( 'Module:Item' )
local commodity = require( 'Module:Commodity' )
local data = mw.loadJsonData( 'Module:Missile/data.json' )


--- @param class string
--- @return string
local function translateClass( class )
    return {
        de_DE = ( data.classTranslations[ class ] or class ),
        en_EN = class,
    }
end


--- @param damage string
--- @return string
local function translateDamage( damage )
    return {
        de_DE = ( data.damageTranslations[ damage ] or damage ),
        en_EN = damage,
    }
end


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


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

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

    api.checkResponseStructure( json, true, false )

    self.apiData = json[ 'data' ]

    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 require( 'Module:Localized' ):getMainTitle()

    local query = {
        '[[' .. queryName .. ']]',
        '?#-=page',
        '?Name#-=name',
        '?Größe#-=size',
        '?Typ=type', '+lang=' .. common.getLocaleForPage(),
        '?Typ=type_de', '+lang=de',
        '?Hersteller#-=manufacturer',
        '?Beschreibung=description', '+lang=' .. common.getLocaleForPage(),
        '?Signaltyp=signal_type', '+lang=' .. common.getLocaleForPage(),
        '?Signaltyp=signal_type_de', '+lang=de',
        '?Erfassungszeit#-=lock_time',
        '?Schaden pro Schuss#-=dps',
        '?Spielversion#-=version',
        'mainlabel=-'
    }

    local data = mw.smw.ask( query )

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

    self.smwData = data[ 1 ]

    query = {
        '[[-Has subobject::' .. queryName .. ']][[Schaden::+]]',
        '?Schadensart#-=damage_type',
        '?Schaden#-=damage',
        'mainlabel=-'
    }

    self.smwData.damages = mw.smw.ask( query )

    return self.smwData
end


--- Base Properties that are shared across all Vehicles
--- @return table SMW Result
function methodtable.setSemanticProperties( self )
    if mw.title.getCurrentTitle().isSubpage then
        return
    end

    -- Api Error, don't set anything
    if self.apiData == nil or self.apiData.missile == nil or type( self.apiData.missile ) ~= 'table' then
        return
    end

    local missile = self.apiData.missile

    local missileType = self.apiData.sub_type or nil
    if missileType ~= nil then
        missileType = {
            missileType .. '@de',
            missileType .. '@en',
        }
    end

	local item = item:new( '', self.apiData )
    local setData = item:makeSmwObject()
    
    setData[ 'Signaltyp' ]          = common.mapTranslation( translateType( missile.signal_type or nil ) )
    setData[ 'Erfassungszeit' ]     = common.formatNum( missile.lock_time or nil, nil )
    setData[ 'Schaden pro Schuss' ] = common.formatNum( missile.damage_total or nil, nil )
    setData[ 'Typ' ]                = missileType

    local result = mw.smw.set( setData )

    local weaponSubobjects = require( 'Module:WeaponSubobjects' ):new()
    weaponSubobjects:setSemanticProperties( missile )

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

    return result
end


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

	if self.lang == 'de' then
	    local cat = 'Rakete'
	    if smwData.type_de == 'Torpedo' then
	        cat = 'Torpedo'
		end

    	table.insert( self.categories, '[[Category:' .. smwData.manufacturer .. '|' .. smwData.name .. ' ]]' )
	    table.insert( self.categories, '[[Category:' .. cat .. '|' .. smwData.name .. ' ]]' )
	
	    if smwData.signal_type ~= nil then
	        table.insert( self.categories, '[[Category:' .. cat .. ' (' .. smwData.signal_type_de .. ')' .. '|' .. smwData.name .. ' ]]' )
	    end
    else
    	table.insert( self.categories, '[[Category:' .. smwData.name ..'|' .. smwData.name .. ' ]]' )
    end
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 type( data ) == 'string' then
        return
	end

    local desc = table.concat( {
        data.name,
        'vom Hersteller',
        data.manufacturer
    }, ' ' )

    if self.frameArgs[ 'description' ] == nil or #self.frameArgs[ 'description' ] == 0 then
        if data.description ~= nil then
            desc = data.description
        end
    else
        desc = self.frameArgs[ 'description' ]
    end
    
    local signalType = data.signal_type
    if type( signalType ) == 'table' then
        signalType = table.concat( signalType, ' ' )
    end

    -- Call to {{#seo:}}
    mw.ext.seo.set{
        author = self.currentFrame:preprocess( '{{SERVER}}/Benutzer:{{urlencode:{{REVISIONUSER}}|WIKI}}' ),
        section = TNT.formatInLanguage( self.lang, 'Module:Missile/i18n.json', 'seo_section' ),
        url = tostring( mw.uri.fullUrl( data.page ) ),
        title = table.concat({
            data.name,
            data.manufacturer,
            self.currentFrame:preprocess( '{{SITENAME}}' )
        }, ' - '),
        title_mode = 'replace',
        keywords = table.concat({
            data.name or '',
            signalType,
            data.manufacturer or '',
            manufacturer.getCodeFromName( data.manufacturer ) or '',
            TNT.formatInLanguage( self.lang, 'Module:Missile/i18n.json', 'seo_section' ),
        }, ', '),
        description = desc,
        image= self.frameArgs[ 'image' ] or '',
        image_alt = desc,
        locale = 'de_DE',
        type = 'article',
    }
end


--- Creates the infobox
--- TODO: This is a mess ;_;
function methodtable.getInfoBox( self )
    local data = self:getSmwData()

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

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

    local box = infobox:new( {
        removeEmpty = true,
        emptyString = '-',
        placeholderImage = 'Platzhalter Waffen.webp',
    } )

    -- Image
    box:renderImage( common.getImage( {
        self.frameArgs[ 'image' ]
    } ) )

    box:renderHeader( {
    	title = data.name,
    	subtitle = TNT.formatInLanguage( self.lang, 'Module:Common/i18n.json', 'lbl_manufacturer' ) .. api.getVersionInfoRef( data.version, self.lang )
    } )

    local signalType = data.signal_type
    if signalType ~= nil then
        signalType = string.format('[[:Kategorie:%s|%s]]', data.signal_type_de, signalType)
    end

	box:renderSection( {
		col = 2,
		content = {
			box:renderItem( TNT.formatInLanguage( self.lang, 'Module:Missile/i18n.json', 'lbl_signal_type' ), signalType ),
			box:renderItem( TNT.formatInLanguage( self.lang, 'Module:Missile/i18n.json', 'lbl_size' ), string.format( 'S%d', data.size or 0 ) ),
			box:renderItem( TNT.formatInLanguage( self.lang, 'Module:Missile/i18n.json', 'lbl_lock_time' ), data.lock_time ),
			box:renderItem( TNT.formatInLanguage( self.lang, 'Module:Missile/i18n.json', 'lbl_damage' ), data.dps ),
		}
	} )

    --[[
        Missile Damages
    ]]--
    if data.damages ~= nil and type( data.damages ) == 'table' then
    	local section = {}

        for _, damage in pairs( data.damages ) do
            local dmg = damage.damage or nil
            if dmg ~= nil then
                dmg = string.format("%.2f", dmg)
            else
                dmg = ' - '
            end

            table.insert( section, box:renderItem( TNT.formatInLanguage( self.lang, 'Module:Common/i18n.json', 'lbl_type' ), damage.damage_type ) )
            table.insert( section, box:renderItem( TNT.formatInLanguage( self.lang, 'Module:Missile/i18n.json', 'lbl_damage' ), dmg ) )
        end
        
        box:renderSection( {
        	title = TNT.formatInLanguage( self.lang, 'Module:Missile/i18n.json', 'lbl_damage_types' ),
        	col = 2,
        	content = section
        } )
    end

    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


--- 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


--- Template entry
function Missile.main( frame )
    local instance = Missile: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()
    instance:setCategories( instance:getSmwData() )

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

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

    setmetatable( instance, metatable )

    return instance
end

return Missile