forked from Anish-Agnihotri/dhof-loot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
81 lines (68 loc) · 2.1 KB
/
parse.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
// Imports
const fs = require("fs");
(async () => {
// Load loot data
const data = await fs.readFileSync("./output/loot.json");
const loot = JSON.parse(data);
// Calculate attribute rarities
let rarityIndex = {};
for (let i = 0; i < loot.length; i++) {
const attributes = loot[i][(i + 1).toString()];
// Add up number of occurences of attributes
for (const attribute of Object.values(attributes)) {
rarityIndex[attribute] = rarityIndex[attribute]
? rarityIndex[attribute] + 1
: 1;
}
}
// Output occurences
await fs.writeFileSync(
"./output/occurences.json",
JSON.stringify(rarityIndex)
);
// Calculate occurence scores
let scores = [];
for (let i = 0; i < loot.length; i++) {
let score = 0;
const attributes = loot[i][(i + 1).toString()];
for (const attribute of Object.values(attributes)) {
score += rarityIndex[attribute];
}
scores.push({ lootId: i + 1, score });
}
// Sort by score
scores = scores.sort((a, b) => a.score - b.score);
// Sort by index of score
scores = scores.map((loot, i) => ({
...loot,
rarest: i + 1,
}));
// Print loot rarity by score
await fs.writeFileSync("./output/rare.json", JSON.stringify(scores));
// Calculate pure probability
let probability = [];
for (let i = 0; i < loot.length; i++) {
let scores = [];
const attributes = loot[i][(i + 1).toString()];
for (const attribute of Object.values(attributes)) {
// Collect probability of individual attribute occurences
scores.push(rarityIndex[attribute] / 8000);
}
// Multiply probabilites P(A and B) = P(A) * P(B)
const p = scores.reduce((a, b) => a * b);
probability.push({ lootId: i + 1, score: p });
}
// Sort by probability
probability = probability.sort((a, b) => a.score - b.score);
// Sort by index of probability
probability = probability.map((loot, i) => ({
...loot,
score: Math.abs(Math.log(loot.score)),
rarest: i + 1,
}));
// Print loot rarity by score
await fs.writeFileSync(
"./output/probability.json",
JSON.stringify(probability)
);
})();