มอดูล:TeamBracket-Compact-Tennis

--
-- This module will implement 
--

local p = {}
local HtmlBuilder = require('Module:HtmlBuilder')
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 
		argsk = 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(rowsindex).css('border-color', 'black')
	if left and round == 1 then
		return nil
	else
		if left or round  0 then
		return name
	end
	local roundFromLast = rounds - round + 1
	if roundFromLast == 1 then
		return "รอบชิงชนะเลิศ"
	elseif roundFromLast == 2 then
		return "รอบรองชนะเลิศ"
	elseif roundFromLast == 3 then
		return "รอบก่อนรองชนะเลิศ"
	else
		return "รอบผู้เข้าแข่งขัน " .. 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(rowsj, r, team, r == 1 and team  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 = HtmlBuilder.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