Difference between revisions of "Module:Tables"

From Foundation - Wiki
Jump to navigation Jump to search
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
local com = require('Module:Common')
local com = require('Module:Common')
local a = require('Module:Args')
local a = require('Module:Arguments')
local i = require('Module:Icon')
local i = require('Module:Icon')
local p = {}
local p = {}


function p.buildings(frame)
function p.buildings(frame)
local buildingList = mw.load('Module:Buildings')
local buildingList = mw.loadData('Module:Buildings')
local args = a.getArgs(frame)
local args = a.getArgs(frame)
local buildingTable = mw.html.create('table')
local buildingTable = mw.html.create('table')
Line 32: Line 32:
:done()
:done()
for k, v in pairs (dataTable) do
for k, v in ipairs(dataTable) do
local titleParts = mw.text.split(v, '%s')
for k2, part in ipairs(titleParts) do
titleParts[k2] = part:upper():sub(1,1) .. part:sub(2)
end
local buildString = '[[%s|%s]]'
local pageTitle = table.concat(titleParts, ' ')
local u = string.upper(string.sub(v,1,1))
local buildingName = buildString:format(pageTitle, u .. string.sub(v, 2))
local c = buildingList[v]
local getPreReq = {}
local getPreReq = {}
table.insert(getPreReq, v.building .. '|building')
table.insert(getPreReq, c.building)
table.insert(getPreReq, v.population .. '|population')
table.insert(getPreReq, c.population)
table.insert(getPreReq, v.splendor .. '|splendor')
if c.splendor then
table.insert(getPreReq, v.influence .. '|influence')
for k, v in pairs(c.splendor) do
table.insert(getPreReq, i.icon(k .. ' splendor', v, nil, nil, 'en'))
end
end
if c.influence then
for k, v in pairs(c.influence) do
table.insert(getPreReq, i.icon(k .. ' influence', v, nil, nil, 'en'))
end
end
local preReq = ''
local preReq = ''
for k, v in ipairs(getPreReq) do
for k, v in ipairs(getPreReq) do
local tag = string.sub(v, string.find(v, '|'))
preReq = preReq .. v
if tag == 'splendor' or tag == 'influence' then
if k ~= #getPreReq then
preReq = preReq i.icon(tag .. ' ' .. k, string.sub(v, string.sub(v, 1,
preReq = preReq .. '<br />'
string.find(v, '|') - 1)), nil, nil, 'en')
else
preReq = preReq .. string.sub(v, string.sub(v, 1,
string.find(v, '|') - 1))
end
end
end
end
local production = ''
if c.requirements then
production = '\'\'\'Requires:\'\'\'<br />'
for k, v in pairs(c.requirements) do
production = production .. i.icon(v, nil, nil, v, 'en') .. '<br />'
end
end
if c.products then
production = production ..'\'\'\'Produces:\'\'\'<br />'
for k, v in ipairs(c.products) do
production = production .. i.icon(v, nil, nil, v, 'en') .. '<br />'
end
end
if not production == '' then
local l = string.len(production)
production = string.sub(production, 1, l - 6)
end
local construction = ''
if c.construction then
for k, v in pairs(c.construction) do
construction = construction .. i.icon(k, v, nil, k, 'en') .. '<br />'
end
local l = string.len(construction)
construction = string.sub(construction, 1, l -6)
end
local newRow = mw.html.create('tr')
local newRow = mw.html.create('tr')
newRow:tag('th')
newRow:tag('th')
:wikitext(k)
:wikitext(buildingName)
:done()
:tag('td')
:wikitext(preReq)
:done()
:tag('td')
:wikitext(production)
:done()
:tag('td')
:wikitext(construction)
:done()
:done()
buildingTable:node(newRow)
end
end
return buildingTable
end
end


return p
return p

Latest revision as of 14:46, 21 February 2024

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

local com = require('Module:Common')
local a = require('Module:Arguments')
local i = require('Module:Icon')
local p = {}

function p.buildings(frame)
	local buildingList = mw.loadData('Module:Buildings')
	local args = a.getArgs(frame)
	local buildingTable = mw.html.create('table')
	local dataTable = {}
	for k, v in pairs(buildingList) do
		if v.group == args['group'] or not args['group'] then
			table.insert(dataTable, k)
		end
	end
	
	table.sort(dataTable)
	buildingTable:addClass('wikitable sortable')
		:tag('tr')
			:tag('th')
				:wikitext('Name')
				:done()
			:tag('th')
				:wikitext('Prerequisites')
				:done()
			:tag('th')
				:wikitext('Production')
				:done()
			:tag('th')
				:wikitext('Construction Requirements')
				:done()
			:done()
	
	for k, v in ipairs(dataTable) do
		local titleParts = mw.text.split(v, '%s')
		for k2, part in ipairs(titleParts) do
			titleParts[k2] = part:upper():sub(1,1) .. part:sub(2)
		end
		local buildString = '[[%s|%s]]'
		local pageTitle = table.concat(titleParts, ' ')
		local u = string.upper(string.sub(v,1,1))
		local buildingName = buildString:format(pageTitle, u .. string.sub(v, 2))
		local c = buildingList[v]
		local getPreReq = {}
		table.insert(getPreReq, c.building)
		table.insert(getPreReq, c.population)
		if c.splendor then
			for k, v in pairs(c.splendor) do
				table.insert(getPreReq, i.icon(k .. ' splendor', v, nil, nil, 'en'))
			end
		end
		
		if c.influence then
			for k, v in pairs(c.influence) do
				table.insert(getPreReq, i.icon(k .. ' influence', v, nil, nil, 'en'))
			end
		end
		
		local preReq = ''
		for k, v in ipairs(getPreReq) do
			preReq = preReq .. v
			if k ~= #getPreReq then
				preReq = preReq .. '<br />'
			end
		end
		
		local production = ''
		if c.requirements then 
			production = '\'\'\'Requires:\'\'\'<br />'
			for k, v in pairs(c.requirements) do
				production = production .. i.icon(v, nil, nil, v, 'en') .. '<br />'
			end
		end
		
		if c.products then
			production = production ..'\'\'\'Produces:\'\'\'<br />'
			for k, v in ipairs(c.products) do
				production = production .. i.icon(v, nil, nil, v, 'en') .. '<br />'
			end
		end
		
		if not production == '' then
			local l = string.len(production)
			production = string.sub(production, 1, l - 6)
		end
		
		local construction = ''
		
		if c.construction then
			for k, v in pairs(c.construction) do
				construction = construction .. i.icon(k, v, nil, k, 'en') .. '<br />'
			end
			local l = string.len(construction)
			construction = string.sub(construction, 1, l -6)
		end
		local newRow = mw.html.create('tr')
		newRow:tag('th')
			:wikitext(buildingName)
			:done()
		:tag('td')
			:wikitext(preReq)
			:done()
		:tag('td')
			:wikitext(production)
			:done()
		:tag('td')
			:wikitext(construction)
			:done()
		buildingTable:node(newRow)
	end
	
	return buildingTable
end

return p