def game_status(request, game_id): game = Game.objects.filter(id=game_id) if not game: error_message = "You have an invalid game id!" logger.error(error_message) return Response(error_message, status=400) game = game.first() if game.success: resp = { 'data': GameSerializer(game), 'message': "CONGRATULATIONS! Your game has been solved!".format( word=game.word.name, attempts=game.attempts) } return Response(resp, status=200) if not game.success and game.attempts < len(game.word.name): msg = "Your game is not complete! Keep guessing" return Response({ 'data': GameInProgressSerializer(game), 'message': msg}, status=200) if not game.success and game.attempts >= len(game.word.name): msg = "Your game is complete! But you FAILED" return Response({ 'data': GameSerializer(game), 'message': msg}, status=200)
def create(self, request): #for creating game user = request.user serializer = GameSerializer(data=request.data) if (serializer.is_valid()): serializer.save(instructor=user) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
def create_game(request): try: data = JSONParser().parse(request) except ParseError: return HttpResponse(status=400) serializer = GameSerializer(data=data, context={'request': request}) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=status.HTTP_201_CREATED) else: return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): game = self.get_object(pk) serializer = GameSerializer(game, data=request.data) if serializer.is_valid(): serializer.save() newGame = self.get_object(pk) content = { 'mineFieldSetup': newGame.mineFieldSetup, 'turn': newGame.turn, 'player1score': newGame.player1score, 'player2score': newGame.player2score } pusher_client.trigger((str(pk) + '_game'), 'event', content) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def game_details(request, id): try: game = Game.objects.get(id=id) except: return Response({"error": "Error getting games"}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': return Response(GameSerializer(game).data, status=status.HTTP_200_OK) elif request.method == 'PUT': try: category = Category.objects.get(id=request.data.get('id')) except: return Response( {"error": "There is an error reading category from database"}, status=status.HTTP_404_NOT_FOUND) game.name = request.data.get('name') game.description = request.data.get('description') game.image = request.data.get('image') game.category = category game.price = request.data.get('price') game.screenshots = request.data.get('screenshots') game.text = request.data.get('text') game.save() return Response({"message": "Game update"}, status=status.HTTP_200_OK) elif request.method == 'DELETE': game.delete() return Response({"message": "game delete"}, status=status.HTTP_200_OK)
def get(self, request): try: games = Game.objects.all() serializer = GameSerializer(games, many=True) return Response(serializer.data, status=status.HTTP_200_OK) except: return Response({"error": "Error getting games"}, status=status.HTTP_404_NOT_FOUND)
def game_guess_word(request, game_id): game = Game.objects.filter(id=game_id) if not game: error_message = "You have an invalid game id!" return Response(error_message, status=400) game = game.first() data = request.data if not data.get('guess'): msg = {'data': GameInProgressSerializer(game), 'message': "Your post must include a guess!"} return Response(msg, status=200) guess = data.get('guess').lower().strip() if guess == game.word.name: game.success = True game.guess = guess game.save() resp = { 'data': GameSerializer(game), 'message': "CONGRATULATIONS! Your game has been solved!".format( word=game.word.name, attempts=game.attempts) } return Response(resp, status=200) else: game.guess = guess game.save() msg = "Your game is complete! But you FAILED" return Response({ 'data': GameSerializer(game), 'message': msg}, status=200)
def games(request: Request): from api.serializers import GameSerializer client_id = request.user.id try: client = Client.objects.get(id=client_id) except Client.DoesNotExist: raise APIException("Client does not exist") game_filter = request.GET.get('status', 'any') games = Game.objects.filter(player__client=client).order_by( '-date_started' if game_filter == 'O' else '-date_ended') if game_filter != 'any': games = games.filter(status=game_filter) return Response(status=status.HTTP_200_OK, data=map(lambda game: GameSerializer(game).data, games))
def game_list(request): if request.method == 'GET': try: games = Game.objects.all() serializer = GameSerializer(games, many=True) return JsonResponse(serializer.data, safe=False) except: return JsonResponse({"status":"505"}, safe=False) if request.method == 'POST': try: category = Category.objects.get(name=request.data['category']) except: return JsonResponse({"status":"505"}, safe=False) Game.objects.create( category = category, name = request.data['name'], description = request.data['description'], image = request.data['image'], requirements = request.data['requirements'] ) return JsonResponse({"status": "200"}, safe=False)
def send_game_update(self): """ Send the updated game information and squares to the game's channel group :return: """ # imported here to avoid circular import from api.serializers import GameSquareSerializer, GameLogSerializer, GameSerializer squares = self.get_all_game_squares() square_serializer = GameSquareSerializer(squares, many=True) # get game log log = self.get_game_log() log_serializer = GameLogSerializer(log, many=True) game_serializer = GameSerializer(self) message = { 'game': game_serializer.data, 'log': log_serializer.data, 'squares': square_serializer.data } game_group = 'game-{0}'.format(self.id) Group(game_group).send({'text': json.dumps(message)})
def get(self, request, access_id): game = Game.objects.filter( Q(access_a=access_id) | Q(access_b=access_id)).first() if game is None: return Response({}) return Response(GameSerializer(game).data)
def game(request: Request, pk): from api.serializers import GameSerializer obj = Game.objects.get(id=pk) return Response(status=status.HTTP_200_OK, data=GameSerializer(obj).data)
def get(self, request, pk, *args, **kwargs): if Game.objects.filter(pk=self.kwargs.get('pk')).exists(): game = Game.objects.get(pk=self.kwargs.get('pk')) serializer = GameSerializer(game) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, pk, format=None): game = self.get_object(pk) serializer = GameSerializer(game) return Response(serializer.data)
def get_serialized_game(uuid): game = Game.objects.get(uuid=uuid) return GameSerializer(game).data
def retrieve(self, request, pk=None): queryset = Game.objects.all() game = get_object_or_404(queryset, pk=pk) serializer = GameSerializer(game) return Response(serializer.data)
def games_list(request): games = Game.objects.all() serializer = GameSerializer(games, many=True, context={'request': request}) return JsonResponse(serializer.data, safe=False, status=status.HTTP_200_OK)
def list(self, request): queryset = Game.objects.all().filter(instructor=request.user) serializer = GameSerializer(queryset, many=True) return Response(serializer.data)
def all(self, request): allgame = Game.objects.all() serialized = GameSerializer(allgame, many=True) return Response(serialized.data)
def get(self, request, id): game = self.get_game(id) serializer = GameSerializer(game) return JsonResponse(serializer.data, safe=False)
def game_detail(request, game): serializer = GameSerializer(game, context={'request': request}) return JsonResponse(serializer.data, status=status.HTTP_200_OK)