diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-28 02:22:49 -0600 |
---|---|---|
committer | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-28 02:22:49 -0600 |
commit | 6145137197023d5be0556416f279c1e29cae3261 (patch) | |
tree | 853ec16f6a43c1ca4d8dcd063f353128a21a9d0c /src | |
parent | f7d2308fd42b2d9f69bd088172c269531d402301 (diff) | |
download | skyblock-api-6145137197023d5be0556416f279c1e29cae3261.tar.gz skyblock-api-6145137197023d5be0556416f279c1e29cae3261.tar.bz2 skyblock-api-6145137197023d5be0556416f279c1e29cae3261.zip |
only add relevant attributes for member to db
Diffstat (limited to 'src')
-rw-r--r-- | src/database.ts | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/database.ts b/src/database.ts index a7f8f0d..561fdb0 100644 --- a/src/database.ts +++ b/src/database.ts @@ -7,6 +7,8 @@ import * as cached from './hypixelCached' import { Collection, Db, MongoClient } from 'mongodb' import NodeCache from 'node-cache' import { CleanMember } from './cleaners/skyblock/member' +import { CleanPlayer } from './cleaners/player' +import { shuffle } from './util' // don't update the user for 3 minutes const recentlyUpdated = new NodeCache({ @@ -15,12 +17,17 @@ const recentlyUpdated = new NodeCache({ useClones: false, }) -interface LeaderboardItem { +interface DatabaseLeaderboardItem { uuid: string stats: any last_updated: Date } +interface LeaderboardItem { + player: CleanPlayer + value: number +} + const cachedLeaderboards: Map<string, any> = new Map() @@ -92,13 +99,13 @@ export async function fetchMemberLeaderboard(name: string) { const leaderboardRaw = await memberLeaderboardsCollection.find(query).sort(sortQuery).limit(100).toArray() - const fetchLeaderboardPlayer = async(item: LeaderboardItem) => { + const fetchLeaderboardPlayer = async(item: DatabaseLeaderboardItem): Promise<LeaderboardItem> => { return { player: await cached.fetchPlayer(item.uuid), value: item.stats[name] } } - const promises = [] + const promises: Promise<LeaderboardItem>[] = [] for (const item of leaderboardRaw) { promises.push(fetchLeaderboardPlayer(item)) } @@ -107,7 +114,7 @@ export async function fetchMemberLeaderboard(name: string) { return leaderboard } -async function getMemberLeaderboardRequirement(name: string): Promise<LeaderboardItem> { +async function getMemberLeaderboardRequirement(name: string): Promise<number> { const leaderboard = await fetchMemberLeaderboard(name) // if there's more than 100 items, return the 100th. if there's less, return null if (leaderboard.length >= 100) @@ -116,6 +123,17 @@ async function getMemberLeaderboardRequirement(name: string): Promise<Leaderboar 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) { + const leaderboardAttributes = getMemberLeaderboardAttributes(member) + const applicableAttributes = [] + for (const [ attributeName, attributeValue ] of Object.entries(leaderboardAttributes)) { + const requirement = await getMemberLeaderboardRequirement(attributeName) + if (!requirement || attributeValue > requirement) + applicableAttributes[attributeName] = attributeValue + } +} + /** Update the member's leaderboard data on the server if applicable */ export async function updateDatabaseMember(member: CleanMember) { if (!client) return // the db client hasn't been initialized @@ -128,7 +146,7 @@ export async function updateDatabaseMember(member: CleanMember) { await constants.addStats(Object.keys(member.rawHypixelStats)) await constants.addCollections(member.collections.map(value => value.name)) - const leaderboardAttributes = getMemberLeaderboardAttributes(member) + const leaderboardAttributes = await getApplicableAttributes(member) await memberLeaderboardsCollection.updateOne( { @@ -150,7 +168,8 @@ export async function updateDatabaseMember(member: CleanMember) { */ async function removeBadMemberLeaderboardAttributes() { const leaderboards = await fetchAllMemberLeaderboardAttributes() - for (const leaderboard of leaderboards) { + // shuffle so if the application is restarting many times itll still be useful + for (const leaderboard of shuffle(leaderboards)) { // wait 10 seconds so it doesnt use as much ram await new Promise(resolve => setTimeout(resolve, 10000)) |