diff --git a/tournaments/models/enums.py b/tournaments/models/enums.py index 2cf51dd..02827a1 100644 --- a/tournaments/models/enums.py +++ b/tournaments/models/enums.py @@ -49,7 +49,7 @@ class FederalMatchCategory(models.IntegerChoices): SINGLE_SET_DECISIVE_POINT = 12, 'Single set with decisive point' def last_set_is_tie_break(value): - if value == FederalMatchCategory.TWO_SETS_FOUR_GAME or value == FederalMatchCategory.TWO_SETS_FOUR_GAME_DECISIVE_POINT or value == FederalMatchCategory.TWO_SETS_SUPER_TIE or value == FederalMatchCategory.SUPER_TIE or value == FederalMatchCategory.MEGA_TIE or value == FederalMatchCategory.TWO_SETS_DECISIVE_POINT_SUPER_TIE or value == FederalMatchCategory.TWO_SETS_OF_SUPER_TIE: + if value == FederalMatchCategory.TWO_SETS_FOUR_GAME or value == FederalMatchCategory.TWO_SETS_FOUR_GAME_DECISIVE_POINT or value == FederalMatchCategory.TWO_SETS_SUPER_TIE or value == FederalMatchCategory.SUPER_TIE or value == FederalMatchCategory.MEGA_TIE or value == FederalMatchCategory.TWO_SETS_DECISIVE_POINT_SUPER_TIE: return True else: return False diff --git a/tournaments/models/group_stage.py b/tournaments/models/group_stage.py index 132ef43..863abea 100644 --- a/tournaments/models/group_stage.py +++ b/tournaments/models/group_stage.py @@ -82,28 +82,43 @@ class GroupStage(models.Model): else: team.losses += 1 - total1 = 0 - total2 = 0 + set_total1 = 0 + set_total2 = 0 + game_total1 = 0 + game_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 + for i in range(len(scores1)): + if scores1[i] > scores2[i]: + set_total1 += 1 + if i == 2 and FederalMatchCategory.last_set_is_tie_break(match.format): + game_total1 += 1 else: - total2 += 1 - elif len(scores1) == 1: - total1 = scores1[0] - total2 = scores2[0] + game_total1 += scores1[i] + game_total2 += scores2[i] + else: + set_total2 += 1 + if i == 2 and FederalMatchCategory.last_set_is_tie_break(match.format): + game_total2 += 1 + else: + game_total1 += scores1[i] + game_total2 += scores2[i] 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 + team1.display_set_difference = FederalMatchCategory.max_number_of_sets(self.format) > 1 + team2.display_set_difference = FederalMatchCategory.max_number_of_sets(self.format) > 1 + team1.set_diff += set_total1 - set_total2 + team2.set_diff += set_total2 - set_total1 + team1.game_diff += game_total1 - game_total2 + team2.game_diff += game_total2 - game_total1 if len(self.match_set.filter(end_date__isnull=False).all()) > 0: - teams = sorted(gs_teams.values(), key=lambda team: -(team.wins * 100 + team.diff)) + teams = sorted( + gs_teams.values(), + key=lambda team: (-(team.wins * 1000 + team.set_diff * 100 + team.game_diff), team.team_registration.id) + ) else: teams = sorted(gs_teams.values(), key=lambda team: team.position) @@ -177,6 +192,9 @@ class GroupStageTeam: self.wins = 0 self.losses = 0 self.diff = 0 + self.set_diff = 0 + self.game_diff = 0 + self.display_set_difference = False self.weight = team_registration.weight self.team_registration = team_registration @@ -187,10 +205,34 @@ class GroupStageTeam: return self.wins + self.losses def formatted_diff(self): - if self.diff >= 0: - return f"+{self.diff}" + if self.display_set_difference: + return f"{self.formatted_set_diff()} {self.sets_word_localized()}" + " / " + f"{self.formatted_game_diff()} {self.games_word_localized()}" + else: + return self.formatted_game_diff() + + def sets_word_localized(self): + if abs(self.set_diff) > 1: + return "sets" + else: + return "set" + + def games_word_localized(self): + if abs(self.game_diff) > 1: + return "jeux" + else: + return "jeu" + + def formatted_set_diff(self): + if self.set_diff >= 0: + return f"+{self.set_diff}" + else: + return f"{self.set_diff}" + + def formatted_game_diff(self): + if self.game_diff >= 0: + return f"+{self.game_diff}" else: - return f"{self.diff}" + return f"{self.game_diff}" def to_dict(self): return { diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index 1acea4e..2fde8c6 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -299,11 +299,7 @@ class Tournament(models.Model): # print(f"Is valid: {is_valid}") if team_registration.walk_out is False: - names = team_registration.team_names() - weight = team_registration.weight - initial_weight = team_registration.initial_weight() - date = team_registration.call_date - team = TeamList(names, weight, date, initial_weight, team_registration.wild_card_bracket, team_registration.wild_card_group_stage, team_registration.logo) + team = TeamList(team_registration) # print(f"Created team: {team}") if team_registration.group_stage_position is not None: team.set_stage("Poule") @@ -330,8 +326,7 @@ class Tournament(models.Model): # print(f"Waiting teams: {len(waiting_teams)}") if len(teams) < self.team_count: - teams.sort(key=lambda s: (s.initial_weight, s.date)) - print("Returning early due to insufficient teams") + teams.sort(key=lambda s: (s.initial_weight, s.team_registration.id)) return teams seeds_count = min(self.team_count, len(teams)) - self.group_stage_count * self.teams_per_group_stage - len(wildcard_bracket) @@ -344,13 +339,12 @@ class Tournament(models.Model): # print(f"Group stage members count: {group_stage_members_count}") if self.team_sorting == TeamSortingType.INSCRIPTION_DATE: - complete_teams.sort(key=lambda s: (s.date is None, s.date or datetime.min, s.initial_weight)) + complete_teams.sort(key=lambda s: (s.date is None, s.date or datetime.min, s.initial_weight, s.team_registration.id)) else: - complete_teams.sort(key=lambda s: (s.initial_weight, s.date is None, s.date or datetime.min)) + complete_teams.sort(key=lambda s: (s.initial_weight, s.team_registration.id)) selected_teams = complete_teams[:self.team_count] - selected_teams.sort(key=lambda s: s.initial_weight) - # print(f"Selected teams: {len(selected_teams)}") + selected_teams.sort(key=lambda s: (s.initial_weight, s.team_registration.id)) if seeds_count > 0: bracket_teams = selected_teams[:seeds_count] + wildcard_bracket @@ -374,15 +368,15 @@ class Tournament(models.Model): if waiting_list_count > 0: waiting_teams = waiting_teams + complete_teams[-waiting_list_count:] if self.team_sorting == TeamSortingType.INSCRIPTION_DATE: - waiting_teams.sort(key=lambda s: (s.date, s.initial_weight)) + waiting_teams.sort(key=lambda s: (s.date is None, s.date or datetime.min, s.initial_weight, s.team_registration.id)) else: - waiting_teams.sort(key=lambda s: (s.initial_weight, s.date)) + waiting_teams.sort(key=lambda s: (s.initial_weight, s.team_registration.id)) else: waiting_teams = [] # print(f"Final waiting teams: {len(waiting_teams)}") - bracket_teams.sort(key=lambda s: s.weight) - group_stage_teams.sort(key=lambda s: s.weight) + bracket_teams.sort(key=lambda s: (s.weight, s.team_registration.id)) + group_stage_teams.sort(key=lambda s: (s.weight, s.team_registration.id)) for team in bracket_teams: if team.stage == "Attente": @@ -887,15 +881,16 @@ class TeamSummon: } class TeamList: - def __init__(self, names, weight, date, initial_weight, wildcard_bracket, wildcard_groupstage, image): - self.names = names - self.date = date - self.weight = weight - self.initial_weight = initial_weight - self.image = image + def __init__(self, team_registration): + self.names = team_registration.team_names + self.date = team_registration.call_date + self.weight = team_registration.weight + self.initial_weight = team_registration.initial_weight() + self.image = team_registration.logo self.stage = "" - self.wildcard_bracket = wildcard_bracket - self.wildcard_groupstage = wildcard_groupstage + self.team_registration = team_registration + self.wildcard_bracket = team_registration.wild_card_bracket + self.wildcard_groupstage = team_registration.wild_card_group_stage def set_stage(self, stage): self.stage = stage