2022-06-04 19:30:54 +03:00
|
|
|
from rest_framework import status
|
2022-06-04 16:01:23 +03:00
|
|
|
|
2022-06-04 19:30:54 +03:00
|
|
|
from rest_framework.generics import GenericAPIView, UpdateAPIView
|
2022-06-06 20:15:45 +03:00
|
|
|
from rest_framework.mixins import (
|
|
|
|
CreateModelMixin,
|
|
|
|
RetrieveModelMixin,
|
|
|
|
ListModelMixin,
|
|
|
|
DestroyModelMixin,
|
|
|
|
UpdateModelMixin,
|
|
|
|
)
|
2022-06-04 19:30:54 +03:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2022-06-05 13:09:27 +03:00
|
|
|
from game.authentication import PlayerAuthentication
|
2022-06-07 23:54:26 +03:00
|
|
|
from game.models import Hero, Deck
|
2022-06-06 20:15:45 +03:00
|
|
|
from game.api.v1.serializers import (
|
|
|
|
CreateHeroSerializer,
|
|
|
|
GetHeroSerializer,
|
|
|
|
CreatePlayerSerializer,
|
|
|
|
ListHeroSerializer,
|
2022-06-07 23:54:26 +03:00
|
|
|
CreateDeckSerializer,
|
2022-06-06 20:15:45 +03:00
|
|
|
GetDeckSerializer,
|
2022-06-10 23:33:40 +03:00
|
|
|
ObtainTokenPairSerializer,
|
2022-06-06 20:15:45 +03:00
|
|
|
)
|
2022-06-05 13:09:27 +03:00
|
|
|
from game.services.jwt import sign_jwt
|
2022-06-04 19:30:54 +03:00
|
|
|
|
|
|
|
|
2022-06-06 20:15:45 +03:00
|
|
|
class ListCreateHeroView(GenericAPIView, CreateModelMixin, ListModelMixin):
|
2022-06-05 13:09:27 +03:00
|
|
|
authentication_classes = (PlayerAuthentication,)
|
2022-06-04 19:30:54 +03:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
return serializer.save()
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
instance = self.perform_create(serializer)
|
|
|
|
return Response({"uuid": instance.uuid}, status=status.HTTP_201_CREATED)
|
|
|
|
|
2022-06-05 13:09:27 +03:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.request.method == "GET":
|
2022-06-06 20:15:45 +03:00
|
|
|
return ListHeroSerializer
|
2022-06-05 13:09:27 +03:00
|
|
|
else:
|
|
|
|
return CreateHeroSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Hero.objects.filter(player_id=self.request.user.id)
|
|
|
|
|
2022-06-04 19:30:54 +03:00
|
|
|
|
|
|
|
class RetrieveHeroView(RetrieveModelMixin, UpdateAPIView, GenericAPIView):
|
|
|
|
serializer_class = GetHeroSerializer
|
|
|
|
lookup_field = "uuid"
|
|
|
|
queryset = Hero.objects.all()
|
|
|
|
|
2022-06-11 23:34:55 +03:00
|
|
|
def get_authenticators(self):
|
|
|
|
if self.request.method != "GET":
|
|
|
|
self.authentication_classes = [PlayerAuthentication]
|
|
|
|
return [auth() for auth in self.authentication_classes]
|
|
|
|
|
2022-06-04 19:30:54 +03:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.retrieve(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def put(self, request, *args, **kwargs):
|
|
|
|
return self.update(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def patch(self, request, *args, **kwargs):
|
|
|
|
return self.partial_update(request, *args, **kwargs)
|
2022-06-05 13:09:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
class PlayerCreateView(GenericAPIView, CreateModelMixin):
|
2022-06-06 20:15:45 +03:00
|
|
|
serializer_class = CreatePlayerSerializer
|
2022-06-05 13:09:27 +03:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
return serializer.save()
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
instance = self.perform_create(serializer)
|
|
|
|
|
2022-06-25 12:36:47 +03:00
|
|
|
access_jwt = instance.get_access_token()
|
|
|
|
refresh_jwt = instance.get_refresh_token()
|
2022-06-06 20:15:45 +03:00
|
|
|
return Response(
|
2022-06-08 19:03:38 +03:00
|
|
|
{
|
|
|
|
"access_token": access_jwt,
|
|
|
|
"refresh_token": refresh_jwt,
|
|
|
|
"deck_id": instance.get_last_deck().id,
|
|
|
|
},
|
2022-06-06 20:15:45 +03:00
|
|
|
status=status.HTTP_201_CREATED,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DeckCreateView(GenericAPIView, CreateModelMixin):
|
2022-06-07 23:54:26 +03:00
|
|
|
serializer_class = CreateDeckSerializer
|
2022-06-06 20:15:45 +03:00
|
|
|
authentication_classes = (PlayerAuthentication,)
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
return serializer.save(player=self.request.user)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
instance = self.perform_create(serializer)
|
|
|
|
heroes_list = ListHeroSerializer(instance.get_heroes(), many=True)
|
2022-06-08 19:03:38 +03:00
|
|
|
heroes_list.data["deck_id"] = instance.id
|
2022-06-06 20:15:45 +03:00
|
|
|
return Response(heroes_list.data, status=status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
|
|
|
|
class RetireUpdateDeleteDeckView(
|
|
|
|
RetrieveHeroView, DestroyModelMixin, UpdateModelMixin, GenericAPIView
|
|
|
|
):
|
2022-06-07 23:54:26 +03:00
|
|
|
lookup_field = "id"
|
|
|
|
queryset = Deck.objects.all()
|
2022-06-06 20:15:45 +03:00
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.request.method == "GET":
|
|
|
|
return GetDeckSerializer
|
2022-06-07 23:54:26 +03:00
|
|
|
else:
|
|
|
|
return CreateDeckSerializer
|
2022-06-06 20:15:45 +03:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.retrieve(request, *args, **kwargs)
|
2022-06-07 23:54:26 +03:00
|
|
|
|
2022-06-10 01:12:45 +03:00
|
|
|
def perform_update(self, serializer):
|
|
|
|
return serializer.update(self.get_object(), self.request.data)
|
2022-06-07 23:54:26 +03:00
|
|
|
|
2022-06-10 01:12:45 +03:00
|
|
|
def put(self, request, *args, **kwargs):
|
|
|
|
if not self._check_user_identity(kwargs["id"]):
|
2022-06-08 19:03:38 +03:00
|
|
|
return Response(
|
|
|
|
"Attempt to change another user's deck",
|
|
|
|
status=status.HTTP_403_FORBIDDEN,
|
|
|
|
)
|
2022-06-10 01:12:45 +03:00
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
instance = self.perform_update(serializer)
|
|
|
|
heroes_list = ListHeroSerializer(instance.get_heroes(), many=True)
|
|
|
|
return Response(heroes_list.data, status=status.HTTP_200_OK)
|
2022-06-07 23:54:26 +03:00
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
2022-06-10 01:12:45 +03:00
|
|
|
if not self._check_user_identity(kwargs["id"]):
|
2022-06-08 19:03:38 +03:00
|
|
|
return Response(
|
|
|
|
"Attempt to delete another user's deck",
|
|
|
|
status=status.HTTP_403_FORBIDDEN,
|
|
|
|
)
|
2022-06-10 01:12:45 +03:00
|
|
|
self.destroy(request, *args, **kwargs)
|
|
|
|
return Response(
|
|
|
|
f"Destroyed deck with id {kwargs['id']}", status=status.HTTP_200_OK
|
|
|
|
)
|
2022-06-08 19:03:38 +03:00
|
|
|
|
2022-06-10 01:12:45 +03:00
|
|
|
def _check_user_identity(self, deck_id) -> bool:
|
2022-06-08 19:03:38 +03:00
|
|
|
return deck_id in list(
|
2022-06-10 01:12:45 +03:00
|
|
|
Deck.objects.filter(player_id=self.request.user.id).values_list(
|
|
|
|
"id", flat=True
|
|
|
|
)
|
2022-06-08 19:03:38 +03:00
|
|
|
)
|
2022-06-10 23:33:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class RefreshAuthKey(GenericAPIView):
|
|
|
|
serializer_class = ObtainTokenPairSerializer
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2022-06-11 23:34:55 +03:00
|
|
|
access_jwt = sign_jwt(
|
|
|
|
{"id": serializer.player_id, "type": "access"}, t_life=3600
|
|
|
|
)
|
2022-06-10 23:33:40 +03:00
|
|
|
return Response({"access_token": access_jwt}, status=status.HTTP_200_OK)
|