1
0
Fork 0
polaris-task-force/src/scripts/gameTick.ts
Z8MB1E 707c5f6c81 feat(event-log): hide routine tick events behind a toggle
Add a client-safe ROUTINE_EVENT_TYPES list (game:tick, economy:tick, market:auto-generate) and hide those types on /events behind an off-by-default Routine ticks switch. The dashboard always excludes them so real events fill the recent list.

Tick bins now skip no-op emissions: game tick requires shipment, construction, or production activity; market tick requires an expiry or generation; economy tick requires processed items or errors. Historical events stay in the database and remain visible via the toggle or the admin panel.
2026-10-19 20:03:51 -04:00

98 lines
3.3 KiB
TypeScript

import type { SanitizedConfig } from "payload";
import { getPayload } from "payload";
import type { GameStructure, GameVehicle, Shipment, Vehicle } from "@/payload-types";
import { advanceShipment } from "@/lib/shipping/lifecycle";
import { processConstructionTick } from "@/lib/map/construction";
import { processProductionTick } from "@/lib/base/production";
import { createBinLogger, errorMessage } from "./lib/binFileLogger";
import { emitGameEvent } from "@/utils/event-log/emit";
import { EventTypes } from "@/utils/event-log/eventTypes";
export const script = async (config: SanitizedConfig) => {
const payload = await getPayload({ config });
const log = createBinLogger("game-tick", payload.logger);
try {
log.info("[Game Tick] Starting game tick...");
const activeShipments = await payload.find({
collection: "shipments",
where: {
status: { in: ["dispatched", "in_transit"] },
},
limit: 100,
depth: 2,
});
let processedCount = 0;
for (const shipment of activeShipments.docs) {
try {
await advanceShipment(
payload,
shipment as unknown as Shipment & {
transportVehicle: GameVehicle & { type: Vehicle };
origin: GameStructure;
destination: GameStructure;
},
new Date(),
log,
);
processedCount++;
} catch (error) {
log.error(`[Game Tick] Error processing shipment ${shipment.id}: ${errorMessage(error)}`);
}
}
const construction = await processConstructionTick(payload, log);
const production = await processProductionTick(payload, log);
// A sweep that touched nothing is pure noise in the event log. Shipments
// are included because advancing an active shipment is itself activity.
const hasActivity =
activeShipments.docs.length > 0 ||
construction.completed > 0 ||
production.processed > 0 ||
production.produced > 0 ||
production.stalled > 0;
if (hasActivity) {
await emitGameEvent(payload, {
type: EventTypes.GameTick,
message: `Game tick processed ${processedCount} of ${activeShipments.docs.length} active shipments`,
data: {
processedCount,
totalActive: activeShipments.docs.length,
construction,
production,
},
});
}
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: new Date().toISOString() }),
}).catch((error: unknown) => {
log.error(`[Game Tick] Failed to notify app clients: ${errorMessage(error)}`);
});
} else {
log.warn("[Game Tick] GAME_TICK_NOTIFY_SECRET not set; skipping client notification");
}
log.info(
`[Game Tick] Complete. Processed ${processedCount} shipments, ${production.produced} production runs.`,
);
process.exit(0);
} catch (error) {
log.error(
`[Game Tick] Fatal: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`,
);
process.exit(1);
}
};