diff options
author | mat <github@matdoes.dev> | 2022-03-31 21:18:30 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-03-31 21:18:30 -0500 |
commit | e92723ae05efcf41d6ced326c06fe53d9bcb4c1f (patch) | |
tree | cce91f2b4550e658c5aab3a193bd06d3a035b1cd /src/database.ts | |
parent | f87e2c16275fd0940d0ab12c97cac6784cbd9d40 (diff) | |
download | skyblock-api-e92723ae05efcf41d6ced326c06fe53d9bcb4c1f.tar.gz skyblock-api-e92723ae05efcf41d6ced326c06fe53d9bcb4c1f.tar.bz2 skyblock-api-e92723ae05efcf41d6ced326c06fe53d9bcb4c1f.zip |
don't add empty profiles to leaderboards db
Diffstat (limited to 'src/database.ts')
-rw-r--r-- | src/database.ts | 63 |
1 files changed, 40 insertions, 23 deletions
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)) { |