forked from deck-monsters/deck-monsters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (39 loc) · 1.03 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
const reduce = require('lodash.reduce');
const zlib = require('zlib');
const Game = require('./game');
const { hydrateCharacter } = require('./characters');
const getOptions = (gameJSON) => {
let gameObj;
if (typeof gameJSON === 'string') {
try {
gameObj = JSON.parse(gameJSON);
} catch (err) {
gameObj = JSON.parse(zlib.gunzipSync(Buffer.from(gameJSON, 'base64')));
}
} else {
gameObj = Object.assign({}, gameJSON);
}
const options = Object.assign({ characters: {} }, gameObj.options);
// Hydrate characters
options.characters = reduce(options.characters, (characters, character, id) => {
characters[id] = hydrateCharacter(character);
return characters;
}, {});
return options;
};
const restoreGame = (publicChannel, gameJSON, log) => {
const options = getOptions(gameJSON);
return new Game(publicChannel, options, log);
};
const resetGame = (game, gameJSON) => {
const options = getOptions(gameJSON);
if (options) {
game.reset(options);
}
};
module.exports = {
Game,
getOptions,
restoreGame,
resetGame
};