Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions apps/api/src/leaderboards/leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const DAYS_IN_WEEK = {

export type LeaderboardAdditionalStats = Record<string, any> & { name: string };

const isScore = (value: unknown): value is number =>
typeof value === "number" && value !== 0 && !Number.isNaN(value);

@Injectable()
export abstract class LeaderboardService {
public constructor(@InjectRedis() protected readonly redis: Redis) {}
Expand All @@ -35,7 +38,8 @@ export abstract class LeaderboardService {
constructor: Constructor<T>,
instance: Flatten<T>,
idField: keyof T,
remove = false
remove = false,
previous?: Flatten<T> | null
) {
const fields = getLeaderboardFields(constructor);
const transaction = Sentry.getCurrentHub().getScope()?.getTransaction();
Expand All @@ -51,10 +55,12 @@ export abstract class LeaderboardService {
const id = instance[idField] as unknown as string;

for (const [field, metadata] of fields) {
const value = instance[field] as unknown as number;
const value = instance[field];
const key = `${name}.${String(field)}`;

if (remove || value === 0 || Number.isNaN(value) || typeof value !== "number") {
if (remove || !isScore(value)) {
if (previous && !isScore(previous[field] ?? metadata.store.default)) continue;

pipeline.zrem(key, id);
continue;
}
Expand Down
12 changes: 9 additions & 3 deletions apps/api/src/player/player.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export class PlayerService {
if (mongoPlayer && mongoPlayer.username !== player.username)
await this.playerSearchService.delete(mongoPlayer.username);

const flatPlayer = await this.saveOne(player, true);
// A selector only loads part of the stored document
const flatPlayer = await this.saveOne(player, true, selector ? undefined : mongoPlayer);

flatPlayer.isNew = !mongoPlayer;

Expand Down Expand Up @@ -170,7 +171,11 @@ export class PlayerService {
return true;
}

public async saveOne(player: Player, registerAutocomplete: boolean) {
public async saveOne(
player: Player,
registerAutocomplete: boolean,
previous?: Flatten<Player> | null
) {
// Serialize and flatten the player
const flatPlayer = flatten(player);
const serializedPlayer = serialize<Player>(Player, flatPlayer);
Expand All @@ -183,7 +188,8 @@ export class PlayerService {
Player,
flatPlayer,
"uuid",
false
false,
previous
),
];

Expand Down
Loading