Module:TennisMatch
Appearance
| This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
| This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
Implements {{TennisMatch}} and {{TennisMatch3}}
Usage
{{TennisMatch|...}} → {{#invoke:TennisMatch|five| ... }}
{{TennisMatch3|...}} → {{#invoke:TennisMatch|three| ... }}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local notblank = function(v) return (v or '') ~= '' end
local bold = function(s) return "'''" .. s .. "'''" end
-- Format tiebreak-like scores (6.00 → 6<sup>0</sup>, etc.)
local function formatScore(scoreA, scoreB)
local s = tostring(scoreA or '')
local n, nB = tonumber(scoreA), tonumber(scoreB)
if not n then -- Non-numeric entry
return s
elseif string.sub(s, 1, 2) == "6." then -- Tiebreak loser
local tb = math.ceil((n - 6) * 100)
return '6<sup>' .. tb .. '</sup>'
elseif n == 7 and nB and string.sub(tostring(scoreB or ''), 1, 2) == "6." then -- Tiebreak winner
local tb = math.ceil((nB - 6) * 100)
tb = math.max(tb + 2, 7)
return '7<sup>' .. tb .. '</sup>'
else
return s
end
end
local function setWinner(args, i, sets)
local a = tonumber(args[tostring(i)]) or 0
local b = tonumber(args[tostring(i + sets)]) or 0
return a - b
end
local function matchWinner(args, sets)
local b = 0
for i = 1, sets do
local winner = setWinner(args, i, sets)
if winner > 0 then
b = b + 1
elseif winner < 0 then
b = b - 1
end
end
return b
end
-- Render player names
local function renderPlayers(args, sets)
local t1, t2 = args.T1P1 or '', args.T2P1 or ''
if t1 == '' then return ' ' end
if notblank(args.T1P2) then
t1 = t1 .. ' / ' .. args.T1P2
t2 = t2 .. ' / ' .. (args.T2P2 or '')
end
if args.np ~= '' then -- not "not played"
if tonumber(args.wo) == 1 then -- walkover awarded to team 1
t1 = bold(t1)
elseif tonumber(args.wo) == 2 then -- walkover awarded to team 2
t2 = bold(t2)
elseif args[1] ~= '' and (not notblank(args.nc) or notblank(args.def)) then
-- A score was entered and either the match was completed or there was a default
if notblank(args.re1) or tonumber(args.def) == 1 then --Team 1 has a player retired or defaulted
t2 = bold(t2)
elseif notblank(args.re2) or tonumber(args.def) == 2 then --Team 2 has a player retired or defaulted
t1 = bold(t1)
else --Normal match, make the team with the higher score bold
local winner = matchWinner(args, sets)
if winner > 0 then
t1 = bold(t1)
elseif winner < 0 then
t2 = bold(t2)
end
end
end
end
return t1 .. '<br />' .. t2
end
-- Render a single score column
local function renderScoreColumn(args, i, sets)
local a = args[tostring(i)]
local b = args[tostring(i + sets)]
if not notblank(a) then
return ' '
else
local scoreA, scoreB = formatScore(a,b), formatScore(b,a)
if args.nc ~= tostring(i) and args.re1 ~= tostring(i) and args.re2 ~= tostring(i) then
--Match didn't end early this set
local winner = setWinner(args, i, sets)
if winner > 0 then
scoreA = bold(scoreA)
elseif winner < 0 then
scoreB = bold(scoreB)
end
end
return scoreA .. '<br />' .. scoreB
end
end
-- Status cell (rightmost column)
local function renderStatus(args)
if not notblank(args.T1P1) then -- Team 1 player 1 empty
return ' '
elseif args.np == '' then
return bold('not<br />played')
elseif tonumber(args.wo) == 1 then
return bold('[[walkover|w/o]] <br /> ')
elseif tonumber(args.wo) == 2 then
return bold(' <br />[[walkover|w/o]]')
elseif notblank(args.nc) then
if tonumber(args.def) == 1 then
return bold('defaulted <br /> ')
elseif tonumber(args.def) == 2 then
return bold(' <br />defaulted')
elseif notblank(args.sus) then
return args.sus
else
return bold('not<br />completed')
end
elseif notblank(args['re1']) then
return bold('retired <br /> ')
elseif notblank(args['re2']) then
return bold(' <br />retired')
else
return ' '
end
end
function p._main(args, sets)
local out = {}
table.insert(out, renderPlayers(args, sets))
for i = 1, sets do
table.insert(out, renderScoreColumn(args, i, sets))
end
table.insert(out, 'style="text-align:center; | ' .. renderStatus(args))
return table.concat(out, '\n| ')
end
function p.five(frame)
local args = getArgs(frame, {removeBlanks = false})
return p._main(args, 5)
end
function p.three(frame)
local args = getArgs(frame, {removeBlanks = false})
return p._main(args, 3)
end
p.main = p.five
return p