Module:Sandbox/MonkeysHK/Mayor/UI

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

local string, table, yesno, mayorMdl, slotutils, _ = loader.require('String', 'Table', 'Yesno', 'Sandbox/MonkeysHK/Mayor', 'Inventory slot/Utils', 'LibraryUtil') local using_ver = require('Module:Sandbox/MonkeysHK/Mayor/Version') local vers = { ['Module:Sandbox/MonkeysHK/Mayor/Moby'] = {229, 240}, ['Module:Sandbox/MonkeysHK/Mayor/0.14'] = {226, 228}, ['Module:Sandbox/MonkeysHK/Mayor/0.11'] = {113, 225}, ['Module:Sandbox/MonkeysHK/Mayor/Fiesta'] = {102, 112}, ['Module:Sandbox/MonkeysHK/Mayor/0.9'] = {88, 101}, } local mayorData, eDT = loader.loadData(using_ver, '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, experimentalMode) 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 experimentalMode and ():format(year) 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 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 .. (experimentalMode and  or ) 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, experimentalMode) local slot1, slot2, mayorName = p._elecResultSlot(year) if not slot1 then return experimentalMode and ():format(year) or '\'\' 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 .. (experimentalMode and  or ) 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%swas elected.'):format(				year,				resultStr:gsub(',', '\\,'),				#(mayorName) > 5 and ('/&7' .. mayorName .. ' ') or (' ' .. mayorName .. '/&7')			)			return slot1, slot2, mayorName end end end

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

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

function p._temporary_generator_12 local ret = {} for i = vers[using_ver][2], vers[using_ver][1], -1 do ret[#ret + 1] = '|-|' .. i .. '=\n Election Year ' .. i .. ' \n=== Election Result ===\n' .. p._mayorUI(i, true) ret[#ret + 1] = '=== Election ===\n' .. p._mayorElectionUI(i, true) end return ('Gen: %s\nRange: [%s, %s]\n\n%s'):format(using_ver, vers[using_ver][1], vers[using_ver][2], table.concat(ret, '\n\n')) end

return p