-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
673 lines (616 loc) · 33.5 KB
/
server.js
File metadata and controls
673 lines (616 loc) · 33.5 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// import modules
let express = require('express'),
bodyParser = require('body-parser'),
parser = require('fast-xml-parser'),
fs = require('fs'),
path = require('path'),
app = express();
// import module used for checking Alexa requests
let alexaVerifier = require('alexa-verifier');
const { eventNames } = require('process');
// get database connection manager from external file
var database_connection = require('./database_connection');
// get by path the .XML file containing data
const fileNamePath = "./il_falso_profeta.XML";
// set the app entry point on the determinated port
let port = process.env.PORT || 8080;
app.listen(port);
console.log('Server is listening on port: ' + port);
// get all chapters from the .XML file
const allChapters = convertToJson();
// set values to build the Alexa skill response
const SKILL_NAME = 'LibroGameReader';
const PAUSE = '<break time="0.3s" />'
const WHISPER = '<amazon:effect name="whispered"/>'
// specify the system to use the JSON data format and get the body
// of the request
app.use(bodyParser.json({
verify: function getRawBody(req, res, buf) {
req.rawBody = buf.toString();
}
}));
// function used to check if the request is actually from
// Alexa. This verification is required, if the developer
// doesn't verify it, an error occurs. If the verification
// goes right, the function does nothing, otherwise notifies
// an error.
function requestVerifier(req, res, next) {
alexaVerifier(
req.headers.signaturecertchainurl,
req.headers.signature,
req.rawBody,
function verificationCallback(err) {
if (err) {
res.status(401).json({
message: 'Verification Failure',
error: err
});
} else {
next();
}
}
);
}
// entry point where all requests arrives
app.post('/', requestVerifier, async function(req, res) {
// get the user unique id request. It will be used to
// store the book progress in the Postgres database
var id_request = req.body.session.user.userId;
// console.log('Richiesta da utente: '+id_request);
// get all users already registered to the database
var allUsers = await database_connection.getAllUsers();
// check if user is already registered to the database
if(checkUsers(allUsers, id_request) == true)
{
// user already registered
var actual_chapter = await database_connection.getActualChapter(id_request);
var last_chapter = await database_connection.getLastChapter(id_request);
}else{
// new user
var actual_chapter = 0;
var last_chapter = 0;
}
// debug log used to verify the correctness of data
// console.log('Penultimo capitolo letto: ' + last_chapter);
// console.log('Ultimo capitolo letto: ' + actual_chapter);
// check what type of request the web service received
if (req.body.request.type === 'LaunchRequest')
{
// user opens the skill
console.log('Open the skill...');
res.json(helloMessage());
} else if (req.body.request.type === 'SessionEndedRequest')
{
// the skill will be closed directly from Alexa
console.log('Session end...');
} else if (req.body.request.type === 'IntentRequest') {
switch (req.body.request.intent.name)
{
case 'AMAZON.StopIntent':
// user wants to close the skill
console.log('Exit from the skill...')
res.json(stopAndExit());
break;
case 'helpIntent':
// user wants to understand what he can do in the skill
console.log('List possible commands...');
res.json(helpCommand());
break;
case 'startReadingIntent':
// user wants to start reading the book
console.log('Start or resume reading...');
res.json(startReading(actual_chapter));
break;
case 'getNextChapterIntent':
// user wants to get a new chapter
console.log('Get new chapter...');
var chapter = req.body.request.intent.slots.capitolo.value;
res.json(getNewChapter(chapter, actual_chapter, id_request));
break;
case 'getLastChapterIntent':
// user wants to go back to the penultimate chapter he read
// if the user is reading the first chapter or he has yet
// to start reading the book, an error will be notified by Alexa
if(actual_chapter == 0 || actual_chapter == 1)
{
var speechOutput = 'Non puoi tornare al capitolo precedente!';
res.json(buildResponseWithRepromt(speechOutput, false, '', ''));
}
else{
res.json(getLastChapter(last_chapter, id_request));
}
break;
case 'readAgainIntent':
// user wants to read again the actual chapter
if (actual_chapter != 0)
{
console.log('Read again actual chapter...');
res.json(readAgainChapter(actual_chapter));
}else{
var speechOutput = 'Devi ancora iniziare la lettura del libro!';
res.json(buildResponseWithRepromt(speechOutput, false, '', ''));
}
break;
case 'getRandomNumberIntent':
// user wants to get a random number in a specified interval
console.log('Get random number...');
var numInf = req.body.request.intent.slots.numInf.value;
var numSup = req.body.request.intent.slots.numSup.value;
res.json(getRandomNumber(numInf, numSup));
break;
case 'restartBookIntent':
// user wants to start reading the book all over again
console.log('Restart book...');
res.json(restartBook(0, id_request));
break;
case 'simulateEntireFightIntent':
// user wants to simulate an entire fight
console.log('Simulate entire fight...');
res.json(simulateEntireFight());
break;
case 'simulateRoundOfFightIntent':
// user wants to simulate just a round of a fight
console.log('Simulate a round of the fight...');
res.json(simulateRoundOfFight());
break;
case 'introductionRulesIntent':
// user wants to know the rules of the game
console.log('Show the user the rules of the game...');
res.json(introductionRules());
break;
default:
}
}
});
// function used to tell the user which abilities he has to bring
// with him during the story
function introductionRules()
{
/* var speechOutput = 'Prima di iniziare il tuo viaggio, è importante conoscere \
quali abilità possiede Lupo Solitario, ovvero tu, che sarai \
il protagonista della storia! Avrai due caratteristiche principali: \
combattività e resistenza. Se hai a disposizione del libro cartaceo, \
puoi determinare i valori iniziali delle tue abilità direttamente \
dalla tabella del destino, altrimenti puoi utilizzare il generatore \
di numeri casuali con l\'apposito comando offerto direttamente dalla skill.\ Ti \
basterà generare un numero tra 0 e 9 ed aggiungere 10 per ottenere il totale \
della tua combattività, mentre dovrai aggiungere 20 per ottenere la tua \
resistenza. Con queste due attività potrai poi prendere parte ai combattimenti. \
Durante il cammino nella tua avventura, incontrerai diversi nemici, dei quali \
verranno indicati combattività e resistenza. Tramite gli opportuni comandi \
usufruibili pronunciando ,scopri le funzioni, potrai simulare l\'intero \
combattimento o solo un round di esso, oppure potrai svolgerlo tu da solo e poi\
proseguire per la relativa strada seguendo le regole specificate nell\'introduzione \
del libro cartaceo. Una volta che hai esaurito i tuoi punti resistenza, la tua \
avventura finisce, e potrai ricominciare la tua storia sperando di avere fortuna. \
In bocca al lupo Lupo Solitario!'; */
var speechOutput = 'È l’autunno del 1995, più di un anno è trascorso dal tuo incarico in Norvegia, da allora hai \
continuato i tuoi studi sui movimenti pagani, le sette, i culti, pubblicando diversi articoli su riviste della Chiesa \
e su L’osservatore Romano, hai partecipato a diverse conferenze per il CESNUR, il Centro Studi Nuove Religioni, ed a \
trasmissioni televisive locali, e ti è stato commissionato un libro per conto delle edizioni San Paolo che raccolga il \
tuo lavoro svolto fin qui. Ritieni che divulgare il più possibile queste informazioni possa essere un valido strumento \
perché le persone si possano difendere dalle influenze del Maligno, riconoscendole e sapendo a chi rivolgersi. È una uggiosa \
mattina d’ottobre quando un confratello ti avvisa che l’abate desidera vederti nel suo studio. Lasci immediatamente quello \
che stai facendo e percorri i silenti e ampi corridoi dell’abbazia per raggiungere il tuo superiore. Raggiungi la porta \
dell’abate, nonostante il cartello “Entrare senza bussare”, bussi e poi apri la porta. \'Oh, padre Alessandro, entra e \
accomodati.\'. L’Abate è al telefono con un qualche vescovo a giudicare dal tono, la sua scrivania è al solito ricoperta di \
libri, plichi, fogli, documenti, richieste che giungono da ogni dove, lettere che riceve e scrive. Ti siedi di fronte a lui \
e attendi che finisca la telefonata. Quando l’abate attacca il ricevitore si inforca gli occhiali e prende una lettera dal \
cumulo che sommerge il suo tavolo. \'Sono molto contento del tuo lavoro\', ti dice \'le tue conoscenze sono sempre più \
approfondite e ho notato che anche il tuo spirito si è decisamente rafforzato dopo la tua esperienza dello scorso anno.\' \
\'Beh, sì, devo dire che è stata molto dura.\'. \'Bene, perché dovrò darti un nuovo incarico, e ci vorrà tutta la tua fede \
e le tue capacità per affrontarlo.\'. \'Vi ascolto.\'. \'Ho ricevuto una lettera stamani, è di un mio vecchio amico, \
Frate Zeno, un cappuccino in odor di santità che una decina di anni fa decise di ritirarsi alla vita di eremita sugli \
Appennini umbri. Aveva la fama di curare le persone e scacciare di demòni con l’imposizione delle mani e la preghiera, e \
queste figure la Chiesa, purtroppo, le vede come ingombranti piuttosto che come risorse. Prima ancora che da Roma partisse \
una indagine nei suoi confronti, come era accaduto al buon Padre Pio da Pietralcina, decise di lasciare la vita pubblica e \
si stabilì in un eremo, dove vive costantemente in preghiera. Alcune settimane fa ricevette nel suo eremo la visita di un \
vecchio sacerdote con una coppia di genitori in cerca di aiuto. I due sostenevano che la loro figlia di 8 anni sia posseduta \
dal demonio, erano anni che giravano per specialisti della psichiatria, l’avevano sottoposta ad esami e cure di ogni tipo, \
ma non c’era niente da fare. Col passare del tempo stava sempre più male, con scatti d’ira in cui gridava oscenità e bestemmie \
con voce gutturale, aveva dimostrato una forze incredibile per una bambina della sua età, e in alcuni casi aveva anche \
profetizzato morti e malattie di persone che l’avevano incrociata. Frate Zeno si commosse e, senza avere alcun permesso da \
parte della Chiesa, acconsentì a vedere la bambina. Una volta raggiunta la casa della bambina chiese di potere restare da solo \
con lei nella sua stanza. Non appena vi entrò la bambina iniziò ad insultarlo, fissandolo con occhi spietati e omicidi, e se \
non fosse stato per le cinghie che la tenevano ferma l’avrebbe certamente aggredito. Bastò un breve colloquio per convincersi \
che effettivamente c’era uno spirito immondo dentro la bambina. Iniziò così un lungo e faticoso esorcismo, ma dopo due settimane \
ancora non vedeva miglioramenti. Sostiene che dentro la bambina ci sia il demone più potente con cui abbia mai avuto a che \
fare dopo il Diavolo, e che non riuscirà mai a scacciarlo finché non saprà il suo nome, e nonostante i suoi sforzi ancora non \
è riuscito a farglielo dire.\'. \'E’ una storia terribile\', commenti \'ma io cosa centro?\'. \'Frate Zeno non ce la farà mai \
da solo, ha bisogno che qualcuno scopra il nome di quello spirito immondo. La bambina è stata adottata alcuni anni fa, il suo \
legame con il demone è molto forte e radicato probabilmente perché la piccola gli è stata votata sin dalla nascita. Forse la \
madre apparteneva a qualche setta satanica, non sarebbe la prima volta che accade.\'. \'Quindi vuole che scopra la storia della \
bambina per trovare il nome di quel demonio?\'. \'Esattamente. Frate Zeno ha scritto nella lettera dove la bambina è stata \
adottata, è il tuo unico indizio.\'. L’abate ti porge un foglio con il recapito della persona a cui rivolgerti. \'Che il \
Signore possa guidarti e ti protegga, padre Alessandro.\'. Prendi il foglietto, saluti l’abate e ti congedi. Inizia \
l’avventura al 1. Pronuncia , inizia la lettura, per iniziare subito, in bocca al lupo!';
var jsonObj = buildResponseWithRepromt(speechOutput, false, '', '');
return jsonObj;
}
// function used when the user wants to simulate just one round
// of the fight. It return how many endurance points the user and
// the opponent lost
function simulateRoundOfFight()
{
// according to the existing table at the end of the book
// in a round the maximum points that a character can lose
// is 18
var userPoints = Math.floor(Math.random() * 19);
var oppPoints = Math.floor(Math.random() * 19);
var speechOutput = 'Al termine di questo round del combattimento, hai \
perso ' + userPoints + ' punti di resistenza, mentre \
il tuo avversario ne ha persi ' + oppPoints + '.';
speechOutput += ' Aggiorna il tuo punteggio della resistenza e controlla se \
puoi procedere con il prossimo round o se tu o il tuo avversario \
avete esaurito i punti resistenza e siete deceduti. In caso \
di morte, ricomincia la storia dall\'inizio con l\'apposito comando, \
perchè la tua avventura finisce qui!';
var jsonObj = buildResponseWithRepromt(speechOutput, false, '', '');
return jsonObj;
}
// function used when the user wants to simulate an entire fight. The user can
// win or lose the fight, rispctively with probability of 75% and 25%. If the user
// loses, he must restart the story from the beginning, otherwise, Alexa will tell
// the user how many endurance point he lost and how many round the fight lasted
function simulateEntireFight()
{
// generate random number between 1 and 100. If number <= 75
// the user wins the fight, otherwise he loses
var number = Math.floor(Math.random() * 100) + 1;
if(number <= 75)
{
// user wins the fight
var rounds = Math.floor(Math.random() * 8) + 1;
var points = Math.floor(Math.random() * 20) + 1;
var speechOutput = 'Complimenti, hai vinto il combattimento!'
speechOutput += ' Il combattimento è durato ' + rounds + ' round, e purtroppo \
hai perso ' + points + ' punti resistenza.';
speechOutput += ' Ricordati di tener traccia della tua combattività e dei\
tuoi punti resistenza per i prossimi combattimenti! Se i punti resistenza \
che hai perso sono più dei tuoi punti resistenza residui, purtroppo \
la tua avventura finisce qua, e dovrai ricominciare la tua storia con \
l\'apposito comando!';
var jsonObj = buildResponseWithRepromt(speechOutput, false, '', '');
return jsonObj;
}else{
// user loses the fight
var speechOutput = 'Purtroppo hai perso tutti i tuoi punti resistenza. La tua \
avventura finisce qui. Ricomincia la tua storia pronunciando \
vai al capitolo 1, sperando questa volta di avere più fortuna!';
var jsonObj = buildResponseWithRepromt(speechOutput, false, '', '');
return jsonObj;
}
}
// function used when te user wants to understand what he can do with the skill
function helpCommand()
{
var speechOutput = 'Con questa skill potrai leggere in maniera dinamica un libro gioco.';
speechOutput += ' Per iniziare la lettura del libro gioco pronuncia ,inizia la lettura,.';
speechOutput += ' Durante la lettura potrai, dove richiesto, tornare al capitolo precedente, \
pronunciando ,torna al capitolo precedente,.';
speechOutput += ' Oppure, potrai proseguire ai capitoli successivi indicati nel testo, \
pronunciando ,vai al capitolo, seguito dal numero del capitolo con il quale \
desideri procedere nella tua avventura.';
speechOutput += ' Se vuoi rileggere il capitolo, ti basterà pronunciare ,rileggi,. ';
speechOutput += ' Potrai inoltre ricominciare la lettura dall\'inizio del libro, perdendo però \
tutti i progressi fatti, pronunciando ,ricomincia libro dall\'inizio,. ';
speechOutput += ' Durante la tua avventura, dovrai affrontare combattimenti e scelte basate su \
numeri casuali estratti come ad esempio con il lancio di un dado. La skill ti \
permette di simulare questo lancio di dado pronunciando ,estrai un numero casuale\
tra numero 1 ed numero 2, dove numero 1 e numero 2 sono i due estremi compresi nell\'intervallo da cui \
desideri estrarre il numero.';
speechOutput += ' Se ti trovi davanti un combattimento, potrai simularlo pronunciando ,simula intero combattimento,. \
Ti verrà notificato l\'esito del combattimento con le informazioni necessarie come i punti \
di resistenza persi o il numero di round in cui il combattimento è stato eseguito.';
speechOutput += ' Potrai inoltre simulare anche un solo round del combattimento pronunciando ,simula un round \
del combattimento,. Ti verranno poi comunicati il numero di punti resistenza persi da te e dal \
tuo nemico.';
speechOutput += ' Potrai infine uscire dalla skill, pronunciando ,stop,.'
speechOutput += ' Inizia ora la lettura pronunciando ,inizia la lettura,.';
var jsonObj = buildResponseWithRepromt(speechOutput, false, '', '');
return jsonObj;
}
// function used to check if the user that made the request is already registered
// to the database. If he is already registered, it returns 'true', otherwise, it
// returns 'false' and the new user is added to the database by the function
// addUser()
function checkUsers(allUsers, id_request)
{
var i;
var exists = 0;
for(var i = 0; i < allUsers.length; i++)
{
if(allUsers[i].user_id == id_request)
{
console.log('User already connected...');
exists = 1;
}
}
if(exists == 0)
{
addUser(id_request);
return false;
}else{
return true;
}
}
// function used to add the new user with his unique id_request to the database
// by performing an SQL query using the database_connection variable
function addUser(id_request)
{
console.log('Add new user...');
var queryString = 'INSERT INTO lastvisitedchapter VALUES (\''+id_request+'\', 0, 0);';
database_connection.pool.query(queryString, function(error) {
if (error) {
console.log(error);
response.status(400).send(error);
}
});
}
// function used whem the user wants to quit the skill
function stopAndExit()
{
const speechOutput = 'Ciao, buona giornata e torna presto!';
var jsonObj = buildResponse(speechOutput, true, '');
return jsonObj;
}
// function used when the user wants to get a random number
// in the specified interval [numInf, numSup]
function getRandomNumber(numInf, numSup)
{
var number = Math.floor(Math.random() * (Number(numSup) - Number(numInf) + 1)) + Number(numInf);
const speechOutput = WHISPER + 'Il tuo numero casuale tra ' + numInf + ' e ' + numSup + ' è ' + number;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
// function used when the user wants to get the penultimate chapter
function getLastChapter(chapter, id_request)
{
const allChapters = convertToJson();
// update into the database the actual chapter which will be the
// penultimate chapter
updateActualChapter(id_request, chapter);
var chapterToRead = allChapters.chapters.chapter[(chapter-1)].description;
const speechOutput = WHISPER + chapterToRead + PAUSE;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
// function used when the user wants to reading the book all over again
function restartBook(chapter, id_request)
{
const allChapters = convertToJson();
// update into the database the actual chapter which will be
// 0, which allow the user to start reading again the entire book
updateActualChapter(id_request, chapter);
updateLastChapter(id_request, chapter);
const speechOutput = WHISPER + 'Hai eliminato tutti i progressi della tua storia. \
Ora dovrai ricominciare il tuo viaggio dall\'inizio. \
Pronuncia vai al capitolo 1 per ricominciare la lettura!' + PAUSE;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
// function used when the user wants to read again the actual chapter
function readAgainChapter(chapter)
{
const allChapters = convertToJson();
console.log('Read again chapter '+chapter+'...');
var chapterToRead = allChapters.chapters.chapter[(chapter-1)].description;
const speechOutput = WHISPER + chapterToRead + PAUSE;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
// function used when the user open the skill. Tell the user what he can do
function helloMessage()
{
var speechOutput = 'Ciao! Benvenuto su Libro Game Reader! Con questa skill potrai \
interagire con un libro gioco in maniera dinamica!';
/* speechOutput += ' Pronuncia ,scopri le funzioni, per ascoltare quali comandi puoi utilizzare \
con questa skill, oppure pronuncia ,leggi le regole del gioco, per \
scoprire le meccaniche e le caratteristiche con il quale Lupo Solitario \
avrà a che fare lungo il suo cammino. In alternativa, se hai già utilizzato la skill \
e sei a conoscenza dei suoi comandi, pronuncia ,inizia la lettura, per iniziare \
o riprendere la lettura da dove avevi lasciato l\'ultima volta!'
*/
speechOutput += 'Pronuncia ,scopri le funzioni, per ascoltare quali comandi puoi utilizzare \
con questa skill, oppure pronuncia ,leggi le regole del gioco, per \
scoprire il contesto in cui ti trovi ed il prologo della storia de ,Il Falso Profeta di Cagliostro,.\
In alternativa, se hai già utilizzato la skill e sei a conoscenza dei suoi comandi, pronuncia \
,inizia la lettura, per iniziare o riprendere la lettura da dove avevi lasciato l\'ultima volta!';
var jsonObj = buildResponseWithRepromt(speechOutput, false, '', '');
return jsonObj;
}
// function used when the user wants to start or resume reading the book
function startReading(actual_chapter)
{
if(actual_chapter == 0)
{
var speechOutput = 'Inizia ora a leggere il , Il Falso Porfeta di Cagliostro,! \
Pronuncia vai al capitolo 1 per iniziare la lettura!';
}else{
var speechOutput = 'Riprendi la lettura dal capitolo '+actual_chapter+'. Pronuncia rileggi\
per riascoltarlo.';
}
var jsonObj = buildResponseWithRepromt(speechOutput, false, '', '');
return jsonObj;
}
// function used to build the response send to Alexa. This function builds
// a response, and after Alexa has read it, the skill will be closed.
function buildResponse(speechText, shouldEndSession, cardText) {
const speechOutput = "<speak>" + speechText + "</speak>"
var jsonObj = {
"version": "1.0",
"response": {
"shouldEndSession": shouldEndSession,
"outputSpeech": {
"type": "SSML",
"ssml": speechOutput
},
"card": {
"type": "Simple",
"title": SKILL_NAME,
"content": cardText,
"text": cardText
}
}
}
return jsonObj;
}
// function used to build the response send to Alexa. This function builds
// a response, and after Alexa has read it, the skill will remain open and the
// user will be able to specify other commands.
function buildResponseWithRepromt(speechText, shouldEndSession, cardText, reprompt)
{
const speechOutput = "<speak>" + speechText + "</speak>"
var jsonObj = {
"version": "1.0",
"response": {
"shouldEndSession": shouldEndSession,
"outputSpeech": {
"type": "SSML",
"ssml": speechOutput
},
"card": {
"type": "Simple",
"title": SKILL_NAME,
"content": cardText,
"text": cardText
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": reprompt,
"ssml": reprompt
}
}
}
}
return jsonObj;
}
// function used to convert XML data to JSON object, which will be used
// to get data more easily. Return a JSON object.
function convertToJson()
{
const xmlData = fs.readFileSync(fileNamePath).toString();
return parser.parse(xmlData);
}
// function used to update the actual chapter into the Postgres database
// by performing a query with the database_connection variable
function updateActualChapter(user_id, chapter)
{
var queryString = 'UPDATE lastvisitedchapter SET actual_chapter = '+chapter+' WHERE user_id=\''+user_id+'\';';
database_connection.pool.query(queryString, function(error) {
if (error) {
console.log(error);
response.status(400).send(error);
}
});
}
// function used to update the penultimate chapter into the Postgres database
// by performing a query with the database_connection variable
function updateLastChapter(user_id, chapter)
{
var queryString = 'UPDATE lastvisitedchapter SET last_chapter = '+chapter+' WHERE user_id=\''+user_id+'\';';
database_connection.pool.query(queryString, function(error) {
if (error) {
console.log(error);
response.status(400).send(error);
}
});
}
// function used when the user wants to get a new chapter. The function checks
// if the specified chapter choosed by the user could be reached from the actual chapter.
// If the chapter can't be reached, Alexa tells the user which chapters can
// be reached.
function getNewChapter(chapter, actual_chapter, id_request)
{
const allChapters = convertToJson();
console.log('Try to get chapter...'+chapter);
if(actual_chapter != 0)
{
if((chapter != actual_chapter))
{
if(allChapters.chapters.chapter[(actual_chapter-1)].nextChapters.nextChapter.length == undefined)
var length = 1;
else
var length = allChapters.chapters.chapter[(actual_chapter-1)].nextChapters.nextChapter.length;
if(length != 1)
{
for(var i = 0; i < length; i++)
{
if(chapter == allChapters.chapters.chapter[(actual_chapter-1)].nextChapters.nextChapter[i])
{
updateLastChapter(id_request, actual_chapter);
updateActualChapter(id_request, chapter);
var chapterToRead = allChapters.chapters.chapter[(chapter-1)].description;
if(allChapters.chapters.chapter[(chapter-1)].flag_death == true)
{
chapterToRead += ' Purtroppo non sei riuscito a concludere la tua avventura. Ricomincia il \
tuo percorso e scegli una strada diversa pronunciando ricomincia dall\'inizio!';
updateActualChapter(id_request, 0);
updateLastChapter(id_request, 0);
}
if(allChapters.chapters.chapter[(chapter-1)].flag_final == true)
{
chapterToRead += ' Complimenti! Sei uscito vittorioso dalla tua avventura! Pronuncia stop per uscire dalla skill\
oppure prununcia ricomincia dal\'inizio per ricominciare la tua avventura percorrendo una strada diversa!';
updateActualChapter(id_request, 0);
updateLastChapter(id_request, 0);
}
const speechOutput = WHISPER + chapterToRead + PAUSE;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
}
var speechOutput = 'Puoi proseguire solamente andando ai capitoli: ';
for(var i = 0; i < length; i++)
{
speechOutput += allChapters.chapters.chapter[(actual_chapter-1)].nextChapters.nextChapter[i]+ ' ';
}
return buildResponseWithRepromt(speechOutput, false, '', '');
}
else
{
if(chapter == allChapters.chapters.chapter[(actual_chapter-1)].nextChapters.nextChapter)
{
updateLastChapter(id_request, actual_chapter);
updateActualChapter(id_request, chapter);
var chapterToRead = allChapters.chapters.chapter[(chapter-1)].description;
const speechOutput = WHISPER + chapterToRead + PAUSE;
if(allChapters.chapters.chapter[(chapter-1)].flag_death == true)
{
chapterToRead += ' Purtroppo non sei riuscito a concludere la tua avventura. Ricomincia il \
tuo percorso e scegli una strada diversa pronunciando ricomincia dall\'inizio!';
updateActualChapter(id_request, 0);
updateLastChapter(id_request, 0);
}
if(allChapters.chapters.chapter[(chapter-1)].flag_final == true)
{
chapterToRead += ' Complimenti! Sei uscito vittorioso dalla tua avventura! Pronuncia stop per uscire dalla skill\
oppure prununcia ricomincia dal\'inizio per ricominciare la tua avventura percorrendo una strada diversa!';
updateActualChapter(id_request, 0);
updateLastChapter(id_request, 0);
}
return buildResponseWithRepromt(speechOutput, false, '', '');
}
var speechOutput = 'Puoi proseguire solamente andando al capitolo: '+allChapters.chapters.chapter[(actual_chapter-1)].nextChapters.nextChapter;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
}
else
{
const speechOutput = 'Ti trovi già al capitolo '+chapter;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
}
else
{
if(chapter == 1)
{
updateLastChapter(id_request, 0);
updateActualChapter(id_request, chapter);
var chapterToRead = allChapters.chapters.chapter[(chapter-1)].description;
const speechOutput = WHISPER + chapterToRead + PAUSE;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
else
{
const speechOutput = WHISPER + 'devi iniziare la lettura dal capitolo 1' + PAUSE;
return buildResponseWithRepromt(speechOutput, false, '', '');
}
}
}