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

Modul:Missile

Aus Star Citizen Wiki

Modulabhängigkeiten

Modulinfo

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

Dieses Modul erzeugt Infoboxen zu Raketen und Torpedos. Daten werden aus der Star Citizen Wiki API abgerufen.


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

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

-- Extensions
local infobox = require( 'Module:Infobox' )
local manufacturer = require( 'Module:Hersteller' )
local common = require( 'Module:Common' )
local TNT = require( 'Module:TNT' )
local data = mw.loadData( 'Module:Missile/Data' )


--- @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( 'ship-items/' .. name, {
        locale = '',
        include = {
            'shops.items',
        }
    } ) )

    common.checkApiResponse( 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 nil
    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.data ) ~= 'table' then
        return
    end

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

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

	if manufacturerName == 'Vanduul Components - Generic' then
		manufacturerName = 'Vanduul'
	end

	if manufacturerName == 'Vanduul Components - Generic' then
		manufacturerName = 'Vanduul'
	end

    local missile = self.apiData.missile.data

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

    --- The base data for the page
    local setData = {
        [ 'Name' ]               = self.apiData.name or nil,
        [ 'Größe' ]              = self.apiData.size or nil,
        [ 'Hersteller' ]         = manufacturerName,
        [ 'Beschreibung' ]       = common.mapTranslation( self.apiData.description or nil ),
        [ 'Signaltyp' ]          = common.mapTranslation( translateType( missile.signal_type or nil ) ),
        [ 'Erfassungszeit' ]     = common.formatNum( missile.lock_time or nil, nil ),
        [ 'Schaden pro Schuss' ] = common.formatNum( missile.damage_total or nil, nil ),
        [ 'Typ' ]                = missileType,
        [ 'Spielversion' ]       = self.apiData.version,
    }

    local result = mw.smw.set( setData )

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

    local commodity = require( 'Module:Commodity' ):new()
    commodity: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 data == nil or nil == data.manufacturer then
        return '<p class="hatnote">Daten werden geladen, dies dauert wenige Minuten</p>'
    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.format( 'I18n/Module:Missile', '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.format( 'I18n/Module:Missile', '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 data == nil or nil == data.manufacturer then
        return '<p class="hatnote">Daten werden geladen, dies dauert wenige Minuten</p>'
    end

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

    local cat = 'Rakete'
    if data.type_de == 'Torpedo' then
        cat = 'Torpedo'
	end

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

    if data.signal_type ~= nil then
        table.insert( self.categories, '[[Category:' .. cat .. ' (' .. data.signal_type_de .. ')' .. translation .. '|' .. data.name .. ' ]]' )
    end

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

    local box = infobox.create( {
        bodyClass = 'floatright',
        allowReplace = false,
        removeEmpty = true,
        emptyString = '-',
        placeholderImage = 'Platzhalter Waffen.webp',
    } )

    -- Image
    box:addImage( common.getImage( {
        self.frameArgs[ 'image' ]
    } ), {
        [ 'alternativtext' ] = data.name,
        'rahmenlos',
        '600px'
    } )

    box:addTitle( data.name )

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

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



    box:addRow( TNT.format( 'I18n/Module:Missile', 'lbl_signal_type' ), signalType, nil, 'col2' )
        :addRow( TNT.format( 'I18n/Module:Missile', 'lbl_size' ), string.format( 'S%d', data.size or 0 ), nil, 'col2' )
        :addRow( TNT.format( 'I18n/Module:Missile', 'lbl_lock_time' ), data.lock_time , nil, 'col2' )
        :addRow( TNT.format( 'I18n/Module:Missile', 'lbl_damage' ), data.dps , nil, 'col2' )

    --[[
        Missile Damages
    ]]--
    if data.damages ~= nil and type( data.damages ) == 'table' then
        local source = self.currentFrame:extensionTag{
            name = 'ref',
            content = TNT.format( 'I18n/Module:Missile', 'msg_version_info', data.version, '[https://github.com/StarCitizenWiki/scunpacked-data GitHub]' )
        }
        box:addHeader( TNT.format( 'I18n/Module:Missile', 'lbl_damage_types' ) .. source )

        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

            box:addRow( TNT.format( 'I18n/Module:Missile', 'lbl_type' ), damage.damage_type, nil, 'col2' )
            box:addRow( TNT.format( 'I18n/Module:Missile', 'lbl_damage' ), dmg, nil, 'col2' )
        end
    end

    if self.currentFrame ~= nil then
        box:addRowsFromArgs( require( 'Module:Arguments' ).getArgs( self.currentFrame ), '!' )
    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()

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

--- New Instance
function Missile.new( self )
    local instance = {
        categories = {}
    }

    setmetatable( instance, metatable )

    return instance
end

return Missile
Cookies helfen uns bei der Bereitstellung dieses Wikis. Durch die Nutzung des Star Citizen Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.