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 10:11, 19 June 2025. 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
    local skip_rows = {}

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

            local participation = args["row"..i.."_participation"]
            local rowspan = tonumber(args["row"..i.."_rowspan"]) or 1

            if participation and participation ~= "" then
                -- Skip next rows if rowspan > 1
                for j = 1, rowspan - 1 do
                    skip_rows[i + j] = true
                end

                table.insert(rows, {
                    games = games,
                    participation = participation,
                    rowspan = 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 ""

                if athletes_val == "no" then
                    table.insert(rows, {
                        games = games,
                        participation = "''did not participate''",
                        rowspan = 1
                    })
                else
                    local athletes_num = tonumber(athletes_val) or 0
                    local 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
    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
            local rowspan_attr = row.rowspan > 1 and (" rowspan=" .. row.rowspan) or ""
            wikitext = wikitext .. string.format("|-\n| align=left | %s || colspan=6%s | %s\n", row.games, rowspan_attr, 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