84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
from __future__ import annotations
|
|
|
|
from games.models import GameModel
|
|
|
|
from django.contrib.auth.models import User
|
|
from django.db.models import CASCADE
|
|
from django.db import models
|
|
|
|
|
|
class TournamentModel(models.Model):
|
|
|
|
name = models.CharField(max_length = 100)
|
|
nb_participants = models.IntegerField()
|
|
round = models.IntegerField()
|
|
started = models.BooleanField(default = False)
|
|
finished = models.BooleanField(default = False)
|
|
winner = models.ForeignKey(User, on_delete=CASCADE, blank=True, null=True)
|
|
|
|
def _register_participant(self, participant: User) -> None:
|
|
TournamentParticipantModel(participant=participant, tournament=self).save()
|
|
|
|
def start(self, participants: list[User]) -> None:
|
|
|
|
games: list[GameModel] = []
|
|
|
|
self.started = True
|
|
|
|
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)
|
|
|
|
self.save()
|
|
|
|
return games
|
|
|
|
def create_game(self, participants: list[User], round: int) -> GameModel:
|
|
|
|
if (self.started == False):
|
|
return None
|
|
|
|
if (len(participants) != 2):
|
|
return None
|
|
|
|
from games.models import GameModel
|
|
|
|
game: GameModel = GameModel().create(participants)
|
|
|
|
TournamentGameModel(tournament=self, game=game, round=round).save()
|
|
|
|
return game
|
|
|
|
def get_games(self) -> list[GameModel]:
|
|
return [tournament_game.game for tournament_game in TournamentGameModel.objects.filter(tournament=self)]
|
|
|
|
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_players_by_round(self, round: int) -> list[User]:
|
|
return [game.get_players() for game in self.get_games_by_round(round)]
|
|
|
|
def get_winners_by_round(self, round: int) -> list[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_state(self) -> str:
|
|
return ("waiting to start", "in progress", "finish")[self.started + self.finished]
|
|
|
|
def is_participanting(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):
|
|
|
|
tournament = models.ForeignKey(TournamentModel, on_delete=CASCADE, null=True, blank=True)
|
|
round = models.IntegerField()
|
|
game = models.ForeignKey(GameModel, on_delete=CASCADE) |