Module:Medal tally

local p = {}

function p.render(frame)

local args = frame:getParent().args

local header = args.header or "Medal Tally"

local class = args.class or ""

local result = {}

table.insert(result, string.format('

', class))

table.insert(result, string.format('

', header))

table.insert(result, '

')

local medals = {}

local totalGold, totalSilver, totalBronze = 0, 0, 0

for i = 1, 8 do

local team = args['team' .. i]

local gold = tonumber(args['gold' .. i]) or 0

local silver = tonumber(args['silver' .. i]) or 0

local bronze = tonumber(args['bronze' .. i]) or 0

local total = gold + silver + bronze

if team then

table.insert(medals, {rank = i, team = team, gold = gold, silver = silver, bronze = bronze, total = total})

totalGold = totalGold + gold

totalSilver = totalSilver + silver

totalBronze = totalBronze + bronze

end

end

table.sort(medals, function(a, b)

if a.gold ~= b.gold then return a.gold > b.gold end

if a.silver ~= b.silver then return a.silver > b.silver end

return a.bronze > b.bronze

end)

for index, entry in ipairs(medals) do

table.insert(result, string.format('

',

index, entry.team, entry.gold, entry.silver, entry.bronze, entry.total))

end

local totalMedals = totalGold + totalSilver + totalBronze

table.insert(result, string.format('

',

totalGold, totalSilver, totalBronze, totalMedals))

table.insert(result, '

%s
RankTeamGoldSilverBronzeTotal
%d%s%d%d%d%d
Total%d%d%d%d
')

return table.concat(result, "\n")

end

return p