fix bracket

redesign-tournament-list
Razmig Sarkissian 8 months ago
parent 5c5ffe1ce7
commit 3078e56a17
  1. 8
      tournaments/models/match.py
  2. 55
      tournaments/templates/tournaments/tournament_bracket.html
  3. 4
      tournaments/views.py

@ -390,7 +390,7 @@ class Match(models.Model):
ended = self.end_date is not None ended = self.end_date is not None
live_format = "Format " + FederalMatchCategory(self.format).format_label_short live_format = "Format " + FederalMatchCategory(self.format).format_label_short
livematch = LiveMatch(title, date, time_indication, court, self.started(), ended, group_stage_name, live_format, self.start_date, self.court_index, self.disabled) livematch = LiveMatch(title, date, time_indication, court, self.started(), ended, group_stage_name, live_format, self.start_date, self.court_index, self.disabled, self.index)
for team in self.live_teams(): for team in self.live_teams():
livematch.add_team(team) livematch.add_team(team)
@ -447,7 +447,7 @@ class Team:
} }
class LiveMatch: class LiveMatch:
def __init__(self, title, date, time_indication, court, started, ended, group_stage_name, format, start_date, court_index, disabled): def __init__(self, title, date, time_indication, court, started, ended, group_stage_name, format, start_date, court_index, disabled, index):
self.title = title self.title = title
self.date = date self.date = date
self.teams = [] self.teams = []
@ -461,6 +461,7 @@ class LiveMatch:
self.disabled = disabled self.disabled = disabled
self.start_date = start_date self.start_date = start_date
self.court_index = court_index self.court_index = court_index
self.index = index
def add_team(self, team): def add_team(self, team):
self.teams.append(team) self.teams.append(team)
@ -481,7 +482,8 @@ class LiveMatch:
"format": self.format, "format": self.format,
"disabled": self.disabled, "disabled": self.disabled,
"start_date": self.start_date, "start_date": self.start_date,
"court_index": self.court_index "court_index": self.court_index,
"index": self.index
} }
def show_time_indication(self): def show_time_indication(self):

@ -58,7 +58,7 @@ function renderBracket() {
rounds.forEach((roundMatches, roundIndex) => { rounds.forEach((roundMatches, roundIndex) => {
const roundDiv = document.createElement('div'); const roundDiv = document.createElement('div');
roundDiv.className = 'butterfly-round'; roundDiv.className = 'butterfly-round';
roundDiv.style.setProperty('--match-width', `${360}px`); roundDiv.style.setProperty('--match-width', `${365}px`);
matchPositions[roundIndex] = []; matchPositions[roundIndex] = [];
roundMatches.forEach((matchTemplate, matchIndex) => { roundMatches.forEach((matchTemplate, matchIndex) => {
@ -78,46 +78,29 @@ function renderBracket() {
nextMatchDistance = 0; nextMatchDistance = 0;
} }
if (roundIndex === 0 || roundIndex === rounds.length - 1) { if (roundIndex > finalRoundIndex) {
top = matchIndex * (matchHeight + matchSpacing);
if (roundIndex === rounds.length - 1) {
nextMatchDistance = baseDistance * Math.pow(2, rounds.length - roundIndex - 1);
matchDiv.classList.add('reverse-bracket'); matchDiv.classList.add('reverse-bracket');
} top = matchPositions[roundCount - roundIndex - 1][matchIndex];
} else { nextMatchDistance = baseDistance * Math.pow(2, rounds.length - roundIndex - 1);
const previousRoundMatches = rounds[roundIndex - 1].length;
const currentRoundMatches = roundMatches.length;
if (currentRoundMatches === previousRoundMatches || roundIndex == finalRoundIndex + 1) {
// Same number of matches as previous round
top = matchPositions[roundIndex - 2][matchIndex];
nextMatchDistance = 0;
} else if (roundIndex == finalRoundIndex) {
if (finalRoundIndex === 2) {
top = matchPositions[0][matchIndex];
} else { } else {
top = matchPositions[0][1 + matchIndex]; nextMatchDistance = baseDistance * Math.pow(2, roundIndex);
} }
if (roundIndex === 0) {
top = matchIndex * (matchHeight + matchSpacing);
} else if (roundIndex == finalRoundIndex) {
let lgth = matchPositions[0].length / 2;
let index = lgth + matchIndex - 1;
// If index goes negative, use 0 instead
top = matchPositions[0][index < 0 ? 0 : index];
nextMatchDistance = baseDistance; nextMatchDistance = baseDistance;
} else if (currentRoundMatches > previousRoundMatches) { } else if (roundIndex < finalRoundIndex) {
// Bracket is reversed - matches are splitting instead of combining
const parentIndex = Math.floor(matchIndex / 2);
if (parentIndex < matchPositions[roundIndex - 1].length) {
const parentPos = matchPositions[roundIndex - 1][parentIndex];
// Calculate offset based on whether it's an even or odd match
const offset = (matchIndex % 2 === 0) ? -baseDistance : baseDistance;
top = parentPos + offset;
}
nextMatchDistance = baseDistance * Math.pow(2, rounds.length - roundIndex - 1);
matchDiv.classList.add('reverse-bracket');
} else {
// Normal case where matches are combining
const parentIndex1 = matchIndex * 2; const parentIndex1 = matchIndex * 2;
const parentIndex2 = parentIndex1 + 1; const parentIndex2 = parentIndex1 + 1;
const parentPos1 = matchPositions[roundIndex - 1][parentIndex1]; const parentPos1 = matchPositions[roundIndex - 1][parentIndex1];
const parentPos2 = matchPositions[roundIndex - 1][parentIndex2]; const parentPos2 = matchPositions[roundIndex - 1][parentIndex2];
top = (parentPos1 + parentPos2) / 2; top = (parentPos1 + parentPos2) / 2;
nextMatchDistance = !isSingleMatchRound ? baseDistance * Math.pow(2, roundIndex) : 0; nextMatchDistance = baseDistance * Math.pow(2, roundIndex);
}
} }
if (roundIndex >= finalRoundIndex - 1) { if (roundIndex >= finalRoundIndex - 1) {
@ -137,9 +120,13 @@ function renderBracket() {
} }
if (roundIndex == 1 || roundIndex == roundCount - 2) { if (roundIndex === finalRoundIndex - 2 || roundIndex === finalRoundIndex + 2) {
nextMatchDistance = nextMatchDistance / 2; nextMatchDistance = (nextMatchDistance - baseDistance);
}
else if (roundIndex == finalRoundIndex - 1 || roundIndex == finalRoundIndex + 1) {
nextMatchDistance = baseDistance;
} }
matchDiv.style.setProperty('--next-match-distance', `${nextMatchDistance}px`); matchDiv.style.setProperty('--next-match-distance', `${nextMatchDistance}px`);
matchDiv.style.top = `${top}px`; matchDiv.style.top = `${top}px`;
matchPositions[roundIndex][matchIndex] = top; matchPositions[roundIndex][matchIndex] = top;

@ -985,7 +985,7 @@ def tournament_bracket(request, tournament_id):
# Add first half of each round (from last to semi-finals) # Add first half of each round (from last to semi-finals)
for round in main_rounds: for round in main_rounds:
matches = round.match_set.all() matches = round.match_set.all().order_by('index')
if matches: if matches:
if len(matches) > 1: if len(matches) > 1:
@ -1008,7 +1008,7 @@ def tournament_bracket(request, tournament_id):
serializable_match_groups.append(match_group) serializable_match_groups.append(match_group)
for round in main_rounds_reversed: for round in main_rounds_reversed:
matches = round.match_set.all() matches = round.match_set.all().order_by('index')
if matches: if matches:
if len(matches) > 1: if len(matches) > 1:
midpoint = int(len(matches) / 2) midpoint = int(len(matches) / 2)

Loading…
Cancel
Save