tournament: add: les crampte

This commit is contained in:
2024-04-22 17:03:45 +02:00
parent 1d8c2c633a
commit aafc61d40a
6 changed files with 191 additions and 152 deletions

View File

@ -1,21 +1,12 @@
from __future__ import annotations
from transcendence.abstract.AbstractRoomMember import AbstractRoomMember
from transcendence.abstract.AbstractRoom import AbstractRoom
from transcendence.abstract.AbstractRoomManager import AbstractRoomManager
from profiles.models import ProfileModel
from games.models import GameModel
from django.contrib.auth.models import User
from django.db.models import CASCADE
from channels.generic.websocket import WebsocketConsumer
from django.db import models
import json
# Create your models here.tu
class TournamentModel(models.Model):
name = models.CharField(max_length = 100)
@ -23,12 +14,14 @@ class TournamentModel(models.Model):
round = models.IntegerField()
started = models.BooleanField(default = False)
finished = models.BooleanField(default = False)
winner = models.ForeignKey(ProfileModel, on_delete=CASCADE, blank=True, null=True)
winner = models.ForeignKey(User, on_delete=CASCADE, blank=True, null=True)
def _register_participant(self, participant: ProfileModel) -> None:
def _register_participant(self, participant: User) -> None:
TournamentParticipantModel(participant=participant, tournament=self).save()
def start(self, participants: list[ProfileModel]) -> None:
def start(self, participants: list[User]) -> None:
games: list[GameModel] = []
self.started = True
@ -36,11 +29,14 @@ class TournamentModel(models.Model):
self._register_participant(player)
for (participant1, participant2) in zip(participants[0::2], participants[1::2]):
self.create_game([participant1, participant2], round=1)
game: GameModel = self.create_game([participant1, participant2], round=1)
games.append(game)
self.save()
def create_game(self, participants: list[ProfileModel], round: int) -> GameModel:
return games
def create_game(self, participants: list[User], round: int) -> GameModel:
if (self.started == False):
return None
@ -62,10 +58,10 @@ class TournamentModel(models.Model):
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[ProfileModel]:
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[ProfileModel]:
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]:
@ -74,13 +70,12 @@ class TournamentModel(models.Model):
def get_state(self) -> str:
return ("waiting to start", "in progress", "finish")[self.started + self.finished]
def is_participanting(self, profile: ProfileModel) -> bool:
def is_participanting(self, profile: User) -> bool:
return TournamentParticipantModel.objects.filter(participant=profile, tournament=self).exists()
class TournamentParticipantModel(models.Model):
participant = models.ForeignKey(ProfileModel, on_delete=CASCADE)
participant = models.ForeignKey(User, on_delete=CASCADE)
tournament = models.ForeignKey(TournamentModel, on_delete=CASCADE)
#prout à encore frappé
class TournamentGameModel(models.Model):