-
Notifications
You must be signed in to change notification settings - Fork 46
/
battle-text.js
276 lines (264 loc) · 8.61 KB
/
battle-text.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
var pokeapi = require('./poke-api.js'),
stateMachine = require('./state-machine.js'),
moves = require('./move-types.js'),
Q = require('q');
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
module.exports = {}
/*
* Return a text string when the command doesn't match defined commands.
*/
module.exports.unrecognizedCommand = function(commandsArray) {
var textString = "I don't recognize the command _{cmd}_ .";
//get rid of the 'pkmn'
commandsArray.shift();
textString = textString.replace("{cmd}", commandsArray.join(" "));
return Q.fcall(function(){ return textString; });
}
/*
* Return a text string when the user chooses a Pokemon.
* Fetch the pokemon from the API, choose 4 random moves, write them to REDIS,
* and then return a message stating the pokemon, its HP, and its moves.
*/
module.exports.userChoosePokemon = function(commandsArray) {
var commandString = commandsArray.join(" "),
pokemonName = commandsArray[3],
textString = "You chose {pkmnn}. It has {hp} HP, and knows ",
moves = [],
movePromises = [];
//validate that the command was "pkmn i choose {pokemon}"
if(!commandString.match(/i choose/i)) {
return module.exports.unrecognizedCommand(commandsArray);
}
return pokeapi.getPokemon(pokemonName).then(function(pkmndata){
moves = shuffle(pkmndata.moves);
for(var i = 0; i < 4; i++) {
movePromises.push(
pokeapi.getMove("http://pokeapi.co"+moves[i].resource_uri)
.then(stateMachine.addMove)
)
//format: "vine whip, leer, solar beam, and tackle."
if(i < 3) {
textString += moves[i].name;
textString += ", ";
} else {
textString += "and ";
textString += moves[i].name;
textString += ".";
}
}
return Q.allSettled(movePromises)
.then(function(){
return stateMachine.setUserHP(pkmndata.hp);
})
.then(function(){
return stateMachine.setUserPkmnTypes(pkmndata.types.map(function(val){
return val.name;
}));
})
.then(function(){
textString = textString.replace("{pkmnn}", pkmndata.name);
textString = textString.replace("{hp}", pkmndata.hp);
var stringy = "" + pkmndata.pkdx_id;
if (stringy.length == 1) {
stringy = "00" + stringy;
} else if (stringy.length == 2) {
stringy = "0" + stringy;
}
return {
text: textString,
spriteUrl: "http://sprites.pokecheck.org/i/"+stringy+".gif"
}
});
});
}
/*
* Return a text string when the NPC chooses a Pokemon.
* Fetch the pokemon from the API, choose 4 random moves, write them to REDIS,
* and then return a message stating the pokemon.
*/
module.exports.npcChoosePokemon = function(dex_no) {
var textString = "I Choose {pkmnn}!",
moves = [],
movePromises = [];
return pokeapi.getPokemon(dex_no).then(function(pkmnData){
moves = shuffle(pkmnData.moves);
for(var i = 0; i < 4; i++) {
movePromises.push(
pokeapi.getMove("http://pokeapi.co"+moves[i].resource_uri)
.then(stateMachine.addMoveNPC)
)
}
return Q.allSettled(movePromises)
.then(function(){
return stateMachine.setNpcHP(pkmnData.hp);
})
.then(function(){
return stateMachine.setNpcPkmnTypes(pkmnData.types.map(function(val){
return val.name;
}));
})
.then(function(){
textString = textString.replace("{pkmnn}", pkmnData.name);
var stringy = "" + pkmnData.pkdx_id;
if (stringy.length == 1) {
stringy = "00" + stringy;
} else if (stringy.length == 2) {
stringy = "0" + stringy;
}
return {
text: textString,
spriteUrl: "http://sprites.pokecheck.org/i/"+stringy+".gif"
}
});
});
}
/*
* Return a text string when the battle starts.
* Stores the player's Slack username and what channel the battle is in,
* and chooses a Pokemon for the NPC from the original 151.
*/
module.exports.startBattle = function(slackData) {
var textString = "OK {name}, I'll battle you! ".replace("{name}", slackData.user_name),
dex_no = Math.ceil(Math.random() * 151);
return stateMachine.newBattle(slackData.user_name, slackData.channel_name)
.then(function() {
return module.exports.npcChoosePokemon(dex_no);
})
.then(function(pkmnChoice){
return {
text: textString + '\n' + pkmnChoice.text,
spriteUrl: pkmnChoice.spriteUrl
}
})
}
module.exports.endBattle = function() {
return stateMachine.endBattle();
}
var effectivenessMessage = function(mult) {
switch(mult) {
case 0:
return "It doesn't have an effect. ";
break;
case 0.5:
case 0.25:
return "It's not very effective... ";
break;
case 1:
return " ";
break;
case 2:
case 4:
return "It's super effective! ";
break;
default:
return " ";
break;
}
}
/*
* Helper function for using one of the user's pokemon's move.
* First check to see if the move is among the allowed moves,
* calculate the type effectiveness, calculate the damage,
* and then return a message.
*/
var useMoveUser = function(moveName) {
var textString = "You used {mvname}! {effctv}",
textStringDmg = "It did {dmg} damage, leaving me with {hp}HP!";
return stateMachine.getUserAllowedMoves()
.then(function(moves){
if(moves.indexOf(moveName) !== -1) {
return stateMachine.getSingleMove(moveName);
} else {
throw new Error("Your pokemon doesn't know that move.");
}
})
.then(function(moveData){
textString = textString.replace("{mvname}", moveName);
return stateMachine.getNpcPkmnTypes()
.then(function(types){
return pokeapi.getAttackMultiplier(moveData.type, types[0], types[1])
.then(function(multiplier){
//do damage
var totalDamage = Math.ceil( (moveData.power / 5) * multiplier )
return stateMachine.doDamageToNpc(totalDamage)
.then(function(hpRemaining){
if(parseInt(hpRemaining, 10) <= 0) {
return stateMachine.endBattle()
.then(function(){
return "You Beat Me!";
})
}
textString = textString.replace("{effctv}", effectivenessMessage(multiplier));
textStringDmg = textStringDmg.replace("{dmg}", totalDamage);
textStringDmg = textStringDmg.replace("{hp}", hpRemaining);
if(multiplier == 0)
return textString;
return textString + textStringDmg;
})
});
})
})
}
/*
* Helper function for using one of the NPC's pokemon's move.
* First check to see if the move is among the allowed moves,
* calculate the type effectiveness, calculate the damage,
* and then return a message.
*/
var useMoveNpc = function() {
var textString = "I used {mvname}! {effctv}",
textStringDmg = "It did {dmg} damage, leaving you with {hp}HP!",
randMove = Math.floor(Math.random() * 4);
return stateMachine.getNpcAllowedMoves()
.then(function(moves){
textString = textString.replace("{mvname}", moves[randMove]);
return stateMachine.getSingleMove(moves[randMove]);
})
.then(function(moveData){
return stateMachine.getUserPkmnTypes()
.then(function(types){
return pokeapi.getAttackMultiplier(moveData.type, types[0], types[1])
.then(function(multiplier){
//do damage
var totalDamage = Math.ceil( (moveData.power / 5) * multiplier )
return stateMachine.doDamageToUser(totalDamage)
.then(function(hpRemaining){
if(parseInt(hpRemaining, 10) <= 0) {
return stateMachine.endBattle()
.then(function(){
return "You Lost!";
})
}
textString = textString.replace("{effctv}", effectivenessMessage(multiplier));
textStringDmg = textStringDmg.replace("{dmg}", totalDamage);
textStringDmg = textStringDmg.replace("{hp}", hpRemaining);
if(multiplier == 0)
return textString;
return textString + textStringDmg;
})
});
})
})
}
/*
* When the user uses a move, calculate the results of the user's turn,
* then the NPC's turn. If either one of them ends the battle, don't show
* the other result.
*/
module.exports.useMove = function(moveName) {
return Q.all([useMoveNpc(), useMoveUser(moveName)])
.then(function(results){
if(results[1] === "You Beat Me!") {
return results[1];
} else if (results[0] === "You Lost!") {
return results[0];
} else {
return results[1] + "\n" + results[0];
}
})
}