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
|
"use strict";
/**
* Store data about members for leaderboards
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateDatabaseMember = exports.fetchMemberLeaderboard = void 0;
const constants = __importStar(require("./constants"));
const cached = __importStar(require("./hypixelCached"));
const mongodb_1 = require("mongodb");
const node_cache_1 = __importDefault(require("node-cache"));
const util_1 = require("./util");
// don't update the user for 3 minutes
const recentlyUpdated = new node_cache_1.default({
stdTTL: 60 * 3,
checkperiod: 60,
useClones: false,
});
const cachedRawLeaderboards = new Map();
const cachedLeaderboards = new Map();
const leaderboardMax = 100;
let client;
let database;
let memberLeaderboardsCollection;
async function connect() {
if (!process.env.db_uri)
return console.warn('Warning: db_uri was not found in .env. Features that utilize the database such as leaderboards won\'t work.');
if (!process.env.db_name)
return console.warn('Warning: db_name was not found in .env. Features that utilize the database such as leaderboards won\'t work.');
client = await mongodb_1.MongoClient.connect(process.env.db_uri, { useNewUrlParser: true, useUnifiedTopology: true });
database = client.db(process.env.db_name);
memberLeaderboardsCollection = database.collection('member-leaderboards');
}
function getMemberCollectionAttributes(member) {
const collectionAttributes = {};
for (const collection of member.collections) {
const collectionLeaderboardName = `collection_${collection.name}`;
collectionAttributes[collectionLeaderboardName] = collection.xp;
}
return collectionAttributes;
}
function getMemberLeaderboardAttributes(member) {
// if you want to add a new leaderboard for member attributes, add it here (and getAllLeaderboardAttributes)
return {
// we use the raw stat names rather than the clean stats in case hypixel adds a new stat and it takes a while for us to clean it
...member.rawHypixelStats,
// collection leaderboards
...getMemberCollectionAttributes(member),
fairy_souls: member.fairy_souls.total,
first_join: member.first_join,
purse: member.purse,
visited_zones: member.visited_zones.length,
};
}
/** Fetch the names of all the leaderboards */
async function fetchAllMemberLeaderboardAttributes() {
return [
// we use the raw stat names rather than the clean stats in case hypixel adds a new stat and it takes a while for us to clean it
...await constants.fetchStats(),
// collection leaderboards
...(await constants.fetchCollections()).map(value => `collection_${value}`),
'fairy_souls',
'first_join',
'purse',
'visited_zones',
];
}
async function fetchMemberLeaderboardRaw(name) {
if (cachedRawLeaderboards.has(name))
return cachedRawLeaderboards.get(name);
// typescript forces us to make a new variable and set it this way because it gives an error otherwise
const query = {};
query[`stats.${name}`] = { '$exists': true };
const sortQuery = {};
sortQuery[`stats.${name}`] = -1;
const leaderboardRaw = await memberLeaderboardsCollection.find(query).sort(sortQuery).limit(leaderboardMax).toArray();
cachedRawLeaderboards.set(name, leaderboardRaw);
return leaderboardRaw;
}
async function fetchMemberLeaderboard(name) {
if (cachedLeaderboards.has(name))
return cachedLeaderboards.get(name);
const leaderboardRaw = await fetchMemberLeaderboardRaw(name);
const fetchLeaderboardPlayer = async (item) => {
return {
player: await cached.fetchPlayer(item.uuid),
value: item.stats[name]
};
};
const promises = [];
for (const item of leaderboardRaw) {
promises.push(fetchLeaderboardPlayer(item));
}
const leaderboard = await Promise.all(promises);
cachedLeaderboards.set(name, leaderboard);
return leaderboard;
}
exports.fetchMemberLeaderboard = fetchMemberLeaderboard;
async function getMemberLeaderboardRequirement(name) {
const leaderboard = await fetchMemberLeaderboardRaw(name);
// if there's more than 100 items, return the 100th. if there's less, return null
if (leaderboard.length >= leaderboardMax)
return leaderboard[leaderboardMax - 1].stats[name];
else
return null;
}
/** Get the attributes for the member, but only ones that would put them on the top 100 for leaderboards */
async function getApplicableAttributes(member) {
const leaderboardAttributes = getMemberLeaderboardAttributes(member);
const applicableAttributes = {};
for (const [attributeName, attributeValue] of Object.entries(leaderboardAttributes)) {
const requirement = await getMemberLeaderboardRequirement(attributeName);
if (!requirement || attributeValue > requirement)
applicableAttributes[attributeName] = attributeValue;
}
return applicableAttributes;
}
/** Update the member's leaderboard data on the server if applicable */
async function updateDatabaseMember(member, profile) {
if (!client)
return; // the db client hasn't been initialized
// the member's been updated too recently, just return
if (recentlyUpdated.get(profile.uuid + member.uuid))
return;
// store the member in recentlyUpdated so it cant update for 3 more minutes
recentlyUpdated.set(profile.uuid + member.uuid, true);
await constants.addStats(Object.keys(member.rawHypixelStats));
await constants.addCollections(member.collections.map(value => value.name));
const leaderboardAttributes = await getApplicableAttributes(member);
await memberLeaderboardsCollection.updateOne({ uuid: member.uuid }, {
'$set': {
stats: leaderboardAttributes,
last_updated: new Date()
}
}, { upsert: true });
for (const [attributeName, attributeValue] of Object.entries(leaderboardAttributes)) {
const existingLeaderboard = await fetchMemberLeaderboard(attributeName);
const existingRawLeaderboard = await fetchMemberLeaderboardRaw(attributeName);
const newLeaderboard = existingLeaderboard
// remove the player from the leaderboard, if they're there
.filter(value => value.player.uuid !== member.uuid)
.concat([{
player: await cached.fetchPlayer(member.uuid),
value: attributeValue
}])
.sort((a, b) => b.value - a.value)
.slice(0, 100);
const newRawLeaderboard = existingRawLeaderboard
// remove the player from the leaderboard, if they're there
.filter(value => value.uuid !== member.uuid)
.concat([{
last_updated: new Date(),
stats: leaderboardAttributes,
uuid: member.uuid
}])
.sort((a, b) => b.stats[attributeName] - a.stats[attributeName])
.slice(0, 100);
cachedLeaderboards.set(attributeName, newLeaderboard);
cachedRawLeaderboards.set(attributeName, newRawLeaderboard);
}
}
exports.updateDatabaseMember = updateDatabaseMember;
/**
* Remove leaderboard attributes for members that wouldn't actually be on the leaderboard. This saves a lot of storage space
*/
async function removeBadMemberLeaderboardAttributes() {
const leaderboards = await fetchAllMemberLeaderboardAttributes();
// shuffle so if the application is restarting many times itll still be useful
for (const leaderboard of util_1.shuffle(leaderboards)) {
// wait 10 seconds so it doesnt use as much ram
await new Promise(resolve => setTimeout(resolve, 10000));
const unsetValue = {};
unsetValue[leaderboard] = '';
const filter = {};
const requirement = await getMemberLeaderboardRequirement(leaderboard);
if (requirement !== null) {
filter[`stats.${leaderboard}`] = {
'$lt': requirement
};
await memberLeaderboardsCollection.updateMany(filter, { '$unset': unsetValue });
}
}
}
connect()
.then(removeBadMemberLeaderboardAttributes);
|