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

Modul:VehicleComponentTable

Aus Star Citizen Wiki

Dokumentation unvollständig.


local VehicleComponentTable = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

--- Query SMW Subobject component data
--- @return table
local function getSMWData( t )
    if t.smwData ~= nil then
        return t.smwData
    end

    local query = table.concat( {
        '[[-Has subobject::' .. t.pageName .. ']][[Komponentenklasse::+]]',
        '?#-=page',
        '?Anzahl=count',
        '?Größe=size',
        '?Hersteller=manufacturer',
        '?Komponentenbefestigungen=mounts',
        '?Komponentenklasse=class',
        '?Maximalgröße=size_max',
        '?Name=name',
        '?Typ=type',

        'mainlabel=-',
        'sort=Komponentenklasse,Typ,Komponentenbefestigungen',
        'order=asc,asc,desc',
    }, '|' )

    t.smwData = mw.smw.ask( query )

    return t.smwData
end

--- Group Components by component class
local function groupComponents( t )
    if t.smwData == nil then
        return
    end

    for _, component in ipairs( t.smwData ) do
        if t.groups[ component.class ] == nil then
            t.groups[ component.class ] = {}
        end

        table.insert( t.groups[ component.class ], component )
    end
end

--- Short size to german
--- Returns original argument if key was not found in table
--- @param size string
--- @return string
local function translateSize( size )
    local translations = {
        [ 'V' ] = 'Fahrzeug',
        [ 'S' ] = 'Klein',
        [ 'M' ] = 'Mittel',
        [ 'L' ] = 'Groß',
        [ 'C' ] = 'Kapitalklasse',
    }

    if translations[ size ] ~= nil then
        return translations[ size ]
    end

    return size
end

--- Generates an icon line
--- @param name string
--- @return string
local function generateIcon( name )
    local iconSize = 'x15px'

    return string.format( '[[Datei:Hardpoints_Icon_%s.svg|%s|class=noviewer|link=]]', name, iconSize )
end

--- Creates the info table
--- @param class string
local function makeTable( class )
    local infoTable = mw.html.create( 'table' )

    local icon = ''

    local colspanHeader = '3'

    if class == 'Waffen' then
        colspanHeader = '4'
    end

    if class == 'Antrieb' then
        icon = generateIcon( 'Kraftwerksanlage' )
    end

    if class == 'Triebwerk' then
        icon = generateIcon( 'Primärtriebwerk' )
    end

    if class == 'Waffen' then
        icon = generateIcon( 'Waffen' )
    end

    if class == 'Systeme' then
        icon = generateIcon( 'Schildgenerator' )
    end

    if class == 'Avionik' then
        icon = generateIcon( 'Rechner' )
    end

    infoTable
            :addClass( 'wikitable mw-collapsible mw-collapsed' )
            :addClass( 'component-table' )
            :addClass( 'scw-table' )
            :addClass( 'component-' .. class )

            :tag( 'tr' )
                :tag( 'th' )
                    :attr( 'colspan', colspanHeader )
                    -- :attr( 'style', 'font-size: 100%; text-align: center')
                    :tag( 'span' )
                        :addClass( 'icon-text' )
                        :wikitext( icon .. '  ' .. class )
                    :done()
                :done()
            :done()

    return infoTable
end

--- Sub header
--- Komponente | Type / Size | Count
--- @param class string
--- @param infoTable table
local function makeSubHeader( class, infoTable )
    local colspan = '1'

    local secondRowHeader = 'Größe [max.]'
    local thirdRowHeader = 'Anzahl [max.]'

    if class == 'Antrieb' or class == 'Triebwerk' then
        -- secondRowHeader = 'Typ'
    end

    if class == 'Waffen' then
        colspan = '2'
    end

    infoTable
            :tag( 'tr' )
                :tag( 'th' )
                    :addClass( 'unsortable' )
                    :attr( 'colspan', colspan )
                    :wikitext( 'Komponente' )
                :done()
                :tag( 'th' )
                    :addClass( 'number' )
                    :wikitext( secondRowHeader )
                :done()
                :tag( 'th' )
                    :addClass( 'number' )
                    :wikitext( thirdRowHeader )
                :done()
            :done()
end

--- Creates a table row
--- Ugly
--- @param t table
--- @param component table
--- @param infoTable table
local function makeRow( t, component, infoTable )
    local firstRowData = generateIcon( component.type ) .. '  ' .. component.type

    if t.previousRowData == firstRowData then
        firstRowData = ''
    else
        t.previousRowData = firstRowData
    end

    if component.class == 'Waffen' then
        local lastRowData = component.mounts

        if component.size ~= nil and component.mounts ~= nil and component.count ~= nil then
            lastRowData = '<span title="Befestigungen">' ..
                    component.mounts .. '</span> &times; <span title="Anzahl">' ..
                    component.count .. '</span> &times; <span title="Größe">' .. component.size ..'</span>'
        end

        infoTable
                :tag( 'tr' )
                    :tag( 'th' )
                    :addClass( 'unsortable' )
                        :wikitext( firstRowData )
                    :done()
                    :tag( 'td' )
                        :addClass( 'unsortable' )
                        :wikitext( component.name )
                    :done()
                    :tag( 'td' )
                        :addClass( 'number' )
                        :wikitext( translateSize( component.size_max ) )
                    :done()
                    :tag( 'td' )
                        :addClass( 'number' )
                        :wikitext( lastRowData )
                    :done()
                :done()
    else
        -- local secondRowData = translateSize( component.size_max )

        -- if component.class == 'Antrieb' or component.class == 'Triebwerk' then
        --     secondRowData = component.name
        -- end

        infoTable
                :tag( 'tr' )
                    :tag( 'th' )
                        :wikitext( firstRowData )
                    :done()
                    :tag( 'td' )
                        :addClass( 'number' )
                        :wikitext( translateSize( component.size_max ) )
                    :done()
                    :tag( 'td' )
                        :addClass( 'number' )
                        :wikitext( component.mounts )
                    :done()
                :done()
    end
end

--- Infotable for each component class
--- @param t table Instance
local function generateInfobox( t )
    local html = '<div class="component-table-wrapper">'

    for class, components in pairs( t.groups ) do
        local infoTable = makeTable( class )

        if type( components ) == 'table' then
            makeSubHeader( class, infoTable )

            for _, component in ipairs( components ) do
                makeRow( t, component, infoTable )
            end
        end

        html = html .. tostring( infoTable:allDone() )
    end

    return tostring( html .. '</div>' )
end

--- Initialize the module
--- @param frame table
function methodtable.init( self, frame )
    if frame:getParent().args.name ~= nil then
        self.pageName = frame:getParent().args.name
    else
        self.pageName = mw.title.getCurrentTitle().subpageText
    end

    getSMWData( self )
    groupComponents( self )
end

--- Entrypoint for templates
--- @param frame table
function VehicleComponentTable.output( frame )
    local instance = VehicleComponentTable:new()

    instance:init( frame )

    return generateInfobox( instance )
end

--- New Instance
--- @return table VehicleComponentTable
function VehicleComponentTable.new( self )
    local instance = {
        smwData = nil,
        pageName = nil,
        groups = {},
        previousRowData = ''
    }

    setmetatable( instance, metatable )

    return instance
end

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