fix loser bracket empty team place holder management

bracket-feature^2
Razmig Sarkissian 8 months ago
parent ab3de71a75
commit 1627265a36
  1. 108
      tournaments/models/match.py
  2. 4
      tournaments/models/round.py

@ -85,12 +85,26 @@ class Match(models.Model):
previous_index = self.round.index + 1 previous_index = self.round.index + 1
# Check if the next index is within the bounds of the rounds array # Check if the next index is within the bounds of the rounds array
if previous_index < self.round.tournament.round_set.count(): return self.round.tournament.round_set.filter(index=previous_index, parent = self.round.parent).first()
return self.round.tournament.round_set.filter(index=previous_index, parent = None).first()
# Return None or an appropriate value if the index is out of bounds # Return None or an appropriate value if the index is out of bounds
return None return None
def get_loser_previous_round(self):
# Calculate the next index
if self.round is None:
return None
previous_index = self.round.index + 1
previous_round = None
# Check if the next index is within the bounds of the rounds array
previous_round = self.round.tournament.round_set.filter(index=previous_index, parent = self.round.parent).first()
if previous_round is None and self.round.parent is not None:
previous_round = self.round.tournament.round_set.filter(id=self.round.parent.id).first()
return previous_round
return None
def precedent_match(self, top): def precedent_match(self, top):
previous_round = self.get_previous_round() previous_round = self.get_previous_round()
#print(previous_round) #print(previous_round)
@ -103,6 +117,17 @@ class Match(models.Model):
match = matches.filter(index=match_index).first() match = matches.filter(index=match_index).first()
return match return match
def loser_precedent_match(self, top):
previous_round = self.get_loser_previous_round()
match = None
if previous_round:
matches = previous_round.match_set.all() # Retrieve the QuerySet
match_index = self.index * 2 + 1
if top == False:
match_index += 1
match = matches.filter(index=match_index).first()
return match
def computed_name(self): def computed_name(self):
if self.round and self.round.parent is None: if self.round and self.round.parent is None:
return self.backup_name() return self.backup_name()
@ -150,44 +175,83 @@ class Match(models.Model):
team_scores = list(self.team_scores.all()) team_scores = list(self.team_scores.all())
previous_top_match = self.precedent_match(True) previous_top_match = self.precedent_match(True)
previous_bottom_match = self.precedent_match(False) previous_bottom_match = self.precedent_match(False)
loser_top_match = self.loser_precedent_match(True)
loser_bottom_match = self.loser_precedent_match(False)
if len(team_scores) == 0: if len(team_scores) == 0:
if (self.round and self.round.tournament.round_set.count() == self.round.index -1): if (self.round and self.round.parent is None and self.round.tournament.round_set.filter(parent__isnull=True, group_stage_loser_bracket=False).count() - 1 == self.round.index):
names = ["Qualifié", '']
team = self.default_live_team(names)
teams.append(team)
names = ["Qualifié", '']
team = self.default_live_team(names)
teams.append(team)
return teams return teams
if (self.group_stage): if (self.group_stage):
return teams names = ["Équipe de poule", '']
if self.round and self.round.parent:
return teams
# No team scores at all
if previous_top_match:
names = [f"Gagnants {previous_top_match.computed_name()}", '']
team = self.default_live_team(names) team = self.default_live_team(names)
teams.append(team) teams.append(team)
if previous_bottom_match: names = ["Équipe de poule", '']
names = [f"Gagnants {previous_bottom_match.computed_name()}", '']
team = self.default_live_team(names) team = self.default_live_team(names)
teams.append(team) teams.append(team)
return teams
elif self.round and self.round.parent:
if loser_top_match:
names = [f"Perdant {loser_top_match.computed_name()}", '']
team = self.default_live_team(names)
teams.append(team)
if loser_bottom_match:
names = [f"Perdant {loser_bottom_match.computed_name()}", '']
team = self.default_live_team(names)
teams.append(team)
if previous_top_match:
names = [f"Gagnant {previous_top_match.computed_name()}", '']
team = self.default_live_team(names)
teams.append(team)
if previous_bottom_match:
names = [f"Gagnant {previous_bottom_match.computed_name()}", '']
team = self.default_live_team(names)
teams.append(team)
elif len(team_scores) == 1: elif len(team_scores) == 1:
# Only one team score, handle missing one # Only one team score, handle missing one
existing_team = team_scores[0].live_team(self) existing_team = team_scores[0].live_team(self)
if self.round and self.round.parent: if (self.group_stage):
teams.append(existing_team)
elif (self.group_stage):
teams.append(existing_team) teams.append(existing_team)
else: names = ["Équipe de poule", '']
if previous_top_match and previous_top_match.disabled == False and previous_top_match.end_date is None: team = self.default_live_team(names)
names = [f"Gagnants {previous_top_match.computed_name()}", ''] teams.append(team)
elif self.round:
if loser_top_match and loser_top_match.disabled == False and loser_top_match.end_date is None:
names = [f"Perdant {loser_top_match.computed_name()}", '']
team = self.default_live_team(names) team = self.default_live_team(names)
teams.append(team) teams.append(team)
teams.append(existing_team) teams.append(existing_team)
elif previous_bottom_match: elif loser_bottom_match:
names = [f"Gagnants {previous_bottom_match.computed_name()}", ''] names = [f"Perdant {loser_bottom_match.computed_name()}", '']
team = self.default_live_team(names) team = self.default_live_team(names)
teams.append(existing_team) teams.append(existing_team)
teams.append(team) teams.append(team)
else: elif previous_top_match and previous_top_match.disabled == False and previous_top_match.end_date is None:
names = [f"Gagnant {previous_top_match.computed_name()}", '']
team = self.default_live_team(names)
teams.append(team)
teams.append(existing_team) teams.append(existing_team)
elif previous_bottom_match:
names = [f"Gagnant {previous_bottom_match.computed_name()}", '']
team = self.default_live_team(names)
teams.append(existing_team)
teams.append(team)
elif (self.round.parent is None and self.round.tournament.round_set.filter(parent__isnull=True, group_stage_loser_bracket=False).count() - 1 == self.round.index):
match_index_within_round = self.index - (int(2 ** self.round.index) - 1)
names = ["Qualifié", '']
team = self.default_live_team(names)
if match_index_within_round < int(2 ** self.round.index) / 2:
teams.append(existing_team)
teams.append(team)
else:
teams.append(team)
teams.append(existing_team)
elif len(team_scores) == 2: elif len(team_scores) == 2:
# Both team scores present # Both team scores present
teams.extend([team_score.live_team(self) for team_score in team_scores]) teams.extend([team_score.live_team(self) for team_score in team_scores])

@ -31,9 +31,9 @@ class Round(models.Model):
if self.index == 0: if self.index == 0:
return "Finale" return "Finale"
elif self.index == 1: elif self.index == 1:
return "Demi-Finales" return "Demi-Finale"
elif self.index == 2: elif self.index == 2:
return "Quarts de finale" return "Quart de finale"
else: else:
squared = 2 ** self.index squared = 2 ** self.index
return f"{squared}ème" return f"{squared}ème"

Loading…
Cancel
Save