This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathindex.js
executable file
·333 lines (315 loc) · 11.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
'use strict';
const Alexa = require('alexa-sdk');
const story = 'Escape the Office.html';
const TableName = null // story.replace('.html','').replace(/\s/g, "-");
var $twine = null;
const linksRegex = /\[\[([^\|\]]*)\|?([^\]]*)\]\]/g;
module.exports.handler = (event, context, callback) => {
console.log(`handler: ${JSON.stringify(event.request)}`);
// read the Twine 2 (Harlowe) story into JSON
var fs = require('fs');
var contents = fs.readFileSync(story, 'utf8');
var m = contents.match(/<tw-storydata [\s\S]*<\/tw-storydata>/g);
var xml = m[0];
// because Twine xml has an attribute with no value
xml = xml.replace('hidden>', 'hidden="true">');
var parseString = require('xml2js').parseString;
parseString(xml, function(err, result) {
$twine = result['tw-storydata']['tw-passagedata'];
});
// prepare alexa-sdk
const alexa = Alexa.handler(event, context);
// APP_ID is your skill id which can be found in the Amazon developer console
// where you create the skill. Optionally set as a Lamba environment variable.
alexa.appId = process.env.APP_ID;
alexa.dynamoDBTableName = TableName;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'LaunchRequest': function() {
console.log(`LaunchRequest`);
if (this.event.session.attributes['room'] !== undefined) {
var room = currentRoom(this.event);
var speechOutput = `Hello, you were playing before and got to the room called ${room['$']['name']}. Would you like to resume? `;
var reprompt = `Say, resume game, or, new game.`;
speechOutput = speechOutput + reprompt;
var cardTitle = `Restart`;
var cardContent = speechOutput;
var imageObj = undefined;
console.log(`LaunchRequest: ${JSON.stringify({
"speak": speechOutput,
"listen": reprompt,
"card" : {
"title": cardTitle,
"content": cardContent,
"imageObj": imageObj
}
})}`);
this.response.speak(speechOutput)
.listen(reprompt)
.cardRenderer(cardTitle, cardContent, imageObj);
this.emit(':responseReady');
} else {
this.emit('WhereAmI');
}
},
'ResumeGame': function() {
console.log(`ResumeGame:`);
this.emit('WhereAmI');
},
'RestartGame': function() {
console.log(`RestartGame:`);
// clear session attributes
this.event.session.attributes['room'] = undefined;
this.event.session.attributes['visited'] = [];
this.emit('WhereAmI');
},
'WhereAmI': function() {
var speechOutput = "";
if (this.event.session.attributes['room'] === undefined) {
// you just started so you are in the first room
this.event.session.attributes['room'] = $twine[0]['$']['pid'];
speechOutput = `Welcome to ${story.replace('.html','')}. Lets start your game. `;
}
var room = currentRoom(this.event);
console.log(`WhereAmI: in ${JSON.stringify(room)}`);
// get displayable text
// e.g "You are here. [[Go South|The Hall]]" -> "You are here. Go South"
var displayableText = room['_'];
linksRegex.lastIndex = 0;
let m;
while ((m = linksRegex.exec(displayableText)) !== null) {
displayableText = displayableText.replace(m[0], m[1]);
linksRegex.lastIndex = 0;
}
// strip html
displayableText = displayableText.replace(/<\/?[^>]+(>|$)/g, "");
displayableText = displayableText.replace("&", "and");
speechOutput = speechOutput + displayableText;
// create reprompt from links: "You can go north or go south"
var reprompt = "";
linksRegex.lastIndex = 0;
while ((m = linksRegex.exec(room['_'])) !== null) {
if (m.index === linksRegex.lastIndex) {
linksRegex.lastIndex++;
}
if (reprompt === "") {
if (!m[1].toLowerCase().startsWith('if you')) {
reprompt = "You can";
}
} else {
reprompt = `${reprompt} or`;
}
reprompt = `${reprompt} ${m[1]}`;
}
var firstSentence = displayableText.split('.')[0];
var lastSentence = displayableText.replace('\n',' ').split('. ').pop();
var reducedContent = `${firstSentence}. ${reprompt}.`;
// say less if you've been here before
if (this.event.session.attributes['visited'] === undefined) {
this.event.session.attributes['visited'] = [];
}
if (this.event.session.attributes['visited'].includes(room['$']['pid'])) {
console.log(`WhereAmI: player is revisiting`);
speechOutput = reducedContent;
} else {
this.event.session.attributes['visited'].push(room['$']['pid']);
}
var cardTitle = firstSentence;
var cardContent = (reprompt > '') ? reprompt : lastSentence;
var imageObj = undefined;
console.log(`WhereAmI: ${JSON.stringify({
"speak": speechOutput,
"listen": reprompt,
"card" : {
"title": cardTitle,
"content": cardContent,
"imageObj": imageObj
}
})}`);
linksRegex.lastIndex = 0;
if (linksRegex.exec(room['_'])) {
// room has links leading out, so listen for further user input
this.response.speak(speechOutput)
.listen(reprompt)
.cardRenderer(cardTitle, cardContent, imageObj);
} else {
console.log(`WhereAmI: at the end of a branch. Game over.`);
// clear session attributes
this.event.session.attributes['room'] = undefined;
this.event.session.attributes['visited'] = [];
this.response.speak(speechOutput)
.cardRenderer(cardTitle, cardContent, imageObj);
}
this.emit(':responseReady');
},
'Go': function() {
console.log(`Go`);
var slotValues = getSlotValues(this.event.request.intent.slots);
followLink(this.event, [slotValues['direction']['resolved'], slotValues['direction']['synonym']]);
this.emit('WhereAmI');
},
'Page': function() {
// old-school cyoa: "to go south turn to page 20"..you say, "page 20"
console.log(`Page`);
followLink(this.event, this.event.request.intent.slots.number.value);
this.emit('WhereAmI');
},
'Fight': function() {
console.log(`Fight`);
followLink(this.event, [this.event.request.intent.slots.npc.value, 'fight']);
this.emit('WhereAmI');
},
'AMAZON.HelpIntent': function() {
var speechOutput = 'This is the Sample Gamebook Skill. ';
var reprompt = 'Say where am I, to hear me speak.';
speechOutput = speechOutput + reprompt;
var cardTitle = 'Help.';
var cardContent = speechOutput;
var imageObj = undefined;
console.log(`HelpIntent: ${JSON.stringify({
"speak": speechOutput,
"listen": reprompt,
"card" : {
"title": cardTitle,
"content": cardContent,
"imageObj": imageObj
}
})}`);
this.response.speak(speechOutput)
.listen(reprompt)
.cardRenderer(cardTitle, cardContent, imageObj);
this.emit(':responseReady');
},
'AMAZON.CancelIntent': function() {
this.emit('CompletelyExit');
},
'AMAZON.StopIntent': function() {
this.emit('CompletelyExit');
},
'CompletelyExit': function() {
var speechOutput = 'Goodbye.';
if (TableName) {
speechOutput = `Your progress has been saved. ${speechOutput}`;
}
var cardTitle = 'Exit.';
var cardContent = speechOutput;
var imageObj = undefined;
console.log(`CompletelyExit: ${JSON.stringify({
"speak": speechOutput,
"listen": null,
"card" : {
"title": cardTitle,
"content": cardContent,
"imageObj": imageObj
}
})}`);
this.response.speak(speechOutput)
.cardRenderer(cardTitle, cardContent, imageObj);
this.emit(':responseReady');
},
'AMAZON.RepeatIntent': function() {
console.log(`RepeatIntent`);
this.emit('WhereAmI');
},
'Unhandled': function() {
// handle any intent in interaction model with no handler code
console.log(`Unhandled`);
followLink(this.event, this.event.request.intent.name);
this.emit('WhereAmI');
},
'SessionEndedRequest': function() {
// "exit", timeout or error. Cannot send back a response
console.log(`Session ended: ${this.event.request.reason}`);
},
};
function currentRoom(event) {
var currentRoomData = undefined;
for (var i = 0; i < $twine.length; i++) {
if ($twine[i]['$']['pid'] === event.session.attributes['room']) {
currentRoomData = $twine[i];
break;
}
}
return currentRoomData;
}
function followLink(event, direction_or_array) {
var directions = [];
if (direction_or_array instanceof Array) {
directions = direction_or_array;
} else {
directions = [direction_or_array];
}
var room = currentRoom(event);
var result = undefined;
directions.every(function(direction, index, _arr) {
console.log(`followLink: try '${direction}' from ${room['$']['name']}`);
var directionRegex = new RegExp(`.*${direction}.*`, 'i');
let links;
linksRegex.lastIndex = 0;
while ((links = linksRegex.exec(room['_'])) !== null) {
if (links.index === linksRegex.lastIndex) {
linksRegex.lastIndex++;
}
result = links[1].match(directionRegex);
var target = links[2] || links[1];
console.log(`followLink: check ${links[1]} (${target}) for ${direction} => ${result} `);
if (result) {
console.log(`followLink: That would be ${target}`);
for (var i = 0; i < $twine.length; i++) {
if ($twine[i]['$']['name'].toLowerCase() === target.toLowerCase()) {
event.session.attributes['room'] = $twine[i]['$']['pid'];
break;
}
}
break;
}
}
return !result;
});
}
//COOKBOOK HELPER FUNCTIONS
function getSlotValues(filledSlots) {
//given event.request.intent.slots, a slots values object so you have
//what synonym the person said - .synonym
//what that resolved to - .resolved
//and if it's a word that is in your slot values - .isValidated
let slotValues = {};
console.log('The filled slots: ' + JSON.stringify(filledSlots));
Object.keys(filledSlots).forEach(function(item) {
//console.log("item in filledSlots: "+JSON.stringify(filledSlots[item]));
var name = filledSlots[item].name;
//console.log("name: "+name);
if (filledSlots[item] &&
filledSlots[item].resolutions &&
filledSlots[item].resolutions.resolutionsPerAuthority[0] &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
switch (filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
case "ER_SUCCESS_MATCH":
slotValues[name] = {
"synonym": filledSlots[item].value,
"resolved": filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name,
"isValidated": true
};
break;
case "ER_SUCCESS_NO_MATCH":
slotValues[name] = {
"synonym": filledSlots[item].value,
"resolved": filledSlots[item].value,
"isValidated": false
};
break;
}
} else {
slotValues[name] = {
"synonym": filledSlots[item].value,
"resolved": filledSlots[item].value,
"isValidated": false
};
}
}, this);
//console.log("slot values: " + JSON.stringify(slotValues));
return slotValues;
}