forked from johreh/gloomycompanion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeckState.js
55 lines (47 loc) · 1.3 KB
/
DeckState.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
import { shuffle_list } from './util';
export default class DeckState {
constructor(draw_pile, discard, name) {
this.draw_pile = draw_pile;
this.discard = discard;
this.name = name;
}
static create(definition, name, storageState) {
let draw_pile = [];
let discard = [];
if (storageState == null) {
for (const [i, cardDef] of definition.cards.entries()) {
const [shuffle, initiative, ...lines] = cardDef;
const card = {
id: `${name}_${i}`,
shuffle_next: shuffle,
initiative,
starting_lines: lines,
};
draw_pile.push(card);
}
shuffle_list(draw_pile);
} else {
draw_pile = storageState.draw_pile || [];
discard = storageState.discard || [];
}
return new DeckState(draw_pile, discard, name);
}
draw_card() {
const drewCard = this.draw_pile[0];
return new DeckState(this.draw_pile.slice(1), [drewCard, ...this.discard], this.name);
}
mustReshuffle() {
if (!this.draw_pile.length) {
return true;
}
if (this.discard.length) {
return this.discard[0].shuffle_next;
}
return false;
}
reshuffle() {
const newDraw = [...this.draw_pile, ...this.discard];
shuffle_list(newDraw);
return new DeckState(newDraw, [], this.name);
}
}