Registers the base-tick bin: pays staff salaries from the treasury, charges structure maintenance, tracks upkeep shortages from storage, and notifies clients via the game-tick SSE path. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
import type { SanitizedConfig } from "payload";
|
|
import { getPayload } from "payload";
|
|
import { processBaseTick } from "@/lib/base";
|
|
import { createBinLogger, errorMessage } from "./lib/binFileLogger";
|
|
|
|
export const script = async (config: SanitizedConfig) => {
|
|
const payload = await getPayload({ config });
|
|
const log = createBinLogger("base-tick", payload.logger);
|
|
|
|
try {
|
|
log.info("[Base Tick] Starting base tick...");
|
|
const processedAt = new Date().toISOString();
|
|
const result = await processBaseTick(payload);
|
|
log.info(
|
|
`[Base Tick] Complete. Salaries paid ${result.salariesPaid}, unpaid ${result.salariesUnpaid}; maintenance paid ${result.maintenancePaid}, unpaid ${result.maintenanceUnpaid}; upkeep shortages ${result.upkeepShortages}.`,
|
|
);
|
|
|
|
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, source: "base-tick" }),
|
|
}).catch((error: unknown) => {
|
|
log.error(`[Base Tick] Failed to notify app clients: ${errorMessage(error)}`);
|
|
});
|
|
}
|
|
|
|
process.exit(0);
|
|
} catch (error) {
|
|
log.error(
|
|
`[Base Tick] Fatal: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
};
|