Module:TeamBracket-Compact-Tennis
Appearance
--
-- This module will implement {{16TeamBracket-Compact-Tennis5}}
--
local p = {}
local args
local rounds
local sets
local padding
local function getArgs(frame)
local parent = frame:getParent();
local args = parent.args;
for k,v in pairs(frame.args) do
args[k] = v
end
return parent.args;
end
function addTableRow(tbl)
return tbl:tag('tr')
end
function addBlank(row, width)
local cell = row:tag('td')
:css('border-width', '0')
:css('border-style', 'solid')
:css('border-color', '#aaa')
if width then
cell:css('width', width)
end
return cell
end
function addPath(rows, index, round, top, left)
local prop = top and 'border-bottom-width' or 'border-top-width'
local cell = addBlank(rows[index]):css('border-color', 'black')
if left and round == 1 then
return nil
else
if left or round < rounds and not left then
cell:css(prop, '2px')
end
return cell
end
end
function getWidth(param, default)
local arg = args[param .. '-width']
if not arg or string.len(arg) == 0 then
arg = default
end
if tonumber(arg) ~= nil then
arg = arg .. 'px'
end
return arg
end
function getTeamArg(round, type, team)
local argname = string.format('RD%d-%s' .. padding, round, type, team)
local value = args[argname]
if not value or string.len(value) == 0 then
return ''
end
return string.gsub(value, " *<[Bb][Rr] */?> * *", "<br/>")
end
function getScore(round, team, set)
local argname = string.format('RD%d-score' .. padding ..'-%d', round, team, set)
return args[argname]
end
function getRoundName(round)
local name = args['RD' .. round]
if name and string.len(name) > 0 then
return name
end
local roundFromLast = rounds - round + 1
if roundFromLast == 1 then
return "Finals"
elseif roundFromLast == 2 then
return "Semifinals"
elseif roundFromLast == 3 then
return "Quarterfinals"
else
return "Round of " .. math.pow(2, roundFromLast)
end
end
function renderTeam(row, round, team, double)
local seedArg = getTeamArg(round, 'seed', team)
local seedCell = addBlank(row)
:css('text-align', 'center')
:css('background-color', '#f2f2f2')
:css('border-width', '1px')
:css('border-right-width', '2px')
:css('border-bottom-width', double and '2px' or '1px')
:wikitext(seedArg)
:newline()
local teamArg = getTeamArg(round, 'team', team)
if not teamArg or string.len(teamArg) == 0 then
teamArg = ' '
end
local teamCell = addBlank(row)
:css('background-color', '#f9f9f9')
:css('border-width', '1px')
:css('border-bottom-width', double and '2px' or '1px')
:css('padding', '0 2px')
:wikitext(teamArg)
:newline()
for s = 1, sets do
addBlank(row)
:css('text-align', 'center')
:css('border-width', '1px')
:css('background-color', '#f9f9f9')
:css('border-bottom-width', double and '2px' or '1px')
:wikitext(getScore(round, team, s))
:newline()
end
end
function renderRound(rows, count, r)
local teams = math.pow(2, rounds - r + 1)
local step = count / teams
local top = true
local open = false
local team = 1
for i = 1, count, step do
if not top then open = not open end
local offset = top and i or i + 1
local height = step - 1
-- add bracket
local j = top and i + height or i
local left = addPath(rows, j, r, top, true)
renderTeam(rows[j], r, team, r == 1 and team < teams and not top)
local right = addPath(rows, j, r, top, false)
if open and r < rounds then
right:css('border-right-width', '2px')
end
-- add blank
for j = 0, height - 1 do
blank = addBlank(rows[offset + j])
:css('border-color', 'black')
:attr('colspan', 4 + sets)
if open and r < rounds then
blank:css('border-right-width', '2px')
end
end
-- increment
team = team + 1
top = not top
end
end
function renderTree(tbl)
local count = math.pow(2, rounds)
local rows = {}
for i = 1, count do
rows[i] = addTableRow(tbl)
end
for r = 1, rounds do
renderRound(rows, count, r)
end
end
function renderHeading(tbl)
local titleRow = addTableRow(tbl)
local widthRow = addTableRow(tbl)
for r = 1, rounds do
addBlank(titleRow)
addBlank(widthRow, r > 1 and '5px' or nil)
:css('height', '7px')
addBlank(titleRow)
:attr('colspan', 2 + sets)
:css('text-align', 'center')
:css('border-width', '1px')
:css('background-color', '#f2f2f2')
:wikitext(getRoundName(r))
addBlank(widthRow, getWidth('seed', '25px'))
:css('height', '7px')
addBlank(widthRow, getWidth('team', '150px'))
:css('height', '7px')
for s = 1, sets do
addBlank(widthRow, getWidth('score', '12px'))
:css('height', '7px')
end
addBlank(titleRow)
addBlank(widthRow, r < rounds and '5px' or nil)
:css('height', '7px')
end
end
function p.teamBracket(frame)
args = getArgs(frame)
rounds = tonumber(args.rounds) or 4
sets = tonumber(args.sets) or 5
local teams = math.pow(2, rounds)
padding = '%0' .. (teams < 10 and 1 or 2) .. 'd'
local tbl = mw.html.create('table')
:css('border-style', 'none')
:css('font-size', '90%')
:css('margin', '1em 2em 1em 1em')
:css('border-collapse', 'collapse')
:css('border-spacing', '0')
:attr('cellpadding', '0')
renderHeading(tbl)
renderTree(tbl)
return tostring(tbl)
end
return p