-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
36 lines (30 loc) · 1.03 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
// Store build div
const build = document.querySelector("#build");
// Store hook button
const roll = document.querySelector("#roll");
// Load item list
const items = await fetch("./items.json").then(r => r.json());
// Items loaded - hook and enable button
roll.addEventListener("click", () => {
function flatRandom(arr) {
arr = arr.flat(Infinity);
return arr[Math.floor(Math.random() * arr.length)];
}
// Roll the basic 4 items
const rolledItems = ["helmet", "armor", "shoes", "weapon"]
.map(type => items[type])
.map(flatRandom);
// If the rolled weapon isn't two-handed, roll an off-hand
if (rolledItems[3].id[0] !== "2") {
rolledItems.push(flatRandom(items.offhand));
}
// Update the displayed build
build.replaceChildren(...rolledItems.map(item => {
const img = document.createElement("img");
img.width = img.height = 217;
img.src = `https://render.albiononline.com/v1/item/T4_${item.id}.png`;
img.alt = img.title = item.name;
return img;
}));
});
roll.removeAttribute("disabled");