모듈:선거결과

이 모듈에 대한 설명문서는 모듈:선거결과/설명문서에서 만들 수 있습니다

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local out = {}

    -- 선거명 + 선거구
    local election_type = (args["유형"] == "참") and "참의원 의원 통상선거" or "중의원 의원 총선거"
    local election_number = args["선거"] or "??"
    local election_name = string.format("제%s회 일본 %s", election_number, election_type)
    local election_link = string.format("[[%s]]", election_name)
    local district = args["선거구"] or ""

    table.insert(out, '<table class="wikitable">')
    table.insert(out, string.format(
        '<caption style="font-size:larger; font-weight:bold;"><big>%s</big><br>%s</caption>',
        election_link, district))

    -- 표 머리: 후보자 + 정당색 셀 병합
    table.insert(out, [[<tr>
<th colspan="2">후보자</th><th>정당</th><th>득표</th><th>득표율</th><th>당락</th><th>비고</th>
</tr>]])

    -- 총득표 계산
    local total = 0
    for i = 1, 20 do
        local votes = tonumber(args["c" .. i .. "vote"] or "0") or 0
        total = total + votes
    end

    -- 후보자 출력
    for i = 1, 20 do
        local name = args["c" .. i .. "name"]
        if name and name ~= "" then
            local age = args["c" .. i .. "age"] or ""
            local party = args["c" .. i .. "party"] or ""
            local color = args["c" .. i .. "color"] or "#ccc"
            local votes = tonumber(args["c" .. i .. "vote"] or "0") or 0
            local outcome = args["c" .. i .. "result"] or ""
            local note = args["c" .. i .. "note"] or ""

            local percent = total > 0 and math.floor(votes * 1000 / total + 0.5) / 10 or 0.0
            local style = ""
            if outcome == "당선" then
                style = ' style="background:pink;"'
            elseif outcome == "석패율" then
                style = ' style="background:#ffe6e6;"'
            end

            local bar = string.format(
                '<div style="display:flex; align-items:center;"><div style="background-color:%s; width:%.1fpx; height:10px; border-radius:3.75px; display:inline-block; margin-right:5px;"></div><span style="font-size:0.833em;"><b>%.1f%%</b></span></div>',
                color, percent * 1.25, percent)

            table.insert(out, string.format(
                '<tr%s><td style="background:%s;"></td><td><span style="font-weight:bold;">%s</span>(%s)</td><td>%s</td><td style="text-align:right;">%s표</td><td style="text-align:left;">%s</td><td style="text-align:center;">%s</td><td>%s</td></tr>',
                style, color, name, age, party,
                mw.language.getContentLanguage():formatNum(votes),
                bar, outcome, note))
        end
    end

    -- 합계 행
    table.insert(out, string.format(
        '<tr style="font-weight:bold; background:#f9f9f9;"><td colspan="3" style="text-align:center;">합계</td><td style="text-align:right;">%s표</td><td style="text-align:right;">투표율: %s%%</td><td colspan="2" style="text-align:center;">유권자 수: %s명</td></tr>',
        mw.language.getContentLanguage():formatNum(total),
        args["투표율"] or "?",
        mw.language.getContentLanguage():formatNum(tonumber(args["유권자"] or "0") or 0)
    ))

    table.insert(out, "</table>")
    return table.concat(out, "\n")
end

return p