diff options
-rw-r--r-- | src/database.ts | 21 | ||||
-rw-r--r-- | src/hypixel.ts | 9 |
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, |