2023-12-26 12:24:23 -05:00
|
|
|
from rest_framework import serializers
|
|
|
|
from .models import GameModel, GameMembersModel
|
|
|
|
|
|
|
|
class GameSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
players_id = serializers.SerializerMethodField()
|
|
|
|
winner_id = serializers.ReadOnlyField()
|
|
|
|
state = serializers.SerializerMethodField()
|
|
|
|
started = serializers.ReadOnlyField()
|
|
|
|
finished = serializers.ReadOnlyField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = GameModel
|
|
|
|
fields = ["id", "winner_id", "state", "started", "finished", "players_id"]
|
|
|
|
|
|
|
|
def get_state(self, instance: GameModel):
|
|
|
|
if (instance.finished):
|
|
|
|
return "finished"
|
|
|
|
if (instance.started):
|
|
|
|
return "started"
|
|
|
|
return "waiting"
|
|
|
|
|
|
|
|
def get_players_id(self, instance: GameModel):
|
2023-12-28 05:33:06 -05:00
|
|
|
players_id = [player_game.player_id for player_game in GameMembersModel.objects.filter(game_id = instance.pk)]
|
|
|
|
return players_id
|