-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstorage.js
More file actions
344 lines (318 loc) · 10.8 KB
/
storage.js
File metadata and controls
344 lines (318 loc) · 10.8 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
333
334
335
336
337
338
339
340
341
342
343
344
settings.register({
name: 'Disable Deck Storage',
key: 'underscript.storage.disable',
refresh: () => onPage('Decks'),
});
settings.register({
name: 'Deck Storage Rows',
key: 'underscript.storage.rows',
type: 'select',
options: ['1', '2', '3', '4', '5', '6'],
refresh: () => onPage('Decks'),
extraPrefix: 'underscript.deck.',
});
onPage('Decks', function deckStorage() {
if (settings.value('underscript.storage.disable')) return;
const mockLastOpenedDialog = {
close() {},
};
let templastOpenedDialog;
style.add('.btn-storage { margin-top: 5px; margin-right: 8px; width: 36px; }');
function getFromLibrary(id, shiny, library) {
return library.find(card => card.id === id && (shiny === undefined || card.shiny === shiny));
}
function getCardData(id, shiny, deep) {
const library = global('deckCollections', 'collections');
if (deep) {
// Search all decks
const keys = Object.keys(library);
for (let i = 0; i < keys.length; i++) {
const card = getFromLibrary(id, shiny, library[keys[i]]);
if (card) return card;
}
return null;
}
return getFromLibrary(id, shiny, library[global('classe')]);
}
eventManager.on('jQuery', () => {
const container = $('<p>');
const buttons = [];
let loading;
let pending = [];
function processNext() {
if (global('lastOpenedDialog') === mockLastOpenedDialog) {
lastOpenedDialog = templastOpenedDialog;
}
const job = pending.shift();
if (job) {
if (job.action === 'validate') {
loadDeck(loading);
if (!pending.length) {
loading = null;
}
return;
} else if (job.action === 'clear') {
global('removeAllCards')();
} else if (job.action === 'remove') {
global('removeCard')(parseInt(job.id), job.shiny === true);
} else if (job.action === 'clearArtifacts') {
global('clearArtifacts')();
} else if (job.action === 'addArtifact') {
debug(`Adding artifact: ${job.id}`);
templastOpenedDialog = lastOpenedDialog;
lastOpenedDialog = mockLastOpenedDialog;
global('addArtifact')(job.id);
} else {
global('addCard')(parseInt(job.id), job.shiny === true);
}
if (!pending.length) {
pending.push({action: 'validate'})
}
}
}
eventManager.on('Deck:postChange', (data) => {
if (!['addCard', 'removeCard', 'removeAllCards', 'clearArtifacts', 'addArtifact'].includes(data.action)) return;
if (data.status === "error") {
pending = [];
return;
}
processNext();
});
for (let i = 1, x = Math.max(parseInt(settings.value('underscript.storage.rows')), 1) * 4; i <= x; i++) {
buttons.push($('<button>')
.text(i)
.addClass('btn btn-sm btn-danger btn-storage'));
}
buttons.forEach((button) => {
container.append(button);
});
const clearDeck = $('#yourCardList > button:last');
clearDeck.after(container);
$('#yourCardList > br').remove();
$('#yourCardList').css('margin-bottom', '35px');
eventManager.on('Deck:Soul', loadStorage);
function fixDeck(id) {
const key = getKey(id);
const deck = JSON.parse(localStorage.getItem(key));
if (!deck) return;
if (!deck.hasOwnProperty('cards')) {
localStorage.setItem(key, JSON.stringify({
cards: deck,
artifacts: [],
}));
}
}
function saveDeck(i) {
const deck = {
cards: [],
artifacts: [],
};
const clazz = global('classe');
global('decks')[clazz].forEach(({id, shiny}) => {
const card = { id, };
if (shiny) {
card.shiny = true;
}
deck.cards.push(card);
});
global('decksArtifacts')[clazz].forEach(({ id }) => deck.artifacts.push(id));
if (!deck.cards.length && !deck.artifacts.length) return;
localStorage.setItem(getKey(i), JSON.stringify(deck));
}
function loadDeck(i) {
if (i === null) return;
debug('loading');
pending = []; // Clear pending
loading = i;
let deck = getDeck(i, true);
const cDeck = global('decks')[global('classe')];
if (cDeck.length) {
const builtDeck = [];
// Build deck options
cDeck.forEach(({id, shiny}) => {
builtDeck.push({
id, shiny,
action: 'remove',
});
});
// Compare the decks
const temp = deck.slice(0);
const removals = builtDeck.filter((card) => {
return !temp.some((card2, i) => {
const found = card2.id === card.id && (card.shiny && card2.shiny || true);
if (found) { // Remove the item
temp.splice(i, 1);
}
return found;
});
});
// Check what we need to do
if (!removals.length && !temp.length) { // There's nothing
debug('Finished');
deck = [];
} else if (removals.length > 13) { // Too much to do (Cards in deck + 1)
pending.push({
action: 'clear',
});
} else {
pending.push(...removals);
deck = temp;
}
}
pending.push(...deck);
if (!matchingArtifacts(i)) {
debug('Loading Artifacts');
pending.push({
action: 'clearArtifacts',
});
getArtifacts(i).forEach((id) => {
pending.push({
id,
action: 'addArtifact',
})
});
}
processNext();
}
function matchingArtifacts(id) {
const dArts = getArtifacts(id);
const cArts = global('decksArtifacts')[global('classe')];
return !dArts.length || dArts.length === cArts.length && cArts.every(({id: id1}) => !!~dArts.indexOf(id1));
}
function getKey(id) {
return `underscript.deck.${global('selfId')}.${global('classe')}.${id}`;
}
function getDeck(id, trim) {
fixDeck(id);
const key = getKey(id);
const deck = JSON.parse(localStorage.getItem(key));
if (!deck) return null;
if (trim) {
return deck.cards.filter(({id, shiny}) => getCardData(id, shiny) !== null);
}
return deck.cards;
}
function getArtifacts(id) {
fixDeck(id);
const key = getKey(id);
const deck = JSON.parse(localStorage.getItem(key));
if (!deck) return [];
return deck.artifacts || [];
}
function cards(list) {
const names = [];
list.forEach((card) => {
let data = getCardData(card.id, card.shiny) || {};
const name = data.name || `<span style="color: red;">${(data = getCardData(card.id) && data && data.name) || 'Disenchanted/Missing'}</span>`;
names.push(`- ${card.shiny ? '<span style="color: yellow;">S</span> ':''}${name}`);
});
return names.join('<br />');
}
function artifacts(id) {
const list = [];
getArtifacts(id).forEach((art) => {
const artifact = global('userArtifacts').find(({id: artID}) => artID === art);
if (artifact) {
list.push(`<span class="${artifact.legendary ? 'yellow' : ''}"><img style="height: 16px;" src="images/artifacts/${artifact.image}.png" /> ${artifact.name}</span>`);
}
});
return list.join(', ');
}
function loadStorage() {
buttons.forEach((b,i) => loadButton(i));
}
function loadButton(i) {
const soul = global('classe');
const deckKey = getKey(i);
const nameKey = `${deckKey}.name`;
const button = buttons[i];
button.off('.deckStorage'); // Remove any lingering events
function refreshHover() {
hover.hide();
button.trigger('mouseenter');
}
function saveButton() {
saveDeck(i);
loadButton(i); // I should be able to reset data without doing all this again...
refreshHover();
}
function hoverButton(e) {
let text = '';
if (e.type === 'mouseenter') {
const deck = getDeck(i);
if (deck) {
text = `
<div id="deckName">${localStorage.getItem(nameKey) || (`${soul}-${i + 1}`)}</div>
<div><input id="deckNameInput" maxlength="28" style="border: 0; border-bottom: 2px solid #00617c; background: #000; width: 100%; display: none;" type="text" placeholder="${soul}-${i + 1}" value="${localStorage.getItem(nameKey) || ''}"></div>
<div style="font-size: 13px;">${artifacts(i)}</div>
<div>Click to load (${deck.length})</div>
<div style="font-size: 13px;">${cards(deck)}</div>
<div style="font-style: italic; color: #b3b3b3;">
* Right Click to name deck<br />
* Shift Click to re-save deck<br />
* CTRL Click to erase deck
</div>
`;
} else {
text = `
<div id="name">${soul}-${i + 1}</div>
<div>Click to save current deck</div>`;
}
}
hover.show(text)(e);
}
if (!localStorage.getItem(deckKey)) {
button.addClass('btn-danger')
.removeClass('btn-primary')
.hover(hoverButton)
.one('click.script.deckStorage', saveButton);
} else {
button.removeClass('btn-danger')
.addClass('btn-primary')
.hover(hoverButton)
.on('click.script.deckStorage', (e) => {
if (e.ctrlKey && e.shiftKey) { // Crazy people...
return;
}
if (e.ctrlKey) { // ERASE
localStorage.removeItem(nameKey);
localStorage.removeItem(deckKey);
loadButton(i); // Reload :(
refreshHover(); // Update
} else if (e.shiftKey) { // Re-save
saveDeck(i); // Save
refreshHover(); // Update
} else { // Load
loadDeck(i);
}
}).on('contextmenu.script.deckStorage', (e) => {
e.preventDefault();
const input = $('#deckNameInput');
const display = $('#deckName');
function storeInput() {
localStorage.setItem(nameKey, input.val());
display.text(input.val()).show();
loadButton(i); // I really hate this
refreshHover();
}
display.hide();
input.show()
.focus()
.select()
.on('keydown.script.deckStorage', (e) => {
if (e.which === 27 || e.which === 13) {
e.preventDefault();
storeInput();
}
}).on('focusout.script.deckStorage', () => {
storeInput();
});
});
}
}
eventManager.on('Deck:Loaded', () => {
loadStorage();
});
clearDeck.on('click', () => pending = []);
});
});