-
Notifications
You must be signed in to change notification settings - Fork 8
/
recipes.js
315 lines (263 loc) · 9.09 KB
/
recipes.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
import {
cacheGet,
cacheSet,
getCurrentSubtab,
getCurrentTab,
getUserItems,
makeElement,
onEvent,
onNavigation
} from '@utils';
import recipesMeConversion from '@data/recipes-me-conversion.json';
import recipesToReorder from '@data/recipes-to-reorder.json';
/**
* Clean up the recipe book.
*/
const cleanUpRecipeBook = () => {
// Re-add the 'All' tab.
const allTab = document.querySelector('.inventoryPage-tagDirectory-tag.all.hidden');
if (allTab) {
allTab.classList.remove('hidden');
}
// get all the inventoryPage-tagDirectory-tag links and attach new onclick events to them
const tagLinks = document.querySelectorAll('.mousehuntHud-page-subTabContent.recipe a.inventoryPage-tagDirectory-tag');
tagLinks.forEach((tagLink) => {
// get the data-tag attribute
const tag = tagLink.getAttribute('data-tag');
// remove the old onclick event
tagLink.removeAttribute('onclick');
// Add the new onclick event
tagLink.addEventListener('click', (e) => {
// showTagGroup(e.target);
app.pages.InventoryPage.showTagGroup(e.target);
// Update the recipes on the page.
const hasBeenUpdated = tagLink.classList.contains('updated');
if (! hasBeenUpdated) {
updateRecipesOnPage(tag);
tagLink.classList.add('updated');
}
});
});
};
/**
* Show a warning when crafting a recipe that may not be worth it.
*
* @param {string} text The warning text.
*/
const showCraftWarning = (text) => {
const confirm = document.querySelector('.mousehuntActionButton.inventoryPage-confirmPopup-suffix-button.confirm');
if (! confirm) {
return;
}
const existing = document.querySelector('.mhui-craft-warning-tooltip');
if (existing) {
existing.remove();
}
const tooltip = makeElement('div', 'mhui-craft-warning-tooltip', text);
confirm.parentNode.append(tooltip);
};
/**
* Warn the user when crafting a recipe that may not be worth it.
*
* @param {number} limit The limit of the number of times to check for the confirm button.
*/
const warnOnBadCrafts = (limit = 0) => {
const confirm = document.querySelector('.mousehuntActionButton.inventoryPage-confirmPopup-suffix-button.confirm');
if (! confirm) {
if (limit <= 3) {
setTimeout(() => {
warnOnBadCrafts(limit + 1);
}, 250);
}
return;
}
const type = confirm.getAttribute('data-confirm-type');
if (! type) {
return;
}
if (! ('recipe' === type || 'potion' === type)) {
return;
}
const popup = document.querySelector('.inventoryPage-confirmPopup');
if (! popup) {
return;
}
const recipe = popup.getAttribute('data-item-type');
if (! recipe) {
return;
}
if ('potion' === type) {
// if its a potion, then check to make sure we're using the sb recipe
const consumed = document.querySelectorAll('.inventoryPage-confirmPopup-itemRow-quantity[data-source="consumed"]');
// make sure sb is one of the consumed items
let hasSB = false;
consumed.forEach((item) => {
if (item.getAttribute('data-item-type') === 'super_brie_cheese') {
hasSB = true;
}
});
if (! hasSB) {
return;
}
}
if (recipesMeConversion.no.includes(recipe)) {
showCraftWarning('This is not worth crafting using Magic Essence.');
} else if (recipesMeConversion.maybe.includes(recipe)) {
showCraftWarning('Check the price of SUPER|brie+ before using Magic Essence.');
}
};
/**
* Modify the smashable tooltip.
*/
const modifySmashableTooltip = async () => {
if ('crafting' !== getCurrentTab() || 'hammer' !== getCurrentSubtab()) {
return;
}
const items = document.querySelectorAll('.inventoryPage-item');
if (! items) {
return;
}
items.forEach(async (item) => {
const tooltip = item.querySelector('.tooltip');
if (! tooltip) {
return;
}
// get the data for the data-produced-item attribute
let producedItem = item.getAttribute('data-produced-item');
if (! producedItem) {
return;
}
item.addEventListener('mouseleave', () => {
item.classList.remove('new-tooltip-loading');
});
item.addEventListener('mouseenter', async () => {
if (item.getAttribute('data-new-tooltip') === 'newTooltip') {
return;
}
item.setAttribute('data-new-tooltip', 'newTooltip');
producedItem = producedItem.includes(',') ? producedItem.split(',') : [producedItem];
const itemType = item.getAttribute('data-item-type');
producedItem.push(itemType);
item.classList.add('new-tooltip-loading');
let itemData = await cacheGet(`smashable-${producedItem.join('-')}`);
if (! itemData) {
itemData = await getUserItems(producedItem);
cacheSet(`smashable-${producedItem.join('-')}`, itemData);
}
if (! itemData || ! itemData[0]) {
return;
}
// get the formatted_parts attribute from the itemData array where the type matches the itemType
const formattedParts = itemData.find((itemDataItem) => itemDataItem.type === itemType).formatted_parts;
if (! formattedParts) {
return;
}
const tooltipWrapper = makeElement('div', ['newTooltip', 'tooltip']);
itemData.forEach((itemDataItem) => {
// get the data in formattedParts where the type matches the itemDataItem.type
const formattedPart = formattedParts.find((formattedPartItem) => formattedPartItem.type === itemDataItem.type);
if (! formattedPart) {
return;
}
const name = formattedPart.name;
const thumb = formattedPart.thumbnail_transparent || itemDataItem.thumbnail;
let quantity = formattedPart.quantity;
if ('gold_stat_item' === itemDataItem.type) {
// convert to k or m
const quantityInt = Number.parseInt(quantity);
if (quantityInt >= 1000000) {
quantity = `${Math.floor(quantityInt / 100000) / 10}m`;
} else if (quantityInt >= 1000) {
quantity = `${Math.floor(quantityInt / 100) / 10}k`;
}
}
// const itemTooltip = makeElement('div', 'new-tooltip-item');
makeElement('div', ['new-tooltip-item', 'inventoryPage-item'], `
<div class="inventoryPage-item-margin clear-block">
<div class="inventoryPage-item-imageContainer">
<div class="itemImage"><img src="${thumb}">
<div class="quantity">${quantity}</div>
</div>
</div>
<div class="inventoryPage-item-content-nameContainer">
<div class="inventoryPage-item-content-name">
<span>${name}</span>
</div>
</div>
</div>`, tooltipWrapper);
});
tooltip.parentNode.insertBefore(tooltipWrapper, tooltip.nextSibling);
item.classList.remove('new-tooltip-loading');
});
});
};
/**
* Move the recipe to the bottom of the list.
*
* @param {string} type The type of recipe.
* @param {HTMLElement} recipesContainer The container for the recipes.
*/
const moveRecipe = (type, recipesContainer) => {
const recipeEl = document.querySelector(`.inventoryPage-item.recipe[data-produced-item="${type}"]`);
if (recipeEl) {
// move it to the bottom of the list
recipeEl.classList.add('reordered');
recipesContainer.append(recipeEl);
}
};
const updateRecipesOnPage = async (type) => {
if (! recipesToReorder[type]) {
return;
}
const recipesContainer = document.querySelector(`.inventoryPage-tagContent-tagGroup[data-tag="${type}"]`);
if (! recipesContainer) {
return;
}
const recipesModifying = [];
const knownRecipes = document.querySelectorAll('.inventoryPage-tagContent-tagGroup.active .inventoryPage-item.recipe.known');
knownRecipes.forEach((recipe) => {
const recipeId = recipe.getAttribute('data-item-type');
recipesModifying.push(recipeId);
});
// if there are no recipes to modify, then we can stop here.
if (recipesModifying.length === 0) {
return;
}
const itemTypes = recipesModifying.map((recipe) => {
return recipesToReorder[type][recipe];
}).filter(Boolean);
// if we're on the crafting items tab, then also check for dragon slayer cannon and then we can remove all the dragon slayer cannon recipes.
if (type === 'crafting_item') {
itemTypes.push('geyser_draconic_weapon');
}
const ownedItems = await getUserItems(itemTypes);
ownedItems.forEach((item) => {
if (! item.quantity || item.quantity < 1) {
return;
}
if ('geyser_draconic_weapon' === item.type) {
// if we have the dragon slayer cannon, then we can remove all the dragon slayer cannon recipes.
moveRecipe('draconic_geyser_chassis_crafting_item', recipesContainer);
moveRecipe('draconic_geyser_chassis_i_crafting_item', recipesContainer);
} else {
moveRecipe(item.type, recipesContainer);
}
});
};
/**
* Initialize the module.
*/
export default async () => {
onNavigation(cleanUpRecipeBook, {
page: 'inventory',
tab: 'crafting',
subtab: 'recipe',
});
onNavigation(modifySmashableTooltip, {
page: 'inventory',
tab: 'crafting',
subtab: 'hammer',
});
onEvent('js_dialog_show', warnOnBadCrafts);
modifySmashableTooltip();
};