aboutsummaryrefslogtreecommitdiff
path: root/src/database.ts
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-02-10 19:30:36 -0600
committermat <github@matdoes.dev>2022-02-10 19:30:36 -0600
commit7d87d0c3e058e1f6a8c129b2808a5e6ade3b47d6 (patch)
tree33cf680ac831e43e05cb3b40647a04c370b32d32 /src/database.ts
parent3effd71de72aa86bfcd61e26f053ca4b7cc46416 (diff)
downloadskyblock-api-7d87d0c3e058e1f6a8c129b2808a5e6ade3b47d6.tar.gz
skyblock-api-7d87d0c3e058e1f6a8c129b2808a5e6ade3b47d6.tar.bz2
skyblock-api-7d87d0c3e058e1f6a8c129b2808a5e6ade3b47d6.zip
fix leaderboards not loading sometimes, hopefully
Diffstat (limited to 'src/database.ts')
-rw-r--r--src/database.ts75
1 files changed, 44 insertions, 31 deletions
diff --git a/src/database.ts b/src/database.ts
index 571ec5e..e195eed 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -313,21 +313,28 @@ async function fetchMemberLeaderboardRaw(name: string): Promise<memberRawLeaderb
sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1
fetchingRawLeaderboardNames.add(name)
- const leaderboardRaw: memberRawLeaderboardItem[] = (await memberLeaderboardsCollection
- .find(query)
- .sort(sortQuery)
- .limit(leaderboardMax)
- .toArray())
- .map((i: DatabaseMemberLeaderboardItem): memberRawLeaderboardItem => {
- return {
- profile: i.profile,
- uuid: i.uuid,
- value: i.stats[name]
- }
- })
- fetchingRawLeaderboardNames.delete(name)
- cachedRawLeaderboards.set(name, leaderboardRaw)
- return leaderboardRaw
+
+ try {
+ const leaderboardRaw: memberRawLeaderboardItem[] = (await memberLeaderboardsCollection
+ .find(query)
+ .sort(sortQuery)
+ .limit(leaderboardMax)
+ .toArray())
+ .map((i: DatabaseMemberLeaderboardItem): memberRawLeaderboardItem => {
+ return {
+ profile: i.profile,
+ uuid: i.uuid,
+ value: i.stats[name]
+ }
+ })
+ fetchingRawLeaderboardNames.delete(name)
+ cachedRawLeaderboards.set(name, leaderboardRaw)
+ return leaderboardRaw
+ } catch (e) {
+ // if it fails while fetching, remove it from fetchingRawLeaderboardNames
+ fetchingRawLeaderboardNames.delete(name)
+ throw e
+ }
}
async function fetchProfileLeaderboardRaw(name: string): Promise<profileRawLeaderboardItem[]> {
@@ -351,22 +358,28 @@ async function fetchProfileLeaderboardRaw(name: string): Promise<profileRawLeade
sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1
fetchingRawLeaderboardNames.add(name)
- const leaderboardRaw: profileRawLeaderboardItem[] = (await profileLeaderboardsCollection
- .find(query)
- .sort(sortQuery)
- .limit(leaderboardMax)
- .toArray())
- .map((i: DatabaseProfileLeaderboardItem): profileRawLeaderboardItem => {
- return {
- players: i.players,
- uuid: i.uuid,
- value: i.stats[name]
- }
- })
- fetchingRawLeaderboardNames.delete(name)
-
- cachedRawLeaderboards.set(name, leaderboardRaw)
- return leaderboardRaw
+ try {
+ const leaderboardRaw: profileRawLeaderboardItem[] = (await profileLeaderboardsCollection
+ .find(query)
+ .sort(sortQuery)
+ .limit(leaderboardMax)
+ .toArray())
+ .map((i: DatabaseProfileLeaderboardItem): profileRawLeaderboardItem => {
+ return {
+ players: i.players,
+ uuid: i.uuid,
+ value: i.stats[name]
+ }
+ })
+ fetchingRawLeaderboardNames.delete(name)
+
+ cachedRawLeaderboards.set(name, leaderboardRaw)
+ return leaderboardRaw
+ } catch (e) {
+ // if it fails while fetching, remove it from fetchingRawLeaderboardNames
+ fetchingRawLeaderboardNames.delete(name)
+ throw e
+ }
}
interface MemberLeaderboard {