Jump to content

Module:Sandbox/Miria~01

From Wikipedia, the free encyclopedia
local p = {}

local function medalIcon(color)
	if color == "Gold" then
		return '[[File:Gold medal olympic.svg|20px|link=]]'
	elseif color == "Silver" then
		return '[[File:Silver medal olympic.svg|20px|link=]]'
	elseif color == "Bronze" then
		return '[[File:Bronze medal olympic.svg|20px|link=]]'
	end
end

local function medalIconBig()
	return '[[File:GoldSilverBronze medal olympic.svg|35px|link=]]'
end

local function buildHeader()
	return [=[{| class="wikitable sortable" style="margin-top:0; text-align:center; font-size:90%;"
|-
!
! colspan="5" style="background-color:#f2f2ce;" |Summer Games
! colspan="5" style="background-color:#cedff2;" |Winter Games
! colspan="5" |Combined total
|-
! Team (IOC code)
]=] .. string.format([[
! style="background-color:#f2f2ce;" |No.
! style="background-color:#f2f2ce;" |%s
! style="background-color:#f2f2ce;" |%s
! style="background-color:#f2f2ce;" |%s
! style="background-color:#f2f2ce;" |%s
! style="background-color:#cedff2;" |No.
! style="background-color:#cedff2;" |%s
! style="background-color:#cedff2;" |%s
! style="background-color:#cedff2;" |%s
! style="background-color:#cedff2;" |%s
! style="width:2em;" |No.
! style="width:2em;" |%s
! style="width:2em;" |%s
! style="width:2em;" |%s
! style="width:2em;" |%s
]],
	medalIcon("Gold"), medalIcon("Silver"), medalIcon("Bronze"), medalIconBig(),
	medalIcon("Gold"), medalIcon("Silver"), medalIcon("Bronze"), medalIconBig(),
	medalIcon("Gold"), medalIcon("Silver"), medalIcon("Bronze"), medalIconBig()
)
end

-- Helper: safely convert to number
local function toNum(val)
	return tonumber(val) or 0
end

local function getArgs(frame)
	return frame:getParent() and frame:getParent().args or frame.args
end

function p.main(frame)
	local args = frame.args
	local rows = toNum(args.rows)
	local out = { buildHeader() }

	local totals = {
		s_games = 0, s_gold = 0, s_silver = 0, s_bronze = 0,
		w_games = 0, w_gold = 0, w_silver = 0, w_bronze = 0,
		c_games = 0, c_gold = 0, c_silver = 0, c_bronze = 0
	}

	for i = 1, rows do
		local team     = args["team" .. i] or ""
		local s_games  = toNum(args["s_games" .. i])
		local s_gold   = toNum(args["s_gold" .. i])
		local s_silver = toNum(args["s_silver" .. i])
		local s_bronze = toNum(args["s_bronze" .. i])
		local s_total  = s_gold + s_silver + s_bronze

		local w_games  = toNum(args["w_games" .. i])
		local w_gold   = toNum(args["w_gold" .. i])
		local w_silver = toNum(args["w_silver" .. i])
		local w_bronze = toNum(args["w_bronze" .. i])
		local w_total  = w_gold + w_silver + w_bronze

		local c_games  = s_games + w_games
		local c_gold   = s_gold + w_gold
		local c_silver = s_silver + w_silver
		local c_bronze = s_bronze + w_bronze
		local c_total  = c_gold + c_silver + c_bronze

		totals.s_games  = totals.s_games  + s_games
		totals.s_gold   = totals.s_gold   + s_gold
		totals.s_silver = totals.s_silver + s_silver
		totals.s_bronze = totals.s_bronze + s_bronze

		totals.w_games  = totals.w_games  + w_games
		totals.w_gold   = totals.w_gold   + w_gold
		totals.w_silver = totals.w_silver + w_silver
		totals.w_bronze = totals.w_bronze + w_bronze

		totals.c_games  = totals.c_games  + c_games
		totals.c_gold   = totals.c_gold   + c_gold
		totals.c_silver = totals.c_silver + c_silver
		totals.c_bronze = totals.c_bronze + c_bronze

		table.insert(out, string.format(
			[=[|-
|style="text-align: left;"| %s
|%d||%d||%d||%d||%d||%d||%d||%d||%d||%d||%d||%d||%d||%d||%d]=],
			team,
			s_games, s_gold, s_silver, s_bronze, s_total,
			w_games, w_gold, w_silver, w_bronze, w_total,
			c_games, c_gold, c_silver, c_bronze, c_total
		))
	end

	table.insert(out, string.format(
		[=[|-
! style="text-align: center;" | Total
!%d!!%d!!%d!!%d!!%d!!%d!!%d!!%d!!%d!!%d!!%d!!%d!!%d!!%d!!%d]=],
		totals.s_games, totals.s_gold, totals.s_silver, totals.s_bronze,
		totals.s_gold + totals.s_silver + totals.s_bronze,
		totals.w_games, totals.w_gold, totals.w_silver, totals.w_bronze,
		totals.w_gold + totals.w_silver + totals.w_bronze,
		totals.c_games, totals.c_gold, totals.c_silver, totals.c_bronze,
		totals.c_gold + totals.c_silver + totals.c_bronze
	))

	table.insert(out, "|}")
	return table.concat(out, "\n")
end

return p