Module:Mayor/UI

local loader = require('Module:Loader') local getArgs = require('Module:Arguments').getArgs

local string, table, yesno, mayorMdl, slotutils, _ = loader.require('String', 'Table', 'Yesno', 'Mayor', 'Inventory slot/Utils', 'LibraryUtil') local mayorData, eDT = loader.loadData('Mayor/Data', 'Mayor/Elections')

local electionsData, uiSpecialCases = eDT.elections, eDT.uiSpecialCases

local p = {}

local candidateColors = { 'c', 'a', 'b', 'e', 'd' } local numToEng = { 'one', 'two', 'three', 'four', 'five' }

-- For Template:Mayor Election UI function p.mayorElectionUI( frame ) local args = getArgs(frame) local year = args.year or args[1] return mw.getCurrentFrame:preprocess(p._mayorElectionUI(year)) end

function p._mayorElectionUI(year, simplemode) local function slotview(slot) local params = '' slot['image'] = nil for k, v in pairs(slot) do			params = ('%s|%s = %s'):format(params, k, v)		end return params end local function uiview_simple(tb) local params = tb[1] and ('|' .. tb[1]) or '' for k, v in pairs(tb) do			if k ~= 1 then params = ('%s\n|%s = %s'):format(params, k, v)			end end return (''):format(params) end local function uiview(tb) local params = '' for k, v in pairs(tb) do			params = ('%s|%s = %s'):format(params, k, v)		end return (''):format(params) end year = tonumber(year) local displayed_year = year local t = table.deepCopy(uiSpecialCases[year] or {}, true) if #t < 1 then local data = electionsData[year] if not data or type(data.data) ~= 'table' then return simplemode and '== Election Year ' .. year .. ' ==\n\'\' UI is missing \'\ or '\'\' UI is missing \'\ end data = data.data for candidate, candidateData in table.sortedPairsByValue(data, function(a, b)			return a.order < b.order		end) do			table.push(t, { candidate, dt = candidateData }) end end for i, v in ipairs(t) do		local info = mayorMdl.mayorTooltip(v[1], 'candidate', {			nums = v.dt.perks,			col = candidateColors[i],			ago = v.dt.last,			params = v.dt.params,		}) local normal_tt = slotutils._useTemplate(			{ table.merge({ id = 'THIS' }, info) }, 'T:Mayor Candidate'		)['THIS'] local voting_tt = slotutils._useTemplate(			{ table.merge({ id = 'THIS' }, info, 'Leading in votes!/') }, 'T:Mayor Candidate Voting'		)['THIS'] local normal_title = string.gsub(normal_tt.title, ',', '\\,') local normal_text = string.gsubAll(normal_tt.text, ',', '\\,', 'Year #', 'Year ' .. displayed_year) local voting_text = string.gsubAll(voting_tt.text, ',', '\\,', 'Year #', 'Year ' .. displayed_year) v.glasspane = ('Gray Stained Glass Pane, election-year-%s-candidate-%s; none, %s, %s'):format(displayed_year, numToEng[i], normal_title, voting_text) v.mayor_simple = ('%s, none, %s, %s'):format(v[1], normal_title, voting_text) v.mayor2 = ('%s, election-year-%s-candidate-%s; none, %s, %s'):format(v[1], displayed_year, numToEng[i], normal_title, voting_text) v.mayor1 = ('%s, election-year-%s-candidate-%s; none, %s, %s'):format(v[1], displayed_year, numToEng[i], normal_title, normal_text) end local function repl(txt, order, curr) return (order == curr) and txt:gsub('/&eClick to vote [^/]-$', '/&aYou voted for this candidate!') or txt end local ret if simplemode then ret = uiview_simple { 'Election', ['2, 1'] = t[1].mayor_simple, ['2, 3'] = t[2].mayor_simple, ['2, 5'] = t[3].mayor_simple, ['2, 7'] = t[4].mayor_simple, ['2, 9'] = t[5].mayor_simple, rows = 3, arrow = 'none', close = 'none', }	else ret = uiview { 'Election', ['2, 1'] = t[1].mayor1, ['2, 3'] = t[2].mayor1, ['2, 5'] = t[3].mayor1, ['2, 7'] = t[4].mayor1, ['2, 9'] = t[5].mayor1, id = 'election-page1-year-'..displayed_year, rows = 3, arrow = 'none', close = 'none', } .. table.concat(table.map(table.map(t, function(d)				return d[1]				end), function(can, i)			return uiview { ('Election, Year %s'):format(displayed_year), ['col 1'] = ('%s; 1, 5'):format(repl(t[1].glasspane, i, 1)), ['col 3'] = ('%s; 1, 5'):format(repl(t[2].glasspane, i, 2)), ['col 5'] = ('%s; 1, 5'):format(repl(t[3].glasspane, i, 3)), ['col 7'] = ('%s; 1, 5'):format(repl(t[4].glasspane, i, 4)), ['col 9'] = ('%s; 1, 5'):format(repl(t[5].glasspane, i, 5)), ['6, 1'] = repl(t[1].mayor2, i, 1), ['6, 3'] = repl(t[2].mayor2, i, 2), ['6, 5'] = repl(t[3].mayor2, i, 3), ['6, 7'] = repl(t[4].mayor2, i, 4), ['6, 9'] = repl(t[5].mayor2, i, 5), id = ('election-year-%s-candidate-%s'):format(displayed_year, numToEng[i]), hide = 'y', arrow = 'none', close = 'none', }		end)) end local endtext = '' if not simplemode then local endtext = table.map(t, function(v)			if mayorData.mayors[v[1]] then				if mayorData.mayors[v[1]].perks_listed then					return (' Candidate %s has perks that are revealed only when Elected. '):format(mayorMdl.getMayorLink(v[1]))				elseif mayorData.mayors[v[1]].type ~= 'regular' then					return (' Candidate %s is a %s mayor. '):format(mayorMdl.getMayorLink(v[1]), string._ucfirst(mayorData.mayors[v[1]].type))				end			end			-- (else return nil)		end) return string.wrapHtml(ret, 'div', { class = 'sbw-ui-tabber' }) .. table.concat(endtext) else return ('== Election Year %s ==\n%s'):format(			year, ret		) end end

function p.mayorUI( frame ) local args = getArgs(frame) local year = args.year or args[1] return p._mayorUI(year) end

function p._mayorUI(year) local slot1, slot2, mayorName = p._elecResultSlot(year) if not slot1 then return '\'\' UI is missing \'\'' end return mw.getCurrentFrame:expandTemplate{ title = 'Mayor/' .. mayorName, args = { slot1, slot2 } } .. '\'\' Note: This UI is recreated based on election data. The interface may not be exact. \'\'' end

function p._elecResultSlot(year) local res = mayorMdl._getElectionStats(year) if #res > 0 then local results = table.map(res, function(val, i)			if val.votes and val.percent and val.name and val.order then				return ('&%s%.2f%%&8 ○ &e%s &evotes&8 | &%s%s'):format( candidateColors[val.order], string._formatNum(val.percent), string._formatNum(val.votes), candidateColors[val.order], val.name )			end		end) if #results == 5 then local resultStr, mayorName = table.concat(results, '/'), res[1].name local info = mayorMdl.mayorTooltip(mayorName, 'mayor', {				nums = res[1].perks,				col = candidateColors[res[1].order],				params = res[1].params,			}) local normal_tt = slotutils._useTemplate(				{ table.merge({ id = 'THIS' }, info) }, 'T:Mayor'			)['THIS'] local normal_title = string.gsub(normal_tt.title, ',', '\\,') local normal_text = string.gsub(normal_tt.text, ',', '\\,') .. (mayorName:match('Jerry') and '//&9Perkpocalypse://&7<#list of perkpocalypse perks>' or '') local slot1 = ('Mayor %s, none, %s, %s'):format(				mayorName,				normal_title,				normal_text			) local slot2 = ('Jukebox, none; none, &bMayor Election Results, &8Year %s//%s//&7These are the votes for the/&7last election in which/&7%s was elected.'):format(				year,				resultStr:gsub(',', '\\,'),				mayorName			) return slot1, slot2, mayorName end end end

function p._temporary_generator local ret = {} for i = 240, 88, -1 do		ret[#ret + 1] = p._mayorElectionUI(i, true) end return table.concat(ret, '\n\n') end

return p