-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (129 loc) · 5.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
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
'use strict';
const Alexa = require('alexa-sdk');
const APP_ID = undefined; // TODO replace with your app ID (OPTIONAL).
const languageStrings = {
'en': {
translation: {
SKILL_NAME: 'DragonSlayer',
HELP_MESSAGE: 'You can say stop to quit skill.',
HELP_REPROMPT: 'What can I help you with?',
STOP_MESSAGE: 'Goodbye!',
},
},
};
var playerHealth = 100;
var dragonHealth = 100;
const handlers = {
'LaunchRequest': function () {
this.emit('NewGame');
},
'NewGame': function () {
playerHealth = 100;
dragonHealth = 100;
this.emit(':ask', 'Welcome to the game! You are now a fierce knight \
locked in a battle with a deadly dragon. ' + healthReport() + promptMove(),this.t('HELP_MESSAGE'));
},
'QuickAttack': function () {
var yourAttackReport = '';
var damage = attack(15, 0.1);
if(damage == 0) {
yourAttackReport += 'Your attack missed!';
} else {
yourAttackReport += 'You dealt ' + damage + ' damage! ';
}
dragonHealth -= damage;
if(checkGameOver() == 1) {
this.emit(':ask', 'Quick attack executed! ' + yourAttackReport + ' Having no health left, the dragon keels over and dies. You win! To play again,\
say; new game; or say stop to exit the game',this.t('HELP_MESSAGE'));
}
var dragonAttack = attack(20, 0.2);
var dragonAttackReport = 'The dragon strikes and';
if(dragonAttack == 0) {
dragonAttackReport += 'misses!';
} else {
dragonAttackReport += 'deals ' + dragonAttack + ' damage! ';
}
playerHealth -= dragonAttack;
if(checkGameOver() == -1) {
this.emit(':ask', 'Looks like you\'ve no health left. The dragon devours you. Game over! To play again,\
say; new game; or say stop to exit the game',this.t('HELP_MESSAGE'));
}
this.emit(':ask', 'Quick attack executed! ' + yourAttackReport + dragonAttackReport + healthReport() + promptMove(),this.t('HELP_MESSAGE'));
},
'HeavyAttack': function () {
var yourAttackReport = '';
var damage = attack(35, 0.4);
if(damage == 0) {
yourAttackReport += 'Your attack missed!';
} else {
yourAttackReport += 'You dealt ' + damage + ' damage! ';
}
dragonHealth -= damage;
if(checkGameOver() == 1) {
this.emit(':ask', 'Quick attack executed! ' + yourAttackReport + ' Having no health left, the dragon keels over and dies. You win! To play again,\
say; new game or say stop to exit',this.t('HELP_MESSAGE'));
}
var dragonAttack = attack(20, 0.2);
var dragonAttackReport = 'The dragon strikes and';
if(dragonAttack == 0) {
dragonAttackReport += 'misses!';
} else {
dragonAttackReport += 'deals ' + dragonAttack + ' damage! ';
}
playerHealth -= dragonAttack;
if(checkGameOver() == -1) {
this.emit(':ask', dragonAttackReport + 'Looks like you\'ve no health left. The dragon devours you. Game over! To play again,\
say; new game or say stop to exit',this.t('HELP_MESSAGE'));
}
this.emit(':ask', 'Heavy attack executed! ' + yourAttackReport + dragonAttackReport + healthReport() + promptMove(), 'Please say that again?');
},
'AMAZON.HelpIntent': function () {
this.emit(':ask', this.t('HELP_MESSAGE') + "You can say 'heavy attack' to launch a \
heavy attack with a higher damage but lower accuracy, or\
say 'quick attack' to launch a quick attack with a lower damage but a \
much higher accuracy. Which attack would you like to execute?", this.t('HELP_MESSAGE'));
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'HiddenTechnique': function () {
dragonHealth = 1;
this.emit(':ask', 'You use a hidden technique! ' + healthReport() + promptMove());
}
};
function healthReport() {
return 'You have ' + playerHealth + ' health points and \
the dragon has ' + dragonHealth + ' health points. ' ;
}
function attack(max, missChance) {
var damage = Math.floor(Math.random() * (max + 1));
if(Math.random() <= missChance) {
damage = 0;
}
return damage;
}
function checkGameOver() {
if(dragonHealth <= 0) {
return 1;
} else if(playerHealth <= 0) {
return -1;
} else {
return 0;
}
}
function promptMove() {
return 'Do you want to execute a quick attack or a heavy attack?';
}
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};