-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
268 lines (245 loc) · 6.85 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
var readline = require('readline');
var Battle = require('./src/Battle');
var entities = require('./src/entities');
var cmd = readline.createInterface({
input: process.stdin,
outpu: process.stdout,
prompt: '> '
});
var parties;
var partyNames = { heroes: 'héroes', monsters: 'monstruos' };
var action;
var battle;
var battleLine;
setupBattle();
function setupBattle() {
battle = new Battle();
battle.setup(getRandomSetup());
battle.on('start', function (charactersByParties) {
parties = charactersByParties;
console.log('¡La batalla comienza!');
});
battle.on('end', function (result) {
console.log('¡Fin de la batalla!');
console.log('Bando ganador: ' + result.winner);
process.exit(0);
});
battle.on('turn', function (turn) {
console.log(
'Turno', turn.number + '.'
);
showTurnLine(turn.activeCharacterId);
showActions(this.options);
});
battle.on('info', function (result) {
switch (result.action) {
case 'attack':
if (!result.success) {
console.log(
result.activeCharacterId,
'trató de golpear a', result.targetId,
'y falló.'
);
} else {
console.log(
result.activeCharacterId,
'golpea a', result.targetId,
'y le produce', result.effect
);
}
break;
case 'defend':
console.log(
result.targetId,
'defendió. Su defensa es ahora',
result.newDefense
);
break;
case 'cast':
if (!result.success) {
console.log(
result.activeCharacterId,
'trató de lanzar un hechizo a', result.targetId,
'y falló.'
);
} else {
console.log(
result.activeCharacterId,
'lanza', result.scrollName, 'a', result.targetId,
'con efecto', result.effect
);
}
break;
}
console.log();
});
battle.start();
}
function showActions() {
var actions = {
attack: 'Atacar',
defend: 'Defender',
cast: 'Lanzar hechizo'
};
var items = battle.options.list();
console.log('Elige qué hacer:');
items.forEach(function (item, index) {
console.log('[' + (index + 1) + ']', actions[item]);
});
waitForAction();
function waitForAction() {
cmd.prompt();
cmd.once('line', function (selection) {
selection = parseInt(selection);
if (!isNaN(selection) && selection > 0 && selection <= items.length) {
var id = items[selection - 1];
battle.options.select(items[selection - 1]);
action = id;
if (id === 'attack') {
showTargets();
}
if (id === 'cast') {
showScrolls();
}
} else {
console.log('Opción incorrecta');
setTimeout(function () {
readline.moveCursor(process.stdin, 0, -2);
readline.clearScreenDown(process.stdin);
waitForAction();
}, 500);
}
});
}
}
function showTargets() {
var items = battle.options.list();
console.log('Elige un objetivo:');
items.forEach(function (item, index) {
console.log('[' + (index + 1) + ']', item);
});
console.log('\n[0] Cancelar');
waitForAction();
function waitForAction() {
cmd.prompt();
cmd.once('line', function (selection) {
selection = parseInt(selection);
if (!isNaN(selection) && selection >= 0 && selection <= items.length) {
if (selection === 0) {
battle.options.cancel();
readline.moveCursor(process.stdin, 0, -(4 + items.length));
readline.clearScreenDown(process.stdin);
if (action === 'attack') {
showActions();
}
if (action === 'cast') {
showScrolls();
}
} else {
var id = items[selection - 1];
battle.options.select(items[selection - 1]);
}
} else {
console.log('Opción incorrecta');
setTimeout(function () {
readline.moveCursor(process.stdin, 0, -2);
readline.clearScreenDown(process.stdin);
waitForAction();
}, 500);
}
});
}
}
function showScrolls() {
var items = battle.options.list();
console.log('Elige un hechizo:');
items.forEach(function (item, index) {
console.log(
'[' + (index + 1) + ']', item,
'(' + battle.options.get(item).cost + ' MP)'
);
});
console.log('\n[0] Cancelar');
waitForAction();
function waitForAction() {
cmd.prompt();
cmd.once('line', function (selection) {
selection = parseInt(selection);
if (!isNaN(selection) && selection >= 0 && selection <= items.length) {
if (selection === 0) {
battle.options.cancel();
readline.moveCursor(process.stdin, 0, -(4 + items.length));
readline.clearScreenDown(process.stdin);
showActions();
} else {
var id = items[selection - 1];
battle.options.select(items[selection - 1]);
readline.moveCursor(process.stdin, 0, -(4 + items.length));
readline.clearScreenDown(process.stdin);
showTargets();
}
} else {
console.log('Opción incorrecta');
setTimeout(function () {
readline.moveCursor(process.stdin, 0, -2);
readline.clearScreenDown(process.stdin);
waitForAction();
}, 500);
}
});
}
}
function showTurnLine(activeCharacterId) {
var nameRegExp = new RegExp(activeCharacterId);
Object.keys(parties).forEach(function (partyId) {
console.log('Bando de los', partyNames[partyId] + ':');
var characters = parties[partyId];
characters.forEach(function (charId) {
var character = battle.characters.get(charId);
var hp = character.hp;
var maxHp = character.maxHp;
var mp = character.mp;
var maxMp = character.maxMp;
var deadToken = character.hp === 0 ? '✝' : ' ';
console.log(
charId === activeCharacterId ? '>' : deadToken,
charId,
hp + '/' + maxHp + ' HP',
mp + '/' + maxMp + ' MP'
);
});
});
console.log();
}
function getRandomSetup() {
var heroMembers = [
entities.characters.heroTank,
entities.characters.heroWizard
];
var monsterMembers = getMonsterParty();
return {
heroes: {
members: heroMembers,
grimoire: [entities.scrolls.health, entities.scrolls.fireball]
},
monsters: {
members: monsterMembers
}
};
}
function getMonsterParty() {
var partySize = Math.floor(Math.random() * 3) + 1;
var members = [];
for (var i = 0; i < partySize; i++) {
members.push(getRandomMonster());
}
return members;
}
function getRandomMonster() {
var monsters = Object.keys(entities.characters).filter(isMonster);
var randomId = monsters[Math.floor(Math.random() * monsters.length)];
return entities.characters[randomId];
function isMonster(id) {
return id.substr(0, 'monster'.length) === 'monster';
}
}