Das Star Citizen Wiki wird von Leuten wie dir gemacht! Bitte überleg doch bei uns mitzuwirken, wir können viel Hilfe gebrauchen!

Modul:CelestialObject

Aus Star Citizen Wiki

Modulinfo[Bearbeiten]

Das Modul:CelestialObject zeigt Infoboxen für astronomische Objekte eines Sternensystems an.

Die Daten werden dazu von der entsprechenden Sternensystemseite geladen.

Instanziierung[Bearbeiten]

-- TYPE = 'Stern' or 'Mond' or 'Planet' ...
local instance = require( 'Module:CelestialObject' ):new( TYPE )

local CelestialObject = {
    objectType = '',
    parentType = '',

    currentFrame = {},
    frameArgs = {},

    pageName = {},
    categories = {},

    celestialObjectData = nil,
    parentObjectData = nil,

    data = require( 'Module:CelestialObject/Data' ),
}

-- Extensions
local infobox = require( 'Module:Infobox' )
local system = require( 'Module:Sternensystem' )
local common = require( 'Module:Common' )
local quelle = require( 'Modul:Quelle' )

-- Returns the 'old' name from a celestial object name
-- Example: Th.us’ūng (Pallas) II
-- Returns: Pallas II
-- If param 'new' is true, returns Th.us’ūng II
function CelestialObject.getName( name, new )
    local split = mw.text.split( name, '(', true )

    if type( split ) == 'table' and split[ 2 ] ~= nil then
        -- Form: Lorem) IV
        local subSplit = mw.text.split( split[ 2 ], ')', true )

        if subSplit[ 1 ] ~= nil and subSplit[ 2 ] ~= nil then
            if new == true then
                return mw.text.trim( split[ 1 ] ) .. ' ' .. mw.text.trim( subSplit[ 2 ] )
            end

            return mw.text.trim( subSplit[ 1 ] ) .. ' ' .. mw.text.trim( subSplit[ 2 ] )
        end
    end

    return name
end

-- Sets the star system category
function CelestialObject.setSystemCategory( self )
    local systemName = self.parentObjectData.page

    local category = mw.title.new( systemName .. ' (Sternensystem)', 14 )

    if category.exists then
        table.insert( self.categories, '[[Category:' .. category.text .. '|' .. self.pageName .. ']]' )
    else
        table.insert( self.categories, '[[Category:' .. systemName .. '|' .. self.pageName .. ']]' )
    end
end

function CelestialObject:setCategories()
    table.insert( self.categories, '[[Category:' .. self.objectType .. '|' .. self.pageName .. ']]' )

    -- Only Celestial Objects have a system_id
    -- If no systemid is set on the parent, or the object is a planet it gets categorized into the system
    if self.parentObjectData.system_id == nil or self.celestialObjectData.type == 'PLANET' then
        CelestialObject.setSystemCategory( self )
    else
        -- Designation _MAY_ be buggy
        table.insert( self.categories, '[[Category:' .. self.parentObjectData.designation .. ']]' )
    end
end

-- Parent object semantic data
-- Either the Starsystem or parent celestial object
function CelestialObject:getParentObjectData()
    if self.celestialObjectData == nil then
        error( 'You need to call "getCelestialObjectData" first.' )
    end

    if self.parentObjectData ~= nil then
        return self.parentObjectData
    end

    local askData = {
        '?#-=page',

        -- CelestialObject
        '?Starmap Code=code',
        '?ID#-=id',
        '?Elternid#-=parent_id',
        '?Sternensystemid#-=system_id',
        '?Typ#-=type',
        '?Name#-=name',
        '?Bezeichnung#-=designation',
        '?Alter#Jahre=age',
        '?Radius=size',
        '?Habitabel=habitable',
        '?Beschreibung=description',

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

        '?Umlaufzeit=orbit_period',
        '?Subtypname=subtype_name',

        -- Starsystem
        '?Status=status',
        '?Sternensystem Größe=system_size',
        '?Sternensystem Alter=system_age',

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

    askData.mainlabel = '-'
    askData.limit = 1

    local id = self.celestialObjectData.parent_id
    if id == nil then
        table.insert( askData, 1, '[[Has subobject::<q>[[Starmap Code::' .. self.celestialObjectData.code .. ']]</q>]]' )
    else
        --        table.insert( askData, 1, '[[ID::' .. self.celestialObjectData.parent_id .. '|+depth=0]][[Typ::!' .. self.celestialObjectData.type .. ']]' )
        table.insert( askData, 1, '[[ID::' .. self.celestialObjectData.parent_id .. '|+depth=0]]' )
    end

    local data = mw.smw.ask( askData )

    if id == nil and (data == nil or data[ 1 ] == nil) then
        table.remove( askData, 1 )
        table.insert( askData, 1, '[[Has subobject::<q>[[Starmap Code::' .. self.celestialObjectData.code .. ']]</q>]]' )

        data = mw.smw.ask( askData )
    end

    if data == nil or data[ 1 ] == nil then
        error( 'Keine Systemdaten für Starmap Code ' .. tostring( id ) .. ' oder Elternid ' .. tostring( self.celestialObjectData.parent_id ) .. ' gefunden.', 0 )
    end

    self.parentObjectData = data[ 1 ]

    if data[ 1 ].affiliation == nil then
        self.parentObjectData.affiliation = {}
    end

    return self.parentObjectData
end

-- Queries the SMW Store
function CelestialObject:getCelestialObjectData( identifier )
    -- Cache multiple calls
    if self.celestialObjectData ~= nil then
        return self.celestialObjectData
    end

    -- We'll try to get the code by pagename
    if identifier == nil then
        identifier = (self.data.new( mw.title.getCurrentTitle().subpageText )).get( 'code' )
    end

    local selector = {
        '[[Starmap Code::' .. identifier .. ']]',
        '[[Bezeichnung::' .. identifier .. ']]',
        '[[Bezeichnung::' .. mw.title.getCurrentTitle().subpageText .. ']]',
    }

    if type( identifier ) == 'number' then
        table.insert( selector, '[[ID::' .. identifier .. ']]' )
    end

    local query = {
        table.concat( selector, '||' ),
        '?#-=page',
        '?Starmap Code=code',
        '?ID#-=id',
        '?Elternid#-=parent_id',
        '?Typ#-=type',
        '?Name#-=name',
        '?Bezeichnung#-=designation',
        '?Alter#Jahre=age',
        '?Radius=size',
        '?Habitabel=habitable',
        '?Beschreibung=description',

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

        '?Umlaufzeit=orbit_period',
        '?Subtypname=subtype_name',
    }

    query.mainlabel = '-'
    query.limit = 1

    local data = mw.smw.ask( query )

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

    self.celestialObjectData = data[ 1 ]

    if data[ 1 ].affiliation == nil then
        self.celestialObjectData.affiliation = {}
    end

    return self.celestialObjectData
end


-- Image
local function getImage( self )
    local image = mw.title.new( common.removeTypeSuffix( mw.title.getCurrentTitle().subpageText, {
        'Sternensystem',
        'Planet',
        'Mond',
        self.objectType,
    } ) .. '.jpg', 6 )

    if self.frameArgs ~= nil and self.frameArgs.image ~= nil then
        image = mw.title.new( self.frameArgs.image, 6 )
    end

    if image.exists then
        return image.prefixedText
    end

    local image = mw.title.new( common.removeTypeSuffix( mw.title.getCurrentTitle().subpageText, {
        'Sternensystem',
        'Planet',
        'Mond',
        self.objectType,
    } ) .. '.png', 6 )

    if image.exists then
        return image.prefixedText
    end

    return nil
end

-- Creates the infobox
-- TBD
function CelestialObject:makeInfoBox()
    if self.celestialObjectData == nil or self.celestialObjectData.page == nil then
        -- Faulty SMW data
        return ''
    end

    local astronomicalData = {
        name = 'Name',
        size = 'Größe',
        age = 'Alter',
        habitable = 'Habitabel',
        orbit_period = 'Umlaufzeit',
        subtype_name = 'Typ',
        star_count = 'Sterne',
        system_size = 'Größe (AE)',
        system_age = 'Alter',
    }

    if self.celestialObjectData.habitable ~= nil then
        if self.celestialObjectData.habitable == true or self.celestialObjectData.habitable == 'Ja' then
            self.celestialObjectData.habitable = 'Ja'
        else
            self.celestialObjectData.habitable = 'Nein'
        end
    end

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

    local options = {
        title = common.removeTypeSuffix( mw.title.getCurrentTitle().subpageText, {
            'Sternensystem',
            'Planet',
            'Mond',
            self.objectType,
        } ),
        top = self.pageName,
        bodyClass = 'wikitable',
    }

    if mw.text.trim( options.title ) == mw.text.trim( options.top ) then
        -- table.remove( options, 2 )
        options.top = nil
    end

    infobox.init( options )

    -- Image
    local image = getImage( self )

    if image ~= nil then
        infobox.addHeader( '[[ ' .. image .. '|alternativtext=' .. self.pageName .. ' ' .. CelestialObject.objectType .. '|rahmenlos|350px]]', 'table-image' )
    end

    local parentPage = '';
    local starmapRef = '';
    local template = '';

    if self.parentObjectData.page ~= nil then
        parentPage = mw.text.split( self.parentObjectData.page, '#', true )[ 1 ]

        local systemCode = mw.text.split( ( self.parentObjectData.code or '' ), '.', true )[ 1 ]

        quelle.setArgs( {
            url = 'https://robertsspaceindustries.com/starmap?location=' .. self.celestialObjectData.code .. '&system=' .. systemCode,
            title = 'Ark Starmap: ' .. self.pageName,
            wrap = true,
            ref_group = 'RSI',
            ref_name = 'starmap',
        } )

        starmapRef = quelle.format()

        infobox.addHeader( '[[' .. parentPage .. '|' .. parentPage .. ' System]]' )
    end

    infobox.addHeader( 'Astronomische Daten' .. starmapRef )

    for smwKey, value in ipairs( astronomicalData ) do
        if self.celestialObjectData[ smwKey ] ~= nil then
            infobox.addRow( value, self.celestialObjectData[ smwKey ] )
        end
    end

    if self.parentObjectData.type ~= nil and self.parentObjectData.planet_count == nil then
        local orbits = self.parentObjectData.designation

        if self.parentObjectData.type == 'STAR' then
            orbits = orbits .. ' (Stern)'
        end

        infobox.addRow( 'Umkreist', '[[' .. orbits .. ']]' )
    end

    infobox.addHeader( 'Politik und Wirtschaft' )

    for _, data in ipairs( politicsAndEconomy ) do
        if self.celestialObjectData[ data.key ] ~= nil then
            if data.key == 'affiliation' then
                infobox.addRow( data.text, system.getAffiliationLines( self.celestialObjectData.affiliation ) )
            else
                infobox.addRow( data.text, self.celestialObjectData[ data.key ] )
            end
        end
    end
    
    if self.currentFrame ~= nil then
    	infobox.addRowsFromArgs( require( 'Module:Arguments' ).getArgs( self.currentFrame ), '!' )
	end

    return infobox
end

--
-- Public Methods
--

-- Entrypoint for {{#seo:}}
function CelestialObject:setSeoData()
    if self.currentFrame == nil then
        error( 'No frame set.', 0 )
    end

    if self.celestialObjectData == nil or self.celestialObjectData.page == nil then
        -- Faulty SMW data, don't call #seo
        return
    end

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

    local desc = table.concat( {
        self.objectType,
        self.pageName,
        'under der Kontrolle',
        system.getAffiliationsAsText( self.celestialObjectData.affiliation )
    }, ' ' )

    local image = getImage( self )

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

-- New Instance
function CelestialObject:new( objectType )
    local instance = {}

    setmetatable( instance, self )
    self.__index = self

    if objectType == nil then
        error( 'Required argument "objectType" missing.' )
    end

    instance.objectType = objectType

    return instance
end

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