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
|
import time
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from tournament.models import TournamentGameModel
|
||||||
|
|
||||||
class GameModel(models.Model):
|
class GameModel(models.Model):
|
||||||
|
|
||||||
@ -27,13 +31,28 @@ class GameModel(models.Model):
|
|||||||
self.start_timestamp = round(time.time() * 1000, 1)
|
self.start_timestamp = round(time.time() * 1000, 1)
|
||||||
self.started = True
|
self.started = True
|
||||||
self.save()
|
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):
|
def finish(self, winner_id):
|
||||||
self.winner_id = winner_id
|
self.winner_id = winner_id
|
||||||
self.finished = True
|
self.finished = True
|
||||||
self.stop_timestamp = round(time.time() * 1000, 1)
|
self.stop_timestamp = round(time.time() * 1000, 1)
|
||||||
self.save()
|
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]:
|
def get_players(self) -> list[User]:
|
||||||
return [game_player.player for game_player in GameMembersModel.objects.filter(game = self)]
|
return [game_player.player for game_player in GameMembersModel.objects.filter(game = self)]
|
||||||
|
|
||||||
|
@ -90,6 +90,9 @@ class TournamentRoom:
|
|||||||
self._member_list.add(member)
|
self._member_list.add(member)
|
||||||
|
|
||||||
return member
|
return member
|
||||||
|
|
||||||
|
def set_game_as_finished(self, game: GameModel):
|
||||||
|
raise NotImplemented()
|
||||||
|
|
||||||
def get_participants_profiles(self) -> list[ProfileModel]:
|
def get_participants_profiles(self) -> list[ProfileModel]:
|
||||||
return [participant._socket.user.profilemodel for participant in self.get_participants()]
|
return [participant._socket.user.profilemodel for participant in self.get_participants()]
|
||||||
|
@ -53,7 +53,7 @@ class TournamentModel(models.Model):
|
|||||||
return game
|
return game
|
||||||
|
|
||||||
def get_games(self) -> list[GameModel]:
|
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]:
|
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)]
|
return [tournament_game.game for tournament_game in TournamentGameModel.objects.filter(tournament=self, round=round)]
|
||||||
|
@ -13,16 +13,20 @@ class TournamentSerializer(serializers.ModelSerializer):
|
|||||||
state = serializers.SerializerMethodField(read_only=True, required=False)
|
state = serializers.SerializerMethodField(read_only=True, required=False)
|
||||||
participants = serializers.SerializerMethodField(read_only=True, required=False)
|
participants = serializers.SerializerMethodField(read_only=True, required=False)
|
||||||
round = serializers.ReadOnlyField()
|
round = serializers.ReadOnlyField()
|
||||||
|
games = serializers.SerializerMethodField(read_only=True, required=False)
|
||||||
started = serializers.ReadOnlyField()
|
started = serializers.ReadOnlyField()
|
||||||
finished = serializers.ReadOnlyField()
|
finished = serializers.ReadOnlyField()
|
||||||
name = serializers.CharField(default="")
|
name = serializers.CharField(default="")
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = TournamentModel
|
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):
|
def get_participants(self, instance: TournamentModel):
|
||||||
return ProfileSerializer(instance.get_participants(), many=True).data
|
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):
|
def get_state(self, instance: TournamentModel):
|
||||||
return ["waiting", "started", "finished"][instance.started + instance.finished]
|
return ["waiting", "started", "finished"][instance.started + instance.finished]
|
||||||
|
Loading…
Reference in New Issue
Block a user