-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
386 lines (356 loc) · 10.9 KB
/
index.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
import fs from "node:fs/promises";
import * as api from "./api.js";
const categoryWeapon = 1;
const weaponTypeHashes = [
5, // Auto Rifle
6, // Hand Cannon
7, // Pulse Rifle
8, // Scout Rifle
9, // Fusion Rifle
10, // Sniper Rifle
11, // Shotgun
12, // Machine Gun
13, // Rocket Launcher
14, // Sidearm
54, // Sword
153950757, // Grenade Launchers
1504945536, // Linear Fusion Rifles
2489664120, // Trace Rifles
3317538576, // Bows
3871742104, // Glaives
3954685534, // Submachine Guns
];
const damageTypeOrder = ["Kinetic", "Arc", "Solar", "Void", "Stasis", "Strand"];
const filterItemByCategory = (categoryHash) => {
return (item) => {
return item.itemCategoryHashes?.includes(categoryHash);
};
};
const filterItemByTier = (tier) => {
return (item) => item.inventory.tierTypeHash === tier;
};
const pickWeaponTypeHash = (item) => {
const potentialWeaponTypeHashes = item.itemCategoryHashes.filter((hash) =>
weaponTypeHashes.includes(hash),
);
let weaponTypeHash;
// Trivial case
if (potentialWeaponTypeHashes.length === 1) {
weaponTypeHash = potentialWeaponTypeHashes[0];
}
// Special logic for weapons with more than one type
if (potentialWeaponTypeHashes.length > 1) {
// Check for Linear Fusion Rifle
if (
potentialWeaponTypeHashes.includes(9) &&
potentialWeaponTypeHashes.includes(1504945536)
) {
weaponTypeHash = 1504945536;
} else {
throw new Error(
`Unknown weapon type hash group for "${item.displayProperties.name}": [${potentialWeaponTypeHashes}]`,
);
}
}
return weaponTypeHash;
};
/**
* Some weapon frames/archetypes are special modifications of existing ones.
* To keep the tables compact, some of them are renamed to match their more
* generic version.
* @param {String} name
* @returns {String}
*/
const remapPerkName = (name) => {
const map = new Map();
map.set("Legacy PR-55 Frame", "Lightweight Frame"); // BxR-55 Battler
map.set("MIDA Synergy", "Lightweight Frame"); // MIDA Mini-Tool
map.set("Shot Package", "Aggressive Frame"); // Felwinter's Lie
map.set("Together Forever", "Adaptive Frame"); // Drang
if (map.has(name)) {
return map.get(name);
} else {
return name;
}
};
const getWeapons = async (manifest) => {
const DestinyDamageTypeDefinition = await api.getApi(
manifest.DestinyDamageTypeDefinition,
);
const DestinySocketTypeDefinition = await api.getApi(
manifest.DestinySocketTypeDefinition,
);
const DestinySocketCategoryDefinition = await api.getApi(
manifest.DestinySocketCategoryDefinition,
);
const DestinyItemCategoryDefinition = await api.getApi(
manifest.DestinyItemCategoryDefinition,
);
const DestinyInventoryItemDefinition = await api.getApi(
manifest.DestinyInventoryItemDefinition,
);
const weapons = Object.values(DestinyInventoryItemDefinition)
.filter(filterItemByCategory(categoryWeapon))
// Legendary
.filter(filterItemByTier(4008398120))
// Exclude dummy items
.filter((item) => !filterItemByCategory(3109687656)(item));
// Only include items with collectible hashes
const weaponInfo = weapons
.map((item) => {
return {
name: item.displayProperties.name,
icon: new URL(item.displayProperties.icon, api.URL_BASE),
iconWatermark: item.quality
? // New format
new URL(
item.quality.displayVersionWatermarkIcons[
item.quality.displayVersionWatermarkIcons.length - 1
],
api.URL_BASE,
)
: // Old format
new URL(item.iconWatermarkShelved, api.URL_BASE),
hash: item.hash,
weaponType:
DestinyItemCategoryDefinition[pickWeaponTypeHash(item)]
.displayProperties.name,
damageType:
DestinyDamageTypeDefinition[item.damageTypeHashes?.[0]]
?.displayProperties.name,
intrinsicPerk: remapPerkName(
item.sockets.socketEntries
.filter((entry) => {
const socketType =
DestinySocketTypeDefinition[entry.socketTypeHash];
const socketCategory =
DestinySocketCategoryDefinition[socketType?.socketCategoryHash];
return (
socketCategory?.displayProperties.name === "INTRINSIC TRAITS"
);
})
.map(
(entry) =>
DestinyInventoryItemDefinition[entry.singleInitialItemHash]
.displayProperties.name,
)[0],
),
// This is the best way I know of to determine craftability
craftable:
item.tooltipNotifications?.filter((tip) =>
tip.displayStyle.includes("deepsight"),
).length > 0,
};
})
.filter((item) => item.damageType !== undefined);
return weaponInfo;
};
const main = async () => {
const manifest = await api.getManifest("en");
const weapons = await getWeapons(manifest);
const weaponTypes = Array.from(
new Set(weapons.map((weapon) => weapon.weaponType)),
);
weaponTypes.sort();
const tables = [];
const iconSize = 48;
for (const weaponType of weaponTypes) {
const weaponsOfType = weapons.filter(
(weapon) => weapon.weaponType === weaponType,
);
const intrinsics = Array.from(
new Set(weaponsOfType.map((weapon) => weapon.intrinsicPerk)),
);
intrinsics.sort((a, b) => a.localeCompare(b));
const damageTypes = Array.from(
new Set(weaponsOfType.map((weapon) => weapon.damageType)),
);
damageTypes.sort(
(a, b) => damageTypeOrder.indexOf(a) - damageTypeOrder.indexOf(b),
);
const rows = [];
for (const intrinsic of intrinsics) {
const row = new Map();
row.set("frame", intrinsic);
for (const damageType of damageTypes) {
const matchingWeapons = weaponsOfType.filter((weapon) => {
return (
(weapon.intrinsicPerk === intrinsic) &
(weapon.damageType === damageType)
);
});
matchingWeapons.sort((a, b) => a.name.localeCompare(b.name));
row.set(damageType, matchingWeapons);
}
rows.push(row);
}
const captionId = weaponType.replace(/\s/g, "-");
// Make HTML
const html = `
<table>
<caption id="${captionId}"><a href="#${captionId}">${weaponType}</a></caption>
<tr>
<th scope="col">Frame</th>
${damageTypes
.map((type) => `<th scope="col">${type}</th>`)
.join("\n")}
</tr>
${rows
.map(
(row) => `
<tr>
${Array.from(row.values())
.map((weapons, index) => {
const val =
index === 0
? // The first item in this array is the intrinsic perk
weapons
: // Otherwise it's an array of weapons
`
<ul>
${weapons
.map((weapon) => {
const name = `${weapon.name}${
weapon.craftable ? " (Craftable)" : ""
}`;
const watermark = weapon.iconWatermark;
const icon = weapon.icon;
const d2FoundryURL = new URL(
`/w/${weapon.hash}`,
"https://d2foundry.gg",
);
return `
<li>
<a href="${d2FoundryURL}" title="${name}">
<div class="icon ${
weapon.craftable ? "craftable" : ""
}">
<img
class="icon-watermark"
src="${watermark}"
alt=""
width="${iconSize}"
height="${iconSize}"
loading="lazy"
>
<img
src="${icon}"
alt="${name}"
width="${iconSize}"
height="${iconSize}"
loading="lazy"
>
</div>
</a>
</li>
`;
})
.join("\n")}
</ul>
`;
return index === 0
? `<th scope="row">${val}</th>`
: `<td>${val}</td>`;
})
.join("\n")}
</tr>
`,
)
.join("\n")}
</table>
`;
tables.push(html);
}
const css = `
:root {
color-scheme: dark;
--bg-dark: rgb(28, 27, 34);
--fg-dark: rgb(251, 251, 254);
}
body {
background: var(--bg-dark);
color: var(--fg-dark);
font-size: 18px;
font-family: sans-serif;
}
a {
color: #8c8cff;
}
a:visited {
color: #ffadff;
}
h1 {
margin: auto;
text-align: center;
font-size: 4em;
margin-bottom: 3rem;
}
caption {
font-size: 3em;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 3em;
}
td,
th {
border: 1px solid grey;
}
tr {
height: 2em;
}
th[scope="row"] {
padding: 0 1em;
}
td {
vertical-align: top;
padding: 0.75em;
}
ul {
list-style: none;
margin: 0;
padding: 0;
display: grid;
grid-template-columns: repeat(3, ${iconSize}px);
gap: 1em;
}
li {
width: ${iconSize}px;
}
.icon {
display: flex;
}
.icon.craftable {
outline-color: rgb(162, 0, 0);
outline-style: solid;
outline-width: 2px;
}
.icon-watermark {
position: absolute;
}
`;
const fullPage = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Destiny 2 Damage Types vs. Weapon Frames</title>
<style>
${css}
</style>
</head>
<body>
<h1>Legendary Weapons</h1>
${tables.join("\n")}
<footer>
<p>Version: <code>${manifest.version}</code></p>
</footer>
</body>
</html>
`;
await fs.writeFile("./index.html", fullPage);
};
main().catch(console.error);