diff --git a/tournaments/models/team_registration.py b/tournaments/models/team_registration.py index cac26a0..846b593 100644 --- a/tournaments/models/team_registration.py +++ b/tournaments/models/team_registration.py @@ -257,3 +257,9 @@ class TeamRegistration(models.Model): # ratio = (wins / total_matches) * 100 return f"{wins}/{total_matches}" return None + + def has_registered_online(self): + for p in self.playerregistration_set.all(): + if p.registered_online: + return True + return False diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index eeef79c..c9abf5d 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -329,15 +329,11 @@ class Tournament(models.Model): return sum(gs.size for gs in self.groupstage_set.all()) def teams(self, include_waiting_list, un_walk_out_team=None): - """ - Get sorted list of teams for the tournament. - - Args: - include_waiting_list (bool): Whether to include teams in waiting list + complete_teams, wildcard_bracket, wildcard_group_stage, waiting_teams = self.computed_teams(un_walk_out_team) + all_teams = self.sort_teams(include_waiting_list, complete_teams, wildcard_bracket, wildcard_group_stage, waiting_teams) + return all_teams - Returns: - list: List of TeamItem objects sorted according to tournament rules - """ + def computed_teams(self, un_walk_out_team=None): # Initialize team categories complete_teams = [] wildcard_bracket = [] @@ -382,8 +378,10 @@ class Tournament(models.Model): complete_teams.append(team) else: waiting_teams.append(team) + wildcard_bracket = [] + return complete_teams, wildcard_bracket, wildcard_group_stage, waiting_teams - + def sort_teams(self, include_waiting_list, complete_teams, wildcard_bracket, wildcard_group_stage, waiting_teams): # Initialize group stage spots group_stage_spots = self.group_stage_spots() bracket_seeds = self.team_count - group_stage_spots - len(wildcard_bracket) @@ -1224,32 +1222,13 @@ class Tournament(models.Model): def min_player_rank(self): return FederalLevelCategory.min_player_rank(self.federal_level_category, self.federal_category, self.federal_age_category) - def first_waiting_list_team(self): - teams = self.teams(True) - + def first_waiting_list_team(self, teams): if len(teams)<=self.team_count: return None waiting_teams = [team for team in teams if team.stage == "Attente"] if len(waiting_teams) > 0: return waiting_teams[0].team_registration - def first_out_of_list(self, un_walk_out_team): - teams = self.teams(True, un_walk_out_team) - waiting_teams = [team for team in teams if team.stage == "Attente"] - - retrieved_teams = [team for team in teams if team.team_registration.id == un_walk_out_team.id] - waiting_teams = [team for team in teams if team.stage == "Attente"] - waiting_team = None - is_in = True - if len(retrieved_teams) > 0: - if retrieved_teams[0].stage == "Attente": - is_in = False - if len(waiting_teams) > 0: - waiting_team = waiting_teams[0].team_registration - - return is_in, waiting_team - - def broadcasted_prog(self): # Get matches from broadcasted_matches_and_group_stages matches, _ = self.broadcasted_matches_and_group_stages() diff --git a/tournaments/services/email_service.py b/tournaments/services/email_service.py index 0912077..a43a2fe 100644 --- a/tournaments/services/email_service.py +++ b/tournaments/services/email_service.py @@ -343,12 +343,6 @@ class TournamentEmailService: @staticmethod def notify(captain, other_player, tournament, message_type: TeamEmailType): - if tournament.should_be_over(): - return - - if tournament.supposedly_in_progress(): - return - if not captain or not captain.registered_online or not captain.email: return diff --git a/tournaments/signals.py b/tournaments/signals.py index d9188f5..735f1ec 100644 --- a/tournaments/signals.py +++ b/tournaments/signals.py @@ -50,10 +50,39 @@ def notify_object_creation_on_discord(created, instance): send_discord_log_message(message) def notify_team(team, tournament, message_type): + #print(team, message_type) if tournament.enable_online_registration is False: return - print(team, message_type) - TournamentEmailService.notify_team(team, tournament, message_type) + if team.has_registered_online() is False: + return + if tournament.should_be_over(): + return + if tournament.supposedly_in_progress(): + return + + captain = None + other_player = None + + for player in team.playerregistration_set.all(): + if player.captain: + captain = player + else: + other_player = player + + if captain: + TournamentEmailService.notify(captain, other_player, tournament, message_type) + if not captain.registered_online or not captain.email: + TournamentEmailService.notify(other_player, captain, tournament, message_type) + else: + # Notify both players separately if there is no captain or the captain is unavailable + players = list(team.playerregistration_set.all()) + if len(players) == 2: + first_player, second_player = players + TournamentEmailService.notify(first_player, second_player, tournament, message_type) + TournamentEmailService.notify(second_player, first_player, tournament, message_type) + elif len(players) == 1: + # If there's only one player, just send them the notification + TournamentEmailService.notify(players[0], None, tournament, message_type) @receiver(pre_delete, sender=TeamRegistration) def unregister_team(sender, instance, **kwargs): @@ -63,7 +92,8 @@ def unregister_team(sender, instance, **kwargs): return notify_team(instance, instance.tournament, TeamEmailType.UNREGISTERED) - first_waiting_list_team = instance.tournament.first_waiting_list_team() + teams = instance.tournament.teams(True) + first_waiting_list_team = instance.tournament.first_waiting_list_team(teams) if first_waiting_list_team and first_waiting_list_team.id != instance.id: notify_team(first_waiting_list_team, instance.tournament, TeamEmailType.OUT_OF_WAITING_LIST) @@ -122,48 +152,40 @@ def check_waiting_list(sender, instance, **kwargs): @receiver(pre_save, sender=TeamRegistration) def warn_team_walkout_status_change(sender, instance, **kwargs): - if instance.id is None: - return - if instance.tournament.enable_online_registration is False: + if instance.id is None or instance.tournament.enable_online_registration is False: return + previous_instance = None try: previous_instance = TeamRegistration.objects.get(id=instance.id) except TeamRegistration.DoesNotExist: - previous_instance = None - - if previous_instance is None: return - previous_teams = previous_instance.tournament.teams(True) + previous_teams = instance.tournament.teams(True) current_teams = instance.tournament.teams(True, instance) previous_retrieved_teams = [team for team in previous_teams if team.team_registration.id == previous_instance.id] current_retrieved_teams = [team for team in current_teams if team.team_registration.id == instance.id] was_out = previous_instance.out_of_tournament() is_out = instance.out_of_tournament() - if len(previous_retrieved_teams) > 0: - if previous_retrieved_teams[0].stage == "Attente": - was_out = True + if len(previous_retrieved_teams) > 0 and previous_retrieved_teams[0].stage == "Attente": + was_out = True - if len(current_retrieved_teams) > 0: - if current_retrieved_teams[0].stage == "Attente": - is_out = True + if len(current_retrieved_teams) > 0 and current_retrieved_teams[0].stage == "Attente": + is_out = True print(was_out, previous_instance.out_of_tournament(), is_out, instance.out_of_tournament()) - if not instance.out_of_tournament() and is_out: + if not instance.out_of_tournament() and is_out and (previous_instance.out_of_tournament() or not was_out): notify_team(instance, instance.tournament, TeamEmailType.OUT_OF_WALKOUT_WAITING_LIST) - - if not previous_instance.out_of_tournament() and instance.out_of_tournament(): + elif was_out and not is_out: + notify_team(instance, instance.tournament, TeamEmailType.OUT_OF_WALKOUT_IS_IN) + elif not previous_instance.out_of_tournament() and instance.out_of_tournament(): notify_team(instance, instance.tournament, TeamEmailType.WALKOUT) if was_out and not is_out: - is_in, first_out_of_list = instance.tournament.first_out_of_list(instance) - if is_in: - notify_team(instance, instance.tournament, TeamEmailType.OUT_OF_WALKOUT_IS_IN) + first_out_of_list = instance.tournament.first_waiting_list_team(current_teams) if first_out_of_list: notify_team(first_out_of_list, instance.tournament, TeamEmailType.UNEXPECTED_OUT_OF_TOURNAMENT) - elif not was_out and is_out: - first_waiting_list_team = instance.tournament.first_waiting_list_team() + first_waiting_list_team = instance.tournament.first_waiting_list_team(previous_teams) if first_waiting_list_team: notify_team(first_waiting_list_team, instance.tournament, TeamEmailType.OUT_OF_WAITING_LIST)