-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
374 lines (358 loc) · 17.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
// Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
// session persistence, api calls, and more.
const Alexa = require('ask-sdk-core');
let p1="";
let p2="";
let solution;
let p1score=0;
let p2score=0;
let z=0;
let round=0;
let first = true;
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle(handlerInput) {
//Welcome message
const speakOutput = 'Welcome to Take a Shot. Currently it is a two player game. Do you have someone to play with?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const CountIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'CountIntent';
},
handle(handlerInput) {
//Two players confirmed
const answer = handlerInput.requestEnvelope.request.intent.slots.ans.value;
let speakOutput;
if (answer === "yes") {
if (first)
speakOutput = 'What are your good names?';
else
speakOutput = "What are your good names again?";
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
else {
if (first)
//Two player condition not satisfied
speakOutput = "Sorry! Why don't you get one of your friends so that we can play later";
else
speakOutput = "It was fun playing with you guys. Come back again later. Have a nice day. "+'<audio src="soundbank://soundlibrary/toys_games/toys/toys_15"/>';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
}
};
var color = ["black", "brown", "white"];
//Array of Questions and Answers to be extended further
var myArray = [
{
"name":"cat",
"src":"soundbank://soundlibrary/animals/amzn_sfx_cat_meow_1x_01",
"kquestions":[
{"question":"Is cat a wild animal or a domestic animal","answer":"domestic"},
{"question":"Does cat have whiskers?","answer":"yes"},
{"question":"How would you describe cats? Are they tall, tiny or something else?", "answer":"small"},
{"question":"What is a male cat called?","answer":"Tom"},
{"question":"What is a female cat called?","answer":"Molly"},
{"question":"What is a cat child called?","answer":"kitten"}
],
"gquestions":[
{"question":"What color do you think is it?", "answer":color[getRandomInt(2)]},
{"question":"How many months old do you think is it?","answer":getRandomInt(40)}]
},
{
"name":"elephant",
"src":"soundbank://soundlibrary/animals/amzn_sfx_elephant_01",
"kquestions":[
{"question":"What is a group of elephants called?","answer":"herd"},
{"question":"An elephant's trunk is similar to what human body part","answer":"nose"},
{"question":"Does elephant eat meat?", "answer":"no"},
{"question":"What is elephant's child called?","answer":"calf"}
],
"gquestions":[
{"question":"What color do you think is it?", "answer":color[getRandomInt(1)]},
{"question":"What is it's height in foot?","answer":getRandomInt(13)}] //between 9 and 13
}
];
var no=Math.floor(Math.random() * Math.floor(myArray.length));
// var no=0;
var source=myArray[no].src
var name=myArray[no].name
var kquestions=myArray[no].kquestions
var gquestions=myArray[no].gquestions
//____________________________________________________________________
//Tells the rules, plays the animal sound and asks for the animal name
const GetNamesIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetNamesIntent';
},
handle(handlerInput) {
p1 = handlerInput.requestEnvelope.request.intent.slots.fname.value;
p2 = handlerInput.requestEnvelope.request.intent.slots.sname.value;
let speakOutput="";
if (first)
speakOutput+='Love your names, '+p1+" and "+p2+". The rules are simple. You hear the voice and answer the questions that follow. There is one simple general knowledge question for each and one fun question where you have to make a guess. ";
const speakOutput1 = ' Listen carefully. <audio src="'+source+'"/> <audio src="'+source+'"/> Which animal did it sound like?';
return handlerInput.responseBuilder
.speak(speakOutput+speakOutput1)
.reprompt('Take a Shot')
.getResponse();
}
};
//Random 1st question and answer
var freddie=Math.floor(Math.random() * Math.floor(kquestions.length));
var selq=kquestions[freddie].question
var sela=kquestions[freddie].answer
//___________________________________________________________________________________________FIRST ROUND
//Verifies the animal's name, and asks player 1 the first question
const GetAnimalIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetAnimalIntent';
},
handle(handlerInput) {
const ans = handlerInput.requestEnvelope.request.intent.slots.animal.value;
let speakOutput;
if (ans === name) {
speakOutput = '<audio src="soundbank://soundlibrary/musical/amzn_sfx_drum_and_cymbal_02"/> You guys are amazing! '+name+" it is. Let's see how much you know about the "+name+". Remember to use 'my answer is' followed by answer. Starting with "+p1+". ";
return handlerInput.responseBuilder
.speak(speakOutput+selq)
.reprompt("Take a shot, you are probably right.")
.getResponse();
}
else {
speakOutput = "Oops! It is a "+name+". That's okay. Let's start the game. "+p1+", it's your turn. ";
return handlerInput.responseBuilder
.speak(speakOutput+selq)
.reprompt("C'mon, you know it!")
.getResponse();
}
}
};
//_____________________________________________________________________________________________________SECOND ROUND
//Getting both their answers for the first question and computing their scores, and asking the 2nd question
let p1ans, p2ans; let winner="";
const GetAnswerIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetAnswerIntent';
},
handle(handlerInput) {
switch (round) {
case 0:
if (z===0) {
p1ans = handlerInput.requestEnvelope.request.intent.slots.answer.value;
if (p1ans===sela) {
p1score+=1;
winner = p1;
}
let speakOutput="The correct answer is "+sela+". ";
if (winner===p1) speakOutput+="Good work, "+p1+". "+'<audio src="soundbank://soundlibrary/musical/amzn_sfx_drum_and_cymbal_01"/>';
else speakOutput+='<audio src="soundbank://soundlibrary/musical/amzn_sfx_drum_comedy_03"/>'
freddie=Math.floor(Math.random() * Math.floor(kquestions.length));
selq=kquestions[freddie].question
sela=kquestions[freddie].answer
speakOutput+=" "+p2+", your question is "+selq;
z=1;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt("C'mon, you know it!")
.getResponse();
}
else {
p2ans = handlerInput.requestEnvelope.request.intent.slots.answer.value;
z=0;
if (p2ans===sela) {
p2score+=1;
winner = p2;
}
let speakOutput="The correct answer is "+sela+". ";
if (winner===p2) speakOutput+="Good work, "+p2+". "+'<audio src="soundbank://soundlibrary/musical/amzn_sfx_drum_and_cymbal_01"/>';
else speakOutput+='<audio src="soundbank://soundlibrary/musical/amzn_sfx_drum_comedy_03"/>';
speakOutput+="Next question for "+p2+". "+gquestions[0].question;
round+=1;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt("C'mon, you know it!")
.getResponse();
}
case 1:
let kela = gquestions[0].answer
if (z===0) {
p2ans = handlerInput.requestEnvelope.request.intent.slots.ganss.value;
if (p2ans===kela) {
p2score+=1;
winner = p2;
}
let speakOutput=p1+", what is your answer?"
z=1;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt("C'mon, you know it!")
.getResponse();
}
else {
p1ans = handlerInput.requestEnvelope.request.intent.slots.ganss.value;
z=0;
if (p1ans===kela) {
p1score+=1;
if (winner === p2) winner = "both";
else winner = p1;
}
let speakOutput="The correct answer is "+kela+". ";
if (winner===p1) speakOutput+="Great guess, "+p1+". ";
else if (winner===p2) speakOutput+="Great guess, "+p2+". ";
else speakOutput+="Great work guys. ";
// speakOutput+="Next question for "+p1+". "+gquestions[1].question;
// round+=1;
// return handlerInput.responseBuilder
// .speak(speakOutput)
// .reprompt("C'mon, you know it!")
// .getResponse();
// }
// case 2:
// kela = gquestions[1].answer;
// if (z===0) {
// p1ans = handlerInput.requestEnvelope.request.intent.slots.gans.value;
// if (p1ans===kela) {
// p1score+=1;
// winner = p1;
// }
// let speakOutput=p2+", what is your answer?"
// z=1;
// return handlerInput.responseBuilder
// .speak(speakOutput)
// .reprompt("C'mon, you know it!")
// .getResponse();
// }
// else {
// p2ans = handlerInput.requestEnvelope.request.intent.slots.gans.value;
// z=0;
// if (p2ans===kela) {
// p2score+=1;
// if (winner === p1) winner = "both";
// else winner = p2;
// }
// let speakOutput="The correct answer is "+kela+". ";
// if (winner===p1) speakOutput+="Superb, "+p1+". ";
// else if (winner===p2) speakOutput+="Superb, "+p2+". ";
// else speakOutput+="Great work guys. "+p1+". ";
if (p1score>p2score)
speakOutput+='We have a winner. <audio src="soundbank://soundlibrary/musical/amzn_sfx_trumpet_bugle_03"/> and the winner is <audio src="soundbank://soundlibrary/musical/amzn_sfx_trumpet_bugle_04"/> '+p1+". ";
else if (p2score>p1score)
speakOutput+='We have a winner. <audio src="soundbank://soundlibrary/musical/amzn_sfx_trumpet_bugle_03"/> and the winner is <audio src="soundbank://soundlibrary/musical/amzn_sfx_trumpet_bugle_04"/> '+p2+". " ;
else
speakOutput+='<audio src="soundbank://soundlibrary/musical/amzn_sfx_trumpet_bugle_03"/><audio src="soundbank://soundlibrary/musical/amzn_sfx_trumpet_bugle_04"/>You guys scored equally, it is a draw. ';
first = false;
speakOutput+="Do you want to play again?"
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt("C'mon, you know it!")
.getResponse();
}
}
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speakOutput = 'You can say hello to me! How can I help?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
},
handle(handlerInput) {
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse();
}
};
// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
},
handle(handlerInput) {
const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
const speakOutput = `You just triggered ${intentName}`;
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.stack}`);
const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
CountIntentHandler,
GetNamesIntentHandler,
GetAnimalIntentHandler,
GetAnswerIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();