from django.db import models from channels.generic.websocket import WebsocketConsumer from games.models import GameModel import json from transcendence.abstract.AbstractRoomMember import AbstractRoomMember from transcendence.abstract.AbstractRoom import AbstractRoom from transcendence.abstract.AbstractRoomManager import AbstractRoomManager # Create your models here.tu class TournamentModel(models.Model): name = models.CharField(max_length=100) nb_players = models.IntegerField() nb_players_by_game = models.IntegerField() level = models.IntegerField() started = models.BooleanField(default=False) finished = models.BooleanField(default=False) def create_game(self, users_id): game_id = GameModel.create(users_id=users_id) TournamentGamesModel(game_id=game_id, tournament_id=self.pk).save() return game_id def get_games_id_by_level(self, level): return list(TournamentGamesModel.objects.filter(tournament_id=self.pk, tournament_level=level)) def get_games_id(self): return list(TournamentGamesModel.objects.filter(tournament_id=self.pk)) def get_players_id(self): lst: [int] = [] for game_id in self.get_games_id(): lst.append(GameMembersModel.objects.filter(game_id=game_id)) return lst class TournamentGamesModel(models.Model): tournament_id = models.IntegerField() tournament_level = models.IntegerField() game_id = models.IntegerField() class TournamentMember(AbstractRoomMember): def __init__(self, user_id: int, socket: WebsocketConsumer, tournament): super().__init__(user_id, socket) self.participate = False self.tournament = tournament def receive(self, text_data: str = None, byte_dates: bytes = None): if (text_data is None): return data: dict = json.loads(text_data) self.update_participate(data.get("participate", self.participate)) def update_participate(self, new_participate: bool): if (self.participate == new_participate): return self.participate = new_participate self.tournament.update_participants() class TournamentRoom(AbstractRoom): def __init__(self, room_manager, tournament_id: int): super().__init__(room_manager) self.tournament_id = tournament_id self.definitive_participant_list = [] self.started = False self.model = TournamentModel.objects.get(pk=tournament_id) def start(self): self.broadcast("tournament_start") def update_participants(self): nb_participants = self.get_nb_participants() self.broadcast("update_participants", {"nb_participants": nb_participants}) if (nb_participants == self.model.nb_players): self.start() def get_nb_participants(self): nb_participants = 0 for member in self._member_list: member: TournamentMember if (member.participate): nb_participants += 1 return nb_participants def get_participants(self): return [member.user_id for member in self._member_list if member.participate] def start(self): self.started = True def append(self, member: TournamentMember): super().append(member) member.send("nb_participants", {"nb_participants": self.get_nb_participants()}) class TournamentRoomManager(AbstractRoomManager): def get(self, tournament_id: int): for room in self._room_list: if (room.tournament_id == tournament_id): return room if (TournamentModel.objects.filter(pk = tournament_id).exists()): room = TournamentRoom(self, tournament_id) self.append(room) return room return None tournament_manager: TournamentRoomManager = TournamentRoomManager()