forked from SkyCryptWebsite/SkyCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollections.js
More file actions
151 lines (123 loc) · 4.55 KB
/
Copy pathcollections.js
File metadata and controls
151 lines (123 loc) · 4.55 KB
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
import * as constants from "../constants.js";
import * as helper from "../helper.js";
import { db } from "../mongo.js";
/**
* Retrieves the collection data for a given player UUID.
*
* @async
* @function
* @param {string} uuid - The UUID of the player to retrieve collection data for.
* @param {Object} profile - The player's profile object.
* @param {boolean} [cacheOnly=false] - Whether to only use cached data.
* @returns {Promise<{[key: string]: {
* name: string,
* id: string,
* texture: string,
* amount: number,
* totalAmount: number,
* tier: number,
* maxTier: number,
* amounts: {username: string, amount: number}[],
* }[]}>} An object containing the player's collection data, with each collection's data organized by its ID.
*/
export async function getCollections(uuid, profile, calculated, cacheOnly = false) {
const output = {};
const userProfile = profile.members[uuid];
if (
(userProfile?.player_data && !("unlocked_coll_tiers" in (userProfile?.player_data ?? {}))) ||
!("collection" in userProfile)
) {
return;
}
const members = (
await Promise.all(Object.keys(profile.members).map((a) => helper.resolveUsernameOrUuid(a, db, cacheOnly)))
).reduce((acc, a) => ((acc[a.uuid] = a.display_name), acc), {});
const { collections: collectionData } = await db.collection("collections").findOne({ _id: "collections" });
for (const [categoryId, categoryData] of Object.entries(collectionData)) {
const category = categoryId.toLowerCase();
output[category] ??= {
name: categoryData.name,
collections: [],
};
for (const collection of categoryData.items) {
const { id, maxTier, name, texture } = collection;
const amount = userProfile.collection[id] || 0;
const amounts = Object.keys(profile.members).map((uuid) => {
return {
username: members[uuid],
amount: (profile.members[uuid].collection && profile.members[uuid].collection[id]) ?? 0,
};
});
const totalAmount = amounts.reduce((a, b) => a + b.amount, 0);
const tier = collection.tiers.findLast((a) => a.amountRequired <= totalAmount)?.tier ?? 0;
output[category].collections.push({
name,
id: id,
texture,
amount,
totalAmount,
tier,
maxTier,
amounts,
});
}
output[category].totalTiers = output[category].collections.length;
output[category].maxTiers = output[category].collections.filter((a) => a.tier === a.maxTier).length;
}
const dungeons = calculated.dungeons ?? {};
const kuudra = calculated.crimson_isle?.kuudra ?? {};
const bossCollections = getBossCollections(dungeons, kuudra);
output["BOSS"] = {
name: "Boss Collections",
collections: bossCollections,
totalTiers: bossCollections.length,
maxTiers: bossCollections.filter((a) => a.tier === a.maxTier).length,
};
output.totalCollections = Object.values(output).reduce((a, b) => a + b.collections.length, 0);
output.maxedCollections = Object.values(output)
.map((a) => a.collections)
.flat()
.filter((a) => a && a.tier === a.maxTier).length;
return output;
}
function getBossCollections(dungeons, kuudra) {
const output = [];
if (dungeons?.catacombs?.floors === undefined) {
return output;
}
const bossCompletions = {};
for (const [floor, data] of Object.entries(dungeons.catacombs.floors)) {
bossCompletions[floor] ??= 0;
bossCompletions[floor] += data.stats.tier_completions ?? 0;
}
if (dungeons.master_catacombs?.floors) {
for (const [floor, data] of Object.entries(dungeons.master_catacombs.floors)) {
bossCompletions[floor] ??= 0;
bossCompletions[floor] += (data.stats.tier_completions ?? 0) * 2;
}
}
for (const collection of constants.BOSS_COLLECTIONS) {
const index = constants.BOSS_COLLECTIONS.indexOf(collection) + 1;
const { name, texture } = collection;
let amount = bossCompletions[index] ?? 0;
if (name === "Kuudra" && kuudra.tiers !== undefined) {
amount = Object.values(kuudra.tiers).reduce((a, b, index) => {
return a + (index + 1) * b.completions;
}, 0);
}
const maxAmount = collection.rewards[collection.rewards.length - 1]?.required;
const tier = collection.rewards.filter((a) => a.required <= amount).length ?? 0;
const maxTier = collection.rewards.length;
const rewards = collection.rewards.filter((reward) => reward.required <= amount);
output.push({
name,
texture,
amount,
maxAmount,
tier,
maxTier,
rewards,
});
}
return output;
}