Startup:
  utterances:
    (ask me a question|ask me some questions):
      go to: Get Question
    ?:
      go to: Intro

Intro:
  dialog: |
    Welcome to endless trivia!  
    I will ask you multiple choice questions on general knowledge.  
    Would you like to get started?
  utterances:
    yes:
      go to: Get Question
    no:
      go to: Exit Game
    stop:
      go to: Exit Game

Get Question:
  web request:
    url: https://opentdb.com/api.php?amount=1&type=multiple&category=17
    script: |
      letters                        = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
      result                         = results[0]
      attributes.answers             = _.shuffle(result.incorrect_answers.concat(result.correct_answer))
      attributes.answer              = result.correct_answer
      attributes.correctAnswerLetter = letters[attributes.answers.indexOf(attributes.answer)]
      formattedAnswers               = attributes.answers.map((answer, i) -> "<emphasis level=\"moderate\">#{letters[i]}</emphasis>: #{answer}").join(", ")
      attributes.question            = "<prosody rate=\"medium\">#{result.question} <break time=\"0.5s\"/> #{formattedAnswers} </prosody>"
  go to: Read Question

Read Question:
  ask: ${question}
  reprompt: Let me repeat that. ${question}
  utterances:
    ( |answer|choice) ${answerLetter}:
      slots:
        answerLetter:
          type: MultipleChoice
          values:
            - a
            - b
            - c
            - d
            - e
            - f
            - g
      go to: Evaluate Answer
    ( |i|i'm) (don't know|dunno|not sure):
      go to: I Don't Know
    next:
      samples:
        - skip
        - next question
        - skip this question
      go to: Get Question
    repeat:
      go to: Read Question
    stop:
      go to: Exit Game

Evaluate Answer:
  speak: You said ${answerLetter}, ${yourAnswer}.
  script: |
    answerIndex = ['a', 'b', 'c', 'd', 'e', 'f', 'g'].indexOf(answerLetter.strict)
    attributes.yourAnswer = if (answerIndex > -1) then (answers or [])[answerIndex] else ''
  condition:
    expression: answerLetter.strict is correctAnswerLetter
    if true:
      go to: Correct Answer
    if false:
      go to: Wrong Answer

Correct Answer:
  speak: That's correct!
  go to: Get Question

I Don't Know:
  speak: The correct answer is ${correctAnswerLetter}, ${answer}.
  go to: Get Question

Wrong Answer:
  speak: That is incorrect.  The correct answer is ${correctAnswerLetter}, ${answer}.
  go to: Get Question

Exit Game:
  speak: So long for now!

