New Game collection reinforcement-pools (name, faction, armaFactions, available, enabled) with manual-edit audit via reinforcement:adjust. The events hook applies reinforcement.spawn/.refund deltas (soft limits, no floor, idempotent by eventId) before the game's next pull, and a new GET /api/arma/v1/reinforcements endpoint serves enabled pools to the server.
70 lines
3.4 KiB
TypeScript
70 lines
3.4 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 { isOperationLedgerEnabled } from "@/lib/operations/rollout";
|
|
import { processOperationEvent } from "@/lib/operations/process";
|
|
|
|
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;
|
|
// 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, doc, req);
|
|
}
|
|
if (doc.type === "player.kill") {
|
|
processPlayerKill(req.payload, doc).catch((err) => {
|
|
req.payload.logger.error(`[KillTracker] Failed to process kill event ${doc.eventId}: ${err}`);
|
|
});
|
|
}
|
|
if (doc.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, doc, req);
|
|
} catch (err) {
|
|
req.payload.logger.error(`[CrateSync] Failed to process box event ${doc.eventId}: ${err}`);
|
|
}
|
|
}
|
|
if (doc.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, doc);
|
|
}
|
|
if (doc.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, doc, req);
|
|
}
|
|
if (doc.type.startsWith("locker.")) {
|
|
// Awaited (same posture as box.*): locker writes join the raw-event
|
|
// transaction; processLockerEvent never throws.
|
|
try {
|
|
await processLockerEvent(req.payload, doc, req);
|
|
} catch (err) {
|
|
req.payload.logger.error(`[LockerSync] Failed to process locker event ${doc.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 },
|
|
],
|
|
};
|