Esempio n. 1
0
def move(request):
    if request.method == 'POST':
        player = request.user
        try:
            game_id = request.session[constants.GAME_SELECTED_SESSION_ID]
        except KeyError:
            return HttpResponseNotFound('')
        try:
            game = Game.objects.get(id=game_id)
        except Game.DoesNotExist:
            return HttpResponseNotFound('')

        move_form = MoveForm(data=request.POST)
        origin = int(request.POST.get('origin'))
        target = int(request.POST.get('target'))
        try:
            Move.objects.create(
                game=game, player=player, origin=origin, target=target)
        except ValidationError:
            move_form.add_error('target', 'Invalid Move: Try again')
            render(request, "mouse_cat/game.html", {'move_form': move_form})

        tablero = ([0] * (Game.MAX_CELL - Game.MIN_CELL + 1))
        tablero[game.mouse] = -1
        tablero[game.cat1] = tablero[game.cat2] = 1
        tablero[game.cat3] = tablero[game.cat4] = 1
        move_form = MoveForm()
        context_dict = {'game': game, 'board': tablero, 'move_form': move_form}
        return render(request, "mouse_cat/game.html", context_dict)
    else:
        return HttpResponseNotFound('')
Esempio n. 2
0
def move(request):
    """
    move (main author: Alejandro Santorum)
    ----------
    Input parameters:
        request: received request. It also contains player, selected game
            information and origin and target of the movement. Player and
            game information are included in session variable. Origin and
            target can be found at the POST body parameters.
    ----------
    Returns:
        It renders "mouse_cat/show_game.html" template; or Error 404 if an
        invalid method is used.
    ----------
    Raises:
        None
    ----------
    Description:
        It develops a movement of a given player in the selected game.
        User is required to be logged.
    """
    if request.method == 'GET':
        return HttpResponse('Invalid method.', status=404)
    # POST
    if not request.session.get(constants.GAME_SELECTED_SESSION_ID):
        return HttpResponse('Invalid method.', status=404)
    game_id = request.session.get(constants.GAME_SELECTED_SESSION_ID)
    game = Game.objects.get(id=game_id)

    origin = int(request.POST.get('origin'))
    target = int(request.POST.get('target'))
    move_form = MoveForm(data=request.POST)
    move = Move(origin=origin, target=target, game=game, player=request.user)
    try:
        move.save()
    except ValidationError as err:
        move_form.add_error('origin', err.messages[0])
        board = [0] * constants.BOARD_SIZE
        board[game.mouse] = -1
        for i in game._get_cat_places():
            board[i] = 1
        context_dict = {'board': board, 'game': game, 'move_form': move_form}
        return render(request, "mouse_cat/game.html", context_dict)
    return redirect(reverse('show_game'))