aboutsummaryrefslogtreecommitdiff
path: root/build
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-04-14 21:24:11 -0500
committerGitHub <noreply@github.com>2021-04-14 21:24:11 -0500
commitc31e4fe77467486dc03226ebdad4c0bc67e1d715 (patch)
tree03cc94dd7155eeb6a522ebd7e7f65eff86ef6e09 /build
parent52907cf2056cd434dad7270475fc9e4a532c04fa (diff)
downloadskyblock-api-c31e4fe77467486dc03226ebdad4c0bc67e1d715.tar.gz
skyblock-api-c31e4fe77467486dc03226ebdad4c0bc67e1d715.tar.bz2
skyblock-api-c31e4fe77467486dc03226ebdad4c0bc67e1d715.zip
Total leaderboards leaderboard working (#6)
* add leaderboards leaderboard * slightly optimize fetchPlayer to not unnecessarily fetch the same player twice * Compiled TS into JS * fix errors with fetch * fix random bugs that made people disappear from leaderboards
Diffstat (limited to 'build')
-rw-r--r--build/cleaners/skyblock/stats.js3
-rw-r--r--build/database.js15
-rw-r--r--build/hypixelCached.js10
3 files changed, 24 insertions, 4 deletions
diff --git a/build/cleaners/skyblock/stats.js b/build/cleaners/skyblock/stats.js
index 0eb94c7..482771c 100644
--- a/build/cleaners/skyblock/stats.js
+++ b/build/cleaners/skyblock/stats.js
@@ -60,7 +60,8 @@ exports.categorizeStat = categorizeStat;
exports.statUnits = {
time: ['_best_time', '_best_time_2'],
date: ['first_join'],
- coins: ['purse']
+ coins: ['purse'],
+ leaderboards: ['leaderboards_count']
};
function getStatUnit(name) {
for (const [unitName, statMatchers] of Object.entries(exports.statUnits)) {
diff --git a/build/database.js b/build/database.js
index e889f1e..b76b85c 100644
--- a/build/database.js
+++ b/build/database.js
@@ -155,6 +155,7 @@ async function fetchAllMemberLeaderboardAttributes() {
'first_join',
'purse',
'visited_zones',
+ 'leaderboards_count'
];
}
exports.fetchAllMemberLeaderboardAttributes = fetchAllMemberLeaderboardAttributes;
@@ -249,6 +250,13 @@ async function getApplicableAttributes(member) {
applicableAttributes[leaderboard] = attributeValue;
}
}
+ let leaderboardsCount = Object.keys(applicableAttributes).length;
+ const leaderboardsCountRequirement = await getMemberLeaderboardRequirement('leaderboards_count');
+ if ((leaderboardsCountRequirement === null)
+ || (leaderboardsCount > leaderboardsCountRequirement)) {
+ // add 1 extra because this attribute also counts :)
+ applicableAttributes['leaderboards_count'] = leaderboardsCount;
+ }
return applicableAttributes;
}
/** Update the member's leaderboard data on the server if applicable */
@@ -273,7 +281,7 @@ async function updateDatabaseMember(member, profile) {
console.log('done constants..');
const leaderboardAttributes = await getApplicableAttributes(member);
if (_1.debug)
- console.log('done getApplicableAttributes..', leaderboardAttributes);
+ console.log('done getApplicableAttributes..', leaderboardAttributes, member.username, profile.name);
await memberLeaderboardsCollection.updateOne({
uuid: member.uuid,
profile: profile.uuid
@@ -288,7 +296,7 @@ async function updateDatabaseMember(member, profile) {
const leaderboardReverse = isLeaderboardReversed(attributeName);
const newRawLeaderboard = existingRawLeaderboard
// remove the player from the leaderboard, if they're there
- .filter(value => value.uuid !== member.uuid)
+ .filter(value => value.uuid !== member.uuid || value.profile !== profile.uuid)
.concat([{
last_updated: new Date(),
stats: leaderboardAttributes,
@@ -354,7 +362,8 @@ connect().then(() => {
// when it connects, cache the leaderboards and remove bad members
removeBadMemberLeaderboardAttributes();
// cache leaderboards on startup so its faster later on
- fetchAllLeaderboards(true);
+ // fetchAllLeaderboards(true)
+ fetchAllLeaderboards(false);
// cache leaderboard players again every 4 hours
setInterval(fetchAllLeaderboards, 4 * 60 * 60 * 1000);
});
diff --git a/build/hypixelCached.js b/build/hypixelCached.js
index a667ec4..8a35482 100644
--- a/build/hypixelCached.js
+++ b/build/hypixelCached.js
@@ -145,14 +145,24 @@ async function usernameFromUser(user) {
return username;
}
exports.usernameFromUser = usernameFromUser;
+let fetchingPlayers = new Set();
async function fetchPlayer(user) {
const playerUuid = await uuidFromUser(user);
if (playerCache.has(playerUuid))
return playerCache.get(playerUuid);
+ // if it's already in the process of fetching, check every 100ms until it's not fetching the player anymore and fetch it again, since it'll be cached now
+ if (fetchingPlayers.has(playerUuid)) {
+ while (fetchingPlayers.has(playerUuid)) {
+ await new Promise(resolve => setTimeout(resolve, 100));
+ }
+ return await fetchPlayer(user);
+ }
+ fetchingPlayers.add(playerUuid);
const cleanPlayer = await hypixel.sendCleanApiRequest({
path: 'player',
args: { uuid: playerUuid }
});
+ fetchingPlayers.delete(playerUuid);
if (!cleanPlayer)
return;
// clone in case it gets modified somehow later