예제 #1
0
def selected(game_token, row, column):
    # db_game = Game.query.get(game_token)
    db_game = Game.query.get(game_token)
    if not db_game:
        abort(400, "That game doesn't exits.")
    board = db_game.board
    game = Chess(existing_board=board)
    index = (int(row), int(column))
    destinations = game.destinations(index)
    requested_index = get_requested_index(request)
    if requested_index:
        if requested_index in destinations:
            # move piece to requested index and re-direct to board
            if game.move(index, requested_index):
                db_game.board = game.export()
                db_game.save()
                url = "chess/game/{}/".format(game_token)
                return redirect(url)
            else:
                print("NOT A VALID MOVE!!!")
        elif game.destinations(requested_index):
            # redirect to a different selected route
            url = "chess/game/{}/selected/{}/{}/".format(
                game_token, requested_index[0], requested_index[1])
            return redirect(url)
        else:
            # redirect to board
            url = "chess/game/{}/".format(game_token)
            return redirect(url)
    return render_template('selected.html',
                           rows=8,
                           columns=8,
                           board=db_game.piece_locations,
                           images=piece_images,
                           destinations=destinations)
예제 #2
0
파일: routes.py 프로젝트: theovoss/ChessApi
def selected(game_token, row, column):
    # db_game = Game.query.get(game_token)
    db_game = Game.query.get(game_token)
    if not db_game:
        abort(400, "That game doesn't exits.")
    board = db_game.board
    game = Chess(existing_board=board)
    index = (int(row), int(column))
    destinations = game.destinations(index)
    requested_index = get_requested_index(request)
    if requested_index:
        if requested_index in destinations:
            # move piece to requested index and re-direct to board
            if game.move(index, requested_index):
                db_game.board = game.export()
                db_game.save()
                url = "chess/game/{}/".format(game_token)
                return redirect(url)
            else:
                print("NOT A VALID MOVE!!!")
        elif game.destinations(requested_index):
            # redirect to a different selected route
            url = "chess/game/{}/selected/{}/{}/".format(game_token, requested_index[0], requested_index[1])
            return redirect(url)
        else:
            # redirect to board
            url = "chess/game/{}/".format(game_token)
            return redirect(url)
    return render_template('selected.html', rows=8, columns=8, board=db_game.piece_locations, images=piece_images, destinations=destinations)
예제 #3
0
    def get_destinations(self, request, pk=None):
        game = Game.objects.get(pk=pk)
        if game.ready_to_play:
            chess = Chess(game.data)

            row = int(request.data['row'])
            column = int(request.data['column'])

            destinations = chess.destinations((row, column))
            return Response(destinations)
        return Response("not your turn", status=status.HTTP_400_BAD_REQUEST)
예제 #4
0
파일: routes.py 프로젝트: theovoss/ChessApi
def board(game_token):
    db_game = Game.query.get(game_token)
    game = Chess(existing_board=db_game.board)
    index = get_requested_index(request)
    if index and game.destinations(index):
        url = "chess/game/{}/selected/{}/{}/".format(game_token, index[0], index[1])
        return redirect(url)  # TODO: figure out how to call url_for...
        # return redirect(url_for('selected', game_token=game_token, row=row, column=col))
    if db_game:
        return render_template('board.html', rows=8, columns=8, board=db_game.piece_locations, images=piece_images)
    else:
        abort(400, "That game doesn't exits.")
예제 #5
0
def board(game_token):
    db_game = Game.query.get(game_token)
    game = Chess(existing_board=db_game.board)
    index = get_requested_index(request)
    if index and game.destinations(index):
        url = "chess/game/{}/selected/{}/{}/".format(game_token, index[0],
                                                     index[1])
        return redirect(url)  # TODO: figure out how to call url_for...
        # return redirect(url_for('selected', game_token=game_token, row=row, column=col))
    if db_game:
        return render_template('board.html',
                               rows=8,
                               columns=8,
                               board=db_game.piece_locations,
                               images=piece_images)
    else:
        abort(400, "That game doesn't exits.")