Jump to content

Module:Check winner by scores

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by McVahl (talk | contribs) at 06:07, 6 June 2020 (+support for scores wrapped in bold/italic markup). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals')

local p = {}

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 ''
	local n2 = args[2] or ''
	local s  = args['sc'] or n1
	
    if n2 ~= '' then s = s .. '-' .. n2 end

    -- following codes obtained from Module:Sports results
	s = format_score(s)

	-- delink if necessary
	if s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]') then
		s = s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]')
	end
	if s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]') then
		s = s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]')
	end

	-- get the scores
	local s1 = tonumber(mw.ustring.gsub( s or '',
		'^%s*([%d][%d%.]*)%s*–%s*([%d][%d%.]*).*', '%1' ) or '') or ''
	local s2 = tonumber(mw.ustring.gsub( s or '',
		'^%s*([%d][%d%.]*)%s*–%s*([%d][%d%.]*).*', '%2' ) or '') or ''
	
	if s1 ~= '' and s2 ~= '' then
		return (s1 > s2) and 'W' or ((s2 > s1) and 'L' or 'T')
	else
		return string.format("''%s''", 'Result unknown')
	end
end

return p