Module:Bracket

local util_table = require('Module:TableUtil') local util_args = require('Module:ArgsUtil') local bracket_wiki = require('Module:Bracket/Wiki') -- wiki localization per game

local h = {}

function h.processArgs(tpl_args) -- format tpl_args local args = {} for k, v in pairs(tpl_args) do		if k:find('R%d+M%d+_.*_') then local match, team, val = k:match('(R%d+M%d+)_(.*)_(.*)') if not args[match] then args[match] = { team1 = {}, team2 = {} } end args[match][team][val] = v		elseif k:find('R%d+M%d+_.*') then local match, val = k:match('(R%d+M%d+)_(.*)') if not args[match] then args[match] = { team1 = {}, team2 = {} } end args[match][val] = v		else args[k] = v		end end return args end

function h.printBracket(args, settings) local tbl = mw.html.create('div') :addClass('bracket-grid') :css({			['grid-template-columns'] = h.gtcSetting(settings, args.roundwidth)		}) for round, col in ipairs(settings) do		h.addLinesColumn(tbl, col.lines, round, not args.notitle) h.addMatchesColumn(tbl, args, col.matches, round, not args.notitle) end return tbl end

function h.gtcSetting(settings, roundwidth) local len = #settings local firstCol = settings[1].lines and next(settings[1].lines) return ('%s repeat(%s, %s 3em)'):format(		firstCol and '3em' or '0',		(len * 2 - 1),		roundwidth or '16em'	) end

function h.addLinesColumn(tbl, lineData, r, addtitle) local roundname = 'round' .. (r - 1) if not lineData then return end for m, row in ipairs(lineData) do		if m == 1 and addtitle then h.addBracketLine(tbl, roundname, row, 2) else h.addBracketLine(tbl, roundname, row, 0) end end return end

function h.addBracketLine(tbl, roundname, linerow, extra) tbl:tag('div') :addClass('bracket-line') :addClass(roundname) :cssText(('grid-row:span %s;'):format(linerow.above + extra)) tbl:tag('div') :addClass('bracket-line') :addClass(linerow.class) :addClass(roundname) :cssText(('grid-row:span %s;'):format(linerow.height)) return end

function h.addMatchesColumn(tbl, args, data, r, addtitle) local roundname = 'round' .. r	if addtitle then h.makeTitle(tbl, roundname, args['R' .. r .. '_title'] or data.title or '') end for m, row in ipairs(data) do		local game = args[('R%sM%s'):format(r, m)] or { team1 = {}, team2 = {} } if row.above then h.addSpacer(tbl, roundname, row.above) end if row.display == 'match' then h.makeMatch(tbl, game, roundname, row.label) elseif row.display == 'hline' then h.makeHorizontalCell(tbl, roundname) end end return end

function h.makeTitle(tbl, roundname, text) tbl:tag('div') :addClass('bracket-grid-header') :addClass(roundname) :tag('div') :addClass('bracket-header-content') :wikitext(text) return end

function h.makeHorizontalCell(tbl, roundname) h.addSpacer(tbl, roundname, 2) tbl:tag('div') :addClass('bracket-spacer') :addClass('horizontal') :addClass(roundname) :cssText('grid-row:span 2;') return end

function h.makeMatch(tbl, game, roundname, label) if game.label then label = game.label end h.addSpacer(tbl, roundname, nil, label) h.makeTeam(tbl, roundname, game.team1, game.winner == '1') h.makeTeam(tbl, roundname, game.team2, game.winner == '2') h.addSpacer(tbl, roundname) return end

function h.addSpacer(tbl, roundname, n, label) local div = tbl:tag('div') :addClass('bracket-spacer') :addClass(roundname) :wikitext(label) if n then div:cssText(('grid-row:span %s;'):format(n)) end return end

function h.makeTeam(tbl, roundname, data, isWinner) local line = tbl:tag('div') :addClass('bracket-team') :addClass(roundname) if isWinner then line:addClass('bracket-winner') end local team = line:tag('div') :addClass('bracket-team-name') if data.free then team:wikitext(data.free) else bracket_wiki.teamDisplay(team, data) end line:tag('div') :addClass('bracket-team-points') :wikitext(data.score or '') return end

local p = {}

function p.main(frame) local tpl_args = util_args.merge(true) -- use require instead of loadData so that we can use next and # local settings = require('Module:Bracket/'.. tpl_args.bracket) local args = h.processArgs(tpl_args) return h.printBracket(args, settings) end

return p