This repository was archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
353 lines (297 loc) · 12.2 KB
/
main.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
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
345
346
347
348
349
350
351
352
353
function processCheckbox(element) {
if ($(element).prop("checked")) {
const match = $(element).prop("id").match(/spell-(\d+)/);
if (match) {
return match[1];
}
}
return null;
}
function processHiddenInput(element) {
const id = $(element).prop("id");
const match = id.match(/spell-(\d+)/);
const count = parseInt($(element).val());
if (!match || isNaN(count)) {
return null;
}
return {spellID: match[1], count: count}
}
function calculateTotalBuffs() {
const buffCountContainer = $("#buff-count-container");
let count = 0;
let badgeClass = "badge-success";
let selectedBuffs = [];
let colorBlindBadge;
$("input[type='checkbox']").each((index, element) => {
const spellID = processCheckbox(element);
if (spellID) {
count += 1;
selectedBuffs.push(spellID);
}
});
$("input[type='hidden']").each((index, element) => {
const inputData = processHiddenInput(element);
if (inputData) {
const stackingCount = inputData["count"];
count += stackingCount;
for (let i = 0; i < stackingCount; i++) {
selectedBuffs.push(inputData["spellID"]);
}
}
});
buffCountContainer.tooltip("dispose");
if (count >= 28 && count < 32) {
badgeClass = "badge-warning";
colorBlindBadge = "#buff-count-warning";
buffCountContainer.tooltip({
title: "You're close to the buff cap of 32.",
html: true,
placement: "bottom",
})
} else if (count === 32) {
badgeClass = "badge-danger";
colorBlindBadge = "#buff-count-warning";
buffCountContainer.tooltip({
title: "Being exactly at the buff cap is dangerous.<br />If any of your buffs are overwritten by another player, it causes both buffs to apply for one batch, temporarily pushing you to 33.",
html: true,
placement: "bottom",
});
} else if (count >= 32) {
badgeClass = "badge-danger";
colorBlindBadge = "#buff-count-danger";
buffCountContainer.tooltip({
title: "You're over the buff cap of 32.",
html: true,
placement: "bottom",
});
}
buffCountContainer.removeClass("badge-success badge-warning badge-danger").addClass(badgeClass)
$("#total-buff-count").text(count);
$("[data-color-blind-help]").addClass("hidden");
$(colorBlindBadge).removeClass("hidden");
calculateCategorySubtotals();
if (selectedBuffs.length > 0) {
const bytes16 = new Uint16Array(selectedBuffs.sort());
const bytes8 = new Uint8Array(bytes16.buffer);
let binary = "";
for (let i = 0; i < bytes8.byteLength; i++) {
binary += String.fromCharCode( bytes8[i] );
}
let hash = btoa(binary);
hash = hash.replace(/\+/g, '-');
hash = hash.replace(/\//g, '_');
history.replaceState(null, null, `#${hash}`);
} else {
history.replaceState(null, null, "#");
}
}
function calculateCategorySubtotals() {
$("[data-primary-category]").each((index, element) => {
const displayElement = $(element).find("[data-category-count]");
let subtotal = 0;
$(element).find("input[type='checkbox']").each((index, element) => {
const spellID = processCheckbox(element);
if (spellID) {
subtotal += 1;
}
});
$(element).find("input[type='hidden']").each((index, element) => {
const inputData = processHiddenInput(element);
if (inputData) {
subtotal += inputData["count"];
}
});
if (subtotal === 0) {
$(displayElement).text(" ");
} else {
$(displayElement).text(` (${subtotal})`);
}
});
}
function calculateSubSection(currentTarget) {
const selectedBuffs = $(currentTarget).closest("ul").find("input[type='checkbox']:checked");
const selectedHiddenElements = $(currentTarget)
.closest("ul")
.find("input[type='hidden']")
.filter(function () {
return $(this).val() > 0;
});
const summary = $(currentTarget).closest(".has-buff-count").find(".buff-summary");
let hiddenElementCount = 0;
selectedHiddenElements.each((index, element) => {
const inputData = processHiddenInput(element);
if (inputData) {
hiddenElementCount += inputData["count"];
}
});
const buffCount = selectedBuffs.length + hiddenElementCount;
$(currentTarget)
.closest(".has-buff-count")
.find(".buff-count").text(buffCount === 0 ? "" : buffCount);
$(summary).tooltip("dispose");
$(summary).empty();
$(selectedBuffs).add(selectedHiddenElements).each((index, element) => {
$(element).closest("li")
.find("a:not(:has(img[data-info])):not([data-info])")
.clone(true)
.appendTo(summary);
});
$(summary).find("a").data("toggle", "tooltip").tooltip();
}
function loadBuffsFromURL() {
let hash = window.location.hash;
if (hash.length > 0) {
hash = hash.replace(/-/g, "+");
hash = hash.replace(/_/g, "/");
hash = hash.replace(/#/g, "");
const raw = atob(hash);
const rawLength = raw.length;
const int8Array = new Uint8Array(new ArrayBuffer(rawLength));
for (let i = 0; i < rawLength; i++) {
int8Array[i] = raw.charCodeAt(i);
}
// If the page is refreshed, input values are carried over, causing duplication of stackable buffs.
$("input[type='hidden']").val(0);
const int16Array = new Uint16Array(int8Array.buffer);
int16Array.forEach(buff => {
if (buff === 20034) {
// Backwards compatibility for Crusader.
buff = 20007;
} else if (buff === 24658) {
// Backwards compatibility for Trinket 1 and 2.
buff = 24427;
}
const spellInput = $(`#spell-${buff}`);
if (spellInput.is(":checkbox")) {
spellInput.prop("checked", true);
} else if (spellInput.is(":hidden")) {
const newCount = parseInt(spellInput.val()) + 1;
spellInput.val(newCount);
spellInput.closest(".stacking-buff").find(".stacking-count").text(newCount);
}
});
$(".has-buff-count").each((index, element) => {
calculateSubSection($(element).find("input[type='checkbox']").first());
});
$("#personal-buffs .has-buff-count").has("input:checked").find(".collapse:not([data-info])").collapse("show");
}
}
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
(() => {
$("#clear-all").click(() => {
$("input[type='checkbox']").prop("checked", false);
$("input[type='hidden']").val(0);
calculateTotalBuffs();
$(".stacking-count").text("");
$(".buff-count").text("");
$(".buff-summary").empty();
});
$(".bc-cell-header button[data-clear-button]").click(e => {
$(e.currentTarget).closest(".bc-cell")
.find("input[type='checkbox']").prop("checked", false).end()
.find(".buff-count").text("").end()
.find(".buff-summary").empty().end()
.find("input[type='hidden']").val(0).end()
.find(".stacking-count").text("")
;
if ($(e.currentTarget).closest(".bc-cell").attr("id") === "personal-buffs") {
$("#personal-buffs .has-buff-count").find(".collapse").collapse("hide");
}
calculateTotalBuffs();
});
$(".buff-count").click(e => {
$(e.currentTarget).closest(".has-buff-count")
.find("input[type='checkbox']").prop("checked", false).end()
.find(".buff-summary").empty();
$(e.currentTarget).text("");
calculateTotalBuffs();
});
$('[data-toggle="tooltip"]').tooltip();
$("input[type='checkbox']").click(e => {
const currentTarget = $(e.currentTarget);
if (currentTarget.closest("#personal-buffs").length > 0) {
const clickedClassType = $(e.currentTarget).closest("[data-class]").data("class");
if (clickedClassType !== undefined) {
const foreignPersonalBuffs = $(`#personal-buffs ul[data-class!='${clickedClassType}']`).find("input[type='checkbox']:checked")
if (foreignPersonalBuffs.length > 0) {
foreignPersonalBuffs.prop("checked", false);
foreignPersonalBuffs.each((index, element) => calculateSubSection(element));
calculateTotalBuffs();
$("#buff-class-removed").text($(foreignPersonalBuffs).one().closest("[data-class]").data("class").toString().capitalize());
$("#buff-class-selected").text(clickedClassType.capitalize());
$("#personal-buff-toast").toast("show");
}
}
}
if (currentTarget.closest(".has-buff-count").length > 0) {
calculateSubSection(e.currentTarget);
}
if (currentTarget.data("inclusion-category") !== undefined) {
$(`input[data-inclusion-category="${(currentTarget.data("inclusion-category"))}"][type="checkbox"]`)
.prop("checked", currentTarget.prop("checked"));
calculateSubSection(e.currentTarget);
}
if (currentTarget.data("exclusive") !== undefined) {
if (currentTarget.prop("checked")) {
const category = currentTarget.data("exclusive");
const inclusionCategory = currentTarget.data("inclusion-category");
$(`input[data-exclusive='${category}'][data-inclusion-category!="${inclusionCategory}"]`).each((index, checkbox) => {
if (checkbox !== e.currentTarget) {
$(checkbox).prop("checked", false);
calculateSubSection(checkbox);
}
});
calculateSubSection(e.currentTarget);
}
}
if (currentTarget.data("vanish") !== undefined) {
const otherVanishInput = $("[data-vanish]").not(e.currentTarget);
if (currentTarget.is(":checked")) {
otherVanishInput.add("[data-stealth]").prop("checked", true);
} else {
otherVanishInput.prop("checked", false);
}
}
if (currentTarget.data("stealth") !== undefined && !currentTarget.prop("checked")) {
$("[data-vanish]").prop("checked", false);
}
calculateTotalBuffs();
});
function changeStackingBuffAmount(currentTarget, amount) {
const input = $(currentTarget).closest(".stacking-buff").find("input[type='hidden']");
const countElement = $(currentTarget).closest(".stacking-buff").find(".stacking-count");
const maximum = $(input).data("maximum") || Number.MAX_SAFE_INTEGER;
const newValue = Math.max(0, Math.min(parseInt(input.val()) + amount, maximum));
countElement.text(newValue === 0 ? "" : newValue);
input.val(newValue);
calculateTotalBuffs();
calculateSubSection(currentTarget);
}
$(".stacking-count, .stacking-buff label")
.click(e => changeStackingBuffAmount(e.currentTarget, 1))
.contextmenu(e => {
changeStackingBuffAmount(e.currentTarget, -1);
e.preventDefault();
return false;
})
.tooltip({
html: true,
title: "This buff can be acquired multiple times.<br />Left click: Add 1<br />Right click: Subtract 1",
});
$(document).on("show.bs.collapse", (e) => {
$(e.target).closest(".has-buff-count").find(".buff-summary").hide();
});
$(document).on("hide.bs.collapse", (e) => {
$(e.target).closest(".has-buff-count").find(".buff-summary").show();
});
const clipboard = new Clipboard("#clipboard-button", {
text: () => window.location.href,
});
clipboard.on("success", () => {
$("#copy-toast").toast("show");
});
loadBuffsFromURL();
calculateTotalBuffs();
})();