Esempio n. 1
0
def handler(event, context):
    connection_id = event["requestContext"].get("connectionId")
    logger.info(
        "Reject game request from connectionId {})".format(connection_id))

    logger.info("Parse event")
    data, err = parse_event(event)
    if err:
        return create_aws_lambda_response(500, err)
    game_id, challengee_id = data["game_id"], data["challengee_id"]
    game = Game.get(game_id)

    logger.info("Check player permissions")
    if not game.is_player_in_game(challengee_id):
        return create_aws_lambda_response(403,
                                          "Player is not part of the game")

    logger.info("Reject game")
    err = reject_game(game, challengee_id)
    if err:
        return create_aws_lambda_response(500, err)

    logger.info("Notify player")
    challenger_id = game.get_opponent_id(challengee_id)
    err = notify_player(challenger_id, "endGame", {"game": game.to_dict()})
    if err:
        return create_aws_lambda_response(500, err)
    return create_aws_lambda_response(200, "Reject game successful")
Esempio n. 2
0
def handler(event, context):
    connection_id = event["requestContext"].get("connectionId")
    logger.info("Leave game request from connectionId {})".format(connection_id))

    logger.info("Parse event")
    data, err = parse_event(event)
    if err:
        return create_aws_lambda_response(500, err)
    game_id, player_id = data["game_id"], data["player_id"]
    game = Game.get(game_id)

    logger.info("Check player permissions")
    if not game.is_player_in_game(player_id):
        return create_aws_lambda_response(403, "Player is not part of the game")

    logger.info("Resign player")
    err = resign_player(game, player_id)
    if err:
        return create_aws_lambda_response(500, err)

    logger.info("Notify players")
    player_ids = [game.whitePlayerId, game.blackPlayerId]
    err = notify_players(player_ids, "endGame", {"game": game.to_dict()})
    if err:
        return create_aws_lambda_response(500, err)
    return create_aws_lambda_response(200, "Leave game successful")
Esempio n. 3
0
def handler(event, context):
    connection_id = event["requestContext"].get("connectionId")
    logger.info("Send message request from connectionId {})".format(connection_id))

    logger.info("Parse event")
    data, err = parse_event(event)
    if err:
        return create_aws_lambda_response(500, err)
    game_id, player_id, text = data["game_id"], data["player_id"], data["text"]
    game = Game.get(game_id)

    logger.info("Check player permissions")
    if not game.is_player_in_game(player_id):
        return create_aws_lambda_response(403, "Player is not part of the game")

    logger.info("Generate message")
    sender = Player.get(player_id)
    message = generate_message(game.id, sender, text)

    logger.info("Save message")
    _, err = create_message(game_id, player_id, message["text"])
    if err:
        return create_aws_lambda_response(500, err)

    logger.info("Send message to players")
    player_ids = [game.whitePlayerId, game.blackPlayerId]
    err = notify_players(player_ids, "sendMessage", {"message": message})
    if err:
        return create_aws_lambda_response(500, err)
    return create_aws_lambda_response(200, "Send message successful")
Esempio n. 4
0
def handler(event, context):
    connection_id = event["requestContext"].get("connectionId")
    logger.info("Create game request from connectionId {})".format(connection_id))

    logger.info("Parse event")
    data, err = parse_event(event)
    if err:
        return create_aws_lambda_response(500, err)
    challenger_id, challengee_id = data["challenger_id"], data["challengee_id"]
    game_options = data["game_options"]

    logger.info("Create game")
    game, err = create_game(challenger_id, challengee_id, game_options)
    if err:
        return create_aws_lambda_response(500, err)

    logger.info("Notify player")
    challenger = Player.get(challenger_id)
    content = {
        "challenger": {
            "id": challenger.id,
            "name": challenger.name,
            "nickname": challenger.nickname,
            "picture": challenger.picture,
        },
        "game": game.to_dict(),
    }
    err = notify_player(challengee_id, "createGame", content)
    if err:
        return create_aws_lambda_response(500, "Failed to notify player")
    return create_aws_lambda_response(200, "Create game successful")
Esempio n. 5
0
def handler(event, context):
    connection_id = event["requestContext"].get("connectionId")
    logger.info("Disconnect request from connectionId {})".format(connection_id))

    for player in Player.connectionIdIndex.query(connection_id):
        attributes = {"status": PlayerStatus.OFFLINE, "updatedAt": datetime.now()}
        err = update_player(player, attributes)
        if err:
            return create_aws_lambda_response(500, err)
    return create_aws_lambda_response(200, "Disconnect successful")
Esempio n. 6
0
def handler(event, context):
    connection_id = event["requestContext"].get("connectionId")
    logger.info(
        "Move piece request from connectionId {})".format(connection_id))

    logger.info("Parse event")
    data, err = parse_event(event)
    if err:
        return create_aws_lambda_response(500, err)
    game_id, player_id, move_san = data["game_id"], data["player_id"], data[
        "move_san"]
    game = Game.get(game_id)

    logger.info("Check player permissions")
    if not game.is_player_in_game(player_id):
        return create_aws_lambda_response(403,
                                          "Player is not part of the game")
    if not game.is_player_turn(player_id):
        return create_aws_lambda_response(403, "It is not player turn to play")

    logger.info("Check move is valid")
    game.moves.append(move_san)
    board = generate_board_from_moves(game.moves)
    if not board.is_valid():
        notify_player(player_id, "movePieceFailure",
                      {"error": str(board.status())})
        return create_aws_lambda_response(400, "Invalid move")

    status, result = is_game_ended(game, board)
    if status:
        logger.info("Update game with the final result")
        err = end_game(game, status, result)
        if err:
            return create_aws_lambda_response(500, err)

        logger.info("Notify players the game has ended")
        player_ids = [game.whitePlayerId, game.blackPlayerId]
        err = notify_players(player_ids, "endGame", {"game": game.to_dict()})
        if err:
            return create_aws_lambda_response(500, err)
        return create_aws_lambda_response(200, "End game successful")

    logger.info("Move piece")
    err = move_piece(game, board.fen())
    if err:
        return create_aws_lambda_response(500, err)

    logger.info("Notify users the move is valid")
    player_ids = [game.whitePlayerId, game.blackPlayerId]
    err = notify_players(player_ids, "movePieceSuccess",
                         {"game": game.to_dict()})
    if err:
        return create_aws_lambda_response(500, "Failed to notify players")

    return create_aws_lambda_response(200, "Move piece successful")
Esempio n. 7
0
def handler(event, context):
    logger.info("Parse event")
    data, err = parse_event(event)
    if err:
        return create_aws_lambda_response(500, err)
    player_id = data["id"]

    logger.info("Get player")
    player, err = get_player(player_id)
    if err:
        return create_aws_lambda_response(500, err)

    return create_aws_lambda_response(200, {"player": player})
Esempio n. 8
0
def handler(event, context):
    logger.info("Authenticate user")
    logger.info(f"Method ARN: {event['methodArn']}")

    try:
        auth_token = get_auth_token(event)
        logger.info(f"Client token: {auth_token}")

        principal_id = decode_jwt_token(auth_token)["sub"]
        return generate_policy(principal_id, "Allow", event["methodArn"])
    except Exception as e:
        return create_aws_lambda_response(400, {"error": str(e)})
Esempio n. 9
0
def handler(event, context):
    connection_id = event["requestContext"]["connectionId"]
    logger.info("Connect request from connectionId {})".format(connection_id))

    logger.info("Parse event")
    token, err = parse_event(event)
    if err:
        return create_aws_lambda_response(500, err)

    logger.info("Decode jwt token")
    payload, err = decode_jwt_token(token)
    if err:
        return create_aws_lambda_response(500, err)

    logger.info("Create player if not existent")
    player_id = payload["sub"]
    player = create_player(player_id)

    logger.info("Update player profile and connection info")
    new_data = {
        # user profile from auth0
        "name": payload["name"],
        "nickname": payload["nickname"],
        "email": payload["email"],
        "picture": payload["picture"],
        # user status
        "status": PlayerStatus.ONLINE,
        # user websocket connection
        "connectionId": connection_id,
        # record updated time
        "updatedAt": datetime.now(),
    }
    err = update_player(player, new_data)
    if err:
        return create_aws_lambda_response(500, err)
    return create_aws_lambda_response(200, "Connect successful")
Esempio n. 10
0
def handler(event, context):
    connectionID = event["requestContext"].get("connectionId")
    logger.info("default request (CID: {})".format(connectionID))

    return create_aws_lambda_response(400, "Unrecognized WebSocket action")
Esempio n. 11
0
def handler(event, context):
    logger.info("Get players")
    players, err = get_players()
    if err:
        return create_aws_lambda_response(500, err)
    return create_aws_lambda_response(200, {"players": players})