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

Modul:SternensystemStruktur

Aus Star Citizen Wiki

Diese Vorlage erzeugt eine strukturelle Übersicht eines Sternensystems oder astronomischen Objektes.

Wird die Vorlage ohne den Paramter code aufgerufen, so wird angenommen, dass der Seitenname dem Namen eines Sternensystems entspricht.

Soll die Struktur eines astronomischen Objekts erstellt werden, so ist der Paramter code zwingend erforderlich.

Der Wert von code hat meistens die Form von: STERNENSYSTEM.TYP.NAME.

Beispielcode des Planeten Erde: SOL.PLANETS.EARTH

Kopiervorlage[Bearbeiten]

{{Sternensystem/Struktur}}

Kopiervorlage astronomisches Objekt[Bearbeiten]

{{Sternensystem/Struktur
|code=SOL.PLANETS.MARS
}}

Beispiel: Struktur des Planeten Mars[Bearbeiten]

🌍 Sol IV

local SternensystemStruktur = {}

local starsystem = require( 'Module:Sternensystem' )
local celestialObject = require( 'Module:CelestialObject' )

local pageName

local system
local celestialObjects
local structure
local types = {}

-- Starsystem
local function getStarsystemData()
	local selector = pageName
	if mw.getCurrentFrame():getParent().args.code then
		selector = 'Starmap Code::' .. mw.getCurrentFrame():getParent().args.code
	end

    local askData = {
        '[[' .. selector .. ']]',
        '?#-=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 data = mw.smw.ask( askData )

    if data ~= nil and data[ 1 ] ~= nil then
        system = data[ 1 ]

        return
    end

    error( 'Could not get Starsystem data' )
end

--
local function getCelestialObjects()
	local selector = '[[-Has subobject::' .. pageName .. ']]'
	if mw.getCurrentFrame():getParent().args.code then
		selector = '[[Sternensystemid::' .. system.system_id .. ']]'
	end

    local query = {
        selector .. '[[Starmap Code::+]]',
        '?#-=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',
        'sort=ID',
    }
    
    query.mainlabel = '-'

    local data = mw.smw.ask( query )

    if data ~= nil and data[ 1 ] ~= nil then
        celestialObjects = data

        return
    end

    error( 'Could not get Celestial Objects' )
end

-- Sorter
local function spairs( t, order )
    -- collect the keys
    local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end

    -- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys
    if order then
        table.sort(keys, function(a,b) return order(t, a, b) end)
    else
        table.sort(keys)
    end

    -- return the iterator function
    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end

-- This is just plain ugly
-- Sorts "recursively"
local function subSort()
    local copy = celestialObjects

    local function subSortWrap( object, container )
        if container[ object.parent_id ] ~= nil then
            container[ object.parent_id ].children[ tonumber(object.id) ] = object

            return true
        else
            for _, parent in pairs( container ) do
                subSortWrap( object, parent.children )
            end
        end
    end

    local i = 0
    while i < 4 do
        if #copy == 0 then
            --return
        end

        i = i + 1

        for id, object in pairs( copy ) do
            if structure[ system.id ].children[ object.parent_id ] ~= nil then
                structure[ system.id ].children[ object.parent_id ].children[ tonumber(object.id) ] = object

                table.remove( copy, id )
            else
                for _, parent in pairs( structure[ system.id ].children ) do
                    if subSortWrap( object, parent.children ) then

                        table.remove( copy, id )
                    end
                end
            end
        end
    end
end

-- Filters from all celestial objects
-- those that have the upper most parent of 'system.id'
local function filterCelestialObjects()
	local filtered = {}
	
	local parentIds = {
		[ system.id ] = '',
	}
	
	for _, object in spairs( celestialObjects ) do
		if parentIds[ object.parent_id ] ~= nil then
			parentIds[ object.id ] = ''
		end
	end
	
	for _, object in spairs( celestialObjects ) do
		if parentIds[ object.parent_id ] ~= nil then
			filtered[ object.id ] = object
		end
	end
	
	return filtered
end

--
local function createDataStructure()
    structure = {
        [ system.id ] = system
    }
    
    types[ system.type ] = ''

    structure[ system.id ].children = {}

    for _, object in spairs( celestialObjects ) do
        object.children = {}
        
        if types[ object.type ] == nil then
        	types[ object.type ] = ''
    	end

        if object.parent_id == nil or object.parent_id == system.id then
        	if object.type == 'JUMPPOINT' then
        		object.id = object.id + 10000 --hack
    		end
            table.insert( structure[ system.id ].children, tonumber(object.id), object )
        end
    end

    subSort()
end

local iconMap = {
    ['SINGLE_STAR'] = '🌌',
    ['BINARY'] = '✨',
    ['JUMPPOINT'] = '⭕',
    ['PLANET'] = '🌍',
    ['ASTEROID_BELT'] = '💫',
    ['ASTEROID_FIELD'] = '☄️️',
    ['SATELLITE'] = '🌑',
    ['MANMADE'] = '🛰️',
    ['STAR'] = '🌟',
    ['BLACKHOLE'] = '⚫',
    ['POI'] = '🔎',
}

local typeTranslation = {
    ['SINGLE_STAR'] = 'Sternensystem',
    ['BINARY'] = 'Binärsystem',
    ['PLANET'] = 'Planet',
    ['ASTEROID_BELT'] = 'Asteroidengürtel',
    ['ASTEROID_FIELD'] = '️Asteroidenfeld',
    ['SATELLITE'] = 'Mond',
    ['MANMADE'] = '️Raumstation',
    ['STAR'] = 'Stern',
    ['JUMPPOINT'] = 'Sprungpunkt',
    ['BLACKHOLE'] = 'Schwarzes Loch',
    ['POI'] = 'Point of Intereset',
}

local function makeEntry( object, infoTable )
    local icon = ''
    if iconMap[ object.type ] ~= nil then
    	icon = string.format(
    		'<span title="%s">%s</span>',
    		typeTranslation[ object.type ],
    		iconMap[ object.type ]
    	)
    end

    local text = celestialObject.getName( object.designation, true )

    if object.name ~= nil then
    	text = text .. '|' .. text .. ': ' .. object.name
	end

    if object.type == 'STAR' or object.type == 'BLACKHOLE' then
    	local starPage = mw.title.new( object.designation, 0 )

    	if starPage.exists then
    		if object.type == 'STAR' then
        		text = object.designation .. ' (Stern)'
    		else
    			text = object.designation .. ' (Schwarzes Loch)'
    		end
    	end
    end

    text = '[[' .. text .. ']]'

    if object.type == 'ASTEROID_BELT' or object.type == 'ASTERPOD_FIELD' then
        text = object.designation
    end

    if object.type == 'JUMPPOINT' then
    	local split = mw.text.split( object.designation, ' - ', true )

        text = '[[' .. starsystem.cleanJumppointDestination( split[ 2 ] ) .. ']]'
    end
    
    infoTable = infoTable
        :tag( 'div', {parent = infoTable} )
            :addClass( 'system-entry' )
            :addClass( 'system-entry-' .. object.type )
            :tag( 'span' )
                :addClass( 'system-entry-label' )
                :wikitext( icon .. ' ' .. text )
            :done()

    if type( object.children ) == 'table' then
        for _, child in spairs( object.children ) do
            infoTable = makeEntry( child, infoTable )
        end
    end

    return infoTable:done()
end

--
local function makeHtml()
	local headText = pageName
	if mw.getCurrentFrame():getParent().args.code then
		headText = system.designation
	end
	
	local icon = string.format(
		'<span title="%s">%s</span>',
		typeTranslation[ system.type ],
		iconMap[ system.type ]
	)

    local infoTable = mw.html.create( 'div' )
        :addClass( 'system-wrapper' )
        :addClass( 'mw-collapsible mw-collapsed' )
            :tag( 'span' )
                :addClass( 'system-label' )
                :wikitext( icon .. ' [[' .. headText .. ']]' )
            :done()
            :tag( 'div' )
                :addClass( 'mw-collapsible-content' )
                :addClass( 'system-content' )

    for _, object in spairs( structure[ system.id ].children ) do
        infoTable = makeEntry( object, infoTable )
    end
    
    infoTable = infoTable
        :tag( 'div', {parent = infoTable} )
            :addClass( 'system-entry' )
            :addClass( 'system-entry-INFO' )
            :tag( 'span' )
                :addClass( 'system-entry-label' )
                :addClass( 'mw-customtoggle-legend' )
                :wikitext( 'Legende' )
            :done()
            :tag( 'div' )
            	:attr( 'id', 'mw-customcollapsible-legend' )
            	:addClass( 'mw-collapsible mw-collapsed' )

    for key, _ in pairs( types ) do
        infoTable = infoTable
        	:tag( 'span' )
                :addClass( 'system-entry-label' )
                :wikitext( iconMap[ key ] .. ' ' .. typeTranslation[ key ] )
            :done()
    end  

    return infoTable:allDone()
end

function SternensystemStruktur.output()
    if mw.getCurrentFrame():getParent().args.name ~= nil then
        pageName = mw.getCurrentFrame():getParent().args.name
    else
        pageName = mw.title.getCurrentTitle().subpageText
    end

    getStarsystemData()
    getCelestialObjects()

	if mw.getCurrentFrame():getParent().args.code then
		celestialObjects = filterCelestialObjects()
	end

    createDataStructure()

    return tostring( makeHtml() )
end

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