From 4385232017d27d0d6119ab798326aa78c05b7877 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 15 Mar 2024 15:35:49 +0100 Subject: [PATCH] Fix group stage team sort and results --- tournaments/models/group_stage.py | 65 +++++++++++++++++-------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/tournaments/models/group_stage.py b/tournaments/models/group_stage.py index 399922e..2cac533 100644 --- a/tournaments/models/group_stage.py +++ b/tournaments/models/group_stage.py @@ -43,40 +43,47 @@ class GroupStage(models.Model): team_scores = match.team_scores.all() if len(team_scores) == 2: - for team_score in match.team_scores.all(): - team_registration = team_score.team_registration - if team_registration in gs_teams: - team = gs_teams[team_registration] - if match.winning_team_id == team_registration.id: - team.wins += 1 - else: - team.losses += 1 - ts1 = team_scores[0] ts2 = team_scores[1] scores1 = ts1.scores() scores2 = ts2.scores() - total1 = 0 - total2 = 0 - - if len(scores1) == len(scores2): - if len(scores1) > 1: # multiple sets - for i in range(len(scores1)): - if scores1[i] > scores2[i]: - total1 += 1 + + if len(scores1) == len(scores2) and len(scores1) > 0: + + for team_score in match.team_scores.all(): + team_registration = team_score.team_registration + if team_registration in gs_teams: + team = gs_teams[team_registration] + if match.winning_team_id == team_registration.id: + team.wins += 1 else: - total2 += 1 - elif len(scores1) == 1: - total1 = scores1[0] - total2 = scores2[0] - - if ts1.team_registration in gs_teams and ts2.team_registration in gs_teams: - team1 = gs_teams[ts1.team_registration] - team2 = gs_teams[ts2.team_registration] - team1.diff = total1 - total2 - team2.diff = total2 - total1 - - teams = sorted(gs_teams.values(), key=lambda t: t.position) + team.losses += 1 + + total1 = 0 + total2 = 0 + + if len(scores1) == len(scores2): + if len(scores1) > 1: # multiple sets + for i in range(len(scores1)): + if scores1[i] > scores2[i]: + total1 += 1 + else: + total2 += 1 + elif len(scores1) == 1: + total1 = scores1[0] + total2 = scores2[0] + + if ts1.team_registration in gs_teams and ts2.team_registration in gs_teams: + team1 = gs_teams[ts1.team_registration] + team2 = gs_teams[ts2.team_registration] + team1.diff = total1 - total2 + team2.diff = total2 - total1 + + if len(self.match_set.all()) == 0: + teams = sorted(gs_teams.values(), key=lambda team: team.position) + else: + teams = sorted(gs_teams.values(), key=lambda team: -team.wins) + for team in teams: lgs.add_team(team) return lgs