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:37, 20 July 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Enforce strict variable usage to avoid bugs
require('strict')

-- Declare module table
local p = {}

-- Helper function to normalize score formatting
-- Converts score formats like "21 - 17", "21 — 17", etc., into "21–17"
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

-- Helper function to wrap result letters in tooltip template
-- e.g., 'W' becomes {{Tooltip|W|Won}}
local function tooltip(letter)
	if letter == 'W' then
		return '{{Tooltip|W|Won}}'
	elseif letter == 'L' then
		return '{{Tooltip|L|Lost}}'
	elseif letter == 'T' then
		return '{{Tooltip|T|Tied}}'
	else
		return letter -- return as-is if unknown
	end
end

-- Main function, invoked by #invoke in wikitext
function p.main(frame)
	-- Load argument handling module
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, { parentFirst = true })

	-- Get scores from args; fallback to 'X' if missing
	local n1 = args[1] or 'X'
	local n2 = args[2] or 'X'

	-- Use explicit score (sc) if provided, otherwise build from n1 and n2
	local s = args['sc'] or (n1 .. '–' .. n2)

	-- Clean and standardize the score string
	s = format_score(s)

	-- If score is in the format [[Some link|21–14]] or [[Team 21–14]], remove the link part
	if s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]') then
		s = s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]')
	end
	if s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]') then
		s = s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]')
	end

	-- Try to extract numeric scores
	-- Example: "21–17" → s1 = 21, s2 = 17
	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 ''

	-- If both scores are numbers, determine the outcome
	if type(s1) == 'number' and type(s2) == 'number' then
		local result = (s1 > s2) and 'W' or ((s2 > s1) and 'L' or 'T')
		return tooltip(result) -- return result with tooltip

	-- If scores are letters like "W–L", return first letter with tooltip
	elseif s1:match('[WL]') and s2:match('[WL]') and s1 ~= s2 then
		return tooltip(s1)

	-- Fallback if parsing fails
	else
		return string.format("''%s''", 'Result unknown')
	end
end

-- Return the module table
return p