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

Modul:WeaponPersonal

Aus Star Citizen Wiki

Modulabhängigkeiten

Modulinfo

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


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

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:WeaponPersonal/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


--- @param magazine string
--- @return string
local function translateMagazine( magazine )
	if magazine == nil or #magazine == 0 then
		return nil
	end

    return {
        de_DE = ( data.magazineTranslations[ magazine ] or magazine ),
        en_EN = magazine,
    }
end


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

    local function addLangCode( key )
        if self.frameArgs[ key ] ~= nil then
            return self.frameArgs[ key ] .. '@de'
        end

        return nil
    end

    local setObj = {
        [ 'Name' ]                 = self.frameArgs[ 'Name' ]                 or nil,
        [ 'Größe' ]                = self.frameArgs[ 'Gewicht' ]              or nil,
        [ 'Hersteller' ]           = self.frameArgs[ 'Hersteller' ]           or nil,
        [ 'Beschreibung' ]         = addLangCode( 'Beschreibung' ),
        [ 'Waffentyp' ]            = self.frameArgs[ 'Waffentyp' ]            or nil,
        [ 'Waffenklasse' ]         = self.frameArgs[ 'Waffenklasse' ]         or nil,
        [ 'Magazingröße' ]         = self.frameArgs[ 'Magazingröße' ]         or nil,
        [ 'Effektive Reichweite' ] = self.frameArgs[ 'Effektive Reichweite' ] or nil,
        [ 'Feuerrate' ]            = self.frameArgs[ 'Feuerrate' ]            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_weapon_personal( name, {
        locale = '',
        include = {
            'modes',
            'damages',
            'attachments',
            'attachmentPorts',
            '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 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',
        '?Magazingröße#-=magazine_size',
        '?Magazintyp=magazine_type', '+lang=' .. common.getLocaleForPage(),
        '?Magazintyp=magazine_type_de', '+lang=de',
        '?Effektive Reichweite#-=range',
        '?Schaden pro Schuss#-=dps',
        '?Feuerrate#-=rof',
        '?Länge=length',
        '?Breite=width',
        '?Höhe=height',
        '?Volumen=volume',
        '?Spielversion#-=version',
        'mainlabel=-'
    } )

    if data == nil or data[ 1 ] == nil then
        error( TNT.format( 'I18n/Module:WeaponPersonal', 'msg_no_data', queryName ), 0 )
    end

    self.smwData = data[ 1 ]

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

    self.smwData.modes = weaponSubobjects:getWeaponModes( queryName )
    self.smwData.defaultAttachments = weaponSubobjects:getDefaultAttachments( queryName )
    self.smwData.attachmentPorts = weaponSubobjects:getAttachmentPorts( queryName )
    self.smwData.damages = weaponSubobjects:getDamages( queryName )
    self.smwData.ammunition = weaponSubobjects:getAmmunition( queryName )

    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 or mw.title.getCurrentTitle().isSubpage then
        return
    end

    local price = self.apiData[ 'msrp' ]

    if price == nil and self.frameArgs[ 'price' ] ~= nil then
        price = self.frameArgs[ 'price' ]
    end

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

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

    if data.weaponManufacturerMap[ self.apiData.name ] ~= nil then
        manufacturerName = data.weaponManufacturerMap[ self.apiData.name ]
    end

    local rof = common.formatNum( self.apiData.rof or nil )
    if rof == false then
        rof = nil
    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 ),
        [ 'Waffentyp' ]            = common.mapTranslation( translateType( self.apiData.type or nil ) ),
        [ 'Waffenklasse' ]         = common.mapTranslation( translateClass( self.apiData.class or nil ) ),
        [ 'Magazingröße' ]         = self.apiData.magazine_size or nil,
        [ 'Magazintyp' ]           = common.mapTranslation( translateMagazine( self.apiData.magazine_type or nil ) ),
        [ 'Effektive Reichweite' ] = self.apiData.effective_range or nil,
        [ 'Schaden pro Schuss' ]   = common.formatNum( self.apiData.damage_per_shot, nil ),
        [ 'Feuerrate' ]            = rof,
        [ 'Länge' ]                = common.formatNum( self.apiData.volume.length or nil, nil ),
        [ 'Breite' ]               = common.formatNum( self.apiData.volume.width or nil, nil ),
        [ 'Höhe' ]                 = common.formatNum( self.apiData.volume.height or nil, nil ),
        [ 'Volumen' ]              = common.formatNum( self.apiData.volume.volume or nil, nil ),
        [ 'Spielversion' ]         = self.apiData.version or nil,
    }

    local result = mw.smw.set( setData )

    local weaponSubobjects = require( 'Module:WeaponSubobjects' ):new()
    weaponSubobjects:setSemanticProperties( self.apiData )

    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 nil == data.manufacturer then
        -- Faulty SMW data, don't call #seo
        return
    end

    require( 'Module:SEO' ).set(
        TNT.format( 'I18n/Module:WeaponPersonal', 'seo_section' ),
        data.page,
        table.concat({
            data.name,
            data.manufacturer,
            self.currentFrame:preprocess( '{{SITENAME}}' )
        }, ' - '),
        'replace',
        {
            data.name or '',
            data.type or '',
            data.class or '',
            data.manufacturer or '',
            manufacturer.getCodeFromName( data.manufacturer ) or '',
        },
        nil,
        self.frameArgs[ 'image' ],
        data.page
    )
end


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

    if nil == data.manufacturer then
        return TNT.format( 'I18n/Module:WeaponPersonal', 'msg_smw_loading' )
    end

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

    if data.type ~= nil then
        table.insert( self.categories, '[[Category:' .. data.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:WeaponPersonal', '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 maxRange = '-'
    local muzzleVelocity = '-'
    if type( data.ammunition ) == 'table' then
        maxRange = data.ammunition[ 1 ][ 'Maximalreichweite' ]
        muzzleVelocity = data.ammunition[ 1 ][ 'Mündungsgeschwindigkeit' ]
    end

    box:addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_type' ), weaponType, nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_class' ), weaponClass, nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_magazine_size' ), data.magazine_size .. ' ' .. TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_shots' ), nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_dps' ), data.dps, nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_magazine_type' ), data.magazine_type, nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_rof' ), ( data.rof or '-' ) .. ' ' .. TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_shot_s' ), nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_muzzle_velocity' ), muzzleVelocity, nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_effective_range' ), data.range, nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_max_range' ), maxRange, nil, 'col2' )
       :addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_weight' ), data.volume or '-', nil, 'col2' )

    --[[
        Weapon Modes
    ]]--
    if data.modes ~= nil and type( data.modes ) == 'table' then
        local source = self.currentFrame:extensionTag{
            name = 'ref',
            content = TNT.format( 'I18n/Module:WeaponPersonal', 'msg_version_info', data.version, '[https://github.com/StarCitizenWiki/scunpacked-data GitHub]' )
        }
        box:addHeader( TNT.format( 'I18n/Module:WeaponPersonal', '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:WeaponPersonal', 'lbl_mode' ), mode.name, nil, 'col3' )
            box:addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_rof' ), (mode.rof or ' - ') .. ' ' .. TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_shot_s' ), nil, 'col3' )
            box:addRow( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_dps' ), dps, nil, 'col3' )
        end
    end

    --[[
        Weapon Attachments
    ]]--
    if data.attachmentPorts ~= nil and type( data.attachmentPorts ) == 'table' then
        box:addHeader( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_attachments' ) )

        for _, attachment in pairs( data.attachmentPorts ) do
            if type( attachment ) == 'table' and attachment.position_de ~= 'Magazin' then
                local size
                if attachment.min_size == attachment.max_size then
                    size = 'S' .. attachment.min_size
                else
                    size = 'S' .. attachment.min_size .. ' - S' .. attachment.max_size
                end

                box:addRow( attachment.position, size, nil, 'col3' )
            end
        end
    end

    --[[
        Weapon Default Loadout
    ]]--
    if data.defaultAttachments ~= nil and type( data.defaultAttachments ) == 'table' then
        box:addHeader( TNT.format( 'I18n/Module:WeaponPersonal', 'lbl_standard_equipment' ) )

        for _, attachment in pairs( data.defaultAttachments ) do
            if type( attachment ) == 'table' and attachment.name ~= nil then
                local size = attachment.size
                if size ~= nil and tonumber( size ) > 0 then
                    size = 'S' .. size .. ' '
                end

                box:addRow( attachment.position, size .. attachment.name, nil, 'col-full' )
            end
        end
    end

    if self.currentFrame ~= nil then
        box:addRowsFromArgs( require( 'Module:Arguments' ).getArgs( self.currentFrame ), '!' )
    end

    return tostring( box ) .. tostring( common.generateInterWikiLinks( data.name ) )
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 WeaponPersonal.main( frame )
    local instance = WeaponPersonal: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 WeaponPersonal.new( self )
    local instance = {
        categories = {}
    }

    setmetatable( instance, metatable )

    return instance
end


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