Jump to content

Module:Check winner by scores/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Abhiramakella (talk | contribs) at 21:48, 20 July 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
require('strict')

local p = {}

-- Helper function: wrap the result letter in a <span> with a "title" attribute for tooltip
local function resultTooltip(letter, description)
    -- Generates: <span title="description">letter</span>
    return mw.text.tag('span', { title = description }, letter)
end

-- Helper function to normalize score formatting (unchanged from original)
local function format_score(s)
    s = mw.ustring.gsub(s or '', '^[%s\']*([%d%.]+)[%s\']*[–−—%-][%s\']*([%d%.]+)', '%1–%2')
    s = mw.ustring.gsub(s, '^%s*([%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
    s = mw.ustring.gsub(s, '^%s*(%[%[[^%[%]]*%|[%d%.]+)%s*%-%s*([%d%.]+)', '%1–%2')
    s = mw.ustring.gsub(s, '^%s*(%[[^%[%]%s]*%s+[%d%.]+)%s*%-%s*([%d%.]+)', '%1–%2')
    s = mw.ustring.gsub(s, '^%s*(%[%[[^%[%]]*%|[%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
    s = mw.ustring.gsub(s, '^%s*(%[[^%[%]%s]*%s+[%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
    return s
end

function p.main(frame)
    local getArgs = require('Module:Arguments').getArgs
    local args = getArgs(frame, { parentFirst = true })

    local n1 = args[1] or 'X'
    local n2 = args[2] or 'X'
    local s  = args['sc'] or (n1 .. '–' .. n2)

    s = format_score(s)

    -- Remove links if present
    if s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]') then
        s = s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]')
    end
    if s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]') then
        s = s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]')
    end

    -- Extract scores as numbers or W/L letters
    local s1 = tonumber(mw.ustring.gsub(s or '', '^%s*([%d][%d%.]*)%s*–%s*([%d][%d%.]*).*', '%1') or nil)
        or mw.ustring.gsub(s or '', '^([WL]*)–([WL]*).*', '%1')
        or ''

    local s2 = tonumber(mw.ustring.gsub(s or '', '^%s*([%d][%d%.]*)%s*–%s*([%d][%d%.]*).*', '%2') or nil)
        or mw.ustring.gsub(s or '', '^([WL]*)–([WL]*).*', '%2')
        or ''

    -- Determine result and return letter with raw HTML tooltip
    if type(s1) == 'number' and type(s2) == 'number' then
        if s1 > s2 then
            return resultTooltip('W', 'Won')
        elseif s2 > s1 then
            return resultTooltip('L', 'Lost')
        else
            return resultTooltip('T', 'Tied')
        end
    elseif s1:match('[WL]') and s2:match('[WL]') and s1 ~= s2 then
        return resultTooltip(s1, (s1 == 'W') and 'Won' or 'Lost')
    else
        return string.format("''%s''", 'Result unknown')
    end
end

return p