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) 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 '\'\' 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 normal_title = string.gsub(normal_tt.title, ',', '\\,') local normal_text = string.gsubAll(normal_tt.text, ',', '\\,', 'Year #', 'Year ' .. displayed_year) v.mayor_simple = ('%s, none, %s, %s'):format(v[1], normal_title, normal_text) end local 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', }	return ret -- return ret .. ' Generated. ' end

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

function p._mayorUI(year) local slot1, slot2, mayorName = p._elecResultSlot(year) if not slot1 then return '\'\' UI is missing \'\'' end local page = 'Mayor/' .. mayorName local extPageTitle = mw.title.new(page, 10) if not extPageTitle.exists then error('Specified template does not exist, page: ' .. page) end local extPageContent = extPageTitle:getContent extPageContent = extPageContent:gsub(, slot1):gsub(, slot2:gsub('__PERCENT__', '%%%%')) -- return extPageContent return extPageContent .. ' Generated. '	-- return extPageContent .. ' Generated. ' 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%s__PERCENT__&8 ○ &e%s &evotes&8 | &%s%s'):format( candidateColors[val.order], (val.percent < 10 and '0%.1f' or '%.1f'):format(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

return p