/
index.cjs.development.js
219 lines (173 loc) · 5.97 KB
/
index.cjs.development.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
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var mtgDecklistToLibrary = require('mtg-decklist-to-library');
var seedrandom = require('random-extra/preset/seedrandom');
var arrayChunkSplit = require('array-chunk-split');
var mtgBaseLand = require('mtg-base-land');
function getRandomFromOptions(options) {
var _options$random;
const random = (_options$random = options === null || options === void 0 ? void 0 : options.random) !== null && _options$random !== void 0 ? _options$random : seedrandom.seedrandom;
return random;
}
function splitChunk(cards, maxChunkLength) {
return arrayChunkSplit.arrayChunkSplit(cards, maxChunkLength !== null && maxChunkLength !== void 0 ? maxChunkLength : 12);
}
function splitThenMerge(cards, options, self) {
let arr = splitChunk(cards, options === null || options === void 0 ? void 0 : options.maxChunkLength);
return getRandomFromOptions(options).arrayShuffle(arr, true).flat();
}
function filterLands(cards) {
return cards.reduce((data, card) => {
let info = mtgBaseLand.parseSnowCoveredOrBaseLand(card.name);
if (!info) {
data.others.push(card);
} else if (info !== null && info !== void 0 && info.snow) {
data.snowLands.push(card);
} else {
data.baseLands.push(card);
}
return data;
}, {
baseLands: [],
snowLands: [],
others: []
});
}
function groupByName(cards) {
return cards.reduce((map, card) => {
var _card$name, _map$_card$name;
(_map$_card$name = map[_card$name = card.name]) !== null && _map$_card$name !== void 0 ? _map$_card$name : map[_card$name] = [];
map[card.name].push(card);
return map;
}, {});
}
function createGroupArray(length) {
const arr = [];
for (let i = 0; i < length; i++) {
arr.push([]);
}
return arr;
}
function distributeGroup(group) {
let arr = createGroupArray(4);
const names = Object.keys(group);
let card;
let i = arr.length - 1;
do {
for (let name of names) {
var _group$name;
card = (_group$name = group[name]) === null || _group$name === void 0 ? void 0 : _group$name.pop();
if (card) {
arr[i % arr.length].push(card);
} else {
delete group[name];
}
}
if (card) {
i++;
}
} while (Object.keys(group).length);
return arr.flat();
}
function distributeCards(cards, options, self) {
let data = filterLands(cards);
let arr = createGroupArray(4);
let idx = 0;
data.baseLands = distributeGroup(groupByName(data.baseLands));
data.snowLands = distributeGroup(groupByName(data.snowLands));
while (data.baseLands.length || data.snowLands.length || data.others.length) {
for (let i = 0; i < 4; i++) {
let card = data.others.pop();
arr[i].push(card);
if (!card || idx % 4 === 0) {
var _data$baseLands$pop, _data$baseLands$pop2;
arr[i].push((_data$baseLands$pop = data.baseLands.pop()) !== null && _data$baseLands$pop !== void 0 ? _data$baseLands$pop : data.snowLands.pop());
arr[i].push((_data$baseLands$pop2 = data.baseLands.pop()) !== null && _data$baseLands$pop2 !== void 0 ? _data$baseLands$pop2 : data.snowLands.pop());
}
}
idx++;
}
return arr.flat().filter(c => c);
}
function findIndexOfLands(cards, startIndex) {
startIndex |= 0;
return cards.reduce((list, card, idx) => {
if (idx >= startIndex) {
let info = mtgBaseLand.parseSnowCoveredOrBaseLand(card.name);
if (info) {
list.push(idx);
}
}
return list;
}, []);
}
function ensureLands(cards, options, self) {
cards = cards.slice();
let min;
if ((options === null || options === void 0 ? void 0 : options.ensureLands) === false) {
return cards;
} else if ((options === null || options === void 0 ? void 0 : options.ensureLands) === true) {
min = 2;
} else {
min = Math.max(Math.min(2, (options === null || options === void 0 ? void 0 : options.ensureLands) | 0 || 2), 0);
}
if (self.handSize < 6 || min <= 0) {
return cards;
} else if (self.handSize === 6) {
min = 1;
}
const hands = cards.slice(0, self.handSize);
const data = filterLands(hands);
const lands = data.snowLands.length + data.baseLands.length;
const diff = lands - min;
if (diff > 0) {
const idxLandArray = findIndexOfLands(cards, self.handSize);
const random = getRandomFromOptions(options);
const fnLibraryLand = random.dfArrayUnique(idxLandArray, diff);
let fnHandCard = random.dfArrayUnique(data.others, diff);
for (let i = 0; i < diff; i++) {
const card = fnHandCard();
const idxLibraryLand = fnLibraryLand();
const idxHand = hands.findIndex(value => {
return value.name === card.name;
});
if (idxHand === -1) {
throw new Error(`Something wrong`);
}
const c1 = cards[idxLibraryLand];
const c2 = cards[idxHand];
cards[idxLibraryLand] = c2;
hands[idxHand] = cards[idxHand] = c1;
}
}
return cards;
}
class DeckLibraryWithShuffle extends mtgDecklistToLibrary.DeckLibrary {
constructor(deck, options) {
super(deck, options);
}
options(options) {
return { ...this._options,
...options
};
}
shuffleStarting() {
var _options$ensureLands;
let options = this.options();
const random = getRandomFromOptions(options);
(_options$ensureLands = options.ensureLands) !== null && _options$ensureLands !== void 0 ? _options$ensureLands : options.ensureLands = random.int(0, 2);
let cards = [distributeCards, splitThenMerge, ensureLands].reduce((cards, fn) => fn(cards, options, this), this.cards);
this.cards = cards;
this._shuffleStarting = true;
}
shuffle(isStarting) {
let random = getRandomFromOptions(this.options());
random.arrayShuffle(this.cards, true);
}
}
exports.DeckLibraryWithShuffle = DeckLibraryWithShuffle;
exports["default"] = DeckLibraryWithShuffle;
exports.distributeCards = distributeCards;
exports.ensureLands = ensureLands;
exports.splitThenMerge = splitThenMerge;
//# sourceMappingURL=index.cjs.development.js.map