Пример #1
0
def post_move():
    start = request.json.get('start')
    end = request.json.get('end')
    is_white = request.json.get('isWhite')
    game_id = request.json.get('gameId')

    user = users.get_current_user()
    logging.info(user.user_id())

    oldBoard = models.Gameboard.query().filter(models.Gameboard.gameID == game_id).fetch(1)

    if len(oldBoard) == 0:
        logging.info("board is of length 0")

    positions = oldBoard[0].board

    move = GameEngine.isValidMove(positions, oldBoard[0].promotions, is_white, start, end)

    logging.info("move made")
    logging.info(start)
    logging.info(end)
    logging.info(game_id)

    if move >= 3:
        oldBoard[0].lastMove = end
        oldBoard[0].isWhite = not is_white
        if move >= 4:
            positions[move - 4] = 0

        for loc in range(0, 32):
            if positions[loc] == start:
                positions[loc] = end
                break

        oldBoard[0].board = positions
        oldBoard[0].promotions = promote_pawn(oldBoard[0])
        oldBoard[0].put()

    checkLocs = GameEngine.inCheckLocs(oldBoard[0].board, oldBoard[0].promotions, not oldBoard[0].isWhite)

    if GameEngine.inCheckMate(oldBoard[0].board, oldBoard[0].promotions, True):
        oldBoard[0].loser = 1
        oldBoard[0].put()
    elif GameEngine.inCheckMate(oldBoard[0].board, oldBoard[0].promotions, False):
        oldBoard[0].loser = 2
        oldBoard[0].put()

    response.content_type = 'application/json'
    return models.move_response_to_json(move, oldBoard[0].board, oldBoard[0].loser, checkLocs, oldBoard[0].promotions)
Пример #2
0
def opponent_moved():

    game_id = request.json.get('gameId')

    oldBoard = models.Gameboard.query().filter(models.Gameboard.gameID == game_id).fetch(1)

    response.content_type = 'application/json'

    if GameEngine.inCheckMate(oldBoard[0].board, oldBoard[0].promotions, True):
        oldBoard[0].loser = 1
        oldBoard[0].put()
    elif GameEngine.inCheckMate(oldBoard[0].board, oldBoard[0].promotions, False):
        oldBoard[0].loser = 2
        oldBoard[0].put()

    pawnLocs = GameEngine.pawnAttacks(oldBoard[0].board, oldBoard[0].promotions, oldBoard[0].isWhite)
    checkLocs = GameEngine.inCheckLocs(oldBoard[0].board, oldBoard[0].promotions, oldBoard[0].isWhite)

    if len(oldBoard) > 0 and oldBoard[0].isWhite:
        return models.opponent_moved_to_json('0', oldBoard[0].board, oldBoard[0].loser, pawnLocs, checkLocs, oldBoard[0].isWhite)
    else:
        return models.opponent_moved_to_json('1', oldBoard[0].board, oldBoard[0].loser, pawnLocs, checkLocs, oldBoard[0].isWhite)