Module:Bracket
From eSportsAmaze
More actions
Documentation for this module may be created at Module:Bracket/doc
local p = {}
local html = mw.html
function p.main(frame)
local args = frame:getParent().args
local teamCount = tonumber(args.teams) or 8 -- Default to 8 teams
local rounds = math.floor(math.log(teamCount) / math.log(2))
if 2 ^ rounds ~= teamCount then
return "<div style='color:red; padding:10px; border:1px solid red; border-radius:6px;'>"
.. "<b>Bracket Error:</b> <code>teams</code> must be a power of 2 (8, 16, 32, 64). "
.. "You entered <b>" .. teamCount .. "</b>.</div>"
end
local container = html.create('div'):addClass('bracket-scroll-wrapper')
local wrapper = container:tag('div'):addClass('bracket-wrapper')
-- Iterate Rounds (1 to Total Rounds)
for r = 1, rounds do
local col = wrapper:tag('div'):addClass('bracket-col bracket-col-' .. r)
local matchesInRound = teamCount / (2^r)
-- Header (e.g., Quarterfinals)
local headerTxt = "Round " .. r
if r == rounds then headerTxt = "Grand Finals"
elseif r == rounds - 1 then headerTxt = "Semifinals"
elseif r == rounds - 2 then headerTxt = "Quarterfinals"
end
col:tag('div'):addClass('bracket-header'):wikitext(headerTxt)
-- Iterate Matches in this Round
for m = 1, matchesInRound do
local matchID = "R" .. r .. "M" .. m
-- Calculate Spacing (The Magic Math)
-- This aligns the matches vertically relative to previous round
local spacer = col:tag('div'):addClass('bracket-spacer')
-- DATA INPUTS
local t1 = args[matchID .. 'team1'] or 'TBD'
local s1 = args[matchID .. 'score1'] or ''
local t2 = args[matchID .. 'team2'] or 'TBD'
local s2 = args[matchID .. 'score2'] or ''
local win = args[matchID .. 'win'] -- '1' or '2' to highlight winner
-- MATCH CARD
local matchBox = col:tag('div'):addClass('bracket-match')
-- Team 1 Row
local row1 = matchBox:tag('div'):addClass('bracket-team')
if win == '1' then row1:addClass('bracket-win') end
row1:tag('div'):addClass('bracket-name'):wikitext(t1)
row1:tag('div'):addClass('bracket-score'):wikitext(s1)
-- Team 2 Row
local row2 = matchBox:tag('div'):addClass('bracket-team')
if win == '2' then row2:addClass('bracket-win') end
row2:tag('div'):addClass('bracket-name'):wikitext(t2)
row2:tag('div'):addClass('bracket-score'):wikitext(s2)
-- Add Connector Lines (except for last round)
if r < rounds then
matchBox:addClass('has-connector')
end
end
end
return tostring(container)
end
return p