diff options
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/database.ts | 63 |
2 files changed, 41 insertions, 24 deletions
diff --git a/package.json b/package.json index 857ae6d..64455e4 100644 --- a/package.json +++ b/package.json @@ -54,4 +54,4 @@ "typescript": "^4.6.3" }, "type": "module" -} +}
\ No newline at end of file diff --git a/src/database.ts b/src/database.ts index 7371fbf..8214208 100644 --- a/src/database.ts +++ b/src/database.ts @@ -673,19 +673,27 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu if (debug) console.debug('done getApplicableMemberLeaderboardAttributes..', leaderboardAttributes, member.username, profile.name) - await memberLeaderboardsCollection.updateOne( - { + if (leaderboardAttributes.length > 0) { + await memberLeaderboardsCollection.updateOne( + { + uuid: member.uuid, + profile: profile.uuid + }, + { + '$set': { + stats: leaderboardAttributes, + last_updated: new Date() + } + }, + { upsert: true } + ) + } else { + // no leaderboard attributes, delete them! + await memberLeaderboardsCollection.deleteOne({ uuid: member.uuid, profile: profile.uuid - }, - { - '$set': { - stats: leaderboardAttributes, - last_updated: new Date() - } - }, - { upsert: true } - ) + }) + } for (const [attributeName, attributeValue] of Object.entries(leaderboardAttributes)) { const existingRawLeaderboard = await fetchMemberLeaderboardRaw(attributeName) @@ -727,19 +735,28 @@ export async function updateDatabaseProfile(profile: CleanFullProfile): Promise< if (debug) console.debug('done getApplicableProfileLeaderboardAttributes..', leaderboardAttributes, profile.name) - await profileLeaderboardsCollection.updateOne( - { + if (leaderboardAttributes.length > 0) { + await profileLeaderboardsCollection.updateOne( + { + uuid: profile.uuid + }, + { + '$set': { + players: profile.members.map(p => p.uuid), + stats: leaderboardAttributes, + last_updated: new Date() + } + }, + { upsert: true } + ) + + } else { + // no leaderboard attributes, delete them! + await profileLeaderboardsCollection.deleteOne({ uuid: profile.uuid - }, - { - '$set': { - players: profile.members.map(p => p.uuid), - stats: leaderboardAttributes, - last_updated: new Date() - } - }, - { upsert: true } - ) + }) + } + // add the profile to the cached leaderboard without having to refetch it for (const [attributeName, attributeValue] of Object.entries(leaderboardAttributes)) { |