Please note that some information on this wiki is still based on the demo version.
If you notice any errors, feel free to correct them.

Module:UnitsRelicsTable: Difference between revisions

From Super Fantasy Kingdom Official Wiki
imported>Cadaei
handling nil
imported>Cadaei
m Cadaei renamed page Module:HeroesRelicsTable to Module:UnitsRelicsTable without leaving a redirect: Standardizing the title
 
(7 intermediate revisions by 2 users not shown)
Line 6: Line 6:


local tables = 'Units'
local tables = 'Units'
local fields = 'name, requiredForRelic, benefitsFromRelics'
local fields = 'name, requiredForRelics, benefitsFromRelics'
local args = {
local args = {
orderBy = 'name'
orderBy = 'name'
Line 20: Line 20:
for i = 1, #units do
for i = 1, #units do
local unit = units[i]
local unit = units[i]
local requiredForRelic = ''
local requiredForRelics = ''
if (unit.requiredForRelic or '') ~= '' then
if (unit.requiredForRelics or '') ~= '' then
requiredForRelic = p.itemLink(unit.requiredForRelic, nil)
local requiredForRelicsEntries = {}
for relic in string.gmatch(unit.requiredForRelics, '%s*([^,]+)') do
table.insert(requiredForRelicsEntries, p.itemLink(relic, nil))
end
requiredForRelics = table.concat(requiredForRelicsEntries, '</br>')
end
end
local rowColumns = {p.itemLink(unit.name, nil), requiredForRelic}
local rowColumns = {p.itemLink(unit.name, nil), requiredForRelics}
for r=1,#allRelics do
for r=1,#allRelics do
local benefitsFromRelic = ''
local benefitsFromRelic = ''
Line 37: Line 41:
end
end
return '<table class="wikitable sortable">'..
return '<table class="wikitable sortable stickyheader stickyfirstcol">'..
'<tr><th>'..table.concat(headerColumns, '</th><th>')..'</th></tr>'..
'<tr><th>'..table.concat(headerColumns, '</th><th>')..'</th></tr>'..
'<tr>'..table.concat(unitRows, '</tr><tr>')..'</tr></table>'
'<tr>'..table.concat(unitRows, '</tr><tr>')..'</tr></table>'

Latest revision as of 20:41, 20 October 2025


local p = {}
local cargo = mw.ext.cargo

function p.Main( frame )
	local allRelics = p.getAllRelics()

	local tables = 'Units'
	local fields = 'name, requiredForRelics, benefitsFromRelics'
	local args = {
		orderBy = 'name'
	}
	local units = cargo.query( tables, fields, args )
	
	local headerColumns = {'Units', 'Unlocks'}
	for i=1,#allRelics do
		table.insert(headerColumns, p.itemLink(allRelics[i], ''))
	end
	
	local unitRows = {}
	for i = 1, #units do
		local unit = units[i]
		local requiredForRelics = ''
		if (unit.requiredForRelics or '') ~= '' then
			local requiredForRelicsEntries = {}
			for relic in string.gmatch(unit.requiredForRelics, '%s*([^,]+)') do
				table.insert(requiredForRelicsEntries, p.itemLink(relic, nil))
			end
			requiredForRelics = table.concat(requiredForRelicsEntries, '</br>')
		end
		
		local rowColumns = {p.itemLink(unit.name, nil), requiredForRelics}
		for r=1,#allRelics do
			local benefitsFromRelic = ''
			if unit.benefitsFromRelics ~= nil and string.find(unit.benefitsFromRelics, allRelics[r]) then
				benefitsFromRelic = '✔'
			end
			table.insert(rowColumns, benefitsFromRelic)
		end
		
		table.insert(unitRows, '<td>'..table.concat(rowColumns, '</td><td>')..'</td>')
	end
	
	return '<table class="wikitable sortable stickyheader stickyfirstcol">'..
		'<tr><th>'..table.concat(headerColumns, '</th><th>')..'</th></tr>'..
		'<tr>'..table.concat(unitRows, '</tr><tr>')..'</tr></table>'
end

function p.getAllRelics()
	-- return a table with all relic names
	local tables = 'Relics'
	local fields = 'name'
	local args = {
		orderBy = 'name'
	}
	local results = cargo.query( tables, fields, args )
	local allRelics = {}
	
	for r = 1, #results do
		table.insert(allRelics, results[r].name)
	end
	return allRelics
end

function p.itemLink(link, name)
	if link == nil or link == '' then
		return ''
	end
	
	local linkPart = ' [['..link..']]'
	local imageLink = ''
	if name ~= nil then
		if name == '' then
			linkPart = ''
			imageLink = '|link='..link
		else
			linkPart = ' [['..link..'|'..name..']]'
		end
	end
	return '[[File:'..link..'.png|16x16px|class=itemlink|alt='..imageLink..']]'..linkPart
end

return p