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 { 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); }