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 => { 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 | null; const current = existingRules ?? {}; const data: Record = {}; 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); }