From 1627265a36d0db120d2b4e15a342908ebe24c17b Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Fri, 28 Feb 2025 14:22:02 +0100 Subject: [PATCH] fix loser bracket empty team place holder management --- tournaments/models/match.py | 108 ++++++++++++++++++++++++++++-------- tournaments/models/round.py | 4 +- 2 files changed, 88 insertions(+), 24 deletions(-) diff --git a/tournaments/models/match.py b/tournaments/models/match.py index b10e051..ea70838 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -85,12 +85,26 @@ class Match(models.Model): previous_index = self.round.index + 1 # 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 = None).first() + return self.round.tournament.round_set.filter(index=previous_index, parent = self.round.parent).first() # Return None or an appropriate value if the index is out of bounds 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): previous_round = self.get_previous_round() #print(previous_round) @@ -103,6 +117,17 @@ class Match(models.Model): match = matches.filter(index=match_index).first() 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): if self.round and self.round.parent is None: return self.backup_name() @@ -150,44 +175,83 @@ class Match(models.Model): team_scores = list(self.team_scores.all()) previous_top_match = self.precedent_match(True) 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 (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 if (self.group_stage): - return teams - - 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()}", ''] + names = ["Équipe de poule", ''] team = self.default_live_team(names) teams.append(team) - if previous_bottom_match: - names = [f"Gagnants {previous_bottom_match.computed_name()}", ''] + names = ["Équipe de poule", ''] team = self.default_live_team(names) 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: # Only one team score, handle missing one existing_team = team_scores[0].live_team(self) - if self.round and self.round.parent: - teams.append(existing_team) - elif (self.group_stage): + if (self.group_stage): teams.append(existing_team) - else: - if previous_top_match and previous_top_match.disabled == False and previous_top_match.end_date is None: - names = [f"Gagnants {previous_top_match.computed_name()}", ''] + names = ["Équipe de poule", ''] + team = self.default_live_team(names) + 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) teams.append(team) teams.append(existing_team) - elif previous_bottom_match: - names = [f"Gagnants {previous_bottom_match.computed_name()}", ''] + elif loser_bottom_match: + names = [f"Perdant {loser_bottom_match.computed_name()}", ''] team = self.default_live_team(names) teams.append(existing_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) + 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: # Both team scores present teams.extend([team_score.live_team(self) for team_score in team_scores]) diff --git a/tournaments/models/round.py b/tournaments/models/round.py index befe57a..2025a81 100644 --- a/tournaments/models/round.py +++ b/tournaments/models/round.py @@ -31,9 +31,9 @@ class Round(models.Model): if self.index == 0: return "Finale" elif self.index == 1: - return "Demi-Finales" + return "Demi-Finale" elif self.index == 2: - return "Quarts de finale" + return "Quart de finale" else: squared = 2 ** self.index return f"{squared}ème"