Example #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)
Example #2
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)
Example #3
0
    def next(self, request, pk=None):
        game = Game.objects.get(pk=pk)

        chess = Chess(game.data)

        chess.next()

        # update db with new board
        game.data = chess.export()
        game.save()
        return Response("success, time to refresh")
Example #4
0
    def first(self, request, pk=None):
        game = Game.objects.get(pk=pk)

        chess = Chess(game.data)

        chess.first()

        # update db with new board
        # TODO: how to navigate history without changing current state?
        # TODO: how to differentiate a takeback vs seeing a previous board state?
        game.data = chess.export()
        game.save()
        return Response("success, time to refresh")
Example #5
0
    def promote(self, request, pk=None):
        game = Game.objects.get(pk=pk)
        if not game.is_my_turn(request.user):
            return Response("not your turn",
                            status=status.HTTP_400_BAD_REQUEST)
        chess = Chess(game.data)

        chess.promote((request.data['row'], request.data['column']),
                      request.data['name'])

        # update db with new board
        game.data = chess.export()
        game.save()

        return Response("success, time to refresh")
Example #6
0
    def move(self, request, pk=None):
        game = Game.objects.get(pk=pk)
        if not game.is_my_turn(request.user):
            return Response("not your turn",
                            status=status.HTTP_400_BAD_REQUEST)

        chess = Chess(game.data)

        data = request.data
        destination_row = int(data['destination']['row'])
        destination_column = int(data['destination']['column'])
        start_row = int(data['start']['row'])
        start_column = int(data['start']['column'])

        chess.move((start_row, start_column),
                   (destination_row, destination_column))

        # update db with new board
        game.data = chess.export()
        game.save()

        return Response("success, time to refresh")