local showSeed = showSeeds
or isnotblank(seedArg)
or isnotblank(pairSeedArg)
if showSeed and (not hideSeeds) then
seedCell = row:tag('td')
:css('text-align', '')
:css('background-color', '#f2f2f2')
:attr('rowspan', (not compact) and '2' or nil)
:wikitext(seedArg)
:newline
addBorders(seedCell, top or otherbye, true, false)
end  
local teamArg = getTeamArg(round, 'team', team)
if isblank(teamArg) then
teamArg = '&nbsp;'
elseif boldwinner ~= '' then
teamArg = unboldParenthetical(teamArg)
end  
if not showSeed and (not hideSeeds) then
tcs = tcs + 1
end  
local teamCell = row:tag('td')
:css('background-color', '#f9f9f9')
:css('padding', '0 ')
:attr('rowspan', (not compact) and '2' or nil)
:attr('colspan', (tcs > 1) and tcs or nil)
:wikitext(teamArg)
:newline
addBorders(teamCell, top or otherbye, true, false)  
local scoreCells = {}
local wins, otherwins = 0, 0
local sumscore, othersumscore = 0, 0
local teamcolspan = tcs
local hassum = false
for s = 1, legs[round] do
local fw = nil
local agg = legs[round] > 1 and s == legs[round] and true or false
local score1 = (agg and getScoreArg(round, team, 'agg') or nil) or
getScoreArg(round, team, s) or ((legs[round] == 1) and getScoreArg(round, team)) or nil
local score2 = (agg and getScoreArg(round, otherteam, 'agg') or nil) or
getScoreArg(round, otherteam, s) or ((legs[round] == 1) and getScoreArg(round, otherteam)) or nil
local showscore = true
if agg and aggregate ~= '' and score1 == nil and hassum then
score1 = (aggregate == 'score') and sumscore
or ((aggregate == 'legs' or aggregate == 'sets') and wins)
or nil
end
if agg and aggregate ~= '' and score2 == nil and hassum then
score2 = (aggregate == 'score') and othersumscore
or ((aggregate == 'legs' or aggregate == 'sets') and otherwins)
or nil
end
if (score1 == nil or score1 == *) and (score2 == nil or score2 == *) then
if hideomittedscores > 0 and s >= hideomittedscores then
teamcolspan = teamcolspan + 1
showscore = false
end
else
hassum = true
end
if showscore then
local winner = scoreCompare(score1, score2, boldwinner ~= 'low')
sumscore = sumScores(sumscore, score1)
othersumscore = sumScores(othersumscore, score2)
if winner == 1 then
if boldwinner ~= '' or (agg and (aggregate == 'score' or aggregate == 'legs' or aggregate == 'sets')) then
if agg and (aggregate == 'legs' or aggregate == 'sets') and (wins <= (legs[round] - 1)/2) then
else
fw = 'bold'
end
end
if not (agg and (aggregate == 'score' or aggregate == 'legs' or aggregate == 'sets')) then wins = wins + 1 end
elseif winner == 2 then
if not (agg and (aggregate == 'score' or aggregate == 'legs' or aggregate == 'sets')) then otherwins = otherwins + 1 end
end
scoreCells[s] = row:tag('td')
:css('text-align', '')
:css('background-color', getShadeArg(round, team, s))
:css('font-weight', fw)
:attr('rowspan', (not compact) and '2' or nil)
:wikitext(score1)
:newline
addBorders(scoreCells[s], top or otherbye, false, s > 1 and s == legs[round] and aggsep or nil)
end
end
if teamcolspan > 1 then
teamCell:attr('colspan', teamcolspan)
end
if boldwinner ~= '' and wins > otherwins then
if (aggregate == 'legs' or aggregate == 'sets') and (wins <= (legs[round] - 1)/2) then
else
if seedCell then
seedCell:css('font-weight', 'bold')
end
if teamCell then
teamCell:css('font-weight', 'bold')
end
end
end
end  
local function renderRound(count, r)
local teams = math.pow(2, rounds - r + 1)
local step = count / teams
local topTeam = true -- is top row in match-up
local topPair = true -- is top match-up in pair of match-ups
local team = 1
local group = 1  
for i = 1, count, step do
local offset, height, blank  
local hideteam = false
local otherhideteam = false
local hideleftpath = false
local hiderightpath = false
if r <= byes then
hideteam = isHidden(r, team)
otherhideteam = isHidden(r, team % 2 == 0 and team-1 or team+1)
end
if (r == 1) and (RD1seedmap[team] <= 0) then
hideteam = true
end
if (r > 1) and (r <= (byes + 1)) then
hideleftpath = isHidden(r-1, 2*team-1) and isHidden(r-1, 2*team)
end
if (r == 2) and (RD1seedmap[2*team-1] <= 0 and RD1seedmap[2*team] <= 0) then
hideleftpath = true
end
if compactFinal and (r == rounds) then
hideleftpath = true
end
if (tonumber(args['RD' .. (r-1) .. '-RD' .. (r) .. '-path']) or 2) == 0 then
hideleftpath = true
end
if (tonumber(args['RD' .. (r) .. '-RD' .. (r + 1) .. '-path']) or 2) == 0 then
hiderightpath = true
end  
-- empty space above or below
if compact then
offset = topTeam and i or i + 1
height = step - 1
-- leave room for groups for teams other than first and last
elseif team == 1 or team == teams then
offset = topTeam and i or i + 2
height = step - 2
else
offset = topTeam and i + 1 or i + 2
height = step - 3
end
if showThird and (r == rounds) and (not topTeam) then
height = offset - offsetThird
end
if compactFinal and (r == (maxround - 1)) then
if team == 2 then
height = height - 3
end
if team == 3 then
height = height - 1
offset = offset + 1
addBlank(offset-3, nil, 1, tonumber(hideSeeds and '2' or '3') + legs[r])
addBlank(offset-4)
addHeading(rows[offset-4], r + 1, getRoundName(r+1), legs[r] - legs[r+1])
local b = addBlank(offset-4, {
['-color'] = 'black',
['-style']= 'solid',
['-width']= '0'}, 2)
b:css('--width', '')
end
end
if height > 0 then
local pad = 0
local betweenTeams = (topTeam == false and topPair == true) or (topTeam == true and topPair == false)
if compactFinal and (r == maxround - 1) then
betweenTeams = false
end
if compactFinal and (r == maxround - 1) and legs[r+1] > legs[r] then
pad = legs[r+1] - legs[r]
end
if compact and betweenTeams then
addBlank(offset, nil, height, 1)
if topPair then
blank = addBlank(offset, nil, 2*height, tonumber(hideSeeds and '1' or '2') + legs[r] + pad)
if args['RD' .. r .. '-group' .. group] then
blank:wikitext(args['RD' .. r .. '-group' .. group])
blank:css('text-align', '')
end
group = group + 1
end
blank = addBlank(offset,
{['-width'] = '0',
['-style'] = 'solid',
['-color'] = 'black'},
height, 1)
else
blank = addBlank(offset,
{['-width'] = '0',
['-style'] = 'solid',
['-color'] = 'black'},
height, tonumber(hideSeeds and '3' or '4') + legs[r] + pad)
end
end
-- add bracket
local j = topTeam and i + step - (compact and 1 or 2) or i
-- add path
addPath(j, r, topTeam, true, hideleftpath and '0' or '')
if hideteam then
addBlank(j, nil, (not compact) and 2 or nil, tonumber(hideSeeds and '1' or '2') + legs[r])
elseif rows[j] then
if compactFinal and (r == maxround) then
renderTeam(rows[j], r, team, topTeam, otherhideteam, legs[r-1] - legs[r])
elseif compactFinal and (r == maxround - 1) then
renderTeam(rows[j], r, team, topTeam, otherhideteam, legs[r+1] - legs[r])
else
renderTeam(rows[j], r, team, topTeam, otherhideteam)
end
end
local rightPath = addPath(j, r, topTeam, false, (hiderightpath or hideteam) and '0' or '')
if not topTeam then topPair = not topPair end
if not topPair and r < maxround and (not (hiderightpath or hideteam)) then
if blank then blank:css('--width', '') end
rightPath:css('--width', '')
end
if compactFinal and (r == maxround) then
local prop = (team == 1) and '-bottom-width' or '-top-width'
rightPath:css('--width', '')
:css(prop, '')
end
team = team + 1
topTeam = not topTeam
end
end  
local function renderGroups(count, round)
local roundFromLast = rounds - round + 1
local groups = math.pow(2, roundFromLast - 2)
local step = count / groups
local group = 1
local offset = 0
local team = 0
local wdef = (tonumber(args['RD' .. (round) .. '-RD' .. (round + 1) .. '-path']) or 2) .. 'px'
local w = wdef  
for r = 1,round do
offset = offset + (hideSeeds and 3 or 4) + legs[r]
end
for i = step / 2, count, step do
local name = 'RD' .. round .. '-group' .. group
addBlank(i, {['height'] = ''})
addBlank(i+1, {['height'] = ''})
addBlank(i, {['text-align'] = ''}, 2, offset-2)
:wikitext(args[name])
:newline
if (round <= byes) then
team = i/(step/2)
w = isHidden(round, 2*team-1) and isHidden(round, 2*team) and '0' or wdef
end
if (round < maxround) then
addBlank(i, {
['-color'] = 'black',
['-style'] = 'solid',