def normal_response(user_response, user_id): response = advent.respond(user_id, user_response).strip() logging.info("user={0} game reply={1}".format(user_id, response.replace("\n", " "))) if advent.yes_no_question(user_id): split_response = response.split("\n") question = split_response[-1] del split_response[-1] respond(user_id, "\n".join(split_response)) smooch.send_postbacks(user_id, question, [("Yes", "yes"), ("No", "no")]) r.set("yesno:" + user_id, "game") return True elif re.search("You scored \d+ out of a possible \d+ using \d+ turns.", response): respond(user_id, response) advent.new_game(user_id) r.set("yesno:" + user_id, "new_game") smooch.send_postbacks(user_id, "Do you want to play again?", [("Yes", "start_new_yes"), ("No", "start_new_no")]) return True else: respond(user_id, response) return True
def new_user(user_response, user_id): logging.info("CREATING NEW USER={}".format(user_id)) advent.new_game(user_id).strip() split_response = ["Welcome to Adventure!!", "Adventure is a text based game, and a port of the classic terminal game Advent."] question = "Would you like instructions?" logging.debug("split_response={} question={}".format(split_response, question)) respond(user_id, "\n".join(split_response)) smooch.send_postbacks(user_id, question, [("Yes", "yes"), ("No", "no")]) r.set("yesno:" + user_id, "game") return True
def new_user(user_response, user_id): logging.info("CREATING NEW USER={}".format(user_id)) advent.new_game(user_id).strip() split_response = [ "Welcome to Adventure!!", "Adventure is a text based game, and a port of the classic terminal game Advent." ] question = "Would you like instructions?" logging.debug("split_response={} question={}".format( split_response, question)) respond(user_id, "\n".join(split_response)) smooch.send_postbacks(user_id, question, [("Yes", "yes"), ("No", "no")]) r.set("yesno:" + user_id, "game") return True
def game_fallback(postback_payload, user_id): if postback_payload.endswith("yes"): response = advent.respond(user_id, "yes") # Checking if user has asked to end the game. if re.search("You scored \d+ out of a possible \d+ using \d+ turns.", response): respond(user_id, response) advent.new_game(user_id) r.set("yesno:" + user_id, "new_game") smooch.send_postbacks(user_id, "Do you want to play again?", [("Yes", "start_new_yes"), ("No", "start_new_no")]) return True respond(user_id, response) r.delete("yesno:" + user_id) return True elif postback_payload.endswith("no"): response = advent.respond(user_id, "no") respond(user_id, response) r.delete("yesno:" + user_id) return True else: logging.error("Invalid postback={}".format(postback_payload)) return False
def yes_no_question(user_response, user_id): response_type = r.get("yesno:" + user_id) logging.debug("Response type={}".format(response_type)) if response_type == b'restart': smooch.send_postbacks(user_id, "Do you want to restart?", [("Yes", "restart_yes"), ("No", "restart_no")]) return True elif response_type == b'new_game': smooch.send_postbacks(user_id, "Do you want to play again?", [("Yes", "start_new_yes"), ("No", "start_new_no")]) return True elif response_type == b'game': smooch.send_postbacks(user_id, "Please answer the question.", [("Yes", "yes"), ("No", "no")]) return True else: logging.error("Extraneous response type={}".format(response_type)) return False
def restart(user_response, user_id): r.set("yesno:" + user_id, "restart") smooch.send_postbacks(user_id, "Do you want to restart?\n I cannot undo this.", [("Yes", "restart_yes"), ("No", "restart_no")]) return True