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

Modul:Sternensystem

Aus Star Citizen Wiki

Dokumentation unvollständig.


local Sternensystem = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

-- Extensions
local api = mw.ext.Apiunto
local infobox = require( 'Module:Infobox' )
local systemData = require( 'Module:Sternensystem/Data' )
local common = require( 'Module:Common' )
local quelle = require( 'Modul:Quelle' )


--- Filters celestial_objects by type key
--- Returns a table containing only the specified type
---
--- @param apiData table - Json API Data
--- @param type string - The type to filter
---
--- @return table
function Sternensystem.getCelestialObjectsForType( apiData, type )
    if apiData == nil or apiData.celestial_objects == nil or apiData.celestial_objects.data == nil then
        return {}
    end

    local objects = {}

    for _, object in pairs( apiData.celestial_objects.data ) do
        if object.type == type then
            table.insert( objects, object )
        end
    end

    return objects
end

--- Extracts the affiliations form api data
---
--- @param affiliationData table
--- @param key string
---
--- @return table
function Sternensystem.extractAffiliations( affiliationData, key )
    if type( affiliationData.data ) ~= 'table' or #affiliationData.data == 0 then
        return {}
    end

    local affiliations = {}

    for _, affiliation in pairs( affiliationData.data ) do
        if key ~= nil and affiliation[ key ] ~= nil then
            table.insert( affiliations, affiliation[ key ] )
        else
            table.insert( affiliations, {
                name = affiliation.name,
                code = affiliation.code,
            })
        end
    end

    return affiliations
end

--- Append 'Mrd. Jahre'
--- @param age number|string
---
--- @return string formatted age
function Sternensystem.formatAge( age )
    if age ~= nil and common.formatNum( age ) ~= nil then
        if type( age ) == 'string' then
            age = tonumber( age, 10 )
        end

        age = age * 1000000000

        return common.formatNum( age ) .. ' Jahre'
    end
end

--- Formats a table for sizes and appends 'km'
--- @param sizes table|nil
---
--- @return table|string
function Sternensystem.formatSize( sizes )
    if sizes == nil then
        return sizes
    end

    if type( sizes ) == 'table' then
        for k, v in pairs( sizes ) do
            if k ~= nil and v ~= nil then
                local formatted = common.formatNum( v )

                if formatted ~= nil then
                    sizes[ k ] = formatted .. ' km'
                end
            end
        end

        return sizes
    end

    local formatted = common.formatNum( sizes )

    if formatted ~= nil then
        return formatted .. ' km'
    end
end

--- Write celestial objects as subobject
---
--- @param celestialObjects table
function Sternensystem.setSubObjects( celestialObjects )
    for _, celestialObject in pairs( celestialObjects ) do
        if celestialObject.habitable ~= nil then
            if celestialObject.habitable == 1 or celestialObject.habitable == true then
                celestialObject.habitable = "Ja"
            else
                celestialObject.habitable = "Nein"
            end
        end

        local data = {
            [ 'ID' ]                = celestialObject.id,
            [ 'Starmap Code' ]      = celestialObject.code,
            [ 'Typ' ]               = celestialObject.type,
            [ 'Name' ]              = celestialObject.name,
            [ 'Bezeichnung' ]       = celestialObject.designation,
            [ 'Alter' ]             = Sternensystem.formatAge( celestialObject.age ),
            [ 'Radius' ]            = Sternensystem.formatSize( celestialObject.size ),
            [ 'Habitabel' ]         = celestialObject.habitable,
            [ 'Beschreibung' ]      = celestialObject.description,
            [ 'Gefahrenlage' ]      = common.formatNum( celestialObject.sensor.danger ),
            [ 'Wirtschaft' ]        = common.formatNum( celestialObject.sensor.economy ),
            [ 'Bevölkerungsgröße' ] = common.formatNum( celestialObject.sensor.population ),
            [ 'Kontrolle' ]         = Sternensystem.extractAffiliations( celestialObject.affiliation, 'name' ),
            [ 'Umlaufzeit' ]        = common.formatNum( celestialObject.orbit_period ),
            [ 'Abstand' ]           = common.formatNum( celestialObject.distance ),
            [ 'Breitengrad' ]       = common.formatNum( celestialObject.latitude ),
            [ 'Längengrad' ]        = common.formatNum( celestialObject.longitude ),
            [ 'Elternid' ]          = celestialObject.parent_id,
            [ 'Sternensystemid' ]   = celestialObject.system_id,
        }

        if celestialObject.subtype ~= nil and type( celestialObject.subtype.data ) == 'table' and celestialObject.subtype.data.id ~= nil then
            data[ 'Subtyp' ] = celestialObject.subtype.data.type
            data[ 'Subtypname' ] = systemData.getTranslatedSubtype( celestialObject.subtype.data.name )
        end

        if celestialObject.type == 'JUMPPOINT' and celestialObject.jumppoint ~= nil and type( celestialObject.jumppoint.data ) == 'table' then
            local jumppoint = celestialObject.jumppoint.data

            data[ 'Größe' ] = jumppoint.size
            data[ 'Richtung' ] = jumppoint.direction

            local designation = ''

            if jumppoint.entry.system_id == celestialObject.system_id then
                designation = jumppoint.entry.designation
            elseif jumppoint.exit.system_id == celestialObject.system_id then
                designation = jumppoint.exit.designation
            end

            local destinations = mw.text.split( designation, ' - ', true )

            if #destinations == 2 then
                data[ 'Sprungpunkt Start' ] = Sternensystem.cleanJumppointDestination( destinations[ 1 ] )
                data[ 'Sprungpunkt Ziel' ]  = Sternensystem.cleanJumppointDestination( destinations[ 2 ] )
            end
        end

        common.checkSmwResult( mw.smw.subobject( data ) )
    end
end

--- Returns the 'old' Starsystem name from a format of
--- Starsystem (New Name)
---
--- @param destination string
---
--- @return string
function Sternensystem.cleanJumppointDestination( destination )
    local clean = mw.text.split( destination, '(', true )

    if clean[ 1 ] ~= nil and type( clean[ 1 ] ) == 'string' then
        return mw.text.trim( clean[ 1 ] )
    end

    return destination
end

--- Returns small logo and link if available
---
--- @param affiliations string|table
---
--- @return string
function Sternensystem.getAffiliationLines( affiliations )
    local mapping = {
        BANU = '[[Banu]]',
        DEV = 'Developing',
        DEVELOPING = 'Developing',
        UEE = '[[Menschen]]',
        UNC = 'Unclaimed',
        UNCLAIMED = 'Unclaimed',
        VANDUUL = '[[Vanduul]]',
        [ 'XI\'AN' ] = '[[Xi\'An]]',
    }

    if type( affiliations ) == 'string' then
        affiliations = { affiliations }
    end

    local lines = {}

    for _, affiliation in pairs( affiliations ) do
        local line = '[[Datei:Systemlogo ' .. mw.ustring.upper( affiliation:gsub( '%\'', '' ) ) .. '.svg|frameless|link=|class=noviewer factionlogo|x15px]] '

        if mapping[ mw.ustring.upper( affiliation ) ] == nil then
            line = line .. mw.ustring.upper( affiliation )
        else
            line = line .. mapping[ mw.ustring.upper( affiliation ) ]
        end

        table.insert( lines, line )
    end

    if #lines == 0 then
        return '-'
    end

    return tostring( table.concat( lines, '<br>' ) )
end

--- Textual Affiliation
---
--- @param affiliations table|string
---
--- @return string
function Sternensystem.getAffiliationsAsText( affiliations )
    if affiliations == nil then
        return ''
    end

    if type( affiliations ) == 'string' then
        affiliations = { affiliations }
    end

    local line = ''

    for _, affiliation in pairs( affiliations ) do
        if #line > 0 then
            line = line .. ' und '
        end

        if affiliation == 'Developing' then
            line = line .. 'von niemandem, dieses System entwickelt sich derzeit'
        elseif affiliation == 'Unclaimend' then
            line = line .. 'von niemandem, keine Fraktion kontrolliert dieses System'
        elseif affiliation == 'UEE' then
            line = line .. 'des United Earth Empire'
        else
            line = line .. 'der ' .. affiliation
        end
    end

    return line .. '.'
end

---
--- Public Methods
---

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

    if type( data.affiliation ) == 'string' then
        data.affiliation = { data.affiliation }
    end

    local cleanTitle = common.removeTypeSuffix( data.page, 'Sternensystem' )

    local desc = table.concat( {
        'Das',
        cleanTitle,
        'Sternensystem ist ein System under der Kontrolle',
        Sternensystem.getAffiliationsAsText( data.affiliation )
    }, ' ' )

    -- Image
    local image = common.getImage( {
        self.pageName .. '_System.jpg',
        self.pageName .. '_System.png',
        self.frameArgs.image
    } )

    -- Call to {{#seo:}}
    mw.ext.seo.set{
        author = self.currentFrame:preprocess( '{{SERVER}}/Benutzer:{{urlencode:{{REVISIONUSER}}|WIKI}}' ),
        section = 'Sternensystem',
        url = tostring( mw.uri.fullUrl( data.page ) ),
        title = table.concat( {
            cleanTitle,
            'Sternensystem',
            self.currentFrame:preprocess( '{{SITENAME}}' )
        }, ' - '),
        title_mode = 'replace',
        keywords = table.concat( {
            'Sternensystem',
            cleanTitle,
            table.concat( data.affiliation, ', '),
        }, ', '),
        description = self.frameArgs.description or desc,
        image = image or '',
        image_alt = 'Bild des ' .. data.Name .. ' Systems',
        locale = 'de_DE',
        type = 'article',
    }
end

--- Request Api Data
--- Using current subpage name
---
--- @return table
function methodtable.getApiDataForCurrentPage( self )
    local name = self.pageName

    local config = systemData.new( self.pageName )

    if config.get( 'api_name' ) ~= '' then
        name = config.get( 'api_name' )
    end

    local json = mw.text.jsonDecode( api.get_starsystem( name, {
        locale = 'de_DE',
        include = {
            'celestial_objects',
            'celestial_objects.jumppoint',
        },
    } ) )

    common.checkApiResponse( json )

    self.apiData = json.data or nil

    return self.apiData
end

--- Returns the highest star age
---
--- @return number
function methodtable.getSystemAge( self )
    local stars = Sternensystem.getCelestialObjectsForType( self.apiData, 'STAR' )
    local age = 0

    for _, star in pairs( stars ) do
        if star.age ~= nil and tonumber( star.age, 10 ) >= age then
            age = tonumber( star.age, 10 )
        end
    end

    return age
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().text

    local query = table.concat( {
        '[[ ' .. queryName .. ' ]]',
        '?#-=page',
        '?Name#-=name',
        '?Starmap Code=code',
        '?Status=status',
        '?Typ=type',
        '?Sternensystem Größe=system_size',
        '?Sternensystem Alter=system_age',

        '?Kontrolle=affiliation',
        '?Bevölkerungsgröße=population_level',
        '?Wirtschaft=economic_level',
        '?Gefahrenlage=threat_level',

        '?Anzahl Sterne=star_count',
        '?Anzahl Planeten=planet_count',
        '?Anzahl Monde=moon_count',
        '?Anzahl Asteroidengürtel=asteroid_belt_count',
        '?Anzahl Sprungpunkte=jumppoint_count',
        'mainlabel=-'
    }, '|' )

    local data = mw.smw.ask( query )

    if data == nil or data[ 1 ] == nil then
        error( 'Seite "' .. queryName .. '" besitzt keine semantischen Daten.', 0 )
    end

    self.smwData = data[ 1 ]

    return self.smwData
end


--- Queries the SMW Store
function methodtable.setSemanticProperties( self )
    -- Api Error, don't set anything
    if self.apiData == nil then
        return
    end

    local setData = {
        [ 'Name' ] = self.apiData.name,
        [ 'ID' ] = self.apiData.id,
        [ 'Starmap Code' ] = self.apiData.code,
        [ 'Status' ] = self.apiData.status,
        [ 'Typ' ] = self.apiData.type,
        [ 'Sternensystem Größe' ] = common.formatNum( self.apiData.aggregated.size ),
        [ 'Sternensystem Alter' ] = common.formatNum( self:getSystemAge() ) .. ' Mrd.Jahre',

        [ 'Gefahrenlage' ] = common.formatNum( self.apiData.aggregated.danger ),
        [ 'Kontrolle' ] = Sternensystem.extractAffiliations( self.apiData.affiliation, 'name' ),
        [ 'Wirtschaft' ] = common.formatNum( self.apiData.aggregated.economy ),
        [ 'Bevölkerungsgröße' ] = common.formatNum( self.apiData.aggregated.population ),

        [ 'Frostlinie' ] = common.formatNum( self.apiData.aggregated.frost_line ),
        -- [ 'Habitable Zone (Innen)' ] = common.formatNum( apiData.aggregated.habitable_zone_inner ),
        -- [ 'Habitable Zone (Außen)' ] = common.formatNum( apiData.aggregated.habitable_zone_outer ),

        [ 'Anzahl Sterne' ] = #Sternensystem.getCelestialObjectsForType( 'STAR' ),
        [ 'Anzahl Planeten' ] = #Sternensystem.getCelestialObjectsForType( 'PLANET' ),
        [ 'Anzahl Monde' ] = #Sternensystem.getCelestialObjectsForType( 'SATELLITE' ),
        [ 'Anzahl Asteroidengürtel' ] = #Sternensystem.getCelestialObjectsForType( 'ASTEROID_BELT' ),
        [ 'Anzahl Sprungpunkte' ] = #Sternensystem.getCelestialObjectsForType( 'JUMPPOINT' ),

        [ 'Beschreibung' ] = self.frameArgs.description or self.apiData.description,
    }

    common.checkSmwResult( mw.smw.set( setData ) )

    if self.apiData.celestial_objects ~= nil and type( self.apiData.celestial_objects.data ) == 'table' then
        Sternensystem.setSubObjects( self.apiData.celestial_objects.data )
    end
end

--- Creates the infobox
function methodtable.getInfoBox( self )
    local data = self:getSmwData()

    if data.page == nil then
        return 'SMW Daten noch nicht geladen, bitte Seite neu laden.'
    end

    local astronomicalData = {
        star_count = 'Sterne',
        system_size = 'Größe (AE)',
        system_age = 'Alter',
    }

    local politicsAndEconomy = {
        {
            key = 'affiliation',
            text = 'Zugehörigkeit',
        },
        {
            key = 'population_level',
            text = 'Bevölkerung',
        },
        {
            key = 'economic_level',
            text = 'Wirtschaft',
        },
        {
            key = 'threat_level',
            text = 'Gefahrenlage',
        },
    }

    local cleanTitle = self.pageName

    if cleanTitle == nil then
        cleanTitle = common.removeTypeSuffix( mw.title.getCurrentTitle().subpageText, 'Sternensystem' )
    end

    local box = infobox.create( {
        bodyClass = 'floatright',
        allowReplace = true,
        removeEmpty = true,
        emptyString = '-',
    } )

    -- Image
    box:addImage( common.getImage( {
        self.pageName .. '_System.jpg',
        self.pageName .. '_System.png',
        self.frameArgs.image
    } ), {
        [ 'alternativtext' ] = 'Bild des ' .. cleanTitle .. ' Sternensystems',
        'rahmenlos',
        '350px'
    } )

    box:addTitle( cleanTitle .. ' System' )

    box:addHeader( 'Astronomische Daten' .. (quelle:new()):format( {
        url = 'https://robertsspaceindustries.com/starmap?location=' .. ( data.code or '' ),
        title = 'Ark Starmap: ' .. self.pageName .. ' System',
        wrap = true,
        ref_name = 'system_starmap',
    } ) )

    for smwKey, value in pairs( astronomicalData ) do
        if data[ smwKey ] ~= nil then
            box:addRow( value, data[ smwKey ], nil, 'col2' )
        end
    end

    box:addRow( 'Planeten', data.planet_count or '-', nil, 'col2' )
       :addRow( 'Monde', data.moon_count or '-', nil, 'col2' )
       :addRow( 'Asteroidengürtel', data.asteroid_belt_count or '-', nil, 'col2' )
       :addRow( 'Sprungpunkte', data.jumppoint_count or '-', nil, 'col2' )
       :addHeader( 'Politik und Wirtschaft' )

    for _, obj in ipairs( politicsAndEconomy ) do
        if data[ obj.key ] ~= nil then
            if obj.key == 'affiliation' then
                box:addRow( obj.text, Sternensystem.getAffiliationLines( data.affiliation ), nil, 'col2' )
            else
                box:addRow( obj.text, data[ obj.key ], nil, 'col2' )
            end
        end
    end

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

    return tostring( box )
end

--- Ouput infobox and categories and set seo
--- Entrypoint for Template calls
function Sternensystem.infoBox( frame )
    local instance = Sternensystem:new()

    instance.currentFrame = frame
    instance.frameArgs = frame:getParent().args

    instance.pageName = instance.frameArgs.name or common.removeTypeSuffix( mw.title.getCurrentTitle().subpageText, 'Sternensystem' )

    if mw.title.getCurrentTitle().namespace == 0 then
        table.insert( instance.categories, '[[Category:Sternensystem|' .. instance.pageName .. ']]' )
    end

    common.setDisplayTitle( instance.currentFrame, instance.pageName )

    instance:getApiDataForCurrentPage()
    instance:setSemanticProperties()
    instance:setSeoData()

    return tostring( instance:getInfoBox() ) .. tostring( table.concat( instance.categories ) )
end

--- New instance
function Sternensystem.new( self )
    local instance = {
        currentFrame = nil,
        frameArgs = nil,
        pageName = nil,
        categories = {},
        apiData = nil,
        smwData = nil,
    }

    setmetatable( instance, metatable )

    return instance
end

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