diff --git a/games/models.py b/games/models.py index 37bfcce..573a891 100644 --- a/games/models.py +++ b/games/models.py @@ -7,6 +7,10 @@ from django.contrib.auth.models import User import time +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from tournament.models import TournamentGameModel class GameModel(models.Model): @@ -27,13 +31,28 @@ class GameModel(models.Model): self.start_timestamp = round(time.time() * 1000, 1) self.started = True self.save() + + def get_tournament(self) -> None | TournamentGameModel: + + from tournament.models import TournamentGameModel + + query = TournamentGameModel.objects.filter(game=self) + if (not query.exists()): + return None + return query[0] def finish(self, winner_id): self.winner_id = winner_id self.finished = True self.stop_timestamp = round(time.time() * 1000, 1) self.save() - + + tournament = self.get_tournament() + if tournament is not None: + from tournament.consumers import tournament_manager + room = tournament_manager.get(tournament) + room.set_game_as_finished(self) + def get_players(self) -> list[User]: return [game_player.player for game_player in GameMembersModel.objects.filter(game = self)] diff --git a/tournament/consumers.py b/tournament/consumers.py index 78aa59f..953b082 100644 --- a/tournament/consumers.py +++ b/tournament/consumers.py @@ -90,6 +90,9 @@ class TournamentRoom: self._member_list.add(member) return member + + def set_game_as_finished(self, game: GameModel): + raise NotImplemented() def get_participants_profiles(self) -> list[ProfileModel]: return [participant._socket.user.profilemodel for participant in self.get_participants()] diff --git a/tournament/models.py b/tournament/models.py index 4f0fe28..6a91526 100644 --- a/tournament/models.py +++ b/tournament/models.py @@ -53,7 +53,7 @@ class TournamentModel(models.Model): return game def get_games(self) -> list[GameModel]: - return [tournament_game.game for tournament_game in TournamentGameModel.objects.filter(tournament=self)] + return [games for games in self.get_games_by_round(i for i in range(1, self.round))] def get_games_by_round(self, round: int) -> list[GameModel]: return [tournament_game.game for tournament_game in TournamentGameModel.objects.filter(tournament=self, round=round)] diff --git a/tournament/serializers.py b/tournament/serializers.py index abf2cfb..cabb773 100644 --- a/tournament/serializers.py +++ b/tournament/serializers.py @@ -13,16 +13,20 @@ class TournamentSerializer(serializers.ModelSerializer): state = serializers.SerializerMethodField(read_only=True, required=False) participants = serializers.SerializerMethodField(read_only=True, required=False) round = serializers.ReadOnlyField() + games = serializers.SerializerMethodField(read_only=True, required=False) started = serializers.ReadOnlyField() finished = serializers.ReadOnlyField() name = serializers.CharField(default="") class Meta: model = TournamentModel - fields = ["name", "nb_participants", "round", "started", "finished", "id", "state", "participants"] + fields = ["name", "nb_participants", "round", "started", "finished", "id", "state", "participants", "games"] def get_participants(self, instance: TournamentModel): return ProfileSerializer(instance.get_participants(), many=True).data + + def get_games(self, instance: TournamentModel): + return GameSerializer(instance.get_games(), many=True).data def get_state(self, instance: TournamentModel): return ["waiting", "started", "finished"][instance.started + instance.finished]