Example #1
0
def end_game_return_score(this_game, answered_correctly, answer_heard,
                          correct_answer, current_question_value):
    """ If the customer answered the last question we end the game """
    logger.debug("=====end_game_return_score fired...")
    this_game.increment_total_games_played()
    this_game.update_game_status("ended")
    update_dynamodb(this_game.get_customer_id(),
                    this_game.ddb_formatted_attributes())

    wrap_up_speech = strings.END_GAME_WRAP_UP.format(str(
        this_game.total_score))

    if answered_correctly:
        speech_output = strings.random_correct_answer_message(
            correct_answer, current_question_value) + wrap_up_speech
        card_text = "Your score is " + str(this_game.total_score) + " points!\n" + \
            "The last word was: " + correct_answer
    else:
        speech_output = strings.WRONG_ANSWER.format(
            str(correct_answer)) + wrap_up_speech
        card_text = "Your score is " + str(this_game.total_score) + " points!\n" + \
            "\nThe last word was: " + correct_answer + "\nYou said: " + answer_heard

    card_title = "Clue Countdown Results"
    reprompt = "Would you like to play Clue Countdown again?"

    return speech(tts=speech_output,
                  attributes=this_game.attributes,
                  should_end_session=False,
                  card_title=card_title,
                  card_text=card_text,
                  answered_correctly=answered_correctly,
                  reprompt=reprompt,
                  music=strings.GAME_OVER)
def not_sure_intent(intent, this_game):
    """ Handle NotSureIntent """
    logger.debug("=====not_sure_intent fired...")
    game_status = this_game.game_status

    if game_status == "in_progress":
        # If we're on the last clue then count this as an answer.
        if this_game.current_clue_index == 4:
            return handle_answer_request(intent, this_game)

        # Otherwise we go to the next clue.
        return next_clue_request(this_game)

    # If it's not started yet the player might have interrupted
    # Alexa during the rules being read so we repeat them.
    if game_status == "not_yet_started":
        return speech(tts=strings.HELP_MESSAGE_BEFORE_GAME,
                      attributes=this_game.attributes,
                      should_end_session=False,
                      reprompt=strings.WELCOME_REPROMPT)

    # Player probably got here because they said something other than
    # yes or no after asking if they wanted to play the game again.
    logger.debug("=====No attributes ending game...")
    return play_end_message()
Example #3
0
def handle_answer_request(intent, this_game):
    """ Check if the answer is right, adjust score, and continue """
    logger.debug("=====handle_answer_request fired...")
    logger.debug(this_game.attributes)

    this_game.update_game_status("in_progress")
    answer_heard = get_answer_from_(intent)
    current_question_value = 50 - int(this_game.current_clue_index * 10)
    correct_answer = this_game.get_answer_for_current_question()

    # Use Levenshtein distance algo to see if the words are similar enough.
    fuzzy_score = fuzz.partial_ratio(answer_heard, correct_answer)

    # Currently using a hardcoded score of 60 or better.
    if correct_answer in answer_heard or fuzzy_score >= 60:
        this_game.update_total_score(current_question_value)

        answered_correctly = True
    else:
        log_wrong_answer(answer_heard, correct_answer)
        answered_correctly = False
        # If clues remain give them the next clue instead of moving on.
        if this_game.current_clue_index != 4:
            return next_clue_request(this_game, answered_correctly=False)

    # If that was the last word and no clues remain, end the game.
    if this_game.current_question_index == this_game.game_length - 1:
        # If this was the latest word pack mark it
        # as played so the player doesn't get it again.
        if this_game.play_newest_word_pack:
            this_game.update_last_word_pack_played(CURRENT_PACK_ID)

        return end_game_return_score(this_game, answered_correctly,
                                     answer_heard, correct_answer,
                                     current_question_value)

    # If that wasn't the last word in the game continue on to next word.
    this_game.move_on_to_next_word()
    next_clue_message = strings.NEXT_ROUND_WITH_CLUE.format(
        this_game.get_first_clue())

    if answered_correctly:
        speech_output = strings.random_correct_answer_message(
            correct_answer, current_question_value) + next_clue_message
        card_text = "The word was:  " + correct_answer + ". You got " + \
            str(current_question_value) + " points!"
        card_title = "You figured out the word!"
    else:
        speech_output = strings.WRONG_ANSWER.format(
            correct_answer) + next_clue_message
        card_text = "The word was:  " + correct_answer + "\n" + \
            "You said:  " + str(answer_heard)
        card_title = "That wasn't the word!"

    return speech(tts=speech_output,
                  attributes=this_game.attributes,
                  should_end_session=False,
                  card_title=card_title,
                  card_text=card_text,
                  answered_correctly=answered_correctly)
Example #4
0
def repeat_clue_request(this_game):
    """ Repeat the last clue """
    logger.debug("=====repeat_clue_request fired...")

    speech_output = "The last clue was:  " + this_game.current_clue

    return speech(tts=speech_output,
                  attributes=this_game.attributes,
                  should_end_session=False)
def help_intent(this_game):
    """ Handle HelpIntent """
    logger.debug("=====help_intent fired...")
    tts = strings.HELP_MESSAGE_BEFORE_GAME

    if this_game.game_status == "in_progress":
        tts = strings.HELP_MESSAGE_DURING_GAME + this_game.current_clue

    return speech(tts=tts,
                  attributes=this_game.attributes,
                  should_end_session=False)
Example #6
0
def play_new_game():
    """play new game intro and build question bank"""
    print("=====play_new_game fired...")
    new_game_message = "Umm, what are we... playing again?  Oh, right! Memory Loss! "\
            "I'll give you a series of words, numbers, or letters that you have to remember. "\
            "Then I'll ask you a question about them. Your job is to answer with either, "\
            "Yes, or, No, within 8 seconds. I won't repeat any questions, and I'll keep going "\
            "until someone tells me to stop. First question coming in... 3... 2... 1... "
    shuffle(QUESTIONS)
    question = choice(QUESTIONS)
    speech_output = new_game_message + question['question']
    should_end_session = False
    attributes = {"question": question, "game_status": "in_progress"}
    return speech(speech_output, attributes, should_end_session, None)
Example #7
0
def on_intent(event_request, session):
    """when customer launches the skill via modal"""
    print("=====on_intent requestId:  " + event_request['requestId'] +
          ", sessionId=" + session['sessionId'])

    intent = event_request['intent']
    intent_name = event_request['intent']['name']
    print("=====intent is: " + intent_name)

    if intent_name == "AnswerIntent":
        print("=====AnswerIntent fired...")
        if 'attributes' in session:
            if 'questions' in session['attributes']:
                return handle_answer_request(intent, session)

        # we probably got here because user said something other than
        # yes or no after asking if they wanted to play the game again
        print("=====no attributes ending game")
        return play_end_message()
    if intent_name == "GameIntent":
        print("=====GameIntent fired...")
        # if there's a session and we're in a game treat this as an answer
        # unfortunately it will be wrong but it's better than starting over
        if 'attributes' in session:
            if session['attributes']['game_status'] == "in_progress":
                return handle_answer_request(intent, session)
        return play_new_game(False)
    if intent_name in ("AMAZON.StartOverIntent", "AMAZON.YesIntent"):
        print("=====StartOverIntent or YesIntent fired...")
        return play_new_game(True)
    if intent_name == "AMAZON.NoIntent":
        print("=====NoIntent fired...")
        # if there's a session and we're in a game treat this as a wrong answer
        if 'attributes' in session:
            if session['attributes']['game_status'] == "in_progress":
                return handle_answer_request(intent, session)
        # otherwise end the game
        return play_end_message()
    if intent_name in ("AMAZON.StopIntent", "AMAZON.CancelIntent"):
        print("=====StopIntent or CancelIntent fired")
        return play_end_message()
    if intent_name == 'AMAZON.HelpIntent':
        print("=====HelpIntent...")
        tts = "During the game I'll give you 6 random brain teasers and only 8 "\
            "seconds to anser each one... To make your mind muscles stronger, I "\
            "won't repeat any of the questions, so try to remember all the "\
            "details... You can say 'Start Over' if you'd like a new game, "\
            "or make your guess for the last question..."
        return speech(tts, session['attributes'], False, None)
def launch_request(household_id, person_id):
    """ Handles LaunchRequests """
    player = get_player_info(household_id, person_id)
    logger.debug("=====Player Info: %s", player)

    tts = determine_welcome_message(household_id, person_id, player)

    session_attributes = {
        "game_status": "not_yet_started",
        "player_info": player
    }

    return speech(tts=tts,
                  attributes=session_attributes,
                  should_end_session=False,
                  reprompt=strings.WELCOME_REPROMPT)
def yes_intent(intent, this_game):
    """ Handle YesIntent """
    logger.debug("=====yes_intent fired...")
    game_status = this_game.game_status

    # If there is a game in progress we treat this as a wrong answer.
    if game_status == "in_progress":
        return handle_answer_request(intent, this_game)

    # If it's not started yet the player wants to hear the rules.
    if game_status == "not_yet_started":
        return speech(tts=strings.HELP_MESSAGE_BEFORE_GAME,
                      attributes=this_game.attributes,
                      should_end_session=False,
                      reprompt=strings.WELCOME_REPROMPT)

    # Otherwise they're trying to play the game again after finishing a game.
    return play_new_game(this_game, replay=True)
def answer_intent(intent, this_game):
    """ Handles AnswerIntent """
    logger.debug("=====answer_intent fired...")
    game_status = this_game.game_status
    if game_status == "in_progress":
        return handle_answer_request(intent, this_game)

    # If the game hasn't started yet, the player may have
    # interrupted Alexa during the rules being read to them.
    if game_status == "not_yet_started":
        return speech(tts=strings.HELP_MESSAGE_BEFORE_GAME,
                      attributes=this_game.attributes,
                      should_end_session=False,
                      reprompt=strings.WELCOME_REPROMPT)

    # We probably got here because the player said something other than
    # yes or no after asking if they wanted to play the game again.
    logger.debug("=====No attributes, ending game!")
    return play_end_message()
def start_over_intent(this_game):
    """ Handle StartOverIntent """
    logger.debug("=====start_over_intent fired...")
    game_status = this_game.game_status

    if game_status == "in_progress":
        return play_new_game(this_game, replay=True)

    # If it's not started yet the player might have interrupted
    # Alexa during the rules being read so we repeat them.
    if game_status == "not_yet_started":
        return speech(tts=strings.HELP_MESSAGE_BEFORE_GAME,
                      attributes=this_game.attributes,
                      should_end_session=False,
                      reprompt=strings.WELCOME_REPROMPT)

    # If the game is over start a new one.
    if game_status == "ended":
        return play_new_game(this_game, replay=True)
def repeat_intent(this_game):
    """ Handle RepeatIntent """
    logger.debug("=====repeat_intent fired...")
    game_status = this_game.game_status

    if game_status == "in_progress":
        return repeat_clue_request(this_game)

    # If it's not started yet the player might have interrupted
    # Alexa during the rules being read so we repeat them.
    if game_status == "not_yet_started":
        return speech(tts=strings.HELP_MESSAGE_BEFORE_GAME,
                      attributes=this_game.attributes,
                      should_end_session=False,
                      reprompt=strings.WELCOME_REPROMPT)

    # Player probably got here because they said something other than
    # yes or no after asking if they wanted to play the game again.
    logger.debug("=====no attributes ending game")
    return play_end_message()
Example #13
0
def next_clue_request(this_game, answered_correctly=None):
    """ Give player the next clue """
    logger.debug("=====next_clue_request fired...")

    # Max of 5 clues.
    if this_game.current_clue_index < 4:
        this_game.move_on_to_next_clue()
        if answered_correctly or answered_correctly is None:
            speech_output = strings.NEXT_CLUE + this_game.current_clue
        else:
            speech_output = strings.WRONG_ANSWER_CLUES_REMAIN + this_game.current_clue

    # Already on the last clue, repeat it.
    else:
        speech_output = strings.NO_MORE_CLUES.format(this_game.current_clue)

    return speech(tts=speech_output,
                  attributes=this_game.attributes,
                  should_end_session=False,
                  answered_correctly=answered_correctly)
Example #14
0
def play_new_game(replay):
    """play new game intro and build question bank for the session"""
    print("=====play_new_game fired...")
    if replay:
        new_game_message = "Get ready... Starting a new game in... 3... 2... 1..."
    else:
        new_game_message = "Welcome to Train My Brain!  I'm going to give you six "\
            "brain teasers and you'll only have eight seconds to answer each one... "\
            "I won't repeat the questions so try to remember all the details...  "\
            "Starting in...  3... 2... 1..."
    questions = pick_random_questions(6)
    speech_output = new_game_message + questions[0]['question']
    should_end_session = False
    attributes = {
        "questions": questions,
        "score": 0,
        "current_question_index": 0,
        "game_length": len(questions),
        "game_status": "in_progress"
    }
    return speech(speech_output, attributes, should_end_session, None)
Example #15
0
def play_new_game(this_game, replay=None):
    """ Play new game intro and build question bank for the session """
    logger.debug("=====play_new_game fired...")
    logger.debug("=====Player Info: %s", this_game.player_info)
    should_play_newest_word_pack = this_game.should_play_newest_word_pack(
        CURRENT_PACK_ID)
    questions = pick_random_questions(5, should_play_newest_word_pack)

    if replay:
        new_game_message = REPLAY_GAME_START
    else:
        new_game_message = GAME_STARTING

    if should_play_newest_word_pack:
        new_game_message = NEW_PACK_MESSAGE + CURRENT_PACK_THEME + "... " + GAME_STARTING

    speech_output = new_game_message + questions[0]['clues'][0]
    this_game.setup_new_game_attributes(questions,
                                        should_play_newest_word_pack)

    return speech(tts=speech_output,
                  attributes=this_game.attributes,
                  should_end_session=False)
Example #16
0
def on_intent(event_request, session):
    """when customer launches the skill via modal"""
    print("=====on_intent requestId:  " + event_request['requestId'] +
          ", sessionId=" + session['sessionId'])

    intent_name = event_request['intent']['name']
    print("=====intent is: " + intent_name)

    if intent_name == "AMAZON.YesIntent":
        print("=====YesIntent fired...")
        if 'attributes' in session:
            if session['attributes']['game_status'] == "in_progress":
                return handle_answer_request('yes', session)
    if intent_name == "AMAZON.NoIntent":
        print("=====NoIntent fired...")
        if 'attributes' in session:
            if session['attributes']['game_status'] == "in_progress":
                return handle_answer_request('no', session)
    if intent_name == "NotSureIntent":
        print("=====NotSureIntent fired...")
        if 'attributes' in session:
            if session['attributes']['game_status'] == "in_progress":
                return handle_answer_request('', session)
    if intent_name in ("AMAZON.StopIntent", "AMAZON.CancelIntent"):
        print("=====StopIntent or CancelIntent fired")
        return play_end_message()
    if intent_name == 'AMAZON.HelpIntent':
        print("=====HelpIntent...")
        tts = "All you have to do is answer Yes or No to each question I ask. "\
            "I won't repeat any of the questions, "\
            "so try to remember all the details. The game is best played with "\
            "a large group, and you should invent a scoring system or hand out "\
            "penalties for wrong answers.  I'll keep "\
            "asking questions until someone tells me to Stop. "\
            "Now, what's your guess for the last question?"
        return speech(tts, session['attributes'], False, None)