Skip to content

Commit

Permalink
fix(leaderboards): remove wiped players from leaderboards (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobk999 authored May 10, 2024
1 parent 3229d52 commit 9d37211
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
25 changes: 13 additions & 12 deletions apps/api/src/leaderboards/leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,22 @@ export abstract class LeaderboardService {

const id = instance[idField] as unknown as string;

fields
.filter(([field]) => remove || typeof instance[field] === "number")
.forEach(([field, metadata]) => {
const value = instance[field] as unknown as number;
const key = `${name}.${String(field)}`;
for (const [field, metadata] of fields) {
const value = instance[field] as unknown as number;
const key = `${name}.${String(field)}`;

if (remove || value === 0 || Number.isNaN(value)) return pipeline.zrem(key, id);
if (remove || value === 0 || Number.isNaN(value) || typeof value !== "number") {
pipeline.zrem(key, id);
continue;
}

pipeline.zadd(key, value, id);
pipeline.zadd(key, value, id);

if (metadata.leaderboard.enabled && metadata.leaderboard.resetEvery) {
const time = this.getLeaderboardExpiryTime(metadata.leaderboard);
pipeline.expireat(key, time);
}
});
if (metadata.leaderboard.enabled && metadata.leaderboard.resetEvery) {
const time = this.getLeaderboardExpiryTime(metadata.leaderboard);
pipeline.expireat(key, time);
}
}

await pipeline.exec();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Fuse } from "fuse.js";
import { Guild, LeaderboardScanner } from "@statsify/schemas";
import { removeFormatting } from "@statsify/util";

const list = LeaderboardScanner.getLeaderboardMetadata(Guild).map(
const list = LeaderboardScanner.getLeaderboardFields(Guild).map(
([key, { leaderboard }]) => ({
value: key,
name: removeFormatting(leaderboard.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const FUSE_OPTIONS = {
};

const fields = entries.reduce((acc, [prefix, value]) => {
const list = LeaderboardScanner.getLeaderboardMetadata(value.type.type).map(
const list = LeaderboardScanner.getLeaderboardFields(value.type.type).map(
([key, { leaderboard }]) => ({ value: key, name: removeFormatting(leaderboard.name) })
);

Expand Down
6 changes: 1 addition & 5 deletions packages/schemas/src/util/leaderboard-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ import { parseAdditionalFields } from "./parse-fields.js";
import type { Constructor } from "@statsify/util";

export class LeaderboardScanner {
public static getLeaderboardMetadata<T>(constructor: Constructor<T>) {
public static getLeaderboardFields<T>(constructor: Constructor<T>) {
const metadata = MetadataScanner.scan(constructor);

const fields = metadata.filter(([, { leaderboard }]) => leaderboard.enabled);

return fields;
}

public static getLeaderboardFields<T>(constructor: Constructor<T>) {
return this.getLeaderboardMetadata(constructor);
}

public static getLeaderboardField<T>(
constructor: Constructor<T>,
key: string,
Expand Down

0 comments on commit 9d37211

Please sign in to comment.