tournament: game: finish event
This commit is contained in:
@ -19,24 +19,35 @@ class TournamentModel(models.Model):
|
||||
def _register_participant(self, participant: User) -> None:
|
||||
TournamentParticipantModel(participant=participant, tournament=self).save()
|
||||
|
||||
def start(self, participants: list[User]) -> None:
|
||||
def create_round(self, participants: set[User], round: int) -> set[GameModel]:
|
||||
|
||||
games: list[GameModel] = []
|
||||
game_list: set[GameModel] = set()
|
||||
|
||||
for i, (participant1, participant2) in enumerate(zip(participants[0::2], participants[1::2])):
|
||||
game: GameModel = self.create_game([participant1, participant2], round, i)
|
||||
game_list.add(game)
|
||||
|
||||
return game_list
|
||||
|
||||
def start(self, participant_list: set[User]) -> None:
|
||||
|
||||
self.started = True
|
||||
self.round = 1
|
||||
|
||||
for player in participants:
|
||||
self._register_participant(player)
|
||||
|
||||
for (participant1, participant2) in zip(participants[0::2], participants[1::2]):
|
||||
game: GameModel = self.create_game([participant1, participant2], round=1)
|
||||
games.append(game)
|
||||
for participant in participant_list:
|
||||
self._register_participant(participant)
|
||||
|
||||
self.save()
|
||||
|
||||
return games
|
||||
def finish(self, winner: User):
|
||||
|
||||
self.finished = True
|
||||
|
||||
def create_game(self, participants: list[User], round: int) -> GameModel:
|
||||
self.winner = winner
|
||||
|
||||
self.save()
|
||||
|
||||
def create_game(self, participants: set[User], round: int, pos: int) -> GameModel:
|
||||
|
||||
if (self.started == False):
|
||||
return None
|
||||
@ -48,37 +59,47 @@ class TournamentModel(models.Model):
|
||||
|
||||
game: GameModel = GameModel().create(participants)
|
||||
|
||||
TournamentGameModel(tournament=self, game=game, round=round).save()
|
||||
TournamentGameModel(tournament=self, game=game, round=round, pos=pos).save()
|
||||
|
||||
return game
|
||||
|
||||
def get_games(self) -> list[GameModel]:
|
||||
return [games for games in self.get_games_by_round(i for i in range(1, self.round))]
|
||||
def get_games(self) -> set[GameModel]:
|
||||
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)]
|
||||
def get_games_by_round(self, round: int) -> set[GameModel]:
|
||||
return {tournament_game for tournament_game in TournamentGameModel.objects.filter(tournament=self, round=round)}
|
||||
|
||||
def get_players_by_round(self, round: int) -> list[User]:
|
||||
return [game.get_players() for game in self.get_games_by_round(round)]
|
||||
def get_participants_by_round(self, round: int) -> set[User]:
|
||||
if round == 1:
|
||||
return self.get_participants()
|
||||
return {game.winner for game in self.get_games_by_round(round - 1)}
|
||||
|
||||
def get_winners_by_round(self, round: int) -> list[User]:
|
||||
return [game.winner for game in self.get_games_by_round(round)]
|
||||
def get_winners_by_round(self, round: int) -> set[User]:
|
||||
return {game.winner for game in self.get_games_by_round(round)}
|
||||
|
||||
def get_participants(self) -> list[TournamentParticipantModel]:
|
||||
return TournamentParticipantModel.objects.filter(tournament=self.pk)
|
||||
def get_participants(self) -> set[User]:
|
||||
return {tournament_participant.participant for tournament_participant in TournamentParticipantModel.objects.filter(tournament=self.pk)}
|
||||
|
||||
def get_state(self) -> str:
|
||||
return ("waiting to start", "in progress", "finish")[self.started + self.finished]
|
||||
|
||||
def is_participanting(self, profile: User) -> bool:
|
||||
def is_participating(self, profile: User) -> bool:
|
||||
return TournamentParticipantModel.objects.filter(participant=profile, tournament=self).exists()
|
||||
|
||||
class TournamentParticipantModel(models.Model):
|
||||
participant = models.ForeignKey(User, on_delete=CASCADE)
|
||||
tournament = models.ForeignKey(TournamentModel, on_delete=CASCADE)
|
||||
|
||||
class TournamentGameModel(models.Model):
|
||||
|
||||
class TournamentGameModel(GameModel):
|
||||
|
||||
tournament = models.ForeignKey(TournamentModel, on_delete=CASCADE, null=True, blank=True)
|
||||
round = models.IntegerField()
|
||||
game = models.ForeignKey(GameModel, on_delete=CASCADE)
|
||||
pos = models.IntegerField()
|
||||
|
||||
def finish(self, winner_id):
|
||||
super().finish(winner_id)
|
||||
|
||||
from .consumers import tournament_manager
|
||||
|
||||
room = tournament_manager.get(self.tournament)
|
||||
room.set_game_as_finished(self)
|
Reference in New Issue
Block a user