add: tournament: graph de bz
This commit is contained in:
@ -32,7 +32,7 @@ class TournamentModel(models.Model):
|
||||
def get_games_id(self):
|
||||
return [tournament_game.game_id for tournament_game in TournamentGamesModel.objects.filter(tournament_id = self.pk)]
|
||||
|
||||
def get_players_id(self):
|
||||
def get_participants_id(self):
|
||||
return [model.participant_id for model in TournamentParticipantsModel.objects.filter(tournament_id=self.pk)]
|
||||
|
||||
def is_a_participant(self, participant_id: int):
|
||||
@ -134,7 +134,7 @@ class TournamentRoom(AbstractRoom):
|
||||
if self.tournament.started:
|
||||
member.participate = self.tournament.is_a_participant(member.user_id)
|
||||
member.send_participating()
|
||||
member.send("nb_participants", {"nb_participants": self.get_nb_participants()})
|
||||
self.broadcast("update_participants", {"participants": [self.get_participants_id()]})
|
||||
|
||||
class TournamentRoomManager(AbstractRoomManager):
|
||||
|
||||
|
@ -1,11 +1,20 @@
|
||||
from rest_framework import serializers
|
||||
from .models import TournamentModel
|
||||
|
||||
from django.db.models.query import QuerySet
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from .models import TournamentModel, tournament_manager
|
||||
|
||||
from profiles.models import ProfileModel
|
||||
from profiles.serializers import ProfileSerializer
|
||||
from games.serializers import GameSerializer
|
||||
|
||||
class TournamentSerializer(serializers.ModelSerializer):
|
||||
|
||||
levels = serializers.SerializerMethodField(read_only=True, required=False)
|
||||
state = serializers.SerializerMethodField(read_only=True, required=False)
|
||||
participants = serializers.SerializerMethodField(read_only=True, required=False)
|
||||
level = serializers.ReadOnlyField()
|
||||
started = serializers.ReadOnlyField()
|
||||
finished = serializers.ReadOnlyField()
|
||||
@ -13,7 +22,32 @@ class TournamentSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = TournamentModel
|
||||
fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels", "id", "state"]
|
||||
fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels", "id", "state", "participants"]
|
||||
|
||||
def get_participants(self, instance: TournamentModel):
|
||||
|
||||
participants_id: list[ProfileModel]
|
||||
|
||||
if (instance.started):
|
||||
participants_id = instance.get_participants_id()
|
||||
else:
|
||||
participants_id = tournament_manager.get(instance.pk).get_participants_id()
|
||||
|
||||
participants_profile: list[ProfileModel] = []
|
||||
for participant_id in participants_id:
|
||||
query: QuerySet = ProfileModel.objects.filter(user_id = participant_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": participants_id
|
||||
}
|
||||
participants_profile.append(profile_data)
|
||||
|
||||
return participants_profile
|
||||
|
||||
def get_state(self, instance: TournamentModel):
|
||||
return ["waiting", "started", "finished"][instance.started + instance.finished]
|
||||
@ -31,6 +65,7 @@ class TournamentSerializer(serializers.ModelSerializer):
|
||||
if (value < 2):
|
||||
raise serializers.ValidationError("The numbers of players must be greather than 2.")
|
||||
return value
|
||||
|
||||
def validate_nb_players_by_game(self, value: int):
|
||||
if (value < 2):
|
||||
raise serializers.ValidationError("The numbers of players by game must be greather than 2.")
|
||||
|
Reference in New Issue
Block a user