tournament: add: game call func when finished
This commit is contained in:
parent
2a63edf739
commit
743bb7edeb
@ -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):
|
||||
|
||||
@ -28,12 +32,27 @@ class GameModel(models.Model):
|
||||
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)]
|
||||
|
||||
|
@ -91,6 +91,9 @@ class TournamentRoom:
|
||||
|
||||
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()]
|
||||
|
||||
|
@ -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)]
|
||||
|
@ -13,17 +13,21 @@ 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]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user