Jump to content

Module:TeamBracket-Compact-Tennis

Frá Wikipedia, hin frælsa alfrøðin

This template implements a variant of Module:TeamBracket for tennis tournaments. It is a generic visual representation of the tournament bracket. For common usage, use the templates in Category:Tennis tournament bracket templates.

Usage

{{#invoke: TeamBracket-Compact-Tennis | teamBracket
| rounds       =
| sets         =

| seed-width   =
| team-width   =
| score-width  =

| RD1          =
| RD1-seed1    =
| RD1-team1    =
| RD1-score1-1 =
| RD1-score1-2 =
| RD1-score1-3 =
}}

Parameter list

The parameters are as follows:

seed-width – the width of the cells for seeds.
team-width – the width of the cells for team names.
score-width – the width of the cells for scores.
RDnThe name of round n.
RDn-seedmThe seed of team m in round n.
RDn-teammThe name of team m in round n.
RDn-scorem-sThe score of set s for team m in round n.

Examples

{{#invoke: TeamBracket-Compact-Tennis | teamBracket
| rounds    = 2
| sets      = 3

| RD1-seed1 = '''1'''
| RD1-seed2 = 4
| RD1-team1 = '''John'''
| RD1-team2 = Declan
| RD1-score1-1 = '''6'''
| RD1-score2-1 = 0
| RD1-score1-2 = 3
| RD1-score2-2 = '''6'''
| RD1-score1-3 = '''7'''
| RD1-score2-3 = 6
| RD1-seed3 = 2
| RD1-seed4 = 3
| RD1-team3 = Barry
| RD1-team4 = Steve

| RD2-seed1 = 1
| RD2-team1 = John
}}
SemifinalsFinals
1 John 6 3 7
4 Declan 0 6 6 1 John
2 Barry  
3 Steve



 --
-- 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] */?> *&nbsp; *", "<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 = '&nbsp;'
	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