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

Modul:VehicleWeapon

Aus Star Citizen Wiki

Modulabhängigkeiten

Modulinfo

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


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

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: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( 'ship-items/weapons/' .. 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 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 nil
    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.weapon == nil or type( self.apiData.weapon.data ) ~= 'table' then
        return
    end

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

        if string.sub( manufacturerName, 1, 3 ) == 'PH ' then
        	manufacturerName = string.sub( manufacturerName, 4 )
    	end
    end

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

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

    local weapon = self.apiData.weapon.data

    --- 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 ),
        [ 'Waffentyp' ]               = common.mapTranslation( translateType( self.apiData.type or nil ) ),
        [ 'Waffenklasse' ]            = common.mapTranslation( translateClass( self.apiData.class or nil ) ),
        [ 'Reichweite' ]              = common.formatNum( weapon.range or nil, nil ),
        [ 'Mündungsgeschwindigkeit' ] = common.formatNum( weapon.speed or nil, nil ),
        [ 'Schaden pro Schuss' ]      = common.formatNum( weapon.damage_per_shot or nil, nil ),
        [ 'Kapazität' ]               = weapon.capacity or nil,
        [ 'Spielversion' ]            = self.apiData.version or nil,
    }

    local result = mw.smw.set( setData )

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

    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 string.format( '<p class="hatnote">%s</p>', TNT.format( 'I18n/Module:VehicleWeapon', 'msg_smw_loading' ) )
    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,
            data.manufacturer,
            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 data == nil or nil == data.manufacturer then
        return string.format( '<p class="hatnote">%s</p>', TNT.format( 'I18n/Module:VehicleWeapon', 'msg_smw_loading' ) )
    end

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

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

    if data.size ~= nil then
        table.insert( self.categories, '[[Category:S' .. data.size .. ' Fahrzeugwaffe' .. 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:VehicleWeapon', 'lbl_manufacturer' ),
        '[[' .. data.manufacturer .. ']]',
        nil,
        'col2'
    )

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

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

    local capacity = data.capacity
    if capacity == 0 or capacity == '0' then
        capacity = '-'
    else
        capacity = capacity .. ' ' .. TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_shot' )
    end

    box:addRow( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_type' ), weaponType, nil, 'col2' )
        :addRow( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_size' ), string.format( '[[:Kategorie:S%d Fahrzeugwaffe' .. translation .. '|S%d]]', data.size or 0, data.size or 0 ), nil, 'col2' )

    if data.type_de ~= 'Traktorstrahl' then
	     box:addRow( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_muzzle_velocity' ), data.muzzle_velocity , nil, 'col2' )
        	:addRow( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_range' ), data.range , nil, 'col2' )
    		--:addRow( 'Klasse', weaponClass, nil, 'col2' )
    		:addRow( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_dps' ), data.dps, nil, 'col2' )
    		:addRow(  TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_capacity' ), capacity, nil, 'col2' )
	end
    --[[
        Weapon Modes
    ]]--
    if data.modes ~= nil and type( data.modes ) == 'table' then
        local source = self.currentFrame:extensionTag{
            name = 'ref',
            content = TNT.format( 'I18n/Module:VehicleWeapon', 'msg_version_info', data.version, '[https://github.com/StarCitizenWiki/scunpacked-data GitHub]' )
        }
        box:addHeader( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_fire_modes' ) .. source )

        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

            box:addRow( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_mode' ), mode.name, nil, 'col3' )
            box:addRow( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_rof' ), (mode.rof or ' - ') .. ' ' .. TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_shot_s' ), nil, 'col3' )
            box:addRow( TNT.format( 'I18n/Module:VehicleWeapon', 'lbl_dpss' ), dps, nil, 'col3' )
        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 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:setSeoData()

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


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

    setmetatable( instance, metatable )

    return instance
end


return VehicleWeapon
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.