1
0
Fork 0
polaris-task-force/src/tools/seed/seedMinigames.ts
Z8MB1E d4d121b5a7 feat(minigames): add period, entry, achievement and registry schema
Phase 0 of the minigame periods feature:

- New collections: minigames (registry), minigame-entries (append-only
  ledger), minigame-periods, achievements; new minigame-rules global
  (anchor, 14-day periods, top-10 ladder, precision minimum).
- Pure registry lib with the seven built-in games, achievement
  type/window enums and rule defaults.
- Permissions: extend the Minigames group with registry/entries/
  periods/achievements CRUD (plus minigame-periods:close) and add a
  Minigame Rules (Global) group; mirror both into ALL_PERMISSION_VALUES.
- Migration add_minigame_periods seeds the seven registry rows and the
  minigame-rules defaults idempotently so production seeds from the
  migration chain; dev seed script mirrors it for local resets.
2026-10-11 17:06:52 -04:00

77 lines
2.5 KiB
TypeScript

import { getPayload } from "payload";
import config from "@payload-config";
import { BUILTIN_MINIGAMES, MINIGAME_RULE_DEFAULTS } from "@/lib/minigames/registry";
/**
* Idempotent seed for the minigame registry and rules global.
*
* Production gets the same defaults from the `add_minigame_periods` migration;
* this script exists for local resets and test/dev databases. Safe to re-run:
* existing registry rows are left untouched and only unset global fields are
* filled.
*/
export const seedMinigames = async (): Promise<void> => {
const payload = await getPayload({ config });
payload.logger.info("Seeding minigame registry...");
for (const game of BUILTIN_MINIGAMES) {
const existing = await payload.find({
collection: "minigames",
where: { key: { equals: game.key } },
limit: 1,
depth: 0,
overrideAccess: true,
});
if (existing.docs.length > 0) {
payload.logger.info(` Minigame "${game.key}" already exists, skipping.`);
continue;
}
const created = await payload.create({
collection: "minigames",
data: game,
overrideAccess: true,
});
payload.logger.info(` Created minigame "${game.key}" (id=${created.id}).`);
}
payload.logger.info("Seeding minigame-rules defaults...");
const existingRules = (await payload.findGlobal({
slug: "minigame-rules",
depth: 0,
overrideAccess: true,
})) as unknown as Record<string, unknown> | null;
const current = existingRules ?? {};
const data: Record<string, unknown> = {};
if (current.enabled == null) data.enabled = MINIGAME_RULE_DEFAULTS.enabled;
if (current.periodLengthDays == null) data.periodLengthDays = MINIGAME_RULE_DEFAULTS.periodLengthDays;
if (!current.anchorDate) data.anchorDate = MINIGAME_RULE_DEFAULTS.anchorDate;
if (current.placementLadderTopN == null) {
data.placementLadderTopN = MINIGAME_RULE_DEFAULTS.placementLadderTopN;
}
if (current.precisionMinAttempts == null) {
data.precisionMinAttempts = MINIGAME_RULE_DEFAULTS.precisionMinAttempts;
}
if (Object.keys(data).length > 0) {
await payload.updateGlobal({
slug: "minigame-rules",
data,
overrideAccess: true,
});
payload.logger.info(` Seeded minigame-rules defaults: ${Object.keys(data).join(", ")}.`);
} else {
payload.logger.info(" minigame-rules already seeded.");
}
};
if (import.meta.main) {
await seedMinigames();
// CLI script: the Payload pool keeps the event loop alive after completion;
// exit explicitly (same convention as the game-tick bins).
process.exit(0);
}