-
Notifications
You must be signed in to change notification settings - Fork 0
/
mh-minluck-and-cre.user.js
416 lines (344 loc) · 11 KB
/
mh-minluck-and-cre.user.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// ==UserScript==
// @name 🐭️ MouseHunt - Minluck & Catch Rate Estimate
// @version 1.45.0
// @description View the minluck and catch rate estimate, right on the camp page.
// @license MIT
// @author bradp
// @namespace bradp
// @match https://www.mousehuntgame.com/*
// @icon https://brrad.com/mouse.png
// @require https://cdn.jsdelivr.net/npm/[email protected]/minlucks.js
// @grant none
// @run-at document-end
// ==/UserScript==
(function () {
'use strict';
const allMiceInfo = window.minlucks;
/**
* Add styles to the page.
*
* @param {string} styles The styles to add.
*/
const addStyles = (styles) => {
const existingStyles = document.getElementById('mh-mouseplace-custom-styles');
if (existingStyles) {
existingStyles.innerHTML += styles;
} else {
const style = document.createElement('style');
style.id = 'mh-mouseplace-custom-styles';
style.innerHTML = styles;
document.head.appendChild(style);
}
};
/**
* Get the current page slug.
*
* @return {string} The page slug.
*/
const getCurrentPage = () => {
const container = document.getElementById('mousehuntContainer');
if (! container || container.classList.length <= 0) {
return null;
}
return container.classList[ 0 ].replace('Page', '').toLowerCase();
};
/**
* Do something when ajax requests are completed.
*
* @param {Function} callback The callback to call when an ajax request is completed.
* @param {string} url The url to match. If not provided, all ajax requests will be matched.
* @param {boolean} skipSuccess Skip the success check.
*/
const onAjaxRequest = (callback, url = false, skipSuccess = false) => {
const req = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
this.addEventListener('load', function () {
if (this.responseText) {
let response = {};
try {
response = JSON.parse(this.responseText);
} catch (e) {
return;
}
if (response.success || skipSuccess) {
if (! url) {
callback(response);
return;
}
if (this.responseURL.indexOf(url) !== -1) {
callback(response);
}
}
}
});
req.apply(this, arguments);
};
};
/**
* Do something when the page or tab changes.
*
* @param {Object} callbacks
* @param {Function} callbacks.show The callback to call when the overlay is shown.
* @param {Function} callbacks.hide The callback to call when the overlay is hidden.
* @param {Function} callbacks.change The callback to call when the overlay is changed.
*/
const onPageChange = (callbacks) => {
const observer = new MutationObserver(() => {
if (callbacks.change) {
callbacks.change();
}
});
const observeTarget = document.getElementById('mousehuntContainer');
if (observeTarget) {
observer.observe(observeTarget, {
attributes: true,
attributeFilter: ['class']
});
}
};
const allType = [
'Arcane',
'Draconic',
'Forgotten',
'Hydro',
'Parental',
'Physical',
'Shadow',
'Tactical',
'Law',
'Rift'
];
const updateMinLucks = async () => {
if ('camp' !== getCurrentPage()) {
return;
}
const effectiveness = await getMiceEffectivness();
const miceNames = Object.values(effectiveness)
.map(({ mice }) => mice).flat()
.map(({ name }) => name).flat();
renderList(miceNames);
};
function renderList(list) {
const minWrap = document.getElementById('minluck-list');
if (minWrap) {
minWrap.remove();
}
const mintable = document.getElementById('minluck-list-table');
if (mintable) {
mintable.remove();
}
const powerEl = document.querySelector('.campPage-trap-trapStat.power');
const luckEl = document.querySelector('.campPage-trap-trapStat.luck');
const powerTypeEl = document.querySelector('.campPage-trap-trapStat.power');
if (! powerEl || ! luckEl || ! powerTypeEl) {
return;
}
const luck = luckEl.innerText.match(/\d/g).join('');
const power = powerEl.innerText.match(/\d/g).join('');
const powerType = powerTypeEl.innerText.match(/[A-Za-z]/g).join('');
const minluckList = document.createElement('div');
minluckList.id = 'minluck-list';
minluckList.className = 'campPage-trap-trapEffectiveness';
const miceheader = document.createElement('th');
miceheader.innerText = 'Mouse';
miceheader.classList = 'mousename-header';
const table = document.createElement('table');
table.id = 'minluck-list-table';
table.appendChild(miceheader);
const minluckheader = document.createElement('th');
minluckheader.innerText = 'Minluck';
table.appendChild(minluckheader);
const crheader = document.createElement('th');
crheader.innerText = 'CRE';
table.appendChild(crheader);
const totalStats = { good: 0, bad: 0, okay: 0, total: 0 };
for (let i = 0; i < list.length; i++) {
const mouseNameConverted = list[ i ];
const powerIndex = allType.indexOf(powerType);
const micePower = allMiceInfo[ mouseNameConverted ].power;
const miceEff = allMiceInfo[ mouseNameConverted ].effs[ powerIndex ];
const minluckString = replaceInfinity(micePower, miceEff);
const catchRateString = convertToCR(power, luck, micePower, miceEff);
const row = document.createElement('tr');
row.className = 'minlucklist-minluck-row';
const mouseName = document.createElement('td');
mouseName.innerText = mouseNameConverted;
mouseName.className = 'minlucklist-minluck-name';
row.appendChild(mouseName);
const minLuck = document.createElement('td');
minLuck.className = 'minlucklist-minluck-data' + (luck >= minluckString ? ' minlucklist-minluck-data-good' : '');
minLuck.innerText = minluckString;
row.appendChild(minLuck);
const catchRate = document.createElement('td');
catchRate.innerText = catchRateString;
if (catchRateString === '100.00%') {
totalStats.good += 1;
catchRate.className = 'minlucklist-minluck-data minlucklist-minluck-data-good';
} else if ((parseInt(catchRateString)) <= 60) {
totalStats.bad += 1;
catchRate.className = 'minlucklist-minluck-data minlucklist-minluck-data-bad';
} else {
catchRate.className = 'minlucklist-minluck-data';
}
totalStats.total += 1;
row.appendChild(catchRate);
table.appendChild(row);
}
minluckList.appendChild(table);
const statsContainer = document.querySelector('.campPage-trap-statsContainer');
if (statsContainer) {
statsContainer.appendChild(minluckList);
}
const trap = document.getElementsByClassName('trapImageView-layerWrapper')[ 0 ];
trap.classList.remove('minluck-indicator-all-good');
trap.classList.remove('minluck-indicator-bad');
trap.classList.remove('minluck-indicator-good');
trap.classList.remove('minluck-indicator-none');
if (totalStats.good === totalStats.total) {
trap.classList.add('minluck-indicator-all-good');
} else if (totalStats.bad === totalStats.total) {
trap.classList.add('minluck-indicator-bad');
} else if (totalStats.good > totalStats.total / 2) {
trap.classList.add('minluck-indicator-good');
} else {
trap.classList.add('minluck-indicator-none');
}
const rowData = table.getElementsByTagName('tr');
for (let i = 0; i < rowData.length - 1; i++) {
for (let j = 0; j < rowData.length - (i + 1); j++) {
if (Number(rowData.item(j).getElementsByTagName('td').item(1).innerHTML.replace(/[^0-9.]+/g, '')) < Number(rowData.item(j + 1).getElementsByTagName('td').item(1).innerHTML.replace(/[^0-9.]+/g, ''))) {
table.insertBefore(rowData.item(j + 1), rowData.item(j));
}
}
}
}
const getUserHash = () => {
// eslint-disable-next-line no-undef
if (typeof unsafeWindow !== 'undefined' && unsafeWindow.user.unique_hash) {
// eslint-disable-next-line no-undef
return unsafeWindow.user.unique_hash;
}
// if window.user exists, return the hash
if (window.user && window.user.unique_hash) {
return window.user.unique_hash;
}
};
const getMiceEffectivness = async () => {
const url = 'https://www.mousehuntgame.com/managers/ajax/users/getmiceeffectiveness.php';
const formData = 'sn=Hitgrab&hg_is_ajax=1&uh=' + getUserHash();
// post the data to the url
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formData,
});
const responseText = await response.text();
const data = JSON.parse(responseText);
if (! data.effectiveness) {
return;
}
return data.effectiveness;
};
function replaceInfinity(mousePower, eff) {
// Can't evalute infinity symbol, so was replaced with 9999 as minluck instead
const infinitySym = String.fromCharCode(0x221E);
eff = eff / 100;
if (eff === 0) {
return infinitySym;
}
const minluck = Math.ceil(Math.ceil(Math.sqrt(mousePower / 2)) / Math.min(eff, 1.4));
if (minluck >= 9999) {
return infinitySym;
}
if (2 * Math.pow(Math.floor(Math.min(1.4, eff) * minluck), 2) >= mousePower) {
return minluck;
}
return minluck + 1;
}
function convertToCR(power, luck, mPower, mEff) {
mEff = mEff / 100;
// eslint-disable-next-line no-mixed-operators
let result = Math.min(1, (power * mEff + 2 * Math.pow(Math.floor(luck * Math.min(mEff, 1.4)), 2)) / (mPower + power * mEff));
result = (result * 100).toFixed(2) + '%';
return result;
}
const makeMinLuckButton = () => {
const button = document.getElementById('minluck-button');
if (! button) {
const minluckButton = document.createElement('a');
minluckButton.id = 'minluck-button';
minluckButton.classList.add('campPage-trap-trapEffectiveness');
minluckButton.textContent = '🐭️ Minluck & Catch Rate Estimate';
const statsContainer = document.querySelector('.campPage-trap-statsContainer');
if (statsContainer) {
statsContainer.appendChild(minluckButton);
}
}
};
addStyles(`
#minluck-button {
margin-top: 10px;
}
#minluck-list {
padding: 10px;
margin-bottom: 10px;
margin-top: -2px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
#minluck-list table {
margin: 0 10px 10px 10px;
width: 100%;
}
#minluck-list table th {
text-align: center;
font-weight: bold;
}
#minluck-list table th.mousename-header {
text-align: left;
}
#minluck-list table:first-child {
text-align: left;
}
.minlucklist-minluck-name {
min-width: 85px;
white-space: nowrap;
overflow: hidden;
max-width: 150px;
text-overflow: ellipsis;
}
.minlucklist-minluck-data {
text-align: center;
min-width: 70px;
}
.minlucklist-minluck-data-good {
color: #138f13;
}
.minlucklist-minluck-data-bad {
color: #bb4646;
}
.minluck-indicator-all-good {
border-top: 5px solid blue;
}
.minluck-indicator-good {
border-top: 5px solid #2f82ec;
}
.minluck-indicator-bad {
border-top: 5px solid #990000;
}
.minluck-indicator-all-good,
.minluck-indicator-good,
.minluck-indicator-bad {
border-top: none;
}`);
onAjaxRequest(updateMinLucks, '/managers/ajax/users/changetrap.php');
onPageChange({ change: updateMinLucks });
makeMinLuckButton();
updateMinLucks();
setTimeout(() => {
makeMinLuckButton();
updateMinLucks();
}, 750);
}());