tournament in coming
This commit is contained in:
@ -4,7 +4,7 @@ from django.db.models.query import QuerySet
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from .models import TournamentModel, tournament_manager
|
||||
from .models import TournamentModel
|
||||
|
||||
from profiles.models import ProfileModel
|
||||
from profiles.serializers.ProfileSerializer import ProfileSerializer
|
||||
@ -12,9 +12,8 @@ from games.serializers import GameSerializer
|
||||
|
||||
class TournamentSerializer(serializers.ModelSerializer):
|
||||
|
||||
rounds = serializers.SerializerMethodField(read_only=True, required=False)
|
||||
state = serializers.SerializerMethodField(read_only=True, required=False)
|
||||
players = serializers.SerializerMethodField(read_only=True, required=False)
|
||||
participants = serializers.SerializerMethodField(read_only=True, required=False)
|
||||
round = serializers.ReadOnlyField()
|
||||
started = serializers.ReadOnlyField()
|
||||
finished = serializers.ReadOnlyField()
|
||||
@ -22,57 +21,25 @@ class TournamentSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = TournamentModel
|
||||
fields = ["name", "nb_players", "nb_players_by_game", "round", "started", "finished", "rounds", "id", "state", "players"]
|
||||
|
||||
def get_players(self, instance: TournamentModel):
|
||||
|
||||
players_id: list[ProfileModel]
|
||||
|
||||
if (instance.started):
|
||||
players_id = instance.get_players_id()
|
||||
else:
|
||||
players_id = tournament_manager.get(instance.pk).get_players_id()
|
||||
|
||||
players_profile: list[ProfileModel] = []
|
||||
for player_id in players_id:
|
||||
query: QuerySet = ProfileModel.objects.filter(user_id = player_id)
|
||||
profile_data: dict
|
||||
if query.exists():
|
||||
profile_data = ProfileSerializer(query[0]).data
|
||||
else:
|
||||
profile_data = {
|
||||
"username": "deleted_user",
|
||||
"avatar": "/static/avatars/default.avif",
|
||||
"user_id": players_id
|
||||
}
|
||||
players_profile.append(profile_data)
|
||||
|
||||
return players_profile
|
||||
|
||||
fields = ["name", "nb_participants", "round", "started", "finished", "id", "state", "participants"]
|
||||
|
||||
def get_participants(self, instance: TournamentModel):
|
||||
return ProfileSerializer(instance.get_participants(), many=True).data
|
||||
|
||||
def get_state(self, instance: TournamentModel):
|
||||
return ["waiting", "started", "finished"][instance.started + instance.finished]
|
||||
|
||||
def get_rounds(self, instance: TournamentModel):
|
||||
rounds: list[list[int]] = []
|
||||
for i in range(instance.round):
|
||||
games_id: list[int] = instance.get_games_id_by_round(i)
|
||||
if (games_id == []):
|
||||
break
|
||||
rounds.append(games_id)
|
||||
return rounds
|
||||
|
||||
def validate_nb_players(self, value: int):
|
||||
def validate_nb_participants(self, value: int):
|
||||
if (value < 2):
|
||||
raise serializers.ValidationError("The numbers of players must be greather than 2.")
|
||||
raise serializers.ValidationError("The numbers of participants must be greather than 2.")
|
||||
return value
|
||||
|
||||
def validate_nb_players_by_game(self, value: int):
|
||||
def validate_nb_participants_by_game(self, value: int):
|
||||
if (value < 2):
|
||||
raise serializers.ValidationError("The numbers of players by game must be greather than 2.")
|
||||
nb_players: str = self.initial_data.get("nb_players")
|
||||
if (nb_players is not None and nb_players.isnumeric()):
|
||||
nb_players: int = int(nb_players)
|
||||
if (value > nb_players):
|
||||
raise serializers.ValidationError("The numbers of players by game must be smaller than the numbers of players.")
|
||||
raise serializers.ValidationError("The numbers of participants by game must be greather than 2.")
|
||||
nb_participants: str = self.initial_data.get("nb_participants")
|
||||
if (nb_participants is not None and nb_participants.isnumeric()):
|
||||
nb_participants: int = int(nb_participants)
|
||||
if (value > nb_participants):
|
||||
raise serializers.ValidationError("The numbers of participants by game must be smaller than the numbers of participants.")
|
||||
return value
|
Reference in New Issue
Block a user