-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeckStorage.js
More file actions
243 lines (226 loc) · 7.3 KB
/
deckStorage.js
File metadata and controls
243 lines (226 loc) · 7.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
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'),
});
onPage('Decks', function deckStorage() {
if (settings.value('underscript.storage.disable')) return;
const container = $('<p>');
const buttons = [];
let loading;
let pending = [];
function processNext() {
const card = pending.shift();
if (card) {
if (card.action === 'validate') {
loadDeck(loading);
if (!pending.length) {
loading = null;
}
} else if (card.action === 'clear') {
removeAllCards();
} else if (card.action === 'remove') {
removeCard(parseInt(card.id), card.shiny === true);
} else {
addCard(parseInt(card.id), card.shiny === true);
if (!pending.length) {
pending.push({action: 'validate'})
}
}
}
}
eventManager.on('Deck:postChange', (data) => {
if (!['addCard', 'removeCard', 'removeAllCards'].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) * 3; i <= x; i++) {
buttons.push($(`<button>${i}</button>`)
.addClass('btn btn-sm btn-danger')
.css({
'margin-top': '5px',
'margin-right': '2px',
width: '58px',
}));
}
buttons.forEach((button) => {
container.append(button);
});
$('#yourCardList > button').after(container);
$('#yourCardList > br').remove();
$('#yourCardList').css('margin-bottom', '35px');
eventManager.on('Deck:Soul', loadStorage);
function saveDeck(i) {
const soul = $('#selectClasses').find(':selected').text();
const deck = [];
$(`#deckCards${soul} li`).each((i, e) => {
const card = {
id: parseInt($(e).attr('id')),
};
if ($(e).hasClass('shiny')) {
card.shiny = true;
}
deck.push(card);
});
if (!deck.length) return;
localStorage.setItem(`underscript.deck.${selfId}.${soul}.${i}`, JSON.stringify(deck));
}
function loadDeck(i) {
if (i === null) return;
pending = []; // Clear pending
loading = i;
const soul = $('#selectClasses').find(':selected').text();
let deck = JSON.parse(localStorage.getItem(`underscript.deck.${selfId}.${soul}.${i}`));
const cDeck = $(`#deckCards${soul} li`);
if (cDeck.length) {
const builtDeck = [];
// Build deck options
cDeck.each((i, e) => {
const id = parseInt($(e).attr('id'));
const shiny = $(e).hasClass('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
return;
} else if (removals.length > 13) { // Too much to do (Cards in deck + 1)
pending.push({
action: 'clear',
});
} else {
pending.push.apply(pending, removals);
deck = temp;
}
}
pending.push.apply(pending, deck);
processNext();
}
function cards(list) {
const names = [];
list.forEach((card) => {
const name = $(`table#${card.id}:lt(1)`).find('.cardName').text() || '<span style="color: red;">Disenchanted/Missing</span>';
names.push(`- ${card.shiny ? '<span style="color: yellow;">S</span> ':''}${name}`);
});
return names.join('<br />');
}
function loadStorage() {
for (let i = 0; i < buttons.length; i++) {
loadButton(i);
}
}
function loadButton(i) {
const soul = $('#selectClasses').find(':selected').text();
const deckKey = `underscript.deck.${selfId}.${soul}.${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 = JSON.parse(localStorage.getItem(deckKey));
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>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();
});
});
}
}
$(window).on('load', () => {
loadStorage();
});
});