Module:RoundN

local p = {} local defaultRounds = {'Quarter Finals', 'Semi Finals', 'Final', 'Third Place'} local defaultColors = {'gold', 'silver', '#C96'} local output = mw.html.create('table') local last = {} local rowNum = {}

function newRow(nonHead) last.row = output:tag('tr') last.row:tag('td') :attr('height', 5) if nonHead then table.insert(rowNum, nonHead, last.row) end end

function drawHead(text) last.cell = last.row:tag('td') last.cell :attr('colspan', 2) :css('text-align', 'center') :css('border', '1px solid #aaa') :css('background-color', '#f2f2f2') :wikitext(text) end

function spacer(width) last.row:tag('td') :attr('width', width) :wikitext(' ') end

function insertPairs(param, v)	if #(param[#param] or {}) == 2 or #param == 0 then table.insert(param, {}) end table.insert(param[#param], v)	return param end

function dpBox(v, r)	rowNum[r]:tag('td') :wikitext(' ' .. (v or '')) :attr('rowspan', 2) :attr('colspan', 2) end

function teamBox(v, r, c)	last.cell = rowNum[r]:tag('td') last.cell :css('background-color', c and c.color and defaultColors[c.color] or '#f9f9f9') :css('border', '1px solid #aaa') :css('padding', '0 2px') :wikitext(' ' .. (v or '')) :attr('rowspan', 2) if c and c['center'] then last.cell:css('text-align', 'center') end end

function p._main(args, frame) output :css('font-size', '90%') :css('margin', '1em 2em 1em 1em') :css('border-spacing', 0) :css('border', 0) :css('padding', 0) newRow--headings row for k = 1, args.columns do 		if k > 1 then last.row:tag('td'):attr('colspan', 2) end drawHead(			args['RD' .. k] or			defaultRounds[k - 1] or			('Round of ' .. math.pow(2, args.columns - k + 1))		) end newRow--column widths row for k = 1, args.columns do		if k > 1 then spacer(15) spacer(20) end spacer(170) spacer(args.widescore == 'yes' and 40 or 30) end local step = 1--Begin output local skip = 0 local skips = 0 local rows = math.pow(2, args.columns) * 3 local rPhase = 0 local rTarg local cap = 0 for c = 1, args.columns do		cap = cap + (rows / math.pow(2, c)) / 3 if c > 1 then rowNum[1]:tag('td') :attr('rowspan', skip) :attr('colspan', 2) end skips = skip for r = 1, rows, 2 do			if c == 1 then newRow(r) newRow(r + 1) end rTarg = r + skips if rowNum[rTarg] and ((step-1)/5+1) <= cap then last.row = rowNum[rTarg] if rPhase == 0 then dpBox(args[step], rTarg) else if rPhase == 1 then teamBox(args[step + 1], rTarg, (args.color == 'yes' and c == args.columns and {color = 1}) or nil) teamBox(args[step + 2], rTarg, {center = 1, color = (args.colors == 'yes' and c == args.columns and 1) or nil}) else teamBox(args[step + 3], rTarg, (args.color == 'yes' and c == args.columns and {color = 2}) or nil) teamBox(args[step + 4], rTarg, {center = 1, color = (args.color == 'yes' and c == args.columns and 2) or nil}) step = step + 5 if skip > 0 and rowNum[rTarg + 2] then last.span = tonumber(args.columns) > c and (skip * 2) or math.max(skip - 11, 1) for s = 2, last.span + 1 do								skips = skips + 1 if c == 1 and (rTarg + s) <= rows then newRow(rTarg + s)								end end rowNum[rTarg + 2]:tag('td') :attr('rowspan', last.span) :attr('colspan', 2) end r = r + skip end end rPhase = (rPhase + 1) % 3 end end if tonumber(args.columns) > c then--begin draw lines to next round last.skip = skip skip = (rows - rows / math.pow(2, c)) / math.pow(2, args.columns - c)			rowNum[1]:tag('td'):attr('rowspan', last.skip + 4) rowNum[1]:tag('td') :attr('rowspan', skip + 4) :css('border-bottom', '1px solid') skips = last.skip + 4 for r = skips + 1, rows, (last.skip * 2 + 6) do				last.cell = rowNum[r]:tag('td') last.cell:attr('rowspan', (last.skip * 2 + 6)) if ((r-skips-1)/(last.skip * 2 + 6)) % 2 == 0 then last.cell:css('border', '3px solid') :css('border-left', '0') if rowNum[r + last.skip + 3] then last.cell = rowNum[r + last.skip + 3]:tag('td') last.cell:attr('rowspan', last.skip * 4 + 12) :css('border-width', '2px 0 '..(rowNum[r + last.skip*5 + 15] and '1px' or 0)) :css('border-style', 'solid') end end end end end if args['3rdplace'] ~= 'no' and args['3rdplace'] ~= 'false' then--begin draw 3rd place rTarg = rows / 2 + 4 + last.span last.row = rowNum[rTarg] drawHead(args.Consol or defaultRounds[4]) last.cell:attr('rowspan', 2) dpBox(args[step], rTarg + 2) teamBox(args[step+1], rTarg + 4, (args.color == 'yes' and {color = 3}) or nil) teamBox(args[step+2], rTarg + 4, {center = 1, color = (args.color == 'yes' and 3) or nil}) teamBox(args[step+3], rTarg + 6) teamBox(args[step+4], rTarg + 6, {center = 1}) end return output end

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

return p