def create(self, request): serializer = GameSessionSerializer(data=request.data) if serializer.is_valid(): game = serializer.validated_data['game'] user = serializer.validated_data['user'] if (game.id in GameOwnership.objects.filter( user=user).values_list('game__id', flat=True)) or \ (self.request.user.is_staff): # User owns the game controller = GameSession.join_game(self, game) if(controller['controllerid'] != -1): #Create game session session = GameSession.objects.create( game=game, user=user, controller=controller['controllerid'], streaming_port=controller['streaming_port'] ) serializer = GameSessionSerializer(session) return Response( serializer.data, status=status.HTTP_201_CREATED ) return Response(status=status.HTTP_400_BAD_REQUEST)
def create(self, request): serializer = GameSessionSerializer(data=request.data) response_data = {} if serializer.is_valid(): game = serializer.validated_data['game'] user = serializer.validated_data['user'] # User has access over the game if (game.id in GameOwnership.objects.filter( user=user).values_list('game__id', flat=True)) or \ (self.request.user.is_staff): session = GameSession.join_game(game, user) # User can play using the valid id if(session != None): serializer = GameSessionSerializer(session) return Response( serializer.data, status=status.HTTP_201_CREATED ) else: response_data['message'] = \ 'Game\'s limit has been reached, try again later.' else: response_data['message'] = 'User does not have access for the game' return Response( response_data, status=status.HTTP_403_FORBIDDEN ) else: response_data['message'] = 'The request data is not valid' return Response( response_data, status=status.HTTP_400_BAD_REQUEST )