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:13, 18 June 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Module:MedalTable
local p = {}

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

    local i = 1
    while i <= 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
            local group = {games}
            local rowspan = 1
            for j = i + 1, maxRows do
                local next_part = args["row"..j.."_participation"]
                local next_games = args["row"..j.."_games"]
                if next_part == participation and next_games and next_games ~= "" then
                    table.insert(group, next_games)
                    rowspan = rowspan + 1
                else
                    break
                end
            end

            table.insert(rows, {
                games = group[1],
                participation = participation,
                rowspan = rowspan
            })

            for k = 2, #group do
                table.insert(rows, {
                    games = group[k],
                    participation = false
                })
            end

            i = i + rowspan
        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
            i = i + 1
        end
    end

    local total = 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'
    .. '!Games!!Athletes!!style="width:3em;font-weight:bold;"|{{gold medal}}!!style="width:3em;font-weight:bold;"|{{silver medal}}!!style="width:3em;font-weight:bold;"|{{bronze medal}}!!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
            if row.rowspan and row.rowspan > 1 then
                wikitext = wikitext .. string.format("|-\n| align=left | %s || rowspan=%d | %s\n", row.games, row.rowspan, row.participation)
            elseif row.participation ~= false then
                wikitext = wikitext .. string.format("|-\n| align=left | %s || colspan=6 | %s\n", row.games, row.participation)
            else
                wikitext = wikitext .. string.format("|-\n| align=left | %s\n", row.games)
            end
        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|}\n", total_gold, total_silver, total_bronze, total, total_rank)

    return wikitext
end

return p