Example #1
0
def move_service(request):
    request.session['playhead'] = -1
    # Check if there's a user
    if not request.user:
        return redirect(reverse('login'))

    player = request.user

    if 'game_id' in request.session.keys():
        game_id = request.session['game_id']
        game = Game.objects.get(id=game_id)

        if request.method == 'POST':
            movement = MoveForm(request.POST)
            if movement.is_valid():
                try:
                    Move.objects.create(game=game,
                                        player=player,
                                        origin=int(movement.data['origin']),
                                        target=int(movement.data['target']))
                except ValidationError:
                    print("Error: Invalid move")
            else:
                print("Error: Invalid move")
            return redirect(reverse('show_game'))

    return HttpResponseNotFound(constants.ERROR_NOT_FOUND)
Example #2
0
def move_service(request):

    if request.method == 'POST':

        move_form = MoveForm(data=request.POST)

        if move_form.is_valid():
            user = request.user
            if 'game_selected' in request.session:
                game = Game.objects.get(id=request.session['game_selected'])
            else:
                raise Http404("ERROR 404")
            try:
                move = Move(origin=request.POST.get('origin'),
                            target=request.POST.get('target'),
                            game=game,
                            player=user)
                move.save()

                return redirect(reverse('show_game'))

            except ValidationError:
                return redirect(reverse('show_game'))
        else:
            print(move_form.errors)
            return redirect(reverse('landing'))

    raise Http404("ERROR 404")
Example #3
0
def move(request):
    # Author: David Cabornero
    if request.method == 'POST':
        form = MoveForm(data=request.POST)
        # Comprobamos si el movimiento es válido en cuanto a rango de valores
        if form.is_valid():
            move = form.save(commit=False)
            move.player = request.user
            # Comprobamos si hay un juego seleccionado
            try:
                pk = request.session[constants.GAME_SELECTED_SESSION_ID]
                game = Game.objects.get(pk=pk)
            except KeyError:
                return JsonResponse({'valid': 0, 'winner': None})
            move.game = game
            # Comprobamos si el movimiento es completamente válido en cuanto
            # a modelo y lógica
            try:
                move.save()
            except ValidationError:
                return JsonResponse({'valid': 0, 'winner': None})
            return JsonResponse({'valid': 1, 'winner': Game.finish(game)})
        else:
            # Imprimimos los errores del formulario
            return JsonResponse({'valid': 0, 'winner': None})
    # No debería ser posible acceder a esta función mediante un método distinto
    # de POST. En caso de que ocurra, hacemos como que no lo hemos visto
    else:
        return JsonResponse({'valid': 0, 'winner': None})
Example #4
0
def move(request):
    if 'game_id' in request.session.keys():
        game_id = request.session['game_id']

        if request.method == 'POST':
            game = Game.objects.get(id=game_id)
            if game.status == GameStatus.FINISHED:
                redirect(reverse('index'))
            move_form = MoveForm(game=game, data=request.POST)
            if move_form.is_valid():
                move = Move(
                    game=game, player=request.user, origin=int(
                        move_form.data['origin']),
                    target=int(move_form.data['target']))
                move.save()
                if game.status == GameStatus.FINISHED:
                    return HttpResponse('<h1>You won</h1> <p><a href="\">Return to homepage</a></p>')
            return redirect(reverse('show_game'))

    return HttpResponseNotFound('<h1>Page Not Found</h1>')
Example #5
0
def move_service(request):
    if request.method == 'POST':
        if constants.GAME_SELECTED_SESSION_ID not in request.session:
            return HttpResponseNotFound("Not Found")
        else:
            moveform = MoveForm(data=request.POST)

            if moveform.is_valid():
                game_id = request.session[constants.GAME_SELECTED_SESSION_ID]
                game = Game.objects.get(id=game_id)
                try:
                    Move.objects.create(game=game, player=request.user,
                                        origin=moveform.cleaned_data['origin'],
                                        target=moveform.cleaned_data['target'])
                except ValidationError:
                    print('Error en el movimiento')
                finally:
                    return redirect(reverse('show_game'))

    return HttpResponseNotFound("Forbidden Get")
Example #6
0
def move_service(request):
    player = request.user
    if 'game_id' in request.session.keys():
        game_id = request.session['game_id']
        game = Game.objects.get(id=game_id)

        if request.method == 'POST':
            if not player:
                redirect(reverse('login'))

            move_form = MoveForm(data=request.POST)

            if move_form.is_valid():
                move = Move(game=Game.objects.get(id=game_id),
                            player=player,
                            origin=move_form.data['origin'],
                            target=move_form.data['target'])
                move.save()

            return redirect(reverse('show_game'))

    return HttpResponseNotFound('<h1>Page Not Found</h1>')