PC Gaming Shelter
An archive dedicated to preserving PC Gaming history and more

Module:Universe

From PC Gaming Shelter
Revision as of 11:46, 31 July 2026 by WikiVisor (talk | contribs)

Documentation for this module may be created at Module:Universe/doc

local utils = require( 'Module:Utils' )

local p = {}

local function getCurrentGame()
	local title = mw.title.getCurrentTitle()
	local game  = title.rootText
	local resource = title.subpageText
	local data = {
		['game'] = game,
		['resource'] = resource,
	}
	return data
end

local function setDisplayTitle(frame, title)
	local displayTitle = frame:preprocess(string.format('{{DISPLAYTITLE:%s|noreplace, noerror}}', title))
	return displayTitle
end

local function escapePattern(value)
	return value:gsub('([^%w])', '%%%1')
end

local function removeGameFromLabels(results, game)
	local gamePattern = escapePattern(game)

	for index, link in ipairs(results) do
		results[index] = link:gsub(
			'|' .. gamePattern .. '%s+',
			'|'
		)
	end

	return results
end

local function renameLabels(results)
	local labels = {
		Mods = 'Mod Archive',
		Players = 'Player Roster',
		Clans = 'Clan History',
	}

	for i, link in ipairs(results) do
		results[i] = link:gsub('|([^%]]+)%]%]', function(label)
			return '|' .. (labels[label] or label) .. ']]'
		end)
	end

	return results
end

local function sortSubpages(results)
	local order = {
		Mods = 1,
		Players = 2,
		Clans = 3,
	}

	table.sort(results, function(a, b)
		-- Extract the final subpage name from the link target.
		local targetA = a:match('^%[%[:.-/([^/|%]]+)|')
		local targetB = b:match('^%[%[:.-/([^/|%]]+)|')

		local rankA = order[targetA] or math.huge
		local rankB = order[targetB] or math.huge

		if rankA ~= rankB then
			return rankA < rankB
		end

		-- Keep all remaining subpages alphabetically ordered.
		return (targetA or a) < (targetB or b)
	end)

	return results
end

local function getSubpages(page)
	local subpages = mw.smw.ask({
		'[[Has parent page::' .. page .. ']][[Modification date::+]]'
	}) or {}
	
	local subpageTable = {}
	
	for _, subpage in ipairs(subpages) do
		local link = subpage[1]

		if link then
			table.insert(subpageTable, link)
		end
	end

	removeGameFromLabels(subpageTable, page)
	renameLabels(subpageTable)
	sortSubpages(subpageTable)

	if #subpageTable == 0 then
		return ''
	end

	local html = mw.html.create()
	
	local universe = html:tag('div'):attr('id', 'universeMenu'):wikitext(' | ' .. table.concat(subpageTable, ' | '))
	
	return tostring(html)
end

local function formatCellValue(value)
	if value == nil then
		return ''
	end

	if type(value) == 'table' then
		local values = {}

		for _, item in ipairs(value) do
			table.insert(values, tostring(item))
		end

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

	return tostring(value)
end

local function renderContentTable(content)
	local tableNode = mw.html.create('table')
	tableNode
		:addClass('wikitable')
		:addClass('sortable')

	local header = tableNode:tag('tr')

	header:tag('th'):wikitext('Page')
	header:tag('th'):wikitext('Game')
	header:tag('th'):wikitext('Asset Type')
	header:tag('th'):wikitext('Game Mode')
	header:tag('th'):wikitext('Developer')
	header:tag('th'):wikitext('Release Date')
	header:tag('th'):wikitext('Status')
	header:tag('th'):wikitext('Description')
	header:tag('th'):wikitext('Game Version')

	for _, row in ipairs(content or {}) do
		local tr = tableNode:tag('tr')

		tr:tag('td'):wikitext(formatCellValue(row[1]))
		tr:tag('td'):wikitext(formatCellValue(row['Related game']))
		tr:tag('td'):wikitext(formatCellValue(row['Has asset type']))
		tr:tag('td'):wikitext(formatCellValue(row['Has game mode']))
		tr:tag('td'):wikitext(formatCellValue(row['Has developer']))
		tr:tag('td'):wikitext(formatCellValue(row['Has release date']))
		tr:tag('td'):wikitext(formatCellValue(row['Has status']))
		tr:tag('td'):wikitext(formatCellValue(row['Has short description']))
		tr:tag('td'):wikitext(formatCellValue(row['Works on game version']))
	end

	return tableNode
end

function p.Mods(frame)
	local data     = getCurrentGame()
	local game     = data.game
	local resource = data.resource
	
	mw.smw.set({
		['Has parent page'] =  game
	})

	local query = {
		'[[Category:Mods||Maps||Skins||Utilities]][[Related game::' .. game .. ']]',
		'?Related game',
		'?-Has asset type',
		'?Has game mode',
		'?Has developer',
		'?Has release date#-F[Y]',
		'?Has status', 
		'?Has short description',
		'?Works on game version',
	}
	local content = mw.smw.ask(query)
	
	local forminput = frame:callParserFunction({ name = '#forminput', '', args = {
		'form=Mod',
		'query string=Mod[Related game]=' .. game,
		'button text=Create or Edit Mod',
		'autofocus=no'
	}}) 

	setDisplayTitle(frame, string.format('%s %s', game, resource))
	
	local html = mw.html.create()
	html:wikitext(getSubpages(game))
	html:wikitext(forminput)
	html:node(renderContentTable(content))
	
	return html
end

function p.Players(frame)
	local data     = getCurrentGame()
	local game     = data.game
	local resource = data.resource

	mw.smw.set({
		['Has parent page'] =  game
	})

	local query = {
		'[[Category:Players]][[Plays game::' .. game .. ']]'		
	}
	
	local content = mw.smw.ask(query)
	
	local forminput = frame:callParserFunction({ name = '#forminput', '', args = {
		'form=Player',
		'query string=Player[Plays game]=' .. game,
		'button text=Create or Edit Player Entry',
		'autocomplete on category=Players',
		'autofocus=no'
	}})

	setDisplayTitle(frame, string.format('%s %s', game, resource))

	local html = mw.html.create()
	html:wikitext(getSubpages(game))
	html:wikitext(forminput)
	
	return html
end

function p.Clans(frame)
	local data     = getCurrentGame()
	local game     = data.game
	local resource = data.resource

	mw.smw.set({
		['Has parent page'] =  game
	})

	local query = {
		'[[Category:Communities]][[Related game::' .. game .. ']]'		
	}
	
	local content = mw.smw.ask(query)
	
	local forminput = frame:callParserFunction({ name = '#forminput', '', args = {
		'form=Community',
		'query string=Community[Related game]=' .. game,
		'button text=Create or Edit Community Entry',
		'autocomplete on category=Communities',
		'autofocus=no'
	}})

	setDisplayTitle(frame, string.format('%s %s', game, resource))

	local html = mw.html.create()
	html:wikitext(getSubpages(game))
	html:wikitext(forminput)
	
	return html
end

function p.test()
	local page = 'Star Trek: Voyager - Elite Force'	

	local query = {
		'[[Category:Mods||Maps||Skins||Utilities]][[Related game::' .. page .. ']]',
		'?Related game',
		'?Has asset type',
		'?Has game mode',
		'?Has developer',
		'?Has release date#-F[Y]',
		'?Has status', 
		'?Has short description',
		'?Works on game version',
	}
	
	local content = mw.smw.ask(query)
	
	return content
end

return p