Esempio n. 1
0
def piece_and_square(req: Dict[str, Any]) -> Dict[str, Any]:
    """Intent handler for when only one piece and one square are given"""
    session_id = get_session_by_req(req)
    params = get_params_by_req(req)

    piece = params["piece"].lower()
    pawn = params["pawn"].lower()
    square = params["square"].lower()

    board = get_user(session_id).board

    if pawn:
        san = square

        if piece and (square[-1] == "1" or square[-1] == "8"):
            # Promotion move
            san = f"{square}={get_piece_symbol(piece, upper=True)}"

        kwargs = handle_san_and_get_response_kwargs(session_id, board, san)

    elif piece:
        san = f"{get_piece_symbol(piece, upper=True)}{square}"
        kwargs = handle_san_and_get_response_kwargs(session_id, board, san)

    else:
        kwargs = {"textToSpeech": "Sorry, can you say that again?"}

    return get_response_for_google(**kwargs)
Esempio n. 2
0
def welcome(req: Dict[str, Any]) -> Dict[str, Any]:

    session_id = get_session_by_req(req)
    color = get_params_by_req(req)["color"]

    if color:
        return start_game_and_get_response(session_id, color)

    response_text = "Howdy! Which color would you like to choose?"
    options = [
        {
            "optionInfo": {
                "key": "white"
            },
            "description": "I like white!",
            "title": "White",
        },
        {
            "optionInfo": {
                "key": "black"
            },
            "description": "I'll choose black.",
            "title": "Black",
        },
        {
            "optionInfo": {
                "key": "random"
            },
            "description": "Choose randomly",
            "title": "Anything works!",
        },
    ]

    return get_response_for_google(textToSpeech=response_text, options=options)
Esempio n. 3
0
def two_squares(req: Dict[str, Any]) -> Dict[str, Any]:
    """Given two squares and a piece, play a move"""
    params = get_params_by_req(req)

    # Extract params and take the lowercase of the square
    squares = [square.lower() for square in params["squares"]]
    piece = params["piece"]

    if len(squares) == 1:
        return piece_and_square(req)

    # Extract board
    session_id = get_session_by_req(req)

    # TODO: Handle case when `session_id not found' when migrating to db
    # Get user
    user = get_user(session_id)

    # Get LAN move
    lan = two_squares_and_piece_to_lan(board=user.board,
                                       squares=squares,
                                       piece=piece)

    # TODO: Store this reply somewhere
    if lan == "illegal move":
        return get_response_for_google(textToSpeech=RESPONSES["illegal_move"])

    # Play move on board
    mediator.play_lan(session_id=session_id, lan=lan)

    kwargs = get_response_kwargs(session_id)
    return get_response_for_google(**kwargs)
Esempio n. 4
0
def undo(req: Dict[str, Any]) -> Dict[str, Any]:
    """Undo the last move of the user"""
    session_id = get_session_by_req(req)

    undone = undo_users_last_move(session_id)

    if undone:
        if len(undone) == 1:
            resp = f"OK! Undid the move {undone[0]}."

        else:
            engine_move, user_move = undone
            resp = (
                f"Alright! The moves {user_move} and {engine_move} have been "
                "undone."
            )

    else:
        resp = "Nothing to undo!"

    text_to_speech = f"{resp} {get_prompt_phrase()}"

    return get_response_for_google(
        textToSpeech=text_to_speech, displayText=resp
    )
Esempio n. 5
0
def show_board(req: Dict[str, Any]) -> Dict[str, Any]:
    """Show the board to player as a PNG image"""
    session_id = get_session_by_req(req)

    # Save board to <IMG_DIR>/<session_id>.png
    card = save_board_as_png_and_get_image_card(session_id)

    resp = get_response_for_google(
        textToSpeech="OK! Here it is. What's your next move?", basicCard=card)

    return resp
Esempio n. 6
0
def resign(req: Dict[str, Any]) -> Dict[str, Any]:
    """Delete the player from the database and return a conclusion response"""
    session_id = get_session_by_req(req)
    card = save_board_as_png_and_get_image_card(session_id)
    delete_user(session_id)

    output = "GG! Thanks for playing."

    return get_response_for_google(textToSpeech=output,
                                   expectUserResponse=False,
                                   basicCard=card)
Esempio n. 7
0
def simply_san(req: Dict[str, Any]) -> Dict[str, Any]:
    """Intent handler for simply SAN moves

    Note: Accepts overspecified SAN (including LAN)
    """
    session_id = get_session_by_req(req)
    san = get_params_by_req(req)["san"]

    # Convert pawn moves like E4, D5 to lowercase i.e. e4, d5
    if re.match(r"^[A-Z][1-8]$", san):
        san = san.lower()

    user = get_user(session_id)
    board = user.board

    kwargs = handle_san_and_get_response_kwargs(session_id, board, san)

    return get_response_for_google(**kwargs)
Esempio n. 8
0
def castle(req: Dict[str, Any]) -> Dict[str, Any]:
    """When asked to castle, try playing the castles move"""

    session_id = get_session_by_req(req)
    user = get_user(session_id)

    queryText = req["queryResult"]["queryText"]

    # Get lan of move
    lan = process_castle_by_querytext(board=user.board, queryText=queryText)

    if lan == "illegal move":
        return get_response_for_google(textToSpeech=RESPONSES["illegal_move"])

    mediator.play_lan(session_id=session_id, lan=lan)

    kwargs = get_response_kwargs(session_id)
    return get_response_for_google(**kwargs)
Esempio n. 9
0
def choose_color(req: Dict[str, Any]) -> Dict[str, Any]:
    """Assign board and color to user"""

    # Get session id to store user data by key
    session_id = get_session_by_req(req)

    # Extract the key of chosen list item
    arguments = req["originalDetectIntentRequest"]["payload"][
        "inputs"]  # Is a list

    for each in arguments:
        if each["intent"] == "actions.intent.OPTION":
            arguments = each["arguments"]
            break

    # Look for chosen key
    for each in arguments:
        if each.get("name") == "OPTION":
            color = each["textValue"]
            break

    return start_game_and_get_response(session_id, color)
Esempio n. 10
0
def test_get_session_by_req():

    result = get_session_by_req(data.sample_request)
    assert result == data.sample_request_sessionid