Module:Sandbox/AB-me/Team bracket

local p = {} local getArgs = require('Module:Arguments').getArgs

local function drawMatchTable(roundNo, matchNo, args) function getMatchArg(text, team, num, nbsp) num = num or 0 empty = ' ' if nbsp == false then empty = '' end if num <= 1 then return args['RD' .. roundNo .. '-' .. text .. matchNo * 2 - 2 + team] or args['RD' .. roundNo .. '-' .. text .. '0' .. matchNo * 2 - 2 + team] or args['RD' .. roundNo .. '-' .. text .. '00' .. matchNo * 2 - 2 + team] or args['RD' .. roundNo .. '-' .. text .. matchNo * 2 - 2 + team .. '-' .. num] or args['RD' .. roundNo .. '-' .. text .. '0' .. matchNo * 2 - 2 + team .. '-' .. num] or args['RD' .. roundNo .. '-' .. text .. '00' .. matchNo * 2 - 2 + team .. '-' .. num] or empty else return args['RD' .. roundNo .. '-' .. text .. matchNo * 2 - 2 + team .. '-' .. num] or args['RD' .. roundNo .. '-' .. text .. '0' .. matchNo * 2 - 2 + team .. '-' .. num] or args['RD' .. roundNo .. '-' .. text .. '00' .. matchNo * 2 - 2 + team .. '-' .. num] or empty end end local output = {} local rowStart = 2 ^ (roundNo - 1) * (matchNo - 1) + 1 local rowEnd = rowStart + 2 ^ (roundNo - 1) - 1 local scoreCells = args['RD' .. roundNo .. '-scoreCells'] or args['RD' .. roundNo .. '-sets'] or args['scoreCells'] or args['sets'] or 1 local scoreWidth = args['score-width'] or '1.5em' table.insert(output, '' .. '\n') table.insert(output, ' ' .. '\n') table.insert(output, getMatchArg('above', 1, 0, false)) table.insert(output, ' ' .. '\n') table.insert(output, '{|class="wikitable" style="margin:0; grid-row:2"' .. '\n') table.insert(output, '|-' .. '\n') table.insert(output, '|style="text-align:left;"|' .. getMatchArg('team', 1)) if (getMatchArg('seed', 1) ~= ' ') then table.insert(output, ' (' .. getMatchArg('seed', 1) .. ') ')	end table.insert(output, '\n') for i=1, scoreCells do table.insert(output, '|style="width:' .. scoreWidth .. ';padding:0;"|' .. getMatchArg('score', 1, i) .. '\n') end table.insert(output, '|-' .. '\n') table.insert(output, '|style="text-align:left;"|' .. getMatchArg('team', 2)) if (getMatchArg('seed', 2) ~= ' ') then table.insert(output, ' (' .. getMatchArg('seed', 2) .. ') ')	end table.insert(output, '\n') for i=1, scoreCells do table.insert(output, '|style="width:' .. scoreWidth .. ';padding:0;"|' .. getMatchArg('score', 2, i) .. '\n') end table.insert(output, '|}' .. '\n') table.insert(output, ' ' .. '\n') table.insert(output, getMatchArg('below', 2, 0, false)) table.insert(output, ' ' .. '\n') table.insert(output, ' ' .. '\n') return table.concat(output) end

local function drawLines(roundNo, matchNo, args) local output = {} if roundNo >= 2 then local leftRowStart = 2 ^ (roundNo - 2) + (matchNo - 1) * 2 * 2 ^ (roundNo - 1) local leftRowEnd = leftRowStart + 2 ^ (roundNo - 1) local rightRowStart = 2 ^ (roundNo - 1) * (matchNo - 1) + 1 local rightRowEnd = rightRowStart + 2 ^ (roundNo - 1) - 1 table.insert(output, ' ' .. '\n') table.insert(output, ' ' .. '\n') end return table.concat(output) end

local function getRoundName(round, totalRounds, args) if args['RD' .. round] then return args['RD' .. round] end local finalRounds = totalRounds - round if finalRounds == 0 then return "Final" elseif finalRounds == 1 then return "Semi-finals" elseif finalRounds == 2 then return "Quarter-finals" else --return 'Round of ' ..(2 ^ (totalRounds - round)) * 2 return 'Round of ' ..(2 ^ (totalRounds - round + 1)) end end

function p.main(frame) local output = {} local args = getArgs(frame) local rounds = args.rounds local teams = 2 ^ rounds local matchWidth = args['match-width'] or '18em'; table.insert(output, '' .. '\n') --table.insert(output, '' .. '\n') for i=1, rounds do table.insert(output, '' .. getRoundName(i, rounds, args) .. ' ' .. '\n') for j=1, teams / (2^i) do			table.insert(output, drawMatchTable(i, j, args))

table.insert(output, drawLines(i, j, args)) end end table.insert(output, ' ' .. '\n') return table.concat(output) end

return p