Exemple #1
0
def launch_request_handler(handler_input):
    # TODO: handle launch with preselected op and diff
    # TODO: manage late answers (i.e. launch when the answer to an exercise question didn't come in time - do you want to continue or start a new session?)

    am = handler_input.attributes_manager
    usage = models.SkillUsage.from_attributes(am.persistent_attributes)
    locale = handler_input.request_envelope.request.locale

    intro = content.intro_message(usage.launch_count, locale)
    # TODO: maybe ask if they want to continue? if so then I'd need to remember the question as well /o\
    usage.session_data = models.SessionData()
    am.session_attributes = models.asdict(usage)
    prompt = content.prompt_for_operation(locale)
    message = utils.combine_messages(intro, prompt)

    return utils.build_response(handler_input, message)
Exemple #2
0
def did_select_operation_handler(handler_input):
    # TODO: what if I already have the difficulty level? i.e. I can directly ask a question

    am = handler_input.attributes_manager
    usage = models.SkillUsage.from_attributes(am.session_attributes)
    locale = handler_input.request_envelope.request.locale
    slots = handler_input.request_envelope.request.intent.slots

    spoken_operation = slots['operation'].value
    operation = models.Operation.from_word(spoken_operation, locale)
    usage.session_data.operation = operation
    am.session_attributes = models.asdict(usage)

    ack = content.confirmation(locale)
    question = content.prompt_for_difficulty(locale)
    message = utils.combine_messages(ack, question)

    return utils.build_response(handler_input, message, question)
Exemple #3
0
def persist_skill_data(handler_input, user_initiated_shutdown=False):
    am = handler_input.attributes_manager

    usage = models.SkillUsage.from_attributes(am.session_attributes)
    usage.launch_count += 1
    usage.previous_session_end = int(time.time())

    if user_initiated_shutdown:
        # we assume they won't want to resume a session on the next
        # launch, hence we persist empty session_data
        usage.session_data = models.SessionData()

    if usage.session_data.operation:
        # TODO FIXIT HACK :(
        operation = usage.session_data.operation
        usage.session_data.operation = operation.value

    am.persistent_attributes = models.asdict(usage)
    am.save_persistent_attributes()
Exemple #4
0
def did_answer_handler(handler_input):
    am = handler_input.attributes_manager
    usage = models.SkillUsage.from_attributes(am.session_attributes)
    locale = handler_input.request_envelope.request.locale
    slots = handler_input.request_envelope.request.intent.slots

    answer = int(slots['answer'].value)
    is_correct = answer == usage.session_data.correct_result

    if is_correct:
        usage.session_data.correct_answers_count += 1
        usage.session_data.streak_count += 1

        outcome = content.correct(locale)
        streak_count = usage.session_data.streak_count
        # pylint: disable=bad-continuation
        if (streak_count == 5
                or (streak_count >= 10 and streak_count % 10 == 0)):
            # on 5 in a row and every 10
            outcome = content.streak_encouragement(streak_count, locale)
    else:
        usage.session_data.streak_count = 0

        correct_result = usage.session_data.correct_result
        outcome = content.incorrect(correct_result, locale)
    usage.session_data.questions_count += 1

    question, result, apl = content.build_question(usage, locale)
    usage.session_data.correct_result = result
    message = utils.combine_messages(outcome, question)
    am.session_attributes = models.asdict(usage)

    rb = handler_input.response_builder
    rb.speak(message).ask(question)
    if utils.has_apl_support(handler_input):
        rb.add_directive(apl)
    return rb.response
Exemple #5
0
def did_select_difficulty_handler(handler_input):
    # TODO: handle also if they say "easier" or "harder" during

    am = handler_input.attributes_manager
    usage = models.SkillUsage.from_attributes(am.session_attributes)
    locale = handler_input.request_envelope.request.locale
    slots = handler_input.request_envelope.request.intent.slots

    spoken_difficulty = slots['difficulty'].value
    difficulty = content.difficulty_to_value(spoken_difficulty, locale)
    usage.session_data.difficulty = difficulty

    ack = content.confirmation(locale)
    start_message = content.start_message(locale)
    question, result, apl = content.build_question(usage, locale)
    usage.session_data.correct_result = result
    message = utils.combine_messages(ack, start_message, question)
    am.session_attributes = models.asdict(usage)

    rb = handler_input.response_builder
    rb.speak(message).ask(question)
    if utils.has_apl_support(handler_input):
        rb.add_directive(apl)
    return rb.response