diff --git a/tournaments/models/match.py b/tournaments/models/match.py index 17ab232..d0c05d8 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -90,6 +90,9 @@ class Match(models.Model): else: return False + def should_appear(self): + return self.start_date and len(self.team_scores.all()) > 0 + def formatted_duration(self): _seconds = self.current_duration() diff --git a/tournaments/models/round.py b/tournaments/models/round.py index 4b3c325..f5b9144 100644 --- a/tournaments/models/round.py +++ b/tournaments/models/round.py @@ -27,8 +27,7 @@ class Round(models.Model): def name(self): if self.loser: - parent = self.loser.name() - return f"Matchs de classement [{parent}]" + return "Matchs de classement" else: if self.index == 0: return "Finale" @@ -39,3 +38,15 @@ class Round(models.Model): else: squared = 2 ** self.index return f"{squared}ème" + + def ranking_matches(self, hide_empty_matches): + matches = [] + for child in self.children.all(): + + child_matches = child.match_set.all() + if hide_empty_matches: + child_matches = [m for m in matches if m.should_appear()] + + matches.extend(child_matches) + matches.extend(child.ranking_matches(hide_empty_matches)) + return matches diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index 4aae976..30e9cbd 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -75,10 +75,10 @@ class Tournament(models.Model): match_groups = [] if group_stage_id: group_stage = self.groupstage_set.filter(id=group_stage_id).first() - match_groups.append(self.group_stage_match_group(group_stage, broadcasted)) + match_groups.append(self.group_stage_match_group(group_stage, broadcasted, hide_empty_matches=False)) elif round_id: round = self.round_set.filter(id=round_id).first() - match_groups = self.round_match_groups(round, broadcasted) + match_groups = self.round_match_groups(round, broadcasted, hide_empty_matches=False) else: match_groups = self.all_groups(broadcasted) @@ -86,29 +86,46 @@ class Tournament(models.Model): def all_groups(self, broadcasted): groups = [] - for group_stage in self.groupstage_set.all(): - group = self.group_stage_match_group(group_stage, broadcasted) - groups.append(group) for round in self.round_set.all(): - groups.extend(self.round_match_groups(round, broadcasted)) + groups.extend(self.round_match_groups(round, broadcasted, hide_empty_matches=True)) + for group_stage in self.groupstage_set.all(): + group = self.group_stage_match_group(group_stage, broadcasted, hide_empty_matches=True) + if group: + groups.append(group) return groups - def group_stage_match_group(self, group_stage, broadcasted): - return self.create_match_group(group_stage.name(), group_stage.match_set.all(), broadcasted) + def group_stage_match_group(self, group_stage, broadcasted, hide_empty_matches): + matches = group_stage.match_set.all() + if hide_empty_matches: + matches = [m for m in matches if m.should_appear()] + + if matches: + return self.create_match_group(group_stage.name(), matches, broadcasted) + else: + return None - def round_match_groups(self, round, broadcasted): + def round_match_groups(self, round, broadcasted, hide_empty_matches): groups = [] - group = self.create_match_group(round.name(), round.match_set.all(), broadcasted) - groups.append(group) - for child in round.children.all(): - children_groups = self.round_match_groups(child, broadcasted) - groups.extend(children_groups) + + matches = round.match_set.all() + if hide_empty_matches: + matches = [m for m in matches if m.should_appear()] + + if matches: + group = self.create_match_group(round.name(), round.match_set.all(), broadcasted) + groups.append(group) + + ranking_matches = round.ranking_matches(hide_empty_matches) + if len(ranking_matches) > 0: + group = self.create_match_group('Matchs de classement', ranking_matches, broadcasted) + groups.append(group) return groups def create_match_group(self, name, matches, broadcasted): - if not broadcasted: - matches = [m for m in matches if len(m.team_scores.all()) > 0] + matches = list(matches) + # if not broadcasted: + # matches = [m for m in matches if m.should_appear()] matches.sort(key=lambda m: m.order) live_matches = [match.live_match() for match in matches] return MatchGroup(name, live_matches) diff --git a/tournaments/views.py b/tournaments/views.py index 6a047c8..bc5ed3f 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -78,8 +78,6 @@ def tournament(request, tournament_id): rounds = tournament.round_set.filter(loser=None).order_by('-index') group_stages = tournament.groupstage_set.order_by('index') - print(f"GROUPS = {len(match_groups)}") - return render(request, 'tournaments/matches.html', { 'tournament': tournament, 'rounds': rounds,