diff --git a/index.js b/index.js index 377712c..effdd5d 100644 --- a/index.js +++ b/index.js @@ -8,262 +8,8 @@ or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -/** - * This sample shows how to create a simple Trivia skill with a multiple choice format. The skill - * supports 1 player at a time, and does not support games across sessions. - */ - 'use strict'; -/** - * When editing your questions pay attention to your punctuation. Make sure you use question marks or periods. - * Make sure the first answer is the correct one. Set at least 4 answers, any extras will be shuffled in. - */ -var questions = [ - { - "Reindeer have very thick coats, how many hairs per square inch do they have?": [ - "13,000", - "1,200", - "5,000", - "700", - "1,000", - "120,000" - ] - }, - { - "The 1964 classic Rudolph The Red Nosed Reindeer was filmed in:": [ - "Japan", - "United States", - "Finland", - "Germany" - ] - }, - { - "Santas reindeer are cared for by one of the Christmas elves, what is his name?": [ - "Wunorse Openslae", - "Alabaster Snowball", - "Bushy Evergreen", - "Pepper Minstix" - ] - }, - { - "If all of Santas reindeer had antlers while pulling his Christmas sleigh, they would all be:": [ - "Girls", - "Boys", - "Girls and boys", - "No way to tell" - ] - }, - { - "What do Reindeer eat?": [ - "Lichen", - "Grasses", - "Leaves", - "Berries" - ] - }, - { - "What of the following is not true?": [ - "Caribou live on all continents", - "Both reindeer and Caribou are the same species", - "Caribou are bigger than reindeer", - "Reindeer live in Scandinavia and Russia" - ] - }, - { - "In what year did Rudolph make his television debut?": [ - "1964", - "1979", - "2000", - "1956" - ] - }, - { - "Who was the voice of Rudolph in the 1964 classic?": [ - "Billie Mae Richards", - "Burl Ives", - "Paul Soles", - "Lady Gaga" - ] - }, - { - "In 1939 what retailer used the story of Rudolph the Red Nose Reindeer?": [ - "Montgomery Ward", - "Sears", - "Macys", - "Kmart" - ] - }, - { - "Santa's reindeer named Donner was originally named what?": [ - "Dunder", - "Donny", - "Dweedle", - "Dreamy" - ] - }, - { - "Who invented the story of Rudolph?": [ - "Robert May", - "Johnny Marks", - "Santa", - "J.K. Rowling" - ] - }, - { - "In what location will you not find reindeer?": [ - "North Pole", - "Lapland", - "Korvatunturi mountain", - "Finland" - ] - }, - { - "What Makes Santa's Reindeer Fly?": [ - "Magical Reindeer Dust", - "Fusion", - "Amanita muscaria", - "Elves" - ] - }, - { - "Including Rudolph, how many reindeer hooves are there?": [ - "36", - "24", - "16", - "8" - ] - }, - { - "Santa only has one female reindeer. Which one is it?": [ - "Vixen", - "Clarice", - "Cupid", - "Cupid" - ] - }, - { - "In the 1964 classic Rudolph The Red Nosed Reindeer, what was the snowman narrators name?": [ - "Sam", - "Frosty", - "Burl", - "Snowy" - ] - }, - { - "What was Rudolph's father's name?": [ - "Donner", - "Dasher", - "Blixen", - "Comet" - ] - }, - { - "In the 1964 movie, What was the name of the coach of the Reindeer Games?": [ - "Comet", - "Blixen", - "Donner", - "Dasher" - ] - }, - { - "In the 1964 movie, what is the name of the deer that Rudolph befriends at the reindeer games?": [ - "Fireball", - "Clarice", - "Jumper", - "Vixen" - ] - }, - { - "In the 1964 movie, How did Donner, Rudolph's father, try to hide Rudolph's nose?": [ - "Black mud", - "Bag", - "Pillow case", - "Sock" - ] - }, - { - "In the 1964 movie, what does the Misfit Elf want to be instead of a Santa Elf?": [ - "Dentist", - "Reindeer", - "Toy maker", - "Candlestick maker" - ] - }, - { - "In the 1964 movie,what was the Bumble's one weakness?": [ - "Could not swim", - "Always hungry", - "Candy canes", - "Cross eyed" - ] - }, - { - "In the 1964 movie, what is Yukon Cornelius really in search of?": [ - "Peppermint", - "Gold", - "India", - "Polar Bears" - ] - }, - { - "In the 1964 movie, why is the train on the Island of Misfit Toys?": [ - "Square wheels", - "No Engine", - "Paint does not match", - "It does not toot" - ] - }, - { - "In the 1964 movie, what is the name of the Jack in the Box?": [ - "Charlie", - "Sam", - "Billy", - "Jack" - ] - }, - { - "In the 1964 movie, why did Santa Claus almost cancel Christmas?": [ - "Storm", - "No snow", - "No toys", - "The Reindeer were sick" - ] - }, - { - "In the 1964 movie, what animal noise did the elf make to distract the Bumble?": [ - "Oink", - "Growl", - "Bark", - "Meow" - ] - }, - { - "In the 1964 movie, what is the name of the prospector?": [ - "Yukon Cornelius", - "Slider Sam", - "Bumble", - "Jack" - ] - }, - { - "How far do reindeer travel when they migrate?": [ - "3000 miles", - "700 miles", - "500 miles", - "0 miles" - ] - }, - { - "How fast can a reindeer run?": [ - "48 miles per hour", - "17 miles per hour", - "19 miles per hour", - "14 miles per hour" - ] - } -]; - // Route the incoming request based on type (LaunchRequest, IntentRequest, // etc.) The JSON body of the request is provided in the event parameter. exports.handler = function (event, context) { @@ -275,7 +21,7 @@ exports.handler = function (event, context) { * prevent someone else from configuring a skill that sends requests to this function. */ - if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.05aecccb3-1461-48fb-a008-822ddrt6b516") { + if (event.session.application.applicationId !== "amzn1.ask.skill.d5e65690-fa47-47d7-9185-3e30c7b52ee4") { context.fail("Invalid Application ID"); } @@ -383,151 +129,82 @@ function onSessionEnded(sessionEndedRequest, session) { // ------- Skill specific business logic ------- -var ANSWER_COUNT = 4; -var GAME_LENGTH = 5; -var CARD_TITLE = "Trivia"; // Be sure to change this for your skill. +var GAME_LENGTH = 1; +var NUMBERWANK = 4; +var CARD_TITLE = "Numberwang"; function getWelcomeResponse(callback) { var sessionAttributes = {}, - speechOutput = "I will ask you " + GAME_LENGTH.toString() - + " questions, try to get as many right as you can. Just say the number of the answer. Let's begin. ", + speechOutput = "Hello, and welcome to Numberwang, the maths quiz that simply everyone is talking about! ", shouldEndSession = false, - gameQuestions = populateGameQuestions(), - correctAnswerIndex = Math.floor(Math.random() * (ANSWER_COUNT)), // Generate a random index for the correct answer, from 0 to 3 - roundAnswers = populateRoundAnswers(gameQuestions, 0, correctAnswerIndex), - - currentQuestionIndex = 0, - spokenQuestion = Object.keys(questions[gameQuestions[currentQuestionIndex]])[0], - repromptText = "Question 1. " + spokenQuestion + " ", + correctAnswerIndex = Math.floor(Math.random() * (NUMBERWANK)), // is it Numberwang? - i, j; + repromptText = "Well, if you're ready, let's play numberwang! "; - for (i = 0; i < ANSWER_COUNT; i++) { - repromptText += (i+1).toString() + ". " + roundAnswers[i] + ". " - } speechOutput += repromptText; sessionAttributes = { "speechOutput": repromptText, "repromptText": repromptText, - "currentQuestionIndex": currentQuestionIndex, - "correctAnswerIndex": correctAnswerIndex + 1, - "questions": gameQuestions, - "score": 0, - "correctAnswerText": - questions[gameQuestions[currentQuestionIndex]][Object.keys(questions[gameQuestions[currentQuestionIndex]])[0]][0] + "correctAnswerIndex": correctAnswerIndex }; callback(sessionAttributes, buildSpeechletResponse(CARD_TITLE, speechOutput, repromptText, shouldEndSession)); } -function populateGameQuestions() { - var gameQuestions = []; - var indexList = []; - var index = questions.length; - - if (GAME_LENGTH > index){ - throw "Invalid Game Length."; - } - - for (var i = 0; i < questions.length; i++){ - indexList.push(i); - } - - // Pick GAME_LENGTH random questions from the list to ask the user, make sure there are no repeats. - for (var j = 0; j < GAME_LENGTH; j++){ - var rand = Math.floor(Math.random() * index); - index -= 1; - - var temp = indexList[index]; - indexList[index] = indexList[rand]; - indexList[rand] = temp; - gameQuestions.push(indexList[index]); - } - - return gameQuestions; -} - -function populateRoundAnswers(gameQuestionIndexes, correctAnswerIndex, correctAnswerTargetLocation) { - // Get the answers for a given question, and place the correct answer at the spot marked by the - // correctAnswerTargetLocation variable. Note that you can have as many answers as you want but - // only ANSWER_COUNT will be selected. - var answers = [], - answersCopy = questions[gameQuestionIndexes[correctAnswerIndex]][Object.keys(questions[gameQuestionIndexes[correctAnswerIndex]])[0]], - temp, i; - var index = answersCopy.length; - - if (index < ANSWER_COUNT){ - throw "Not enough answers for question."; - } - - // Shuffle the answers, excluding the first element. - for (var j = 1; j < answersCopy.length; j++){ - var rand = Math.floor(Math.random() * (index - 1)) + 1; - index -= 1; - - var temp = answersCopy[index]; - answersCopy[index] = answersCopy[rand]; - answersCopy[rand] = temp; - } - - // Swap the correct answer into the target location - for (i = 0; i < ANSWER_COUNT; i++) { - answers[i] = answersCopy[i]; - } - temp = answers[0]; - answers[0] = answers[correctAnswerTargetLocation]; - answers[correctAnswerTargetLocation] = temp; - return answers; -} function handleAnswerRequest(intent, session, callback) { var speechOutput = ""; var sessionAttributes = {}; var gameInProgress = session.attributes && session.attributes.questions; var answerSlotValid = isAnswerSlotValid(intent); - var userGaveUp = intent.name === "DontKnowIntent"; + //var userGaveUp = intent.name === "DontKnowIntent"; - if (!gameInProgress) { + /*if (!gameInProgress) { // If the user responded with an answer but there is no game in progress, ask the user // if they want to start a new game. Set a flag to track that we've prompted the user. sessionAttributes.userPromptedToContinue = true; speechOutput = "There is no game in progress. Do you want to start a new game? "; callback(sessionAttributes, buildSpeechletResponse(CARD_TITLE, speechOutput, speechOutput, false)); - } else if (!answerSlotValid && !userGaveUp) { - // If the user provided answer isn't a number > 0 and < ANSWER_COUNT, - // return an error message to the user. Remember to guide the user into providing correct values. + } else*/ if (!answerSlotValid) { //} && !userGaveUp) { + // If the user provided answer isn't a number var reprompt = session.attributes.speechOutput; - var speechOutput = "Your answer must be a number between 1 and " + ANSWER_COUNT + ". " + reprompt; + var speechOutput = "I'm sorry - your answer must be a number!. " + reprompt; callback(session.attributes, buildSpeechletResponse(CARD_TITLE, speechOutput, reprompt, false)); } else { - var gameQuestions = session.attributes.questions, - correctAnswerIndex = parseInt(session.attributes.correctAnswerIndex), - currentScore = parseInt(session.attributes.score), - currentQuestionIndex = parseInt(session.attributes.currentQuestionIndex), - correctAnswerText = session.attributes.correctAnswerText; + var correctAnswerIndex = parseInt(session.attributes.correctAnswerIndex); + //currentScore = parseInt(session.attributes.score); var speechOutputAnalysis = ""; - - if (answerSlotValid && parseInt(intent.slots.Answer.value) == correctAnswerIndex) { - currentScore++; - speechOutputAnalysis = "correct. "; + if (answerSlotValid && (correctAnswerIndex == (NUMBERWANK - 1))) { + //currentScore++; + speechOutputAnalysis = "That's Numberwang!"; } else { - if (!userGaveUp) { - speechOutputAnalysis = "wrong. " + //if (!userGaveUp) { + speechOutputAnalysis = "You've been Wangernumbed!"; + /* + } else { + speechOutputAnalysis = "Better luck next time!"; } - speechOutputAnalysis += "The correct answer is " + correctAnswerIndex + ": " + correctAnswerText + ". "; + */ } + /* // if currentQuestionIndex is 4, we've reached 5 questions (zero-indexed) and can exit the game session if (currentQuestionIndex == GAME_LENGTH - 1) { speechOutput = userGaveUp ? "" : "That answer is "; speechOutput += speechOutputAnalysis + "You got " + currentScore.toString() + " out of " + GAME_LENGTH.toString() + " questions correct. Thank you for playing!"; + */ + + // DEBUG: + //speechOutput += "answerSlotValid is " + answerSlotValid + "; correctAnswerIndex is " + correctAnswerIndex + " - "; + speechOutput += speechOutputAnalysis + " Thanks for playing!"; callback(session.attributes, buildSpeechletResponse(CARD_TITLE, speechOutput, "", true)); + /* } else { currentQuestionIndex += 1; var spokenQuestion = Object.keys(questions[gameQuestions[currentQuestionIndex]])[0]; @@ -555,7 +232,7 @@ function handleAnswerRequest(intent, session, callback) { }; callback(sessionAttributes, buildSpeechletResponse(CARD_TITLE, speechOutput, repromptText, false)); - } + }*/ } } @@ -584,11 +261,9 @@ function handleGetHelpRequest(intent, session, callback) { // Do not edit the help dialogue. This has been created by the Alexa team to demonstrate best practices. - var speechOutput = "I will ask you " + GAME_LENGTH + " multiple choice questions. Respond with the number of the answer. " - + "For example, say one, two, three, or four. To start a new game at any time, say, start game. " - + "To repeat the last question, say, repeat. " + var speechOutput = "I will ask you to tell me a number, and I will tell you if it is Numberwang. " + "Would you like to keep playing?", - repromptText = "To give an answer to a question, respond with the number of the answer . " + repromptText = "Please give me a number . " + "Would you like to keep playing?"; var shouldEndSession = false; callback(session.attributes, @@ -598,13 +273,14 @@ function handleGetHelpRequest(intent, session, callback) { function handleFinishSessionRequest(intent, session, callback) { // End the session with a "Good bye!" if the user wants to quit the game callback(session.attributes, - buildSpeechletResponseWithoutCard("Good bye!", "", true)); + buildSpeechletResponseWithoutCard("Good Numberwang!", "", true)); } function isAnswerSlotValid(intent) { var answerSlotFilled = intent.slots && intent.slots.Answer && intent.slots.Answer.value; var answerSlotIsInt = answerSlotFilled && !isNaN(parseInt(intent.slots.Answer.value)); - return answerSlotIsInt && parseInt(intent.slots.Answer.value) < (ANSWER_COUNT + 1) && parseInt(intent.slots.Answer.value) > 0; + // TODO: fix!!! + return true; //answerSlotIsInt; } // ------- Helper functions to build responses -------