How can I structure a Django REST API for a lot of similar online games?
I am very new to Django and the Django REST Framework and I want to implement an API for 4 relatively similar games. The most basic game consists of a player labelling an image and receiving points for this if they enter the same labels as their co-player for the same image. One game session consists of 3 rounds.
What I have done so far is create a view for the game type, game session, game round, image to be shown and the labels, which will be divided into Taggings (a user has used this label as input) and Tags (more than one user has entered this very same label for the same picture).
All of those views look similar to the Gametype and Tagging views below.
"""
API View that handles retrieving the correct type of a game
"""
serializer_class = GametypeSerializer
def get_queryset(self):
gametypes = Gametype.objects.all().order_by("name")
return gametypes
def get(self, request, *args, **kwargs):
gametype = self.get_queryset()
serializer = GametypeSerializer(gametype, many=True)
return Response(serializer.data)
class Tagging(APIView):
"""
API View to do everything to do with taggings
"""
serializer_class = TaggingSerializer
def get_queryset(self):
taggings = Tagging.objects.all().filter(resource=8225)
return taggings
def get(self, request, *args, **kwargs):
tagging = self.get_queryset()
serializer = TaggingSerializer(tagging, many=True)
return Response(serializer.data)
def post(self, request, *args, **kwargs):
tagging = request.data.get_queryset()
serializer = TaggingSerializer(data=tagging)
if serializer.is_valid(raise_exception=True):
saved_tagging = serializer.save()
return Response(saved_tagging)
The Tag Tagging and Gamesession views will also need POST requests on top of this (I am still working on those). I am also only doing the backend for this web app so I can only test all of this with a GUI I have written in Python (with tkinter) so far. What I am struggling with is: how do I connect the views with each other, such that the game logic works properly? Or do I have to write one view per game and write a separate serialised for this? Better said - how do I make the backend of the game functional with those views? What am I missing?
if i was doing it i implement like this
def check_the_player_token(token):
# check logic here ...
if valid_user:
return True, user_token
else:
return False, ''
class Tagging(viewsets.ViewSet):
# post requests
def create(self, request):
# check the player of the token
success, user = check_the_player_token(token)
# check if the player is in this game
# check if round is <= 3
# check the lable of the other player in database or if the label of other player wasn't added yet pass
# get this player label and send to serializer
serializer = TaggingLabelSerializer(data=request.data)
if serializer.is_valid():
# save into database
serializer.save()
# update the current round
# check if the label is right or wrong
# return the response
else:
# return a error
The viewset tagging, or your case APIView will get the post request, and use functions like check_the_player_token to do actions needed