Module:RoundN
Appearance
local p = {
RD = {
'Quarter Finals',
'Semi Finals',
'Final',
'Third Place'
},
reuseStr = {},
--always call this like so: (p.reuseStr.name or p:saveStr('name', 'string' .. 'string' .. etc))
--this avoids repeating the concatenation operation with each loop
saveStr = function(self, name, concatString)
self.reuseStr[name] = concatString
return concatString
end,
cleaned = {}
}
local rowNum, head = {}, {}
local tab = mw.html.create'table'
local nodeFunc = {
helper = {
freeCells = function(self)
local count = 0
if not self.info.occupied.full then
for _, x in ipairs(self.info.occupied) do
for _, y in ipairs(x) do
count = count + 1
end
end
self.info.occupied.full = self.info.totalCells == count
end
if self.info.occupied.full then
return false
end
return self.info.totalCells - count
end,
showBox = function()
return {}
end
},
line = {--this node is omitted and replaced with a line
main = function(x)
local helper = p.getNodeFunc().helper
if helper:freeCells() == 12 then
local v = helper.info
for k = 0, 1 do
rowNum[v.row + k * 4]:tag'td'
:css{
['border-' .. (v.top == 0 and 'top' or 'bottom')] = k ~= v.top and p.reuseStr.solid or nil,
}
:attr{
rowspan = k == 0 and 4 or 2,
colspan = 2
}
end
helper.info.occupied.full = true
else
return nil
end
return x
end
},
bridge = {--Draw a line to the neighboring node in the same column that is not connected to the current node
main = function(x)
v = p.getNodeFunc().helper.info
p.getNodeFunc().bridge.lay[v.col][
v.num - v.first + 1 + (v.top == 1 and 1 or -1)
] = true
return x
end,
lay = {}
},
canvas = {--Merges all cells in node. Content will be the next parameter.
main = function()
local helper = p.getNodeFunc().helper
if helper:freeCells() == 12 then
rowNum[helper.info.row]:tag'td'
:attr{rowspan = 6, colspan = 2}
helper.info.occupied.full = true
else
return nil
end
end
}
}
function p.getNodeFunc()
return nodeFunc
end
--Provides a convenient naming shortcut up to {{#invoke:RoundN|N512}} = {{invoke:RoundN|main|columns = 9}}
for c = 1, 9 do
local N = math.pow(2, c)
p['N' .. N] = function(frame)
return p.main(frame.args, c)
end
p['n' .. N] = p['N' .. N]--to make case insensitive
end
function newRow(bodyRow)
tab.r = tab:tag'tr'
tab.r:tag'td'
:css(p.flex_tree.css)
:wikitext(p.flex_tree.wt)
if bodyRow then
table.insert(rowNum, bodyRow, tab.r)
end
end
function drawHead(text, row3rd)
(row3rd and rowNum[row3rd]:tag'td':attr{rowspan = 2}
or head.row:tag'td')
:attr{colspan = 2}
:css{
['text-align'] = 'center',
border = '1px solid #aaa',
background = '#f2f2f2'
}
:wikitext(text)
end
function spacer(width)
tab.r:tag'td'
:attr{width = width}
:wikitext(p.no_column_head and '' or ' ')
end
function dpBox(v, r)
tab.r = rowNum[r]:tag'td'
:wikitext(v or ' ')
:attr{rowspan = 2, colspan = 2}
end
function boldWin(s1, s2, manual)
local result = {}
if p.bold and not manual then
s1 = tonumber(string.gsub(s1 or '', '%D', ''), 10) or math.huge
s2 = tonumber(string.gsub(s2 or '', '%D', ''), 10) or math.huge
result = ({{s1 < s2, s1 > s2}, {s1 > s2, s1 < s2}})[p.bold]
end
return result
end
function maxSpan(span, start, rows)
return math.min(span, math.max(0, rows - start + 1))
end
function teamBox(v, r, f)
tab.oldR = tab.r
tab.r = rowNum[r]:tag'td'
:css{
border = '1px solid #aaa',
background = ({'gold', 'silver', '#C96', '#f9f9f9'})[f.color or 4],
['text-align'] = f[1]
}
:attr{rowspan = 2}
tab.r:tag(f.bold and 'b' or ''):wikitext(' ' .. (v or ''))
end
function p._main(args, frame)
function args:clean(key, params)--prevent html comments from breaking named args and reduces repeat concatenation
params = params or {}
if params.checkClean then
if p.cleaned[key] then
return p.cleaned[key]
end
end
p.cleaned[key] = string.gsub(string.gsub(args[key] and mw.text.decode(args[key]) or params.ifNil or '', '<!.+>', ''), '[^%w-;]', '') .. (params.append or '')
p.cleaned[key] = p.cleaned[key] ~= '' and p.cleaned[key] or nil
args[key] = params.keepOld and (args[key] or params.ifNil) or p.cleaned[key]
local clean = p.cleaned[key]
p.cleaned[key] = params.checkClean and p.cleaned[key] or nil
return clean
end
p.cols = tonumber(args:clean('columns'))
p.bold = ({low = 1, high = 2})[args:clean('bold_winner')]
local skipMatch, unBold = {}, {}--(skip|manualbold)match# to boolean
for k, _ in pairs(args) do
local mType, mNum = string.match(k, '^(%a+)match(%d*)$')
mType, mNum = ({skip = skipMatch, manualbold = unBold})[mType], tonumber(mNum)
if mType then
if mNum then
mType[mNum] = args:clean(k) == 'yes' or args[k] == 'true'
elseif ({['-']=1,[';']=1})[string.match(args:clean(k), '%d([-;])%d')] then--match ranges when no skipmatch
for _, x in ipairs(mw.text.split(args[k], ';')) do
local m = mw.text.split(x, '-')
for y = tonumber(m[1] or x) or 1, tonumber(m[2] or m[1] or x) or 0 do
mType[y] = true
end
end
end
end
end
for _, v in ipairs({
--more args to boolean
'widescore',
'template',
'color',
'3rdplace',
'omit_blanks',
'scroll_head_unlock',
'previewnumbers',
'flex_tree',
'no_column_head'
--[['one_per_branch']]
}) do
if args[v] and (p[v] == nil or type(p[v]) == 'boolean') then
p[v] = args:clean(v) == 'yes' or args[v] == 'true'
end
end
p.flex_tree = {
css = p.flex_tree and {} or {['font-size'] = '50%'},
wt = p.flex_tree and '' or ' '
}
tab
:cssText(
(args.scroll_height and 'padding' or 'margin') .. ':1em 2em 1em 1em;border:0;font-size:90%;'
.. (args.style or '')
)
:attr{cellpadding = 0, cellspacing = 0}
if not p.no_column_head then--headings row
newRow()
head.row = tab.r
newRow()
else
tab.r = tab:tag'tr'
tab.r:tag'td'
end
local sp = {--set column widths
args['team-width'] or 170,
args['score-width'] or (p.widescore and 40) or 30,
15,
20
}
for k = 1, p.cols do
if k > 1 then
spacer(sp[3])
spacer(sp[4])
if not p.no_column_head then
head.row:tag'td':attr{colspan = 2}
end
end
spacer(sp[1])
spacer(sp[2])
if not p.no_column_head then
head.wt = args['RD' .. k]
or p.RD[#p.RD + k - p.cols - 1]
or ('Round of ' .. math.pow(2, p.cols - k + 1))
drawHead(
k == 1 and p.template and mw.getCurrentFrame():expandTemplate{
title = 'tnavbar-header',
args = {head.wt, 'Template:' .. args.name}
} or head.wt
)
end
end
local step, bump, bumps, RD, m, rows, ns = 1, 0, 0, {tot = 0}, {num = 1, phase = 0}, math.pow(2, p.cols) * 3, mw.title.getCurrentTitle().namespace--Begin tab
tab.line = {--reduces concats and memory usage
{
[true] = args:clean('line_px', {ifNil = 3, append = 'px'}),
[false] = 0
},
args.line_px .. ' ' .. args.line_px
}
for r = 1, rows do
newRow(r)
end
p.reuseStr.solid = p.reuseStr.solid or p:saveStr('solid', tab.line[1][true] .. ' solid')
for c = 1, p.cols do
RD.tot = RD.tot + (rows / math.pow(2, c)) / 3
if c > 1 then
tab.r = rowNum[1]:tag'td'
:attr{rowspan = bump, colspan = 2}
:wikitext' '
if p.no_column_head and p.template and c == p.col then
tab.r
:wikitext(mw.getCurrentFrame():expandTemplate{
title = 'tnavbar-header',
args = {head.wt, 'Template:' .. args.name}
})
:css('vertical-align', 'top')
end
end
bumps = bump
RD.top = m.num
nodeFunc.bridge.lay[c] = {}
for r = 1, rows, 2 do
RD.r = r + bumps
if rowNum[RD.r] and m.num <= RD.tot then
if skipMatch[m.num] then
if m.phase == 0 then
rowNum[RD.r]:tag'td':attr{rowspan = maxSpan(6 + bump * 2, RD.r, rows), colspan = 2}
elseif m.phase == 2 then
m.num = m.num + 1
step = step + (p.omit_blanks and 0 or 5)
bumps = bumps + maxSpan(bump * 2, RD.r, rows)
end
elseif m.phase == 0 then
local node_function = string.match(args[step] or '', 'node_function{([^}]*)')
if node_function then
node_function = mw.text.split(node_function, '[%s,]+')
nodeFunc.helper.info = {
called = {},
occupied = {{},{}},
totalCells = 12,
notFinished = true,
row = RD.r,
col = c,
num = m.num,
first = RD.top,
top = (m.num - RD.top) % 2--node is top of fork if top is 0
}
for x, y in ipairs(node_function) do
if nodeFunc[y] and nodeFunc[y].main then
nodeFunc.helper.info.called[y] = nodeFunc[y].main(x)
end
end
step = step + 1
if not nodeFunc.helper:freeCells() then
m.showBox = {}
end
else
m.showBox = {1, 2, --[[]]2}--[[not p.one_per_branch and 2 or nil}]]
end
if m.showBox[1] then
dpBox(args[step], RD.r, skipMatch[m.num])
end
tab.r:node(args.previewnumbers and ns ~= 0 and
mw.html.create'div'
:css{
float = 'left',
display = 'inline-block',
border = '1px solid red',
padding = '0 .5ex',
['color'] = 'red'
}
:wikitext(m.num)
:attr{title = 'This number is only visible outside artcle space (e.g. template) when |numberpreview=yes'}
)
m.bold = boldWin(args[step + 2], args[step + 4], not (m.showBox[2] or m.showBox[3]) or unBold[m.num])
else
if m.showBox[m.phase] then
teamBox(args[step + (m.phase - 1) * 2 + 1], RD.r, {color = p.color and c == p.cols and m.phase, bold = m.bold[m.phase]})
teamBox(args[step + (m.phase - 1) * 2 + 2], RD.r, {'center', color = p.color and c == p.cols and m.phase, bold = m.bold[m.phase]})
end
--not yet implemented
--[[if p.one_per_branch and m.showBox[2] then
m.phase = 2
tab.oldR:attr{rowspan = 4}
tab.r:attr{rowspan = 4}
bumps = bumps + 2
end]]
if m.phase == 2 then
step = step + (m.showBox[1] or 0) + (m.showBox[2] or 0) + (m.showBox[3] or 0)
m.num = m.num + 1
if bump > 0 and rowNum[RD.r + 2] then
p.span = p.cols > c and (bump * 2) or math.max((bump - 1) / 2, 2)
bumps = bumps + p.span
rowNum[RD.r + 2]:tag'td':attr{rowspan = p.span, colspan = 2}
end
r = r + bump
end
end
m.phase = (m.phase + 1) % 3
end
end
if p.cols > c then--draw lines to next round
p.unit = bump + 3
bump = (rows - rows / math.pow(2, c)) / math.pow(2, p.cols - c)
bumps = p.unit + 1
rowNum[1]
:tag'td':attr{rowspan = bumps}
:tag'td'
:attr{rowspan = bump + 4}
:css(nodeFunc.bridge.lay[c][0] and
{['border-right'] = p.reuseStr.solid}
or {}
)
--:css('border-bottom', not skipMatch[RD.top] and '1px solid' or nil)
RD.n = 0
for r = bumps + 1, rows, p.unit * 2 do
tab.r = rowNum[r]:tag'td'
local interval = ((r - bumps - 1) / (p.unit * 2)) % 4
if interval % 2 == 0 then
--RD.t and RD.t2 control whether lines are drawn
RD.t = RD.t2 or skipMatch[RD.tot + RD.n / 2 + 1] and 3 or ((skipMatch[RD.top] and 1 or 0) + (skipMatch[RD.top + 1] and 2 or 0))
RD.n = RD.n + 2
RD.t2 = skipMatch[RD.tot + RD.n / 2 + 1] and 3 or ((skipMatch[RD.top + RD.n] and 1 or 0) + (skipMatch[RD.top + RD.n + 1] and 2 or 0))
if RD.t == 0 then
tab.r
:attr{rowspan = maxSpan(p.unit * 2, r, rows)}
:css(skipMatch[RD.tot + RD.n / 2] and {} or {
border = p.reuseStr.solid,
['border-left'] = 0
})
else
tab.r
:attr{rowspan = maxSpan(p.unit, r, rows)}
:cssText(RD.t == 2 and
(p.reuseStr.topRight or p:saveStr('topRight', 'border-width:' .. tab.line[2] .. ' 0 0;border-style:solid'))
or RD.t == 1 and 'vertical-align:bottom'
or nil
)
:node(RD.t == 1 and interval > 0 and mw.html.create'div':css{height = tab.line[1][true], ['border-right'] = p.reuseStr.solid})
rowNum[r + p.unit]:tag'td'
:attr{rowspan = maxSpan(p.unit, r + p.unit, rows)}
:cssText(RD.t == 1 and
(p.reuseStr.bttmRght or p:saveStr('bttmRght', 'border-width:0 ' .. tab.line[2] .. ' 0;border-style:solid'))
or RD.t == 2 and 'vertical-align:top'
or nil
)
:node(RD.t == 2 and interval ~= 2 and mw.html.create'div':css{height = tab.line[1][true], ['border-right'] = p.reuseStr.solid})
end
RD.t = {
RD.t < 3,
rowNum[r + p.unit * 5] and RD.t2 < 3 or false
}
rowNum[r + p.unit]:tag'td'
:attr{rowspan = maxSpan(p.unit * 4, r + p.unit, rows)}
:css(interval == 0 and (RD.t[1] or RD.t[2]) and {
['border-width'] = tab.line[1][RD.t[1]] .. ' 0 ' .. tab.line[1][RD.t[2]],
['border-style'] = 'solid'
} or {})
else
tab.r
:attr{rowspan = maxSpan(p.unit * 2, r, rows)}
:css(nodeFunc.bridge.lay[c][RD.n] and
{['border-right'] = p.reuseStr.solid}
or {}
)
end
end
end
end
if p['3rdplace'] or p.cols > 3 and p['3rdplace'] == nil then--begin 3rd place
RD.r = rows / 2 + 4 + (p.span or 3)
for r = rows + 1, RD.r + 7 do
newRow(r)
end
if rowNum[rows + 1] and p.cols > 1 then
rowNum[rows + 1]:tag'td':attr{
rowspan = RD.r + 7 - rows,
colspan = (p.cols - 1) * 4
}
end
drawHead(args.Consol or args['RD' .. (p.cols + 1)] or p.RD[4], RD.r)
dpBox(args[step], RD.r + 2)
m.bold = boldWin(args[step + 2], args[step + 4], unBold[m.num])
for k, v in ipairs({
{4, {color = p.color and 3, bold = m.bold[1]}},
{4, {'center', color = p.color and 3, bold = m.bold[1]}},
{6, {bold = m.bold[2]}},
{6, {'center', bold = m.bold[2]}}
}) do
teamBox(args[step + k], RD.r + v[1], v[2])
end
end
return args.scroll_height and
mw.html.create'div'
:cssText'border-bottom:1px solid #eee;display:inline-block'
:node(not (p.scroll_head_unlock or p.no_column_head) and mw.html.create'div'
:css{
overflow = 'hidden',
height = '3em',
['border-bottom'] = 'inherit',
['margin-right'] = '17px'
}
:node(mw.clone(tab))
)
:tag'div'
:cssText('overflow-y:scroll;max-height:'
.. args.scroll_height .. (string.match(args.scroll_height, '^%d*$') and 'px' or '')
)
:node(not (p.scroll_head_unlock or p.no_column_head) and
tab:css('margin-top', '-3em')
or tab
)
:done()
or tab
end
function p.main(frame, columns)
local args = require'Module:Arguments'.getArgs(frame, {trim = false})
args.columns = args.columns or columns
return p._main(args, frame)
end
return p