tournament: add: game call func when finished

This commit is contained in:
2024-04-23 15:41:18 +02:00
parent 2a63edf739
commit 743bb7edeb
4 changed files with 29 additions and 3 deletions

View File

@ -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()]

View File

@ -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)]

View File

@ -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]