2022-06-06 20:15:45 +03:00
|
|
|
from abc import ABC
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
from rest_framework.exceptions import ValidationError
|
|
|
|
|
|
|
|
from game.models import Hero, Player, HeroInDeck, Deck
|
|
|
|
|
|
|
|
|
|
|
|
class CreateHeroSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Hero
|
|
|
|
fields = (
|
|
|
|
"type",
|
|
|
|
"idle_img",
|
|
|
|
"attack_img",
|
|
|
|
"die_img",
|
|
|
|
"health",
|
|
|
|
"attack",
|
|
|
|
"speed",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class GetHeroSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Hero
|
|
|
|
fields = (
|
|
|
|
"added",
|
|
|
|
"type",
|
|
|
|
"idle_img",
|
|
|
|
"attack_img",
|
|
|
|
"die_img",
|
|
|
|
"health",
|
|
|
|
"attack",
|
|
|
|
"speed",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ListHeroSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Hero
|
|
|
|
fields = (
|
|
|
|
"uuid",
|
|
|
|
"type",
|
|
|
|
"idle_img",
|
|
|
|
"attack_img",
|
|
|
|
"die_img",
|
|
|
|
"health",
|
|
|
|
"attack",
|
|
|
|
"speed",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CreatePlayerSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Player
|
|
|
|
fields = ("ton_wallet", "name")
|
|
|
|
|
|
|
|
|
2022-06-07 23:54:26 +03:00
|
|
|
class CreateDeckSerializer(serializers.ModelSerializer):
|
2022-06-06 20:15:45 +03:00
|
|
|
hero_ids = serializers.ListSerializer(
|
|
|
|
child=serializers.UUIDField(), min_length=16, max_length=16
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Deck
|
|
|
|
fields = ("hero_ids",)
|
|
|
|
|
|
|
|
def validate_hero_ids(self, value):
|
2022-06-07 23:54:26 +03:00
|
|
|
if self.context["request"].method == "POST":
|
|
|
|
for x in value:
|
|
|
|
if not (hero := Hero.objects.filter(uuid=x)):
|
|
|
|
raise ValidationError(f"Hero with uuid {x} doesn't exist")
|
|
|
|
|
|
|
|
if deck := HeroInDeck.objects.filter(hero=hero.first()):
|
|
|
|
raise ValidationError(
|
|
|
|
f"Hero with uuid {x} is already in deck with id {deck.first().deck.id}"
|
|
|
|
)
|
|
|
|
elif self.context["request"].method in ["PUT", "PATCH"]:
|
|
|
|
print(value)
|
2022-06-06 20:15:45 +03:00
|
|
|
return value
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
deck = Deck.objects.create(player=self.context["request"].user)
|
|
|
|
for x in validated_data["hero_ids"]:
|
|
|
|
HeroInDeck.objects.create(hero_id=x, deck=deck)
|
|
|
|
return deck
|
|
|
|
|
2022-06-07 23:54:26 +03:00
|
|
|
def update(self, instance, validated_data):
|
|
|
|
print(instance, validated_data)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
class GetPlayerSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Player
|
|
|
|
fields = ("id", "name")
|
|
|
|
|
2022-06-06 20:15:45 +03:00
|
|
|
|
|
|
|
class GetDeckSerializer(serializers.ModelSerializer):
|
2022-06-07 23:54:26 +03:00
|
|
|
player = GetPlayerSerializer()
|
2022-06-06 20:15:45 +03:00
|
|
|
heroes = ListHeroSerializer(many=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Deck
|
2022-06-07 23:54:26 +03:00
|
|
|
fields = ("player", "heroes")
|