require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local p = {}
function p._total(frame, played, won, drawn, lost, category)
if played == '-' or played == '—' then
return '—'
elseif not played then
if not won and not drawn and not lost then
return ''
end
return frame:expandTemplate{title = 'Number table sorting', args = { (won or 0) + (drawn or 0) + (lost or 0) }}
elseif tonumber(played) ~= won + drawn + lost then
return '<span class="error" style="font-size:100%">' .. frame:expandTemplate{title = 'abbr', args = { 'error', 'GP not equal to W + D + L' }} .. (category or '') .. '</span>'
else
return frame:expandTemplate{title = 'Number table sorting', args = { played }}
end
end
function p.total(frame)
local args = getArgs(frame)
return p._total(frame, args[1], args[2], args[3], args[4], args.category)
end
-- TODO: Write a Module:Decimals with basically this code
function p.roundAndPad(n, decimals)
if tonumber(decimals) > 12 then
decimals = 12
end
local mult = 10^decimals
n = math.floor(n * mult + 0.5) / mult
if decimals < 0 then
return tostring(n)
else
return string.format('%.' .. decimals .. 'f', n)
end
end
local function displayWinPercent(frame, winPercent, decimals)
local retval = ''
if winPercent < 10 then
retval = '<span style="visibility:hidden;color:transparent;">0</span>'
end
retval = retval .. frame:expandTemplate{title = 'Number table sorting', args = { roundAndPad(winPercent, decimals or 2) }}
if winPercent >= 100 then -- XXX: Should >= be ==? This should never be over 100, but that's what the original template had
return retval .. '<span style="visibility:hidden;color:transparent;">0</span>'
else
return retval
end
end
function p._pct(frame, played, won, drawn, lost, decimals)
if played == '-' or played == '—' then
return '—'
elseif not played then
if not won and not drawn and not lost then
return ''
elseif (won or 0) + (drawn or 0) + (lost or 0) <= 0 then
return '<span style="display:none">!</span>—'
end
return displayWinPercent(frame, 100 * (won or 1) / ((won or 1) + (drawn or 0) + (lost or 0)), decimals) -- XXX: Why is 1 used in the numerator instead of 0 if won isn't set? It's not like that in the case where played is set. It makes sense in the denominator to avoid a divide by zero
elseif tonumber(played) <= 0 then
return '<span style="display:none">!</span>—'
else
return displayWinPercent(frame, 100 * (won or 0) / played, decimals)
end
end
function p.pct(frame)
local args = getArgs(frame)
return p._pct(frame, args[1], args[2], args[3], args[4], args.decimals)
end
function p.main(frame)
local args = getArgs(frame)
local tableprefix = '| style="text-align:' .. (args.align or 'center') .. '" |'
local retval = tableprefix .. p._total(frame, args[1], args[2], args[3], args[4], args.demospace and '' or '[[Category:WDL error]]') .. '\n'
retval = retval .. tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { args[2] }} .. '\n'
retval = retval .. tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { args[3] }} .. '\n'
retval = retval .. tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { args[4] }} .. '\n'
if args['for'] then
retval = retval .. tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { args['for'] }} .. '\n'
end
if args.against then
retval = retval .. tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { args.against }} .. '\n'
end
if args.diff == 'yes' then
if tonumber(args['for']) and tonumber(args.against) then
retval = retval .. tableprefix .. (args['for'] >= args.against and '+' or '') .. (args['for'] - args.against) .. '\n'
else
retval = retval .. tableprefix .. '<span style="display:none">!</span>—\n'
end
end
return retval .. tableprefix .. p._pct(frame, args[1], args[2], args[3], args[4], args.decimals) .. '\n'
end
return p