tournament: game: finish event
This commit is contained in:
@ -7,6 +7,7 @@ from django.db.models import QuerySet
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from games.models import GameModel
|
||||
from games.serializers import GameSerializer
|
||||
from profiles.models import ProfileModel
|
||||
from profiles.serializers.ProfileSerializer import ProfileSerializer
|
||||
from .models import TournamentModel
|
||||
@ -83,6 +84,8 @@ class TournamentRoom:
|
||||
self._room_manager: TournamentRoomManager = room_manager
|
||||
self._member_list: set[TournamentMember] = set()
|
||||
self._model: TournamentModel = tournament
|
||||
self._game_in_progress_list: set[GameModel] = set()
|
||||
self._current_round = 0
|
||||
|
||||
def join(self, socket: TournamentWebConsumer) -> TournamentMember:
|
||||
|
||||
@ -92,21 +95,52 @@ class TournamentRoom:
|
||||
return member
|
||||
|
||||
def set_game_as_finished(self, game: GameModel):
|
||||
raise NotImplemented()
|
||||
self._game_in_progress_list.remove(game)
|
||||
|
||||
data: dict = GameSerializer(game).data
|
||||
|
||||
data.update({"round": self._current_round})
|
||||
|
||||
self.broadcast("game_update", data)
|
||||
|
||||
if len(self._game_in_progress_list) == 0:
|
||||
self._round_finished()
|
||||
|
||||
def _finish(self, winner: User):
|
||||
self._model.finish(winner)
|
||||
|
||||
def _round_finished(self):
|
||||
|
||||
if self._current_round == self._model.round:
|
||||
last_game: GameModel = self._model.get_games_by_round(self._current_round)[0]
|
||||
self._finish(last_game.winner)
|
||||
return
|
||||
|
||||
self._start_round()
|
||||
|
||||
def _start_round(self):
|
||||
|
||||
self._current_round += 1
|
||||
|
||||
participant_list: set[User] = self._model.get_participants_by_round(self._current_round)
|
||||
|
||||
self._game_in_progress_list = self._model.create_round(participant_list, self._current_round)
|
||||
|
||||
for game in self._game_in_progress_list:
|
||||
for player in game.get_players():
|
||||
participant: TournamentMember = self.get_participant_by_profile(player)
|
||||
participant.send_goto(game)
|
||||
|
||||
def get_participants_profiles(self) -> list[ProfileModel]:
|
||||
return [participant._socket.user.profilemodel for participant in self.get_participants()]
|
||||
|
||||
def start(self) -> None:
|
||||
|
||||
games: list[GameModel] = self._model.start()
|
||||
self._model.start()
|
||||
|
||||
self.broadcast("start")
|
||||
|
||||
for game in games:
|
||||
for player in game.get_players():
|
||||
participant: TournamentMember = self.get_participant_by_profile(player)
|
||||
participant.send_goto(game)
|
||||
self._start_round()
|
||||
|
||||
def get_participant_by_profile(self, profile: ProfileModel):
|
||||
for participant in self.get_participants():
|
||||
|
Reference in New Issue
Block a user