Module:Infobox System: Difference between revisions

From AEGIS
AEGIS>Pookawboo
No edit summary
 
m (1 revision imported)
(No difference)

Revision as of 16:42, 31 March 2024

Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:Infobox System/doc. Changes can be proposed in the talk page.
Function list
L 13 — hyperlaneLink
L 20 — tablelength
L 26 — System.main
L 255 — getAstroSection

local System = {}
local infobox = require( 'Module:Infobox' ):new({
	class = 'system',
	externallink = true
})
local tabber = require( 'Module:Tabber' ).renderTabber
local hatnote = require( 'Module:Hatnote' )._hatnote
local systems = mw.loadJsonData( 'Module:System/data.json' )
local systemclasses = mw.loadJsonData( 'Module:Infobox System/systemclasses.json' )
local planetclasses = mw.loadJsonData( 'Module:Infobox System/planetclasses.json' )
local beltclasses = mw.loadJsonData( 'Module:Infobox System/beltclasses.json' )

function hyperlaneLink(systemid, systemname)
	local urlName = string.gsub(systemname, "%s+", "_")
	systemid = string.gsub(systemid, "%s+", "")
	local link = "<span class='plainlinks'>[" .. mw.site.server .. "/index.php?action=edit&redlink=1&preload=Template:Systems/Preload&preloadparams%5B%5D=" .. systemid .. "&title=" .. urlName .. " To " .. systemname .. "]</span>"
	return link
end

function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

function System.main(frame)
	
	local parentFrame = frame:getParent()
	local parentArgs = parentFrame.args
	
    if parentArgs.id == nil then
		return hatnote( mw.ustring.format(
				'A required field (%s) is missing!',
				'id'
			),
			{ icon = 'WikimediaUI-Error.svg', extraclasses = 'error' }
		)
    end
    
    local filename = mw.ustring.format( 'system%s.json!', parentArgs.id )
    local sytemdata = {}
	local externaldata, errors = mw.ext.externalData.getFileData { 
		directory = "systemdata", 
		["file name"] = "system" .. parentArgs.id .. ".json", 
		format = "JSON",
		data = "data=__json"
	}
	if externaldata == nil then 
		return hatnote( mw.ustring.format(
				'Invalid system ID (%s)!',
				parentArgs.id
			),
			{ icon = 'WikimediaUI-Error.svg', extraclasses = 'error' }
		)
	else
		systemdata = externaldata.data[1]
	end
	

	local sortedPlanets = {}

	for k,v in pairs(systemdata.planets) do
		table.insert(sortedPlanets, v)
	end

	table.sort(
		sortedPlanets, 
		function(a,b) 
			return a.id < b.id
		end
	)
	
	local basicsTable = {}
	local systemsTable = {}
	local speciesTable = {}
	local techTable = {}

	-- Render Header Box --

	local imageURL = mw.ustring.format(
		'%s/map/images/systemmaps/system%s.jpg',
		mw.site.server,
		systemdata.id
		)
	infobox:renderImage( imageURL )

	if parentArgs.nation then
		infobox:renderIndicator( {
			data = 'Claimed',
			desc = '[[' .. parentArgs.nation .. ']]',
			class = "claimed"
		} )
	else 
		infobox:renderIndicator( {
			data = 'Unclaimed',
			class = "unclaimed"
		} )
	end


	infobox:renderHeader( {
		title = systemdata.name,
	} )

  local planetcount = 0
  local asteroidcount = 0
  local mooncount = 0

  local planetsection = {}
  local asteroidsection = {}

  for k,v in pairs(sortedPlanets) do 
  	if v.type == 'asteroid' then
  		asteroidcount = asteroidcount + 1
			table.insert(
				asteroidsection,
				infobox:renderItem( {
					data = "[[" .. v.name .. "]] (" .. planetclasses[v.class].name .. ")"
				} )
			)
  	end
  		if v.type == 'planet' then
  			planetcount = planetcount + 1
			table.insert(
				planetsection,
				infobox:renderItem( {
					data = "[[ " .. v.name .. "]] (" .. planetclasses[v.class].name .. ")"
				} )
			)
  		end
  		if v.type == 'moon' then
  			mooncount = mooncount + 1

  			local nk, nv = next(sortedPlanets, k)
  			local moonClass = "moonItem"
  			
  			if nk then 
  				if nv.moon_of ~= v.moon_of then
  					moonClass = "moonItemEnd"
  				end
  			else 
  				moonClass = "moonItemEnd"
  			end
  			
			table.insert(
				planetsection,
				infobox:renderItem( {
					data = "<div class='" .. moonClass .. "'>[[" .. v.name .. "]] (" .. planetclasses[v.class].name .. ")</div>"
				} )
			)
  		end
  end
  
  local hyperlanesection = {}
  
  for k,v in pairs(systemdata.routes) do
  			local link = hyperlaneLink(v.to, systems[v.to].name)
			table.insert(
				hyperlanesection,
				infobox:renderItem( {
					data = link
				} )
			)
  end
  
	local sortedBelts = {}
	local beltCount = 0

	for k,v in pairs(systemdata.belts) do
		beltCount = beltCount + 1
		table.insert(sortedBelts, v)
	end

	table.sort(
		sortedBelts, 
		function(a,b) 
			return a.orbit < b.orbit
		end
	)
  
  local beltsection = {}
  local beltIndex = 1
  
  for k,v in pairs(sortedBelts) do
  			
  			local beltLabel = beltclasses[v.type].name

  			if beltCount > 1 and v.name == nil then
  				if beltCount == 2 and beltIndex == 1 then beltLabel = "Inner" end
  				if beltCount == 2 and beltIndex == 2 then beltLabel = "Outer" end
  				if beltCount == 3 and beltIndex == 1 then beltLabel = "Inner" end
  				if beltCount == 3 and beltIndex == 2 then beltLabel = "Middle" end
  				if beltCount == 3 and beltIndex == 3 then beltLabel = "Outer" end
				if beltCount == 4 and beltIndex == 1 then beltLabel = "Inner" end
  				if beltCount == 4 and beltIndex == 2 then beltLabel = "Middle" end
  				if beltCount == 4 and beltIndex == 3 then beltLabel = "Outer" end
  				if beltCount == 4 and beltIndex == 4 then beltLabel = "Outermost" end
  				if beltCount == 4 and beltIndex == 1 then beltLabel = "Innermost" end
				if beltCount == 4 and beltIndex == 2 then beltLabel = "Inner" end
  				if beltCount == 4 and beltIndex == 3 then beltLabel = "Middle" end
  				if beltCount == 4 and beltIndex == 4 then beltLabel = "Outer" end
  				if beltCount == 4 and beltIndex == 5 then beltLabel = "Outermost" end
  				beltLabel = beltLabel .. " (" .. beltclasses[v.type].name .. ")"
  				beltIndex = beltIndex + 1
  			end 
  			
  			if v.name then 
  				beltLabel = v.name .. " (" .. beltclasses[v.type].name .. ")" 
  			end
  			
			table.insert(
				beltsection,
				infobox:renderItem( {
					data = "[[" .. systemdata.name .. " " .. beltLabel .. " Asteroid Belt" .. "|" .. beltLabel .. "]]"
				} )
			)
  end


	local systemClass = systemclasses[systemdata.class]


	basicsTable = {
		infobox:renderItem( {
			label = 'Class',
			data = systemClass.label
		} ),
		infobox:renderItem( {
			label = 'Coordinates',
			data = systemdata.x .. " × " .. systemdata.y
		} ),
		infobox:renderItem( {
			label = 'Hyperlanes',
			data = tablelength(systemdata.routes)
		} ),
		infobox:renderItem( {
			label = 'Planets (Moons)',
			data = planetcount .. " (" .. mooncount .. ")"
		} ),
		infobox:renderItem( {
			label = 'Asteroids',
			data = asteroidcount
		} ),
		infobox:renderItem( {
			label = 'Belts',
			data = beltCount
		} ),
	}
	
	infobox:renderSection( {
		content = table.concat( basicsTable ),
		col = 3
	} )

	local function getAstroSection()
		local tabberData = {}
		local section

		tabberData[ 'label1' ] = 'Planets'

		tabberData[ 'content1' ] = infobox:renderSection( { content = planetsection, class = "planetsection" }, true )
		
		tabberData[ 'label2' ] = 'Hyperlanes'

		tabberData[ 'content2' ] = infobox:renderSection( { content = hyperlanesection, class = "hyperlanesection" }, true )
		
		tabberData[ 'label3' ] = 'Asteroids'

		tabberData[ 'content3' ] = infobox:renderSection( { content = asteroidsection, class = "asteroidsection" }, true )

		tabberData[ 'label4' ] = 'Belts'

		tabberData[ 'content4' ] = infobox:renderSection( { content = beltsection, class = "beltsection" }, true )

		return tabber( tabberData )
	end


	infobox:renderSection( {
		class = 'infobox__section--tabber',
		content = getAstroSection(),
	} )


local shortDesc = ''

if systemClass.type == 'single' then 
	shortDesc = mw.ustring.format(
		'Single %s Class System',
		systemClass.label
		)
end
if systemClass.type == 'binary' or systemClass.type == 'trinary' then 
	shortDesc = mw.ustring.format(
		'%s Class System',
		systemClass.label
		)
end
if systemClass.type == 'special' then 
	shortDesc = mw.ustring.format(
		'%s System',
		systemClass.label
		)
end

frame:callParserFunction( 'SHORTDESC', shortDesc)

local footerContents = 'File No ' .. parentArgs.id

infobox:renderFooter( {
	content = footerContents
})

	return infobox:renderInfobox( nil, systemdata.name)
end

return System