aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-04-08 14:17:40 -0500
committermat <27899617+mat-1@users.noreply.github.com>2021-04-08 14:17:40 -0500
commit541731a63c5e05baa58dd6fcb598c14dd4ce9d88 (patch)
treee901127694d2398d8e68697fc3b557dfe14b04d6
parentddefc69df40827fe04902ab2da4baa4da3db96d5 (diff)
downloadskyblock-api-541731a63c5e05baa58dd6fcb598c14dd4ce9d88.tar.gz
skyblock-api-541731a63c5e05baa58dd6fcb598c14dd4ce9d88.tar.bz2
skyblock-api-541731a63c5e05baa58dd6fcb598c14dd4ce9d88.zip
add member leaderboard spots api
-rw-r--r--src/database.ts40
-rw-r--r--src/index.ts10
2 files changed, 45 insertions, 5 deletions
diff --git a/src/database.ts b/src/database.ts
index 66d4142..6e5a41b 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -23,6 +23,7 @@ const recentlyUpdated = new NodeCache({
interface DatabaseLeaderboardItem {
uuid: string
+ profile: string
stats: any
last_updated: Date
}
@@ -178,7 +179,12 @@ async function fetchMemberLeaderboardRaw(name: string): Promise<DatabaseLeaderbo
const sortQuery: any = {}
sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1
- const leaderboardRaw = await memberLeaderboardsCollection.find(query).sort(sortQuery).limit(leaderboardMax).toArray()
+ const leaderboardRaw: DatabaseLeaderboardItem[] = await memberLeaderboardsCollection
+ .find(query)
+ .sort(sortQuery)
+ .limit(leaderboardMax)
+ .toArray()
+
cachedRawLeaderboards.set(name, leaderboardRaw)
return leaderboardRaw
}
@@ -209,6 +215,32 @@ export async function fetchMemberLeaderboard(name: string): Promise<Leaderboard>
}
}
+/** Get the leaderboard positions a member is on. This may take a while depending on whether stuff is cached */
+export async function fetchMemberLeaderboardSpots(player: string, profile: string) {
+ const fullProfile = await cached.fetchProfile(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 memberLeaderboardSpots = []
+
+ for (const leaderboardName in applicableAttributes) {
+ const leaderboard = await fetchMemberLeaderboardRaw(leaderboardName)
+ const leaderboardPositionIndex = leaderboard.findIndex(i => i.uuid === fullMember.uuid && i.profile === fullProfile.uuid)
+ memberLeaderboardSpots.push({
+ name: leaderboardName,
+ positionIndex: leaderboardPositionIndex,
+ value: applicableAttributes[leaderboardName],
+
+ })
+ }
+
+ return memberLeaderboardSpots
+}
+
async function getMemberLeaderboardRequirement(name: string): Promise<number> {
const leaderboard = await fetchMemberLeaderboardRaw(name)
// if there's more than 100 items, return the 100th. if there's less, return null
@@ -219,7 +251,7 @@ async function getMemberLeaderboardRequirement(name: string): Promise<number> {
}
/** Get the attributes for the member, but only ones that would put them on the top 100 for leaderboards */
-async function getApplicableAttributes(member): Promise<StringNumber> {
+async function getApplicableAttributes(member: CleanMember): Promise<StringNumber> {
const leaderboardAttributes = getMemberLeaderboardAttributes(member)
const applicableAttributes = {}
for (const [ leaderboard, attributeValue ] of Object.entries(leaderboardAttributes)) {
@@ -277,7 +309,8 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu
.concat([{
last_updated: new Date(),
stats: leaderboardAttributes,
- uuid: member.uuid
+ uuid: member.uuid,
+ profile: profile.uuid
}])
.sort((a, b) => leaderboardReverse ? a.stats[attributeName] - b.stats[attributeName] : b.stats[attributeName] - a.stats[attributeName])
.slice(0, 100)
@@ -329,6 +362,7 @@ async function removeBadMemberLeaderboardAttributes(): Promise<void> {
/** Fetch all the leaderboards, used for caching. Don't call this often! */
async function fetchAllLeaderboards(fast?: boolean): Promise<void> {
const leaderboards = await fetchAllMemberLeaderboardAttributes()
+
// shuffle so if the application is restarting many times itll still be useful
if (debug) console.log('Caching leaderboards!')
for (const leaderboard of shuffle(leaderboards)) {
diff --git a/src/index.ts b/src/index.ts
index 0d66c55..451ef95 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,7 +1,7 @@
+import { fetchAllLeaderboardsCategorized, fetchMemberLeaderboard, fetchMemberLeaderboardSpots } from './database'
import { fetchMemberProfile, fetchUser } from './hypixel'
-import express from 'express'
-import { fetchAllLeaderboardsCategorized, fetchMemberLeaderboard } from './database'
import rateLimit from 'express-rate-limit'
+import express from 'express'
const app = express()
@@ -40,6 +40,12 @@ app.get('/player/:user/:profile', async(req, res) => {
)
})
+app.get('/player/:user/:profile/leaderboards', async(req, res) => {
+ res.json(
+ await fetchMemberLeaderboardSpots(req.params.user, req.params.profile)
+ )
+})
+
app.get('/leaderboard/:name', async(req, res) => {
res.json(
await fetchMemberLeaderboard(req.params.name)