Mô đun:TeamBracket-Robocon

--
-- Mô đun này thực hiện nhiều bản mẫu nhánh đấu giải robocon
--
local p = {}
local args = {}
local rounds
local sets = {}
local compact
local byes
local hideSeeds
local showSeeds
local hideHeadings
local showThird
local offsetThird
local compactFinal
local RD1seedmap = {}
local tcats = 
local function isnotblank(s)
	return s and s ~= 
end
local function isblank(s)
	return (not s) or (s == )
end
local function parseArgs(frame)
	local fargs = frame.args
	local pargs = frame:getParent().args;
	local r = tonumber(fargs.rounds or ) or tonumber(pargs.rounds or ) or 2
	local teams = math.pow(2, r)
	local rdstr = 'RD' .. tostring(r)
	local rdbstr = 'RD' .. tostring(r) .. 'b'
	local rdp1str = 'RD' .. tostring(r+1)
	for i=1,2 do
		local targs = (i == 1) and pargs or fargs
		for k,v in pairs(targs) do
			if type(k) == 'string' then
				if k:find('^R3Drd1-9b?%-a-za-z*00*') then
					k = mw.ustring.gsub(k, '^(R3Drd1-9b?%-a-za-z*)00*', '%1')
					if (teams  0 then
			RD1seedmapr = c
			c = c + 1
		end
	end
end
local function parseSets(s)
	s = mw.text.split((s or '5') .. '/', '%s*/%s*')
	local n = showThird and (rounds + 1) or (rounds)
	for r=1,n do
		if sr ~= nil and sr ~=  and tonumber(sr) then
			setsr = tonumber(sr)
		elseif setsr-1 then
			setsr = setsr-1
		else
			setsr = 5
		end
	end
end
local function addTableRow(tbl)
	return tbl:tag('tr')
end
local function addBlank(row, width)
	local cell = row:tag('td')
	if width then
		cell:css('width', width)
	end
	return cell
end
local function addHeading(row, r, text)
	row:tag('td')
		:attr('colspan', tonumber(hideSeeds and '1' or '2') + setsr)
		:css('text-align', 'center')
		:css('border', '1px solid #aaa')
		:css('background-color', args'RD-shade' or '#f2f2f2')
		:wikitext(text)
		:newline()
end
local function getWidth(param, default)
	local arg = argsparam .. '-width'
	if isblank(arg) then
		arg = default
	end
	if tonumber(arg) ~= nil then
		arg = arg .. 'px'
	end
	return arg
end
local function getTeamArgName(round, type, team)
	if round > rounds then
		return string.format('3rd-%s%d', type, team)
	else
		if (round == 1) then
			team = RD1seedmapteam
			if team == 0 then
				return 'NIL'
			end
		end
		return string.format('RD%d-%s%d', round, type, team)
	end
end
local function getShadeArg(round, team, s)
	local argname = getTeamArgName(round, 'shade', team) .. (s and ('-' .. s) or )
	local value = argsargname
	if isblank(value) then
		return '#f9f9f9'
	end
	return value
end
local function getScoreArg(round, team, s)
	local argname = getTeamArgName(round, 'score', team) .. (s and ('-' .. s) or )
	local value = argsargname
	if isblank(value) then
		return 
	end
	return value
end
local function getTeamArg(round, type, team)
	local argname = getTeamArgName(round, type, team)
	local value = argsargname
	if isblank(value) then
		return 
	end
	if mw.ustring.find(value, '%s*%s *&NnBbSsPp;%s*') then
		tcats = 'Thể loại:Trang sử dụng một nhánh đấu quần vợt có nbsp'
	end
	return mw.ustring.gsub(value, '%s*%s *&NnBbSsPp;%s*', )
end
local function isHidden(r, team)
	return isblank( getTeamArg(r, 'team', team) )
end
local function getRoundName(round)
	local name = args'RD' .. round
	if isnotblank(name) then
		return name
	end
	local roundFromLast = rounds - round + 1
	if roundFromLast == 1 then
		return "Chung kết"
	elseif roundFromLast == 2 then
		return "Bán kết"
	elseif roundFromLast == 3 then
		return "Tứ kết"
	else
		return "Vòng " .. math.pow(2, roundFromLast)
	end
end
local function addPath(rows, index, round, top, left, w)
	local prop = top and 'border-bottom-width' or 'border-top-width'
	if left and round == 1 then
		if compact then
			addBlank(rowsindex)
		else
			addBlank(rowsindex):css('height', '7px')
			addBlank(rowsindex + 1):css('height', '7px')
		end
		return nil
	else
		local cell = addBlank(rowsindex)
			:attr('rowspan', (not compact) and '2' or nil)
			:css('border-width', '0')
			:css('border-style', 'solid')
			:css('border-color', 'black')
		if left or round  1) and (r  0 then
			blank = addBlank(rowsoffset)
				:attr('colspan', tonumber(hideSeeds and '3' or '4') + setsr)
				:attr('rowspan', height)
				:css('border-color', 'black')
				:css('border-style', 'solid')
				:css('border-width', '0')
		end
		-- add bracket
		local j = topTeam and i + step - (compact and 1 or 2) or i
		-- add left path
		addPath(rows, j, r, topTeam, true, hideleftpath and '0' or '2px')
		if hideteam then
			rowsj:tag('td')
				:attr('colspan', tonumber(hideSeeds and '1' or '2') + setsr)
				:attr('rowspan', (not compact) and 2 or nil)
		else
			renderTeam(rowsj, r, team, topTeam, otherhideteam)
		end
		local rightPath = addPath(rows, j, r, topTeam, false, hideteam and '0' or '2px')
		if not topTeam then topPair = not topPair end
		if not topPair and r  1) then
				local blank = addBlank(rowsi)
				blank:attr('colspan', tonumber(hideSeeds and '3' or '4') + sets1)
				if compact and (rounds > 2) then
					blank = addBlank(rowsi)
					blank:attr('colspan', tonumber(hideSeeds and '3' or '4') + sets1)
				end
			end
		end
	end
	if not compact then
		-- fill rows with groups
		for r = 1, rounds - 1 do
			renderGroups(rows, count, r)
		end
	end
	-- fill rows with bracket
	for r = 1, rounds do
		renderRound(rows, count, r)
	end
	if showThird then
		renderThird(rows, count, compact)
	end
end
local function renderHeadings(tbl)
	local titleRow = addTableRow((not hideHeadings) and tbl or mw.html.create('table'))
	local widthRow = addTableRow(tbl)
	for r = 1, (compactFinal and (rounds-1) or rounds) do
		addBlank(titleRow)
		addBlank(widthRow, r > 1 and '5px' or '1px')
		addHeading(titleRow, r, getRoundName(r) )
		local seedCell
		if (not hideSeeds) then
			seedCell = addBlank(widthRow, getWidth('seed', '25px'))
		end
		local teamCell = addBlank(widthRow, getWidth('team', '150px'))
		local scoreCells = {}
		for s = 1, setsr do
			scoreCellss = addBlank(widthRow, getWidth('score', '12px'))
		end
		addBlank(titleRow)
		addBlank(widthRow, r  4) and compact and args'compact-final' and (args'compact-final' == 'yes' or args'compact-final' == 'y'))
	hideSeeds = (args'seeds' and (args'seeds' == 'no' or args'seeds' == 'n'))
	showSeeds = (args'seeds' and (args'seeds' == 'yes' or args'seeds' == 'y'))
	byes = (args'byes' and (args'byes' == 'yes' or args'byes' == 'y') and 1) or (tonumber(args'byes' or '0') or 0)
	hideHeadings = (args'headings' and (args'headings' == 'no' or args'headings' == 'n'))
	showThird = isnotblank(args'3rd') or isnotblank(args'3rd-team1') or isnotblank(args'3rd-team2')
	parseSeedmap(args'RD1-omit')
	parseSets(args.sets)
	-- create the table
	local tbl = mw.html.create('table')
		:css('border-style', 'none')
		:css('font-size', '90%')
		:css('margin', '1em 2em 1em 1em')
		:css('border-collapse', 'separate')
		:css('border-spacing', '0')
		:attr('cellpadding', '0')
	if (args'nowrap' and (args'nowrap' == 'yes' or args'nowrap' == 'y')) then
		tbl:css('white-space', 'nowrap')
	end
	renderHeadings(tbl)
	renderTree(tbl)
	return tostring(tbl) .. tcats
end
return p