Difference between revisions of "Module:WDL/sandbox"
Jump to navigation
Jump to search
blackwiki>Pppery (Remove use of Module:Decimals) |
m (22 revisions imported) |
||
| (5 intermediate revisions by one other user not shown) | |||
| Line 6: | Line 6: | ||
local p = {} | local p = {} | ||
| − | local function total( | + | local function total(won, drawn, lost) |
| − | + | if not won and not drawn and not lost then | |
| − | + | return '' | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | return ' | ||
else | else | ||
| − | return | + | return (won or 0) + (drawn or 0) + (lost or 0) |
end | end | ||
end | end | ||
| Line 24: | Line 17: | ||
local retval = '' | local retval = '' | ||
if winPercent < 10 then | if winPercent < 10 then | ||
| + | retval = '<span style="visibility:hidden;color:transparent;">00</span>' | ||
| + | elseif winPercent < 100 then | ||
retval = '<span style="visibility:hidden;color:transparent;">0</span>' | retval = '<span style="visibility:hidden;color:transparent;">0</span>' | ||
end | end | ||
| − | + | return retval .. frame:expandTemplate{title = 'Number table sorting', args = { roundAndPad(winPercent, decimals or 2) }} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
| − | local function pct(frame, played, won, drawn, lost, decimals) | + | local function pct(frame, played, won, drawn, lost, decimals, winpctdraw) |
if played == '-' or played == '—' then | if played == '-' or played == '—' then | ||
return '—' | return '—' | ||
| Line 43: | Line 33: | ||
return '<span style="display:none">!</span>—' | return '<span style="display:none">!</span>—' | ||
end | end | ||
| − | + | ||
| + | played = (won or 0) + (drawn or 0) + (lost or 0) | ||
elseif tonumber(played) <= 0 then | elseif tonumber(played) <= 0 then | ||
return '<span style="display:none">!</span>—' | return '<span style="display:none">!</span>—' | ||
| − | |||
| − | |||
end | end | ||
| + | |||
| + | local wins = (won or 0) | ||
| + | local draws = tonumber(drawn) or 0 | ||
| + | local games = played | ||
| + | if draws > 0 then | ||
| + | if winpctdraw == 'ignore'then | ||
| + | -- treat ignored draws like the game was never played | ||
| + | games = games - draws | ||
| + | elseif winpctdraw == 'loss' then | ||
| + | -- don't have to do anything - this was the previous behavior | ||
| + | else -- default to 'half' | ||
| + | wins = wins + (draws / 2) | ||
| + | end | ||
| + | end | ||
| + | |||
| + | return displayWinPercent(frame, 100 * wins / games, decimals) | ||
end | end | ||
| − | function p.main(frame) | + | function p.main(frame, otherargs) |
| − | local args = getArgs(frame) | + | local args = otherargs or getArgs(frame) |
local tableprefix = string.format('| style="%stext-align:%s" |', args.total and 'font-weight:bold;background:#efefef;' or '', args.align or 'center') | local tableprefix = string.format('| style="%stext-align:%s" |', args.total and 'font-weight:bold;background:#efefef;' or '', args.align or 'center') | ||
| − | local | + | local played = total(args[2], args[3], args[4]) |
| + | local retval = tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { played }} .. '\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[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[3] }} .. '\n' | ||
| Line 66: | Line 72: | ||
if args.diff == 'yes' then | if args.diff == 'yes' then | ||
if tonumber(args['for']) and tonumber(args.against) then | if tonumber(args['for']) and tonumber(args.against) then | ||
| − | retval = retval .. tableprefix .. string.format('%s%d\n', args['for'] < args.against and '−' or '+', math.abs(args['for'] - args.against)) | + | retval = retval .. tableprefix .. string.format('%s%d\n', tonumber(args['for']) < tonumber(args.against) and '−' or '+', math.abs(args['for'] - args.against)) |
else | else | ||
retval = retval .. tableprefix .. '<span style="display:none">!</span>—\n' | retval = retval .. tableprefix .. '<span style="display:none">!</span>—\n' | ||
end | end | ||
end | end | ||
| − | return retval .. tableprefix .. pct(frame, | + | if args.winpctdraw then |
| + | retval = retval .. tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { args.winpctdraw }} .. '\n' | ||
| + | end | ||
| + | |||
| + | return retval .. tableprefix .. pct(frame, played, args[2], args[3], args[4], args.decimals, args.winpctdraw) | ||
end | end | ||
return p | return p | ||
Latest revision as of 05:11, 30 September 2020
Documentation for this module may be created at Module:WDL/sandbox/doc
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local roundAndPad = require('Module:Math')._precision_format
local p = {}
local function total(won, drawn, lost)
if not won and not drawn and not lost then
return ''
else
return (won or 0) + (drawn or 0) + (lost or 0)
end
end
local function displayWinPercent(frame, winPercent, decimals)
local retval = ''
if winPercent < 10 then
retval = '<span style="visibility:hidden;color:transparent;">00</span>'
elseif winPercent < 100 then
retval = '<span style="visibility:hidden;color:transparent;">0</span>'
end
return retval .. frame:expandTemplate{title = 'Number table sorting', args = { roundAndPad(winPercent, decimals or 2) }}
end
local function pct(frame, played, won, drawn, lost, decimals, winpctdraw)
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
played = (won or 0) + (drawn or 0) + (lost or 0)
elseif tonumber(played) <= 0 then
return '<span style="display:none">!</span>—'
end
local wins = (won or 0)
local draws = tonumber(drawn) or 0
local games = played
if draws > 0 then
if winpctdraw == 'ignore'then
-- treat ignored draws like the game was never played
games = games - draws
elseif winpctdraw == 'loss' then
-- don't have to do anything - this was the previous behavior
else -- default to 'half'
wins = wins + (draws / 2)
end
end
return displayWinPercent(frame, 100 * wins / games, decimals)
end
function p.main(frame, otherargs)
local args = otherargs or getArgs(frame)
local tableprefix = string.format('| style="%stext-align:%s" |', args.total and 'font-weight:bold;background:#efefef;' or '', args.align or 'center')
local played = total(args[2], args[3], args[4])
local retval = tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { played }} .. '\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 .. string.format('%s%d\n', tonumber(args['for']) < tonumber(args.against) and '−' or '+', math.abs(args['for'] - args.against))
else
retval = retval .. tableprefix .. '<span style="display:none">!</span>—\n'
end
end
if args.winpctdraw then
retval = retval .. tableprefix .. frame:expandTemplate{title = 'Number table sorting', args = { args.winpctdraw }} .. '\n'
end
return retval .. tableprefix .. pct(frame, played, args[2], args[3], args[4], args.decimals, args.winpctdraw)
end
return p