1
0
Fork 0
polaris-task-force/src/lib/minigames/periods/ledger.ts
Z8MB1E 316797d27d feat(minigames): add period leaderboards, all-time view and achievements
Phase 5 of the minigame periods feature:

- Read model (lib/minigames/periods/standings.ts) that shapes period boards
  from the ledger (rank, placement points, names) and All-Time boards from the
  untouched profile aggregates into one row type.
- GET /api/minigames/leaderboards: ?period=current|<id>|all-time, returning
  the period, the period list, the boards and the achievements honor roll.
- New /minigames/leaderboards page: period selector (current, every past
  period, All-Time), per-game tabs, and the achievements honor roll. Core-only
  in period mode; every registered game in All-Time.
- Shared client pieces: useLeaderboards hook, StandingsTable,
  PeriodStandingsCard (period by default, All-Time toggle) and
  AchievementsHonorRoll.
- Each Core game page gains an "Anchored period standings" card so the period
  board is visible on the game itself; its existing all-time board and
  mode/difficulty selectors are untouched, and it links to the full page.
  (Plan said replace the inline board; keeping it preserves the per-mode
  selectors, so the period board is added instead. Easy to swap if you prefer.)
- Ledger fetch helper extracted to lib/minigames/periods/ledger.ts.
- Integration test for the period boards, the All-Time path and the honor roll.
2026-10-11 17:06:52 -04:00

48 lines
1.5 KiB
TypeScript

import type { Payload, Where } from "payload";
import type { LedgerEntry } from "./boards";
/**
* Read helpers for the `minigame-entries` ledger. Shared by the close pipeline
* and the leaderboard endpoint so both interpret rows identically.
*/
interface EntryDoc {
user: number | { id: number };
game: string;
score: number | null;
durationMs: number | null;
accuracy: number | null;
createdAt: string;
}
const userIdOf = (user: EntryDoc["user"]): number => (typeof user === "object" ? user.id : user);
export const toLedgerEntry = (doc: EntryDoc): LedgerEntry => ({
userId: userIdOf(doc.user),
game: doc.game,
score: typeof doc.score === "number" ? doc.score : 0,
durationMs: typeof doc.durationMs === "number" ? doc.durationMs : 0,
accuracy: typeof doc.accuracy === "number" ? doc.accuracy : null,
createdAtMs: new Date(doc.createdAt).getTime() || 0,
});
/** Fetch every ledger entry matching `where`, following pagination. */
export async function fetchLedgerEntries(payload: Payload, where: Where): Promise<LedgerEntry[]> {
const entries: LedgerEntry[] = [];
let page = 1;
for (;;) {
const res = await payload.find({
collection: "minigame-entries",
where,
sort: "createdAt",
limit: 500,
page,
depth: 0,
overrideAccess: true,
});
for (const doc of res.docs) entries.push(toLedgerEntry(doc as unknown as EntryDoc));
if (!res.hasNextPage || page >= 500) break;
page += 1;
}
return entries;
}