aboutsummaryrefslogtreecommitdiff
path: root/build
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-04-14 20:23:59 -0500
committerGitHub <noreply@github.com>2021-04-14 20:23:59 -0500
commit0e5887326f1ba1f8005ca0bd217c5b332a1fb6cf (patch)
treed59cc024e2cf845b7bb044760a907da799989829 /build
parentd5554e51b51ed69bfb60a21723652e11c06fe7eb (diff)
downloadskyblock-api-0e5887326f1ba1f8005ca0bd217c5b332a1fb6cf.tar.gz
skyblock-api-0e5887326f1ba1f8005ca0bd217c5b332a1fb6cf.tar.bz2
skyblock-api-0e5887326f1ba1f8005ca0bd217c5b332a1fb6cf.zip
Total leaderboards leaderboard (#5)
* add leaderboards leaderboard * slightly optimize fetchPlayer to not unnecessarily fetch the same player twice * Compiled TS into JS * fix errors with fetch
Diffstat (limited to 'build')
-rw-r--r--build/database.js14
-rw-r--r--build/hypixelApi.js1
-rw-r--r--build/hypixelCached.js10
3 files changed, 22 insertions, 3 deletions
diff --git a/build/database.js b/build/database.js
index e889f1e..2c57d8c 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;
@@ -214,7 +215,7 @@ async function fetchMemberLeaderboardSpots(player, profile) {
const fullMember = fullProfile.members.find(m => m.username.toLowerCase() === player.toLowerCase() || m.uuid === player);
// update the leaderboard positions for the member
await updateDatabaseMember(fullMember, fullProfile);
- const applicableAttributes = await getApplicableAttributes(fullMember);
+ const applicableAttributes = await getApplicableAttributes(fullMember, fullProfile);
const memberLeaderboardSpots = [];
for (const leaderboardName in applicableAttributes) {
const leaderboard = await fetchMemberLeaderboardRaw(leaderboardName);
@@ -238,7 +239,7 @@ async function getMemberLeaderboardRequirement(name) {
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) {
+async function getApplicableAttributes(member, profile) {
const leaderboardAttributes = getMemberLeaderboardAttributes(member);
const applicableAttributes = {};
for (const [leaderboard, attributeValue] of Object.entries(leaderboardAttributes)) {
@@ -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 + 1;
+ }
return applicableAttributes;
}
/** Update the member's leaderboard data on the server if applicable */
@@ -271,7 +279,7 @@ async function updateDatabaseMember(member, profile) {
await constants.addSlayers(member.slayers.bosses.map(s => s.raw_name));
if (_1.debug)
console.log('done constants..');
- const leaderboardAttributes = await getApplicableAttributes(member);
+ const leaderboardAttributes = await getApplicableAttributes(member, profile);
if (_1.debug)
console.log('done getApplicableAttributes..', leaderboardAttributes);
await memberLeaderboardsCollection.updateOne({
diff --git a/build/hypixelApi.js b/build/hypixelApi.js
index 2b3a601..c40fe0f 100644
--- a/build/hypixelApi.js
+++ b/build/hypixelApi.js
@@ -56,6 +56,7 @@ async function sendApiRequest({ path, key, args }) {
fetchResponse = await node_fetch_1.default(fetchUrl, { agent: () => httpsAgent });
}
catch {
+ console.log('error in fetch :/');
// if there's an error, wait a second and try again
await new Promise((resolve) => setTimeout(resolve, 1000));
return await sendApiRequest({ path, key, args });
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