Jump to content

Module:Medals table country

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Miria~01 (talk | contribs) at 16:26, 18 June 2025 (Undid revision 1296215969 by Miria~01 (talk)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function p.render(frame)
    local args = frame:getParent().args
    local country = args["country"] or "Country"
    local maxRows = 50
    local rows = {}
    local total_gold, total_silver, total_bronze = 0, 0, 0

    for i = 1, maxRows do
        local games = args["row"..i.."_games"]
        if not games or games == "" then break end

        local participation = args["row"..i.."_participation"]
        if participation and participation ~= "" then
            table.insert(rows, {
                games = games,
                participation = participation
            })
        else
            local athletes_val = args["row"..i.."_athletes"]
            local gold = tonumber(args["row"..i.."_gold"]) or 0
            local silver = tonumber(args["row"..i.."_silver"]) or 0
            local bronze = tonumber(args["row"..i.."_bronze"]) or 0
            local rank_raw = args["row"..i.."_rank"] or ""
            local year = games:match("(%d%d%d%d)") or ""

            local athletes_cell = ""
            if athletes_val == "no" then
                athletes_cell = "''did not participate''"
                table.insert(rows, {
                    games = games,
                    participation = athletes_cell
                })
            else
                local athletes_num = tonumber(athletes_val) or 0
                athletes_cell = string.format("[[%s at the %s Summer Olympics|%d]]", country, year, athletes_num)

                local total = gold + silver + bronze
                total_gold = total_gold + gold
                total_silver = total_silver + silver
                total_bronze = total_bronze + bronze

                local rank = tonumber(rank_raw)
                local rank_link = rank and string.format("[[%s Summer Olympics medal table|%d]]", year, rank) or rank_raw

                local bgcolor = ""
                if rank == 1 then
                    bgcolor = "#F7F6A8"
                elseif rank == 2 then
                    bgcolor = "#dce5e5"
                elseif rank == 3 then
                    bgcolor = "#ffdab9"
                end

                table.insert(rows, {
                    games = games,
                    athletes = athletes_cell,
                    gold = gold,
                    silver = silver,
                    bronze = bronze,
                    total = total,
                    rank = rank_link,
                    bgcolor = bgcolor
                })
            end
        end
    end

    local total_medals = total_gold + total_silver + total_bronze
    local total_rank = args["total_rank"] or ""

    local wikitext = '{| class="wikitable" style="text-align:center; font-size:90%;"\n'
    wikitext = wikitext .. "|-\n! Games !! Athletes !! style=\"width:3em; font-weight:bold;\"| Gold !! style=\"width:3em; font-weight:bold;\"| Silver !! style=\"width:3em; font-weight:bold;\"| Bronze !! style=\"width:3em; font-weight:bold;\"| Total !! style=\"width:3em; font-weight:bold;\"| Rank\n"

    for _, row in ipairs(rows) do
        if row.participation then
            wikitext = wikitext .. string.format("|-\n| align=left | %s || colspan=6 | %s\n", row.games, row.participation)
        else
            local line = "|-\n| align=left | " .. row.games .. " || " .. row.athletes
            line = line .. string.format(" || %d || %d || %d || %d", row.gold, row.silver, row.bronze, row.total)
            if row.bgcolor ~= "" then
                line = line .. string.format(" || style=\"background-color:%s\" | %s", row.bgcolor, row.rank)
            else
                line = line .. " || " .. row.rank
            end
            wikitext = wikitext .. line .. "\n"
        end
    end

    wikitext = wikitext .. string.format("|-\n! colspan=2 | Total !! %d !! %d !! %d !! %d !! %s\n",
        total_gold, total_silver, total_bronze, total_medals, total_rank)
    wikitext = wikitext .. "|}"

    return wikitext
end

return p