Module:RoundN

local p = { RD = { 'Quarter Finals', 'Semi Finals', 'Final', 'Third Place' } } local output = mw.html.create'table' local rowNum, head = {}, {}

function newRow(bodyRow) p.tag = output:tag'tr' :tag'td' :attr{height = 5} :done if bodyRow then table.insert(rowNum, bodyRow, p.tag) end end

function drawHead(text, row3rd) (row3rd and rowNum[row3rd]:tag'td':attr{rowspan = 2}	or head.row:tag'td') :attr{colspan = 2} :css{ border = '1px solid #aaa', background = '#f2f2f2', ['text-align'] = 'center' }		:wikitext(text) end

function spacer(width) p.tag:tag'td' :attr{width = width} :wikitext' ' end

function dpBox(v, 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)	p.tag = rowNum[r]:tag'td' :css{ background = ({'gold', 'silver', '#C96', '#f9f9f9'})[f.color or 4], border = '1px solid #aaa', padding = '0 2px' }		:attr{rowspan = 2} :css{['text-align'] = f[1]} :tag(f.bold and 'b' or ):wikitext(' ' .. (v or )) end

function p._main(args, frame) p.cols = tonumber(args.columns) local skipMatch, unBold = {}, {}--skipmatch#/manualboldmatch# to boolean p.bold = ({low = 1, high = 2})[args.bold_winner] for k, v in pairs(args) do		skipMatch[tonumber(string.match(k, 'skipmatch(%d+)')) or 0] = v == 'yes' or v == 'true' unBold[tonumber(string.match(k, 'manualboldmatch(%d+)')) or 0] = v == 'yes' or v == 'true' end for _, v in ipairs({--more args to boolean		'widescore',		'template',		'color',		'3rdplace',		'omit_blanks',		'scroll_head_unlock'	}) do		if args[v] ~= nil then p[v] = args[v] == 'yes' or args[v] == 'true' end end output :cssText(			'margin:1em 2em 1em 1em;border:0;font-size:90%;border-spacing:0;' 			.. (args.style or '')		) :attr{cellpadding = 0} newRow--headings row head.row = p.tag newRow--set column widths for k = 1, p.cols do		if k > 1 then spacer(15) spacer(20) head.row:tag'td':attr{colspan = 2} end spacer(args['team-width'] or 170) spacer(args['score-width'] or (p.widescore and 40) or 30) p.head = 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 frame:expandTemplate{				title = 'tnavbar-header',				args = {p.head, 'Template:' .. args.name}			} or p.head		) end local step, bump, bumps, RD, m, rows = 1, 0, 0, {tot = 0}, {num = 1, phase = 0}, math.pow(2, p.cols) * 3--Begin output for c = 1, p.cols do		RD.tot = RD.tot + (rows / math.pow(2, c)) / 3 if c > 1 then rowNum[1]:tag'td' :attr{rowspan = bump, colspan = 2} :wikitext' ' end bumps = bump RD.top = m.num for r = 1, rows, 2 do			if c == 1 then newRow(r) newRow(r + 1) end RD.r = r + bumps if rowNum[RD.r] and m.num <= RD.tot then if c == 1 and skipMatch[m.num] then if m.phase == 0 then rowNum[RD.r]:tag'td':attr{rowspan = 6, colspan = 2} elseif m.phase == 2 then m.num = m.num + 1 step = step + (p.omit_blanks and 0 or 5) end elseif m.phase == 0 then dpBox(args[step], RD.r, skipMatch[m.num]) m.bold = boldWin(args[step + 2], args[step + 4], unBold[m.num]) else 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]}) if m.phase == 2 then step = step + 5 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 for s = 2, p.span + 1 do								if c == 1 and (RD.r + s) <= rows then newRow(RD.r + s)								end end 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('border-bottom', '1px solid') RD.n = 0 for r = bumps + 1, rows, p.unit * 2 do				p.tag = rowNum[r]:tag'td' if ((r - bumps - 1) / (p.unit * 2)) % 2 == 0 then RD.t = (skipMatch[RD.top + RD.n] and 1 or 0) + (skipMatch[RD.top + RD.n + 1] and 2 or 0) RD.n = RD.n + 2 if c > 1 or RD.t == 0 then p.tag :attr{rowspan = maxSpan(p.unit * 2, r, rows)} :css{ border = '3px solid', ['border-left'] = 0 }					else p.tag :attr{rowspan = maxSpan(p.unit, r, rows)} :cssText(RD.t == 2 and 'border-width:3px 3px 0 0;border-style:solid' or nil) rowNum[r + p.unit]:tag'td' :attr{rowspan = maxSpan(p.unit, r + p.unit, rows)} :cssText(RD.t == 1 and 'border-width:0 3px 3px 0;border-style:solid' or nil) end rowNum[r + p.unit]:tag'td' :attr{rowspan = maxSpan(p.unit * 4, r + p.unit, rows)} :css{ ['border-width'] = '2px 0 ' .. (rowNum[r + p.unit * 5] and '1px' or 0), ['border-style'] = 'solid' }				else p.tag:attr{rowspan = maxSpan(p.unit * 2, r, rows)} 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' :css{position = 'relative', ['border-bottom'] = '1px solid #eee'} :node(not p.scroll_head_unlock and mw.html.create'div'				:css{					display = 'inline-block', 					overflow = 'hidden',					height = '3em',					['border-bottom'] = 'inherit',					['margin-right'] = '17px'				}				:node(mw.clone(output))			) :tag'div' :cssText('display:inline-block;overflow-y:scroll;height:'					.. args.scroll_height					.. (args.scroll_height == string.gsub(args.scroll_height, '%D', ) and 'px' or )				) :node(output					:css('margin-top', '-3em')				) :allDone or output end

function p.main(frame) local args = require'Module:Arguments'.getArgs(frame, {trim = false}) return p._main(args, frame) end

return p