86 lines
4.2 KiB
TypeScript
86 lines
4.2 KiB
TypeScript
import { CollectionConfig } from "payload";
|
|
import { processPlayerKill } from "@/lib/arma-bridge/processSyncEvents";
|
|
import { processBoxEvent } from "@/lib/arma-bridge/crates";
|
|
import { processMoneyEvent } from "@/lib/arma-bridge/money";
|
|
import { processReinforcementEvent } from "@/lib/arma-bridge/reinforcements";
|
|
import { processLockerEvent } from "@/lib/arma-bridge/lockers";
|
|
import { processGarageEvent } from "@/lib/arma-bridge/garages";
|
|
import { isOperationLedgerEnabled } from "@/lib/operations/rollout";
|
|
import { processOperationEvent } from "@/lib/operations/process";
|
|
import { toRawSyncEvent } from "@/lib/operations/reconcile";
|
|
|
|
export const ArmaSyncEvents: CollectionConfig = {
|
|
slug: "arma-sync-events",
|
|
admin: { group: "Server", useAsTitle: "eventId", defaultColumns: ["eventId", "type", "server", "occurredAt"] },
|
|
access: { read: ({ req }) => !!req.user, create: () => false, update: () => false, delete: () => false },
|
|
hooks: {
|
|
afterChange: [
|
|
async ({ doc, req, operation }) => {
|
|
if (operation !== "create") return;
|
|
// Payload hands afterChange a doc whose relationship fields are
|
|
// populated at defaultDepth (an object, not the numeric FK). Every
|
|
// downstream processor expects `server` to be a number, so normalize
|
|
// once here; without this they all bail as "malformed".
|
|
const raw = toRawSyncEvent(doc as unknown as Record<string, unknown>);
|
|
// Ledger first: every raw event is consumed into the operation ledger
|
|
// before any downstream projection runs. Evidence is always stored;
|
|
// only derived effects are gated by the kill switch.
|
|
if (isOperationLedgerEnabled()) {
|
|
await processOperationEvent(req.payload, raw, req);
|
|
}
|
|
if (raw.type === "player.kill") {
|
|
processPlayerKill(req.payload, raw).catch((err) => {
|
|
req.payload.logger.error(`[KillTracker] Failed to process kill event ${raw.eventId}: ${err}`);
|
|
});
|
|
}
|
|
if (raw.type.startsWith("box.")) {
|
|
// Awaited (not fire-and-forget) so the crate writes join the raw-event
|
|
// transaction; processBoxEvent never throws, so ingestion cannot be
|
|
// rolled back by crate sync.
|
|
try {
|
|
await processBoxEvent(req.payload, raw, req);
|
|
} catch (err) {
|
|
req.payload.logger.error(`[CrateSync] Failed to process box event ${raw.eventId}: ${err}`);
|
|
}
|
|
}
|
|
if (raw.type === "money.delta") {
|
|
// Awaited so that by the time the game's flush returns, the bank
|
|
// balance already reflects it (the game pulls balances right after
|
|
// flushing). processMoneyEvent never throws.
|
|
await processMoneyEvent(req.payload, raw);
|
|
}
|
|
if (raw.type.startsWith("reinforcement.")) {
|
|
// Awaited (same posture as money.delta): the pool balance must be
|
|
// current before the game's next reinforcement pull, and
|
|
// processReinforcementEvent never throws.
|
|
await processReinforcementEvent(req.payload, raw, req);
|
|
}
|
|
if (raw.type.startsWith("locker.")) {
|
|
// Awaited (same posture as box.*): locker writes join the raw-event
|
|
// transaction; processLockerEvent never throws.
|
|
try {
|
|
await processLockerEvent(req.payload, raw, req);
|
|
} catch (err) {
|
|
req.payload.logger.error(`[LockerSync] Failed to process locker event ${raw.eventId}: ${err}`);
|
|
}
|
|
}
|
|
if (raw.type.startsWith("garage.")) {
|
|
// Awaited (same posture as locker.*): garage writes join the
|
|
// raw-event transaction; processGarageEvent never throws.
|
|
try {
|
|
await processGarageEvent(req.payload, raw, req);
|
|
} catch (err) {
|
|
req.payload.logger.error(`[GarageSync] Failed to process garage event ${raw.eventId}: ${err}`);
|
|
}
|
|
}
|
|
},
|
|
],
|
|
},
|
|
fields: [
|
|
{ name: "eventId", type: "text", required: true, unique: true, index: true },
|
|
{ name: "server", type: "relationship", relationTo: "game-servers", required: true, index: true },
|
|
{ name: "type", type: "text", required: true, index: true },
|
|
{ name: "occurredAt", type: "date", required: true },
|
|
{ name: "payload", type: "json", required: true },
|
|
],
|
|
};
|