forked from deck-monsters/deck-monsters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
466 lines (371 loc) · 11.3 KB
/
game.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
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
const find = require('lodash.find');
const reduce = require('lodash.reduce');
const zlib = require('zlib');
const { all: cardTypes, draw } = require('./cards');
const { all: itemTypes } = require('./items');
const { all: monsterTypes } = require('./monsters');
const { COINS_PER_VICTORY, COINS_PER_DEFEAT } = require('./constants/coins');
const { create: createCharacter } = require('./characters');
const { globalSemaphore } = require('./helpers/semaphore');
const { listen, loadHandlers } = require('./commands');
const { sortByXP } = require('./helpers/sort');
const { XP_PER_VICTORY, XP_PER_DEFEAT } = require('./helpers/experience');
const announcements = require('./announcements');
const aws = require('./helpers/aws');
const BaseClass = require('./shared/baseClass');
const cardProbabilities = require('./card-probabilities.json');
const ChannelManager = require('./channel');
const dungeonMasterGuide = require('./build/dungeon-master-guide');
const Exploration = require('./exploration');
const monsterManual = require('./build/monster-manual');
const playerHandbook = require('./build/player-handbook');
const Ring = require('./ring');
const PUBLIC_CHANNEL = 'PUBLIC_CHANNEL';
class Game extends BaseClass {
constructor (publicChannel, options, log = () => {}) {
super(options, globalSemaphore);
this.log = log;
this.key = `DeckMonsters.Backup.${Date.now()}`;
this.channelManager = new ChannelManager({}, this.log);
this.channelManager.addChannel({ channel: publicChannel, channelName: PUBLIC_CHANNEL });
this.publicChannel = ({ announce }) => this.channelManager.queueMessage({ announce, channelName: PUBLIC_CHANNEL });
this.ring = new Ring(this.channelManager, { spawnBosses: this.options.spawnBosses }, this.log);
this.exploration = new Exploration(this.channelManager, {}, this.log);
this.initializeEvents();
loadHandlers();
this.on('stateChange', () => this.saveState());
this.emit('initialized');
}
reset (options) {
this.optionsStore = {};
this.setOptions(options);
}
get characters () {
if (this.options.characters === undefined) this.characters = {};
return this.options.characters || {};
}
set characters (characters) {
this.setOptions({
characters
});
}
get saveState () {
return () => {
const buffer = zlib.gzipSync(JSON.stringify(this));
if (this.stateSaveFunc) {
const string = buffer.toString('base64');
setImmediate(this.stateSaveFunc, string);
}
setImmediate(aws.save, this.key, buffer, this.log);
};
}
set saveState (stateSaveFunc) {
if (stateSaveFunc) {
this.stateSaveFunc = stateSaveFunc;
} else {
delete this.stateSaveFunc;
}
}
initializeEvents () {
announcements.initialize(this);
this.on('creature.win', this.handleWinner);
this.on('creature.loss', this.handleLoser);
this.on('creature.permaDeath', this.handlePermaDeath);
this.on('creature.fled', this.handleFled);
}
handleCommand ({
command,
game = this
}) {
return listen({
command,
game
});
}
handleWinner (className, monster, { contestant }) {
// Draw a card, maybe kick off more events (that could be messaged)
// Add XP to the character in the case of victory
contestant.character.xp += XP_PER_VICTORY;
// Also give coins to the victor
contestant.character.coins += COINS_PER_VICTORY;
// Also draw a new card for the player
const card = this.drawCard({}, monster);
contestant.character.addCard(card);
this.emit('cardDrop', {
contestant,
card
});
this.emit('gainedXP', {
contestant,
creature: contestant.character,
xpGained: XP_PER_VICTORY,
coinsGained: COINS_PER_VICTORY
});
}
handlePermaDeath (className, monster, { contestant }) {
// Award XP, maybe kick off more events (that could be messaged)
// The character still earns a small bit of XP and coins in the case of defeat
contestant.character.xp += XP_PER_DEFEAT * 2;
contestant.character.coins += COINS_PER_DEFEAT * 2;
this.emit('gainedXP', {
contestant,
creature: contestant.character,
xpGained: XP_PER_DEFEAT * 2,
coinsGained: COINS_PER_DEFEAT * 2
});
}
handleLoser (className, monster, { contestant }) { // eslint-disable-line class-methods-use-this
// Award XP, maybe kick off more events (that could be messaged)
// The character still earns a small bit of XP and coins in the case of defeat
contestant.character.xp += XP_PER_DEFEAT;
contestant.character.coins += COINS_PER_DEFEAT;
this.emit('gainedXP', {
contestant,
creature: contestant.character,
xpGained: XP_PER_DEFEAT,
coinsGained: COINS_PER_DEFEAT
});
}
handleFled (className, monster, { contestant }) {
// The character and monster still earn a small bit of XP and coins when he/she/it flees.
monster.xp += XP_PER_DEFEAT;
contestant.character.xp += XP_PER_DEFEAT;
contestant.character.coins += COINS_PER_DEFEAT;
this.emit('gainedXP', {
contestant,
creature: contestant.character,
xpGained: XP_PER_DEFEAT,
coinsGained: COINS_PER_DEFEAT
});
this.emit('gainedXP', {
contestant,
creature: monster,
xpGained: XP_PER_DEFEAT
});
}
clearRing () {
this.ring.clearRing();
}
getCharacter ({
channel,
gender,
icon,
id,
name,
type
}) {
const game = this;
return Promise
.resolve(this.characters[id])
.then((existingCharacter) => {
if (!existingCharacter) {
return createCharacter(channel, {
name, type, gender, icon, game
})
.then((character) => {
game.characters[id] = character;
game.emit('characterCreated', { character });
return character;
});
}
return existingCharacter;
});
}
static getCardTypes () {
return cardTypes.reduce((obj, Card) => {
obj[Card.cardType.toLowerCase()] = Card;
return obj;
}, {});
}
static getItemTypes () {
return itemTypes.reduce((obj, Item) => {
obj[Item.itemType.toLowerCase()] = Item;
return obj;
}, {});
}
static getMonsterTypes () {
return monsterTypes.reduce((obj, Monster) => {
obj[Monster.monsterType.toLowerCase()] = Monster;
return obj;
}, {});
}
getAllMonstersLookup () {
return reduce(this.characters, (all, character) => {
character.monsters.forEach((monster) => {
all[monster.givenName.toLowerCase()] = monster;
});
return all;
}, {});
}
findCharacterByName (name) {
return find(this.characters, character => character.givenName.toLowerCase() === name.toLowerCase());
}
lookAtCharacter (channel, characterName, self) {
if (characterName) {
const character = this.findCharacterByName(characterName);
const isSelf = character === self;
if (character) return character.look(channel, isSelf);
}
return Promise.reject(channel({
announce: `I can find no character by the name of ${characterName}.`,
delay: 'short'
}));
}
getCreatureRankings (creatures, top = 5) { // eslint-disable-line class-methods-use-this
const sortedCreatures = sortByXP(creatures).reverse();
sortedCreatures.length = Math.min(sortedCreatures.length, top);
const maxLength = sortedCreatures.reduce((length, creature) => Math.max(creature.givenName.length, length), 0);
const padding = ' '.repeat(maxLength);
const results = sortedCreatures.map((creature) => {
const paddedName = `${creature.givenName}${padding}`.slice(0, maxLength);
return `${paddedName} ${creature.xp}`;
});
return results;
}
lookAtCharacterRankings (channel) {
const results = this.getCreatureRankings(Object.values(this.characters));
return Promise.resolve(channel({
announce:
`*Top ${results.length} Players by XP:*
\`\`\`
${results.join('\n')}
\`\`\`
`,
delay: 'short'
}));
}
lookAtMonsterRankings (channel) {
const results = this.getCreatureRankings(Object.values(this.getAllMonstersLookup()));
return Promise.resolve(channel({
announce:
`*Top ${results.length} Monsters by XP:*
\`\`\`
${results.join('\n')}
\`\`\`
`,
delay: 'short'
}));
}
editCharacter (channel, characterName) {
if (characterName) {
const character = this.findCharacterByName(characterName);
if (character) return character.edit(channel);
}
return Promise.reject(channel({
announce: `I can find no character by the name of ${characterName}.`,
delay: 'short'
}));
}
lookAtMonster (channel, monsterName, character) {
if (monsterName) {
const allMonsters = this.getAllMonstersLookup();
const monster = allMonsters[monsterName.toLowerCase()];
const ownsMonster = character.ownsMonster(monsterName);
if (monster) return monster.look(channel, ownsMonster);
}
return Promise.reject(channel({
announce: `I can find no monster by the name of ${monsterName}.`,
delay: 'short'
}));
}
editMonster (channel, monsterName) {
if (monsterName) {
const allMonsters = this.getAllMonstersLookup();
const monster = allMonsters[monsterName.toLowerCase()];
if (monster) return monster.edit(channel);
}
return Promise.reject(channel({
announce: `I can find no monster by the name of ${monsterName}.`,
delay: 'short'
}));
}
lookAtCard (channel, cardName) {
if (cardName) {
const cards = this.constructor.getCardTypes();
const Card = cards[cardName.toLowerCase()];
if (Card) {
const card = new Card();
return card.look(channel, true);
}
}
return Promise.reject(channel({
announce: `Sorry, we don't carry ${cardName} cards here.`,
delay: 'short'
}));
}
lookAtItem (channel, itemName) {
if (itemName) {
const items = this.constructor.getItemTypes();
const Item = items[itemName.toLowerCase()];
if (Item) {
const item = new Item();
return item.look(channel, true);
}
}
return Promise.reject(channel({
announce: `Sorry, we don't carry ${itemName} here.`,
delay: 'short'
}));
}
getCardProbabilities () { // eslint-disable-line class-methods-use-this
return cardProbabilities;
}
getRing () {
return this.ring;
}
getExploration () {
return this.exploration;
}
lookAtRing (channel, ringName = 'main', showCharacters, summary) {
return this.getRing(ringName).look(channel, showCharacters, summary);
}
lookAtRingCards (channel, ringName = 'main') {
return this.getRing(ringName).lookAtCards(channel);
}
lookAt (channel, thing) {
if (thing) {
// What is this thing?
// Is it a dungeon master guide?
if (thing.match(/(?:dungeon master(?:s)?|dm) guide|dmg/i)) {
return dungeonMasterGuide({ channel });
}
// Is it a monster manual?
if (thing.match(/monster(?:s)? manual/i)) {
return monsterManual({ channel });
}
// Is it a player handbook?
if (thing.match(/player(?:s)? handbook/i)) {
return playerHandbook(channel);
}
// Is it a monster?
const monsters = this.getAllMonstersLookup();
const monster = monsters[thing];
if (monster) return monster.look(channel);
// Is it a card?
const cards = this.constructor.getCardTypes();
const Card = cards[thing];
if (Card) {
const card = new Card();
return card.look(channel);
}
// Is it an item?
const items = this.constructor.getItemTypes();
const Item = items[thing];
if (Item) {
const item = new Item();
return item.look(channel);
}
}
return Promise.reject(channel({
announce: `I don't see a ${thing} here.`,
delay: 'short'
}));
}
drawCard (options, monster) {
const card = draw(options, monster);
this.emit('cardDrawn', { card });
return card;
}
}
Game.eventPrefix = 'game';
module.exports = Game;