Module:Election results/sandbox

This is an old revision of this page, as edited by Frietjes (talk | contribs) at 23:50, 18 August 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
require('Module:No globals')
local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local index, cols = {}, 3
	local cand, party, seats, sc, vp = false, false, false, false, false
	local winner, winner_votes = {0, 0}, {0, 0}
	local rounds = 1
	local valid = {0, 0}
	local invalid = {tonumber(args.invalid) or 0, tonumber(args.invalid2) or 0}
	local electorate = {tonumber(args.electorate) or 0, tonumber(args.electorate2) or tonumber(args.electorate) or 0}
	local row
	for i = 1, 20 do
		if args['cand' .. i] then
			cand = true
			table.insert(index, i)
			if not party and args['party' .. i] then
				party = true
				cols = cols + 2
			end
			if not vp and args['vp' .. i] then
				vp = true
				cols = cols + 1
			end
			if args['votes' .. i] then
				local votesi = tonumber(args['votes' .. i]) or 0
				args['votes' .. i] = votesi
				if votesi > winner_votes[1] then
					winner[1] = i
					winner_votes[1] = votesi
				end
				valid[1] = valid[1] + votesi
			end
			if args['votes' .. i .. '_2'] then
				rounds = 2
				local votesi = tonumber(args['votes' .. i .. '_2']) or 0
				args['votes' .. i .. '_2'] = votesi
				if votesi > winner_votes[2] then
					winner[2] = i
					winner_votes[2] = votesi
				end
				valid[2] = valid[2] + votesi
			end
			if args['sc' .. i] then
				sc = true
			end
			if args['seats' .. i] then
				seats = true
			end
		end
	end
	cand = ((sc or seats) and false) or cand
	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('caption')
			:wikitext(args.caption)
	if vp then
		row = root:tag('tr')
		row
			:tag('th')
				:wikitext('President')
				:attr('colspan', 2)
				:attr('rowspan', (rounds > 1) and 2 or nil)
			:tag('th')
				:wikitext('Vice president')
				:attr('rowspan', (rounds > 1) and 2 or nil)
			:tag('th')
				:wikitext('Party')
				:attr('rowspan', (rounds > 1) and 2 or nil)
		if (rounds > 1) then
			row
				:tag('th')
					:wikitext('First round')
					:attr('colspan', 2)
				:tag('th')
					:wikitext('Second round')
					:attr('colspan', 2)
			row = root:tag('tr')
		end
		for k=1,rounds do
			row
				:tag('th')
					:wikitext('Votes')
				:tag('th')
					:wikitext('%')
		end
	elseif cand then
		row = root:tag('tr')
		row
			:tag('th')
				:wikitext('Candidate')
				:attr('colspan', 2)
				:attr('rowspan', (rounds > 1) and 2 or nil)
			:tag('th')
				:wikitext('Party')
				:attr('rowspan', (rounds > 1) and 2 or nil)
		if (rounds > 1) then
			row
				:tag('th')
					:wikitext('First round')
					:attr('colspan', 2)
				:tag('th')
					:wikitext('Second round')
					:attr('colspan', 2)
			row = root:tag('tr')
		end
		for k=1,rounds do
			row
				:tag('th')
					:wikitext('Votes')
				:tag('th')
					:wikitext('%')
		end
	elseif sc then
		row = root:tag('tr')
		if (rounds > 1) then
			row
				:tag('th')
					:wikitext('Party')
					:attr('colspan', 2)
					:attr('rowspan', 2)
				:tag('th')
					:wikitext('First round')
					:attr('colspan', 2)
				:tag('th')
					:wikitext('Second round')
					:attr('colspan', 2)
				:tag('th')
					:wikitext('Seats')
					:attr('rowspan', 2)
				:tag('th')
					:wikitext('+/–')
					:attr('rowspan', 2)
			row = root:tag('tr')
			for k=1,rounds do
				row
					:tag('th')
						:wikitext('Votes')
					:tag('th')
						:wikitext('%')
			end
		else
			row
				:tag('th')
					:wikitext('Party')
					:attr('colspan', 2)
				:tag('th')
					:wikitext('Votes')
				:tag('th')
					:wikitext('%')
				:tag('th')
					:wikitext('Seats')
				:tag('th')
					:wikitext('+/–')
		end
	else
		row = root:tag('tr')
		if (rounds > 1) then
			row
				:tag('th')
					:wikitext('Party')
					:attr('colspan', 2)
					:attr('rowspan', 2)
				:tag('th')
					:wikitext('First round')
					:attr('colspan', 2)
				:tag('th')
					:wikitext('Second round')
					:attr('colspan', 2)
				:tag('th')
					:wikitext('Seats')
					:attr('rowspan', 2)
			row = root:tag('tr')
			for k=1,rounds do
				row
					:tag('th')
						:wikitext('Votes')
					:tag('th')
						:wikitext('%')
			end
		else
			row
				:tag('th')
					:wikitext('Party')
					:attr('colspan', 2)
				:tag('th')
					:wikitext('Votes')
				:tag('th')
					:wikitext('%')
				:tag('th')
					:wikitext('Seats')
		end
	end
	local lang = mw.getContentLanguage()
	local function fmt(n)
		return n and lang:formatNum(n) or nil
	end
	local rsuff = (rounds > 1) and {'', '_2'} or {''}
	for i, v in ipairs(index) do
		row = root:tag('tr')
		local color = args['colour' .. v] or args['color' .. v] 
			or (mw.title.new('Template:' .. args['party' .. v] .. '/meta/color').exists
				and frame:expandTemplate{title = args['party' .. v] .. '/meta/color'})
			or nil
		row
			:tag('td')
				:css('background-color', color)
		if vp then
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['vp' .. v]))
			if args['party_name' .. v] then
				row
					:tag('td')
						:wikitext(string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]))
			else
				row
					:tag('td')
						:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
			end
			for kk, suf in ipairs(rsuff) do
				if(args['votes' .. v .. suf]) then
					row:tag('td')
						:css('text-align', 'right')
						:wikitext(fmt(args['votes' .. v .. suf]))
					row:tag('td')
						:css('text-align', 'right')
						:wikitext(string.format('%.2f%%', args['votes' .. v .. suf] / valid[kk] * 100))
				else
					row:tag('td'):attr('colspan', 2)
				end
			end
		elseif cand then
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
			row
				:tag('td')
					:wikitext(args['party_name' .. v] and
						string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]) or
						string.format('\[\[%s\]\]', args['party' .. v]) or nil)
			for kk, suf in ipairs(rsuff) do
				if(args['votes' .. v .. suf]) then
					row:tag('td')
						:css('text-align', 'right')
						:wikitext(fmt(args['votes' .. v .. suf]))
					row:tag('td')
						:css('text-align', 'right')
						:wikitext(string.format('%.2f%%', args['votes' .. v .. suf] / valid[kk] * 100))
				else
					row:tag('td'):attr('colspan', 2)
				end
			end
		elseif seats then
			row
				:tag('td')
					:wikitext(args['party_name' .. v] and
						string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]) or
						string.format('\[\[%s\]\]', args['party' .. v]) or nil)
			for kk, suf in ipairs(rsuff) do
				if(args['votes' .. v .. suf]) then
					row:tag('td')
						:css('text-align', 'right')
						:wikitext(fmt(args['votes' .. v .. suf]))
					row:tag('td')
						:css('text-align', 'right')
						:wikitext(string.format('%.2f%%', args['votes' .. v .. suf] / valid[kk] * 100))
				else
					row:tag('td'):attr('colspan', 2)
				end
			end
			row
				:tag('td')
					:css('text-align', 'right')
					:wikitext(fmt(args['seats' .. v]))
				:tag('td')
					:css('text-align', 'right')
					:wikitext(args['sc' .. v])
			end
	end
	row = root
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
	row
		:tag('td')
			:wikitext('Valid votes')
			:attr('colspan', cols - 2)
	        :css('text-align', 'left')
	for k=1,rounds do
		row
			:tag('td')
				:wikitext(fmt(valid[k]))
			:tag('td')
				:wikitext(string.format('%.2f%%', valid[k] / (valid[k] + invalid[k]) * 100))
	end
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
	row
		:tag('td')
			:wikitext('Invalid votes')
			:attr('colspan', cols - 2)
 	        :css('text-align', 'left')
   	for k=1,rounds do
		row
			:tag('td')
				:wikitext(fmt(invalid[k]))
			:tag('td')
				:wikitext(string.format('%.2f%%', invalid[k] / (valid[k] + invalid[k]) * 100))
	end
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('font-weight', 'bold')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
	row
		:tag('td')
			:wikitext('Total votes')
			:attr('colspan', cols - 2)
	        :css('text-align', 'left')
   	for k=1,rounds do
		row
			:tag('td')
				:wikitext(fmt(valid[k] + invalid[k]))
			:tag('td')
				:wikitext('100%')
	end
	
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
	row:tag('td')
		:wikitext('Registered voters/turnout')
		:attr('colspan', cols - 2)
 		:css('text-align', 'left')
   	for k=1,rounds do
		row:tag('td')
			:wikitext(fmt(electorate[k]))
		row:tag('td')
			:wikitext(string.format('%.2f%%', (valid[k] + invalid[k]) / electorate[k] * 100))
	end
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
	row:tag('td')
		:wikitext('Source: ', args.source)
		:attr('colspan', cols + 2*(rounds-1))
 		:css('text-align', 'left')
 		
	return tostring(root)
end

return p