Difference between revisions of "Module:Medals table/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Zackmann08
(syncing to main)
blackwiki>Zackmann08
(sync)
Line 8: Line 8:
 
end
 
end
  
 +
    local team = args['team'] or 'Nation'
 
local root = mw.html.create()
 
local root = mw.html.create()
local host = args['host']
+
local host = args['host'] or ''
 
local hostColor = '#ccccff'
 
local hostColor = '#ccccff'
 
local defaultRowColor = '#f8f9fa'
 
local defaultRowColor = '#f8f9fa'
 
local flagTemplate = args['flag_template'] or 'flagteam'
 
local flagTemplate = args['flag_template'] or 'flagteam'
 
local event = args['event']
 
local event = args['event']
 +
local legendpos = (args['legend_position'] or 't'):lower()
 +
local header, footer = '', ''
 
local totalGold = 0
 
local totalGold = 0
 
local totalSilver = 0
 
local totalSilver = 0
Line 26: Line 29:
 
local showLimit = tonumber(args['show_limit'])
 
local showLimit = tonumber(args['show_limit'])
  
 +
-- build the legend
 +
if host ~= '' then
 +
if args['name_' .. host] then
 +
host = 'Host nation (' .. args['name_' .. host] .. ')'
 +
elseif host:match('^([A-Z][A-Z][A-Z])') then
 +
host = frame:expandTemplate{title = 'country alias', args = {host, event} }
 +
host = 'Host nation (' .. host .. ')'
 +
end
 +
host = host .. (args['host_note'] or '')
 +
host = frame:expandTemplate{title = 'color box', args = {hostColor, ' * ', 'border=darkgray'}} ..' '.. host
 +
end
 +
 +
if legendpos == 't' then
 +
header = header .. host
 +
else
 +
footer = footer .. host
 +
end
 +
 
root = root
 
root = root
 
:tag('table')
 
:tag('table')
Line 41: Line 62:
 
:wikitext('Rank')
 
:wikitext('Rank')
 
:tag('th')
 
:tag('th')
:wikitext('Nation')
+
:wikitext(team)
 
:tag('th')
 
:tag('th')
 
:addClass('headerSort')
 
:addClass('headerSort')
Line 68: Line 89:
 
local nation = k:match('^gold_([A-Z][A-Z][A-Z])$')  
 
local nation = k:match('^gold_([A-Z][A-Z][A-Z])$')  
 
if nation then
 
if nation then
local total = tonumber(args['total' .. nation]) or (
+
local gold  = (tonumber(args['gold_' .. nation]) or 0)
(tonumber(args['gold_' .. nation]) or 0) +
+
local silver = (tonumber(args['silver_' .. nation]) or 0)
(tonumber(args['silver_' .. nation]) or 0) +
+
local bronze = (tonumber(args['bronze_' .. nation]) or 0)
(tonumber(args['bronze_' .. nation]) or 0))
+
local noskip = args['skip_' .. nation] and 0 or 1
table.insert(rowNums, {total, nation})  
+
table.insert(rowNums, {gold, silver, bronze, noskip, nation})  
 
end
 
end
 
end
 
end
 
 
table.sort(rowNums, function (a, b) return a[1] > b[1] end)
+
table.sort(rowNums, function (a, b)  
 +
return a[1] > b[1] or (a[1] == b[1] and a[2] > b[2])
 +
or (a[1] == b[1] and a[2] == b[2] and a[3] > b[3])
 +
or (a[1] == b[1] and a[2] == b[2] and a[3] == b[3] and a[4] > b[4])
 +
or (a[1] == b[1] and a[2] == b[2] and a[3] == b[3] and a[4] == b[4] and a[5] < b[5])
 +
end
 +
)
 
 
local lasttotal, lastspan, lastrankcell = -1, 1, nil
+
local lastGold, lastSilver, lastBronze = -1
 +
local rank = 0
 +
local lastspan, lastrankcell = 1, nil
 
for i, anum in ipairs(rowNums) do
 
for i, anum in ipairs(rowNums) do
local IOC = anum[2]
+
local IOC = anum[5]
local name  = args['name_' .. IOC]
+
if args['skip_' .. IOC] then
local gold  = args['gold_' .. IOC]
+
lastGold, lastSilver, lastBronze, lastspan = -1, -1, -1, 1
local silver = args['silver_' .. IOC]
+
else
local bronze = args['bronze_' .. IOC]
+
rank = rank + 1
 +
end
 +
local nation = args['name_' .. IOC] or
 +
frame:expandTemplate{title = flagTemplate, args = {IOC, event} }
 +
local gold  = tonumber(args['gold_' .. IOC]) or 0
 +
local silver = tonumber(args['silver_' .. IOC]) or 0
 +
local bronze = tonumber(args['bronze_' .. IOC]) or 0
 
local isHost = args['host_' .. IOC]
 
local isHost = args['host_' .. IOC]
 
-- this is mainly for the parameter names example so you can override it.
 
-- this is mainly for the parameter names example so you can override it.
 
local total  = args['total_' .. IOC] or gold + silver + bronze
 
local total  = args['total_' .. IOC] or gold + silver + bronze
local color
+
local color = isHost and hostColor or defaultRowColor
if isHost then color = hostColor else color = defaultRowColor end
 
  
 
if args['grand_total'] then else
 
if args['grand_total'] then else
Line 96: Line 130:
 
totalBronze = totalBronze + bronze
 
totalBronze = totalBronze + bronze
 
end
 
end
local nation
+
 
if name then
+
if args['host_' .. IOC] then
nation = name
+
nation = nation .. '*'
else
+
end
nation = mw.getCurrentFrame():expandTemplate({title = flagTemplate, args = { IOC , event } })
+
 
 +
if args['note_' .. IOC] then
 +
nation = nation .. args['note_' .. IOC]
 
end
 
end
  
Line 114: Line 150:
 
--:css('background-color', color)
 
--:css('background-color', color)
  
if total == lasttotal then
+
if (gold == lastGold) and (silver == lastSilver) and (bronze == lastBronze) then
 
lastspan = lastspan + 1
 
lastspan = lastspan + 1
 
lastrankcell:attr('rowspan',lastspan)
 
lastrankcell:attr('rowspan',lastspan)
 
else
 
else
 
lastspan = 1
 
lastspan = 1
lastrankcell = row:tag('td'):wikitext(i)
+
if args['skip_' .. IOC] then
lasttotal = total
+
lastrankcell = row:tag('td'):wikitext(frame:expandTemplate{title = 'sort', args = {'999', '-'}})
 +
else
 +
lastrankcell = row:tag('td'):wikitext(rank)
 +
lastGold  = gold
 +
lastSilver = silver
 +
lastBronze = bronze
 +
end
 
end
 
end
 
row:tag('td')
 
row:tag('td')
Line 127: Line 169:
 
:wikitext(nation)
 
:wikitext(nation)
 
:tag('td')
 
:tag('td')
:css('background-color', color)
 
 
:wikitext(gold)
 
:wikitext(gold)
 
:tag('td')
 
:tag('td')
:css('background-color', color)
 
 
:wikitext(silver)
 
:wikitext(silver)
 
:tag('td')
 
:tag('td')
:css('background-color', color)
 
 
:wikitext(bronze)
 
:wikitext(bronze)
 
:tag('td')
 
:tag('td')
:css('background-color', color)
 
 
:wikitext(total)
 
:wikitext(total)
 
end
 
end
remainingEnd = i
+
remainingEnd = rank
 
end
 
end
 
 
 
if limitReached then
 
if limitReached then
 
root:tag('tr')
 
root:tag('tr')
:css('font-style', 'italic')
 
 
:tag('td')
 
:tag('td')
 
:wikitext(remainingStart..'–'..remainingEnd)
 
:wikitext(remainingStart..'–'..remainingEnd)
 
:tag('td')
 
:tag('td')
+
:css('font-style', 'italic')
 
:css('text-align','left')
 
:css('text-align','left')
:wikitext(args['remaining_link'] or 'Remaining')
+
:wikitext(args['remaining_link'] or args['remaining_text'] or 'Remaining')
 
:tag('td')
 
:tag('td')
 
:wikitext(remainingGold)
 
:wikitext(remainingGold)
Line 164: Line 201:
 
:addClass('sortbottom')
 
:addClass('sortbottom')
 
:tag('th')
 
:tag('th')
:wikitext('Totals')
+
:wikitext('Totals ('..remainingEnd..' '..team..'s)')
 
:attr('colspan',2)
 
:attr('colspan',2)
 
:tag('th')
 
:tag('th')
Line 175: Line 212:
 
:wikitext(args['grand_total'] or totalGold+totalSilver+totalBronze)
 
:wikitext(args['grand_total'] or totalGold+totalSilver+totalBronze)
  
if host then
+
-- Build the rest of the footer
root
+
if args['source'] or args['notes'] then
:tag('p')
+
if footer ~= '' then
:wikitext(host)
+
footer = footer .. '<br>'
:tag('span')
+
end
:css('background-color', hostColor)
+
footer = frame:expandTemplate{ title = 'refbegin' } .. footer
:css('text-align', 'center')
+
:css('padding', '2px')
+
if args['source'] then
:css('margin', '2px')
+
footer = footer .. 'Source: ' .. args['source']
:css('border', '1px solid darkgray')
+
end
:css('float', 'left')
+
if args['notes'] then
:css('height', '1em')
+
if args['source'] then
:wikitext('&nbsp;&nbsp;&nbsp;')
+
footer = footer .. '<br>'
 +
end
 +
footer = footer .. 'Notes: ' .. args['notes']
 +
end
 +
footer = footer .. frame:expandTemplate{ title = 'refend' }
 
end
 
end
  
return tostring(root)
+
return header .. tostring(root) .. footer
 
end
 
end
 +
 
return p
 
return p

Revision as of 17:48, 22 September 2018

Documentation for this module may be created at Module:Medals table/sandbox/doc

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs

local p = {}
function p.createTable(frame, args)
	if not args then
		args = getArgs(frame)
	end

    local team = args['team'] or 'Nation'
	local root = mw.html.create()
	local host = args['host'] or ''
	local hostColor = '#ccccff'
	local defaultRowColor = '#f8f9fa'
	local flagTemplate = args['flag_template'] or 'flagteam'
	local event = args['event']
	local legendpos = (args['legend_position'] or 't'):lower()
	local header, footer = '', ''
	local totalGold = 0
	local totalSilver = 0
	local totalBronze = 0
	
	local remainingGold = 0
	local remainingSilver = 0
	local remainingBronze = 0
	local remainingStart = 0
	local remainingEnd = 0
	local limitReached = false
	local showLimit = tonumber(args['show_limit'])

	-- build the legend
	if host ~= '' then
		if args['name_' .. host] then
			host = 'Host nation (' .. args['name_' .. host] .. ')'
		elseif host:match('^([A-Z][A-Z][A-Z])') then
			host = frame:expandTemplate{title = 'country alias', args = {host, event} }
			host = 'Host nation (' .. host .. ')'
		end
		host = host .. (args['host_note'] or '')
		host = frame:expandTemplate{title = 'color box', args = {hostColor, '&nbsp;*&nbsp;', 'border=darkgray'}} ..'&nbsp;'.. host
	end
	
	if legendpos == 't' then
		header = header .. host
	else
		footer = footer .. host
	end
	
	root = root
		:tag('table')
		:addClass('wikitable')
		:addClass('sortable')
		:addClass('plainrowheaders')
		:addClass('jquery-tablesorter')
		:css('text-align', 'center')
	
	root:tag('caption')
		:wikitext(args['caption'])
	-- add the header row
	root:tag('tr')
			:tag('th')
				:wikitext('Rank')
			:tag('th')
				:wikitext(team)
			:tag('th')
				:addClass('headerSort')
				:css('width', '6em')
				:css('background-color', 'gold')
				:wikitext('Gold')
			:tag('th')
				:addClass('headerSort')
				:css('width', '6em')
				:css('background-color', 'silver')
				:wikitext('Silver')
			:tag('th')
				:addClass('headerSort')
				:css('width', '6em')
				:css('background-color', '#c96')
				:wikitext('Bronze')
			:tag('th')
				:css('width', '6em')
				:wikitext('Total')
	
	-- enumerate the rows
	local rowNums = {}
	
	for k,v in pairs(args) do
		k = ''..k
		local nation = k:match('^gold_([A-Z][A-Z][A-Z])$') 
		if nation then
			local gold   = (tonumber(args['gold_' .. nation]) or 0)
			local silver = (tonumber(args['silver_' .. nation]) or 0)
			local bronze = (tonumber(args['bronze_' .. nation]) or 0)
			local noskip = args['skip_' .. nation] and 0 or 1
			table.insert(rowNums, {gold, silver, bronze, noskip, nation}) 
		end
	end
	
	table.sort(rowNums, function (a, b) 
		return a[1] > b[1] or (a[1] == b[1] and a[2] > b[2]) 
			or (a[1] == b[1] and a[2] == b[2] and a[3] > b[3]) 
			or (a[1] == b[1] and a[2] == b[2] and a[3] == b[3] and a[4] > b[4])
			or (a[1] == b[1] and a[2] == b[2] and a[3] == b[3] and a[4] == b[4] and a[5] < b[5])
			end
	)
	
	local lastGold, lastSilver, lastBronze = -1
	local rank = 0
	local lastspan, lastrankcell = 1, nil
	for i, anum in ipairs(rowNums) do
		local IOC = anum[5]
		if args['skip_' .. IOC] then 
			lastGold, lastSilver, lastBronze, lastspan = -1, -1, -1, 1
		else 
			rank = rank + 1 
		end
		local nation = args['name_' .. IOC] or 
			frame:expandTemplate{title = flagTemplate, args = {IOC, event} }
		local gold   = tonumber(args['gold_' .. IOC]) or 0
		local silver = tonumber(args['silver_' .. IOC]) or 0
		local bronze = tonumber(args['bronze_' .. IOC]) or 0
		local isHost = args['host_' .. IOC]
		-- this is mainly for the parameter names example so you can override it.
		local total  = args['total_' .. IOC] or gold + silver + bronze
		local color = isHost and hostColor or defaultRowColor

		if args['grand_total'] then else
				totalGold = totalGold + gold
				totalSilver = totalSilver + silver
				totalBronze = totalBronze + bronze
		end

		if args['host_' .. IOC] then
			nation = nation .. '*'
		end

		if args['note_' .. IOC] then
			nation = nation .. args['note_' .. IOC]
		end

		if showLimit and (i>showLimit) then 
			if remainingStart == 0 then remainingStart = i end
			limitReached = true
			remainingGold = remainingGold + gold
			remainingSilver = remainingSilver + silver
			remainingBronze = remainingBronze + bronze
		else
			local row = root:tag('tr')
			--Don't put the color on the row because of ranks spanning multiple rows. 
			--:css('background-color', color)

			if (gold == lastGold) and (silver == lastSilver) and (bronze == lastBronze) then
				lastspan = lastspan + 1
				lastrankcell:attr('rowspan',lastspan)
			else
				lastspan = 1
				if args['skip_' .. IOC] then
					lastrankcell = row:tag('td'):wikitext(frame:expandTemplate{title = 'sort', args = {'999', '-'}})
				else
					lastrankcell = row:tag('td'):wikitext(rank)
					lastGold   = gold
					lastSilver = silver
					lastBronze = bronze
				end
			end
			row:tag('td')
					:css('background-color', color)
					:css('text-align','left')
					:wikitext(nation)
				:tag('td')
					:wikitext(gold)
				:tag('td')
					:wikitext(silver)
				:tag('td')
					:wikitext(bronze)
				:tag('td')
					:wikitext(total)
		end
		remainingEnd = rank
	end
	
	if limitReached then
		root:tag('tr')
				:tag('td')
					:wikitext(remainingStart..'–'..remainingEnd)
				:tag('td')
					:css('font-style', 'italic')
					:css('text-align','left')
					:wikitext(args['remaining_link'] or args['remaining_text'] or 'Remaining')
				:tag('td')
					:wikitext(remainingGold)
				:tag('td')
					:wikitext(remainingSilver)
				:tag('td')
					:wikitext(remainingBronze)
				:tag('td')
					:wikitext(remainingGold+remainingSilver+remainingBronze)
	end

	root:tag('tr')
		:addClass('sortbottom')
		:tag('th')
			:wikitext('Totals ('..remainingEnd..' '..team..'s)')
			:attr('colspan',2)
		:tag('th')
			:wikitext(args['total_gold'] or totalGold)
		:tag('th')
			:wikitext(args['total_silver'] or totalSilver)
		:tag('th')
			:wikitext(args['total_bronze'] or totalBronze)
		:tag('th')
			:wikitext(args['grand_total'] or totalGold+totalSilver+totalBronze)

	-- Build the rest of the footer
	if args['source'] or args['notes'] then
		if footer ~= '' then
			footer = footer .. '<br>'
		end
		footer = frame:expandTemplate{ title = 'refbegin' } .. footer
		
		if args['source'] then 
			footer = footer .. 'Source: ' .. args['source']
		end
		if args['notes'] then
			if args['source'] then
				footer = footer .. '<br>'
			end
			footer = footer .. 'Notes: ' .. args['notes']
		end
		footer = footer .. frame:expandTemplate{ title = 'refend' }
	end

	return header .. tostring(root) .. footer
end

return p