ft_transcendence/tournament/models.py

47 lines
1.4 KiB
Python
Raw Normal View History

2023-12-20 13:15:47 -05:00
from django.db import models
from games.models import GameModel
# Create your models here.tu
class TournamentModel(models.Model):
name = models.CharField()
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(self, nb_players: int, nb_players_by_game: int, name: str = ""):
self.level = 1
number: int = nb_players
while (number != nb_players_by_game):
number = number // 2 + (number % 2)
self.level += 1
self.nb_players = nb_players
self.nb_players_by_game = nb_players_by_game
self.name = name
return self.save()
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()