1
0
Fork 0
polaris-task-force/src/lib/minigames/loadRegistry.ts
Z8MB1E deb88ded56 feat(minigames): drive the nav and period scoring from the registry
Phase 4 of the minigame periods feature:

- Server registry loader (lib/minigames/loadRegistry.ts) reads the
  minigames collection and falls back to the built-in seven when it is empty
  or unreadable, so the nav and boards never render blank.
- The sidebar minigames dropdown now renders from the registry (Core and
  Cardtable groups, retired games hidden, registry order), with lucide icons
  resolved from the icon-name string through a client icon map and a
  safe fallback.
- Period scoring is registry-driven: the close pipeline uses core games with
  countsForPeriods instead of a hardcoded list. A retired game simply stops
  accruing entries and drops out of the next period while its history stays.
- Integration tests for the fallback, ordering, retirement filtering and the
  non-scoring-game exclusion; the older minigame specs now wipe the registry
  too so files stay order-independent.
2026-10-11 17:06:52 -04:00

60 lines
2 KiB
TypeScript

import type { Payload } from "payload";
import {
BUILTIN_MINIGAMES,
type BuiltinMinigame,
type MinigameCategory,
type MinigameStatus,
} from "./registry";
/**
* Server-side registry loader.
*
* The `minigames` collection is the source of truth (admins can add, retire or
* reorder games), but this falls back to `BUILTIN_MINIGAMES` when the table is
* empty or the read fails, so the nav and boards never render blank (a fresh
* test database, for instance, has the schema without the seeded rows).
*
* Not client-safe: imports the Payload type. Client components take the
* resolved list as a prop and use `RegistryGame` from the pure `registry.ts`.
*/
export async function loadMinigameRegistry(payload: Payload): Promise<BuiltinMinigame[]> {
try {
const res = await payload.find({
collection: "minigames",
limit: 200,
depth: 0,
sort: "order",
overrideAccess: true,
});
const rows = res.docs.map(
(doc): BuiltinMinigame => ({
key: doc.key,
label: doc.label,
description: doc.description ?? "",
route: doc.route,
icon: doc.icon || "Gamepad2",
category: (doc.category ?? "core") as MinigameCategory,
status: (doc.status ?? "active") as MinigameStatus,
order: typeof doc.order === "number" ? doc.order : 0,
countsForPeriods: Boolean(doc.countsForPeriods),
scoreLabel: doc.scoreLabel || "Score",
}),
);
if (rows.length > 0) return rows;
} catch {
// Fall through to the built-in list below.
}
return [...BUILTIN_MINIGAMES];
}
/** Games visible in the nav: everything not retired, in registry order. */
export function visibleRegistryGames(games: BuiltinMinigame[]): BuiltinMinigame[] {
return games.filter((game) => game.status !== "retired");
}
/** Registry keys whose ledger entries feed anchored period boards. */
export function periodScoringGameKeys(games: BuiltinMinigame[]): string[] {
return games
.filter((game) => game.category === "core" && game.countsForPeriods)
.map((game) => game.key);
}