-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquickPacks.js
More file actions
332 lines (305 loc) · 10.3 KB
/
quickPacks.js
File metadata and controls
332 lines (305 loc) · 10.3 KB
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
/* eslint-disable no-multi-assign, no-nested-ternary */
import eventManager from 'src/utils/eventManager.js';
import eventEmitter from 'src/utils/eventEmitter.js';
import { global, globalSet } from 'src/utils/global.js';
import onPage from 'src/utils/onPage.js';
import * as hover from 'src/utils/hover.js';
import { blankToast, toast as basicToast } from 'src/utils/2.toasts.js';
import * as api from 'src/utils/4.api.js';
import formatNumber from 'src/utils/formatNumber.js';
import { getCollection } from 'src/utils/user.js';
import { buttonCSS as css, window } from 'src/utils/1.variables.js';
import Item from 'src/structures/constants/item.js';
import length from 'src/utils/length';
onPage('Packs', async function quickOpenPack() {
const collection = await getCollection();
const results = {
packs: 0,
cards: [],
};
const status = {
state: 'waiting', // waiting, processing, canceled
pack: '',
original: 0,
total: 0, // Total packs to open
remaining: 0, // How many packs still need to be opened
pending: 0, // How many packs are being waited on
pingTimeout: 0,
errors: 0,
};
const events = eventEmitter();
let timeoutID;
function setupPing(reset = true) {
if (status.state === 'waiting') return;
clearPing(reset);
timeoutID = setTimeout(() => {
status.pingTimeout += 1;
if (status.pingTimeout > 10) {
events.emit('cancel');
}
setupPing(false);
events.emit('next');
}, 200);
}
function clearPing(safe = true) {
if (safe) status.pingTimeout = 0;
if (timeoutID) clearTimeout(timeoutID);
timeoutID = 0;
}
function open(pack, count) {
status.pending = 0;
const openPack = global('openPack');
// const amt = Math.min(step, count);
for (let i = 0; i < count; i++) {
status.pending += 1;
globalSet('canOpen', true);
openPack(pack);
}
}
function showCards() {
const show = global('revealCard', 'show');
$('.slot .cardBack').each((i, e) => { show(e, i); });
}
events.on('start', ({
pack = '',
count: amt = 0,
offset = false,
}) => {
if (openingPacks()) return;
results.packs = 0;
results.cards = [];
status.state = 'processing';
status.pack = pack;
status.total = amt;
status.remaining = amt - offset;
status.pending = 0;
status.errors = 0;
if (!offset) {
events.emit('next');
}
setupPing();
});
let toast = blankToast();
events.on('pack', (cards = []) => {
status.pending -= 1;
results.packs += 1;
cards.forEach((card) => {
const cCard = collection.find((c) => c.id === card.id && c.shiny === card.shiny);
// Don't have one yet?
if (!cCard) {
// Add to collection
collection.push({
...card,
quantity: 1,
});
// Mark as new
card.new = true;
}
});
results.cards.push(...cards);
events.emit('next');
setupPing();
});
events.on('next', () => {
if (status.state === 'waiting') return;
if (status.state === 'processing') {
if (status.remaining > 0 && status.pending <= 0) {
const count = Math.min(status.remaining, 5);
status.remaining -= count;
open(status.pack, count);
}
events.emit('update');
}
const notWaiting = status.pending <= 0;
const finishedOpening = results.packs === status.total;
const canceled = status.state === 'canceled';
const timedout = canceled && status.pingTimeout;
if (timedout || notWaiting && (finishedOpening || canceled)) {
events.emit('finished');
}
});
events.on('update', () => {
if (toast.exists()) {
toast.setText(`<progress value="${results.packs}" max="${status.total}"></progress>`);
} else {
// TODO: translation
toast = basicToast({
title: `Opening ${formatNumber(status.total)} packs`,
text: `<progress value="${results.packs}" max="${status.total}"></progress>`,
className: 'dismissable',
buttons: {
text: 'Stop',
className: 'dismiss',
css,
onclick: (e) => {
events.emit('cancel');
},
},
});
}
});
const rarity = ['DETERMINATION', 'LEGENDARY', 'EPIC', 'RARE', 'COMMON'];
events.on('finished', () => {
if (status.state === 'waiting') return; // Invalid state
status.state = 'waiting';
clearPing();
// Post results
$(`#nb${status.pack.substring(4, status.pack.length - 4)}Packs`).text(status.original - results.packs);
const event = eventManager.cancelable.emit('openedPacks', {
count: results.packs,
cards: Object.freeze([...results.cards]),
});
if (!event.canceled) {
const cardResults = {
shiny: 0,
};
rarity.forEach((type) => {
cardResults[type] = {};
});
results.cards.forEach((card) => { // Convert each card
const r = cardResults[card.rarity];
const c = r[card.name] = r[card.name] || { total: 0, shiny: 0, new: false };
c.total += 1;
if (card.new) c.new = true;
if (card.shiny) {
if (status.pack !== 'openShinyPack') {
cardResults.shiny += 1;
}
c.shiny += 1;
}
});
// Magic numbers, yep. Have between 6...26 cards showing
let limit = Math.min(Math.max(Math.floor(window.innerHeight / 38), 6), 26);
// Increase the limit if we don't have a specific rarity
rarity.forEach((key) => {
if (!length(cardResults[key])) {
limit += 1;
}
});
let text = '';
// Build visual results
rarity.forEach((key) => {
const cards = cardResults[key];
const keys = Object.keys(cards);
if (!keys.length) return;
const buffer = [];
let count = 0;
let shiny = 0;
if (keys.length > limit) keys.sort((a, b) => cards[b].new - cards[a].new); // Push new cards to top of list if we have cards that'll get cut off
keys.forEach((name) => {
const card = cards[name];
count += card.total;
shiny += card.shiny;
if (limit) {
limit -= 1;
buffer.push(`${card.new ? `<span class="yellow">{${$.i18n('cosmetics-new')}}</span>` : ''}${card.shiny ? '<span class="yellow">S</span> ' : ''}${name}${card.total > 1 ? ` (${formatNumber(card.total)}${card.shiny ? `, ${card.shiny}` : ''})` : ''}${limit ? '' : '...'}`);
}
});
text += `${key} (${count}${shiny ? `, ${shiny} shiny` : ''}):${buffer.length ? `\n- ${buffer.join('\n- ')}` : ' ...'}\n`;
});
// Create result toast
const total = results.cards.length;
basicToast({
// TODO: translation?
title: `Results: ${formatNumber(results.packs)} Packs${cardResults.shiny ? ` (${total % 4 ? `${formatNumber(total)}, ` : ''}${formatNumber(cardResults.shiny)} shiny)` : total % 4 ? ` (${formatNumber(total)})` : ''}`,
text,
css: { 'font-family': 'inherit' },
});
// Show cards... I guess
showCards();
}
toast.close();
});
events.on('cancel', () => { // Sets the canceled flag
if (status.state === 'processing') {
status.state = 'canceled';
clearPing();
}
});
events.on('error', (err) => {
status.pending -= 1;
setupPing(); // We got a result, just not what we wanted
if (status.state !== 'processing') return; // Invalid state
status.errors += 1;
// Retry for every pack 3 times (this should act as if you're pushing the button 3 times... which usually opens all packs)
if (status.errors <= status.total * 3) {
status.remaining += 1;
}
});
let autoOpen = false;
eventManager.on('BootstrapDialog:preshow', function cancel(dialog) {
if (openingPacks() && dialog.getTitle() === $.i18n('dialog-error')) {
this.canceled = true;
}
});
eventManager.on('jQuery', () => {
globalSet('translateFromServerJson', function override(message) {
try {
return this.super(message);
} catch {
return message;
}
});
$(document).ajaxComplete((event, xhr, s) => {
if (s.url !== 'PacksConfig' || !s.data) return;
const data = xhr.responseJSON;
const error = data.action === 'getError';
if (data.action !== 'getCards' && !error) return;
if (data.cards) { // This has to always be done, to update the collection
events.emit('pack', JSON.parse(data.cards));
}
if (openingPacks()) {
if (data.status || error) {
events.emit('error', data.message);
}
} else if (autoOpen && !data.status) {
showCards();
}
});
// TODO: translation
$('[id^="btnOpen"]').on('click.script', (event) => {
autoOpen = event.ctrlKey;
const type = $(event.target).prop('id').substring(7);
const count = autoOpen ? 1 : parseInt($(`#nb${type}Packs`).text(), 10);
if (event.shiftKey) {
openPacks(type, count, 1);
hover.hide();
} else if (count === 1) { // Last pack?
hover.hide();
}
}).on('mouseenter.script', hover.show(`<span style="font-style: italic;">
* CTRL Click to auto reveal one (1) pack<br />
* Shift Click to auto open ALL packs
</span>`)).on('mouseleave.script', hover.hide);
});
function openingPacks() {
return status.state !== 'waiting';
}
function openPacks(type, count, start = 0) {
if (openingPacks()) return;
const packs = parseInt($(`#nb${type}Packs`).text(), 10);
// eslint-disable-next-line no-param-reassign
count = Math.max(Math.min(count, packs), 0);
if (count === 0) return;
status.original = packs;
events.emit('start', {
pack: `open${type}Pack`,
count,
offset: start,
});
}
const types = ['', 'DR', 'UTY', 'Shiny', 'Super', 'Final'];
const packTypes = [Item.UT_PACK, Item.DR_PACK, Item.UTY_PACK, Item.SHINY_PACK, Item.SUPER_PACK, Item.FINAL_PACK];
api.register('openPacks', (count, type = '') => {
if (openingPacks()) throw new Error('Currently opening packs');
if (type instanceof Item) {
const index = packTypes.indexOf(type);
if (index === -1) throw new Error(`Unsupported Item: ${type}`);
openPacks(types[index], count);
return;
}
if (!types.includes(type)) throw new Error(`Unsupported Pack: ${type}`);
openPacks(type, count);
});
api.register('openingPacks', openingPacks);
}, 'quickPacks');