aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-03-31 21:44:29 -0500
committermat <github@matdoes.dev>2022-03-31 21:44:29 -0500
commit36134e0ee0f5097260306f48f548bcf5a5edae42 (patch)
tree5e920b1ea6bf6eb8e14af6ec0a7e1c32b010c6ee
parente92723ae05efcf41d6ced326c06fe53d9bcb4c1f (diff)
downloadskyblock-api-36134e0ee0f5097260306f48f548bcf5a5edae42.tar.gz
skyblock-api-36134e0ee0f5097260306f48f548bcf5a5edae42.tar.bz2
skyblock-api-36134e0ee0f5097260306f48f548bcf5a5edae42.zip
remove deleted profiles from leaderboard
-rw-r--r--src/database.ts21
-rw-r--r--src/hypixel.ts9
2 files changed, 28 insertions, 2 deletions
diff --git a/src/database.ts b/src/database.ts
index 8214208..505efda 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -647,6 +647,27 @@ async function getApplicableProfileLeaderboardAttributes(profile: CleanFullProfi
return applicableAttributes
}
+/**
+ * Make sure there's no lingering profiles from when a player's profile was
+ * deleted. This only makes one database call if there's no profiles to delete.
+ */
+export async function removeDeletedProfilesFromLeaderboards(memberUuid: string, profilesUuids: string[]) {
+ const profilesUuidsFromDatabase = (await (await memberLeaderboardsCollection.find({
+ uuid: memberUuid,
+ })).toArray()).map(m => m.profile)
+
+ for (const profileUuid of profilesUuidsFromDatabase) {
+ if (!profilesUuids.includes(profileUuid)) {
+ await memberLeaderboardsCollection.deleteOne({
+ uuid: memberUuid,
+ profile: profileUuid
+ })
+ if (debug)
+ console.log(`Profile ${profileUuid} (member ${memberUuid}) was deleted but was still in leaderboards database, removed.`)
+ }
+ }
+}
+
/** Update the member's leaderboard data on the server if applicable */
export async function updateDatabaseMember(member: CleanMember, profile: CleanFullProfile): Promise<void> {
if (!client) return // the db client hasn't been initialized
diff --git a/src/hypixel.ts b/src/hypixel.ts
index 44a5bd8..59d09aa 100644
--- a/src/hypixel.ts
+++ b/src/hypixel.ts
@@ -14,9 +14,11 @@ import {
AccountSchema,
fetchAccount,
queueUpdateDatabaseMember,
- queueUpdateDatabaseProfile
+ queueUpdateDatabaseProfile,
+ removeDeletedProfilesFromLeaderboards
} from './database.js'
import { cleanElectionResponse, ElectionData } from './cleaners/skyblock/election.js'
+import { cleanItemListResponse, ItemListData } from './cleaners/skyblock/itemList.js'
import { CleanBasicMember, CleanMemberProfile } from './cleaners/skyblock/member.js'
import { cleanSkyblockProfilesResponse } from './cleaners/skyblock/profiles.js'
import { CleanPlayer, cleanPlayerResponse } from './cleaners/player.js'
@@ -25,7 +27,6 @@ import typedHypixelApi from 'typed-hypixel-api'
import * as cached from './hypixelCached.js'
import { debug } from './index.js'
import { WithId } from 'mongodb'
-import { cleanItemListResponse, ItemListData } from './cleaners/skyblock/itemList.js'
export type Included = 'profiles' | 'player' | 'stats' | 'inventories' | undefined
@@ -142,12 +143,16 @@ export async function fetchUser({ user, uuid, username }: UserAny, included: Inc
activeProfile = profile
}
}
+
+ // we don't await so it happens in the background
+ removeDeletedProfilesFromLeaderboards(uuid, profilesData!.map(p => p.uuid))
}
let websiteAccount: WithId<AccountSchema> | null = null
if (websiteAccountPromise)
websiteAccount = await websiteAccountPromise
+
return {
player: playerData,
profiles: profilesData ?? basicProfilesData,