Modul:VehicleWeapon

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:VehicleWeapon/Doku erstellt werden

local VehicleWeapon = {}

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.loadData( 'Module:VehicleWeapon/Data' )

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

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


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

    local setObj = {
        [ 'Name' ]                    = self.frameArgs[ 'Name' ] or nil,
        [ 'Größe' ]                   = self.frameArgs[ 'Größe' ] or nil,
        [ 'Hersteller' ]              = self.frameArgs[ 'Hersteller' ] or nil,
        [ 'Beschreibung' ]            = self.frameArgs[ 'Beschreibung' ] or nil,
        [ 'Waffentyp' ]               = self.frameArgs[ 'Waffentyp' ] or nil,
        [ 'Waffenklasse' ]            = self.frameArgs[ 'Waffenklasse' ] or nil,
        [ 'Reichweite' ]              = self.frameArgs[ 'Reichweite' ] or nil,
        [ 'Mündungsgeschwindigkeit' ] = self.frameArgs[ 'Mündungsgeschwindigkeit' ] or nil,
        [ 'Schaden pro Schuss' ]      = self.frameArgs[ 'Schaden pro Schuss' ] or nil,
        [ 'Kapazität' ]               = self.frameArgs[ 'Kapazität' ] 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[ 'name' ] or mw.title.getCurrentTitle().subpageText

    local json = mw.text.jsonDecode( mw.ext.Apiunto.get_raw( 'v2/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 mw.title.getCurrentTitle().rootText

    local data = mw.smw.ask( {
        '[[' .. queryName .. ']]',
        '?#-=page',
        '?Name#-=name',
        '?Größe#-=size',
        '?Hersteller#-=manufacturer',
        '?Beschreibung=description', '+lang=' .. common.getLocaleForPage(),
        '?Waffentyp=type', '+lang=' .. common.getLocaleForPage(),
        '?Waffentyp=type_de', '+lang=de',
        '?Waffenklasse=class', '+lang=' .. common.getLocaleForPage(),
        '?Waffenklasse=class_de', '+lang=de',
        '?Reichweite=range',
        '?Mündungsgeschwindigkeit=muzzle_velocity',
        '?Schaden pro Schuss#-=dps',
        '?Kapazität#-=capacity',
        '?Spielversion#-=version',
        'mainlabel=-'
    } )

    if data == nil or data[ 1 ] == nil then
        return TNT.formatInLanguage( self.lang, 'Item', 'msg_smw_loading' )
    end

    self.smwData = data[ 1 ]

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

    self.smwData.modes = weaponSubobjects:getWeaponModes( queryName )

    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.vehicle_weapon == nil or type( self.apiData.vehicle_weapon ) ~= 'table' then
        return
    end

    local weapon = self.apiData.vehicle_weapon

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

    setData[ 'Waffentyp' ]               = common.mapTranslation( translateType( weapon.type or nil ) )
	setData[ 'Waffenklasse' ]            = common.mapTranslation( translateClass( weapon.class or nil ) )
	setData[ 'Reichweite' ]              = common.formatNum( weapon.ammunition.range or nil, nil )
    setData[ 'Mündungsgeschwindigkeit' ] = common.formatNum( weapon.ammunition.speed or nil, nil )
    setData[ 'Schaden pro Schuss' ]      = common.formatNum( weapon.damage_per_shot or nil, nil )
    setData[ 'Kapazität' ]               = weapon.capacity or nil

    local result = mw.smw.set( setData )

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

    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 type( data ) == 'string' then
        return
	end

    -- Call to {{#seo:}}
    mw.ext.seo.set{
        author = self.currentFrame:preprocess( '{{SERVER}}/Benutzer:{{urlencode:{{REVISIONUSER}}|WIKI}}' ),
        section = 'Fahrzeugwaffen',
        url = tostring( mw.uri.fullUrl( data.page ) ),
        title = table.concat({
            data.name or '',
            data.manufacturer or '',
            self.currentFrame:preprocess( '{{SITENAME}}' )
        }, ' - '),
        title_mode = 'replace',
        keywords = table.concat({
            data.name or '',
            data.type or '',
            data.class or '',
            data.manufacturer or '',
            manufacturer.getCodeFromName( data.manufacturer ) or '',
        }, ', '),
        image= self.frameArgs[ 'image' ] or '',
        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 = '[[' .. data.manufacturer .. ']]' .. api.getVersionInfoRef( data.version, self.lang )
    } )

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

    local weaponClass = data.class
    if weaponClass ~= nil then
        weaponClass = string.format( '[[:Kategorie:%s|%s]]', data.class_de, weaponClass )
    end

    local capacity = data.capacity
    if capacity == 0 or capacity == '0' or capacity == nil then
        capacity = '-'
    else
        capacity = capacity .. ' ' .. TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_shot' )
    end

	local content = {
		box:renderItem( TNT.formatInLanguage( self.lang, 'Item', 'lbl_type' ), weaponType ),
		box:renderItem( TNT.formatInLanguage( self.lang, 'Item', 'lbl_size' ), string.format( '[[:Kategorie:S%d Fahrzeugwaffe|S%d]]', data.size or 0, data.size or 0 ) ),
	}

    if data.type_de ~= 'Traktorstrahl' then
    	table.insert( content, box:renderItem( TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_muzzle_velocity' ), data.muzzle_velocity or '-' ) )
    	table.insert( content, box:renderItem( TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_range' ), data.range or '-' ) )
    	table.insert( content, box:renderItem( TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_dps' ), data.dps or '-' ) )
    	table.insert( content, box:renderItem( TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_capacity' ), capacity or '-' ) )
	end

    box:renderSection( {
    	content = content,
		col = 2
    } )

    --[[
        Weapon Modes
    ]]--
    if data.modes ~= nil and type( data.modes ) == 'table' then
    	local modes = {}

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

    		table.insert( modes, box:renderItem( TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_mode' ), mode.name ) )
    		table.insert( modes, box:renderItem( TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_rof' ), (mode.rof or ' - ') .. ' ' .. TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_shot_s' ) ) )
    		table.insert( modes, box:renderItem( TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_dpss' ), dps ) )
        end

	    box:renderSection( {
	    	title = TNT.formatInLanguage( self.lang, 'VehicleWeapon', 'lbl_fire_modes' ),
	    	content = modes,
			col = 2
	    } )
    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


--- 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
	    table.insert( self.categories, '[[Category:' .. smwData.manufacturer .. '|' .. smwData.name .. ' ]]' )
	    table.insert( self.categories, '[[Category:Fahrzeugwaffe|' .. smwData.name .. ' ]]' )

	    if smwData.Waffentyp ~= nil then
	        table.insert( self.categories, '[[Category:' .. smwData.type_de .. '|' .. smwData.name .. ' ]]' )
	    end

	    if smwData.size ~= nil then
	        table.insert( self.categories, '[[Category:S' .. smwData.size .. ' Fahrzeugwaffe|' .. smwData.name .. ' ]]' )
	    end
    else
    	table.insert( self.categories, '[[Category:' .. smwData.name ..'|' .. smwData.name .. ' ]]' )
    end
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 VehicleWeapon.main( frame )
    local instance = VehicleWeapon: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:setCategories( instance:getSmwData() )
    instance:setSeoData()

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


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

    setmetatable( instance, metatable )

    return instance
end


return VehicleWeapon