Phase 3 of the minigame periods feature: - Record each ledger row's own instant (`occurredAt`, server clock at submit) so calendar windows score runs that actually happened in them. Backfilled from createdAt for existing rows. - Pure UTC calendar windows (lib/minigames/periods/calendar.ts): completed month and year windows, with the first window of each kind partial because it begins at the anchor, and nothing crowned until the window has ended. - Close pipeline extended: after the period sweep it crowns Player of the Month and Player of the Year for every completed window, scored with the same placement ladder as the period champion. Idempotent like the period close (achievement unique key plus an existence check). - period-tick logs the crowned windows; integration tests cover window enumeration, both crowns and idempotency.
51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
import type { SanitizedConfig } from "payload";
|
|
import { getPayload } from "payload";
|
|
import { closeDuePeriods } from "@/lib/minigames/periods/lifecycle";
|
|
import { createBinLogger, errorMessage } from "./lib/binFileLogger";
|
|
|
|
export const script = async (config: SanitizedConfig) => {
|
|
const payload = await getPayload({ config });
|
|
const log = createBinLogger("period-tick", payload.logger);
|
|
|
|
try {
|
|
log.info("[Period Tick] Starting period tick...");
|
|
|
|
const nowIso = new Date().toISOString();
|
|
const summary = await closeDuePeriods(payload);
|
|
|
|
for (const closed of summary.closed) {
|
|
log.info(
|
|
`[Period Tick] Closed "${closed.label}" (id ${closed.periodId}) with ${closed.achievements} achievement(s).`,
|
|
);
|
|
}
|
|
|
|
for (const window of summary.calendar) {
|
|
log.info(`[Period Tick] Crowned the ${window.unit} "${window.label}" champion.`);
|
|
}
|
|
|
|
const notifyUrl = (process.env.APP_URL ?? "http://localhost:3000").replace(/\/$/, "");
|
|
const notifySecret = process.env.GAME_TICK_NOTIFY_SECRET;
|
|
if (notifySecret) {
|
|
await fetch(`${notifyUrl}/api/game-tick/notify`, {
|
|
method: "POST",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"x-game-tick-secret": notifySecret,
|
|
},
|
|
body: JSON.stringify({ processedAt: nowIso, source: "period-tick" }),
|
|
}).catch((error: unknown) => {
|
|
log.error(`[Period Tick] Failed to notify app clients: ${errorMessage(error)}`);
|
|
});
|
|
} else {
|
|
log.warn("[Period Tick] GAME_TICK_NOTIFY_SECRET not set; skipping client notification");
|
|
}
|
|
|
|
log.info(`[Period Tick] Complete. Closed ${summary.closed.length} period(s).`);
|
|
process.exit(0);
|
|
} catch (error) {
|
|
log.error(
|
|
`[Period Tick] Fatal: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
};
|