diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-28 16:19:35 -0600 |
---|---|---|
committer | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-28 16:19:35 -0600 |
commit | 37b8857107e529344b2077409aaeff5efe9a2432 (patch) | |
tree | 36dbe72808f3f64e8d138d8e153dd3afd126c581 /src | |
parent | dfd4016d4ea907719d0b4edf28e4b9529534870c (diff) | |
download | skyblock-api-37b8857107e529344b2077409aaeff5efe9a2432.tar.gz skyblock-api-37b8857107e529344b2077409aaeff5efe9a2432.tar.bz2 skyblock-api-37b8857107e529344b2077409aaeff5efe9a2432.zip |
make first_join be sorted in reverse
Diffstat (limited to 'src')
-rw-r--r-- | src/database.ts | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/database.ts b/src/database.ts index b6507cc..afbcf08 100644 --- a/src/database.ts +++ b/src/database.ts @@ -34,7 +34,9 @@ const cachedRawLeaderboards: Map<string, DatabaseLeaderboardItem[]> = new Map() const cachedLeaderboards: Map<string, LeaderboardItem[]> = new Map() const leaderboardMax = 100 - +const reversedStats = [ + 'first_join' +] let client: MongoClient let database: Db @@ -111,15 +113,19 @@ export async function fetchAllMemberLeaderboardAttributes(): Promise<string[]> { ] } +function isLeaderboardReversed(name: string): boolean { + return reversedStats.includes(name) +} + async function fetchMemberLeaderboardRaw(name: string): Promise<DatabaseLeaderboardItem[]> { 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 } + query[`stats.${name}`] = { '$exists': true, '$ne': NaN } const sortQuery: any = {} - sortQuery[`stats.${name}`] = -1 + sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1 const leaderboardRaw = await memberLeaderboardsCollection.find(query).sort(sortQuery).limit(leaderboardMax).toArray() cachedRawLeaderboards.set(name, leaderboardRaw) @@ -194,6 +200,7 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu for (const [ attributeName, attributeValue ] of Object.entries(leaderboardAttributes)) { const existingLeaderboard = await fetchMemberLeaderboard(attributeName) const existingRawLeaderboard = await fetchMemberLeaderboardRaw(attributeName) + const leaderboardReverse = isLeaderboardReversed(attributeName) const newLeaderboard = existingLeaderboard // remove the player from the leaderboard, if they're there .filter(value => value.player.uuid !== member.uuid) @@ -201,7 +208,7 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu player: await cached.fetchPlayer(member.uuid), value: attributeValue }]) - .sort((a, b) => b.value - a.value) + .sort((a, b) => leaderboardReverse ? a.value - b.value : b.value - a.value) .slice(0, 100) const newRawLeaderboard = existingRawLeaderboard // remove the player from the leaderboard, if they're there @@ -211,7 +218,7 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu stats: leaderboardAttributes, uuid: member.uuid }]) - .sort((a, b) => b.stats[attributeName] - a.stats[attributeName]) + .sort((a, b) => leaderboardReverse ? a.stats[attributeName] - b.stats[attributeName] : b.stats[attributeName] - a.stats[attributeName]) .slice(0, 100) cachedLeaderboards.set(attributeName, newLeaderboard) cachedRawLeaderboards.set(attributeName, newRawLeaderboard) |