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.
129 lines
4.8 KiB
TypeScript
129 lines
4.8 KiB
TypeScript
import type { Payload, PayloadRequest } from "payload";
|
|
import type { ReinforcementPool } from "@/payload-types";
|
|
import { emitGameEvent } from "@/utils/event-log/emit";
|
|
import { EventTypes } from "@/utils/event-log/eventTypes";
|
|
|
|
/**
|
|
* Reinforcement pools <-> Arma game server sync (bridge).
|
|
*
|
|
* The game queues a `reinforcement.spawn` event for every curator placement
|
|
* that matches a pool's Arma factions and a `reinforcement.refund` event when
|
|
* a curator deletes such a unit (killed units stay spent). The events route
|
|
* dedupes by `serverId:eventId`, so a retried delivery never double-spends.
|
|
* Balances are soft limits: they may go negative and nothing ever blocks.
|
|
*
|
|
* Pool balances live on `reinforcement-pools.available`. The game pulls them
|
|
* through GET /api/arma/v1/reinforcements, so this processor must have applied
|
|
* an event before the next pull returns (the ArmaSyncEvents hook awaits it).
|
|
*/
|
|
|
|
/**
|
|
* Marker header the processor passes through the local API `req` option so the
|
|
* ReinforcementPools afterChange audit hook can tell bridge writes apart from
|
|
* manual GM edits (manual edits emit `reinforcement:adjust`, bridge writes are
|
|
* audited here as spawn/refund events instead).
|
|
*/
|
|
export const REINFORCEMENT_SYSTEM_HEADER = "x-reinforcement-system";
|
|
|
|
interface ReinforcementDelta {
|
|
poolId: number;
|
|
count: number;
|
|
unitClass: string;
|
|
}
|
|
|
|
/** Payload shape from ptf_reinforcements: [poolId, count, unitClass]. */
|
|
export function parseReinforcementPayload(raw: unknown): ReinforcementDelta | null {
|
|
if (!Array.isArray(raw) || raw.length < 2) return null;
|
|
const poolId =
|
|
typeof raw[0] === "number" ? Math.trunc(raw[0]) : Number.parseInt(String(raw[0]), 10);
|
|
if (!Number.isInteger(poolId) || poolId <= 0) return null;
|
|
const count = typeof raw[1] === "number" ? Math.trunc(raw[1]) : 0;
|
|
if (!Number.isInteger(count) || count <= 0) return null;
|
|
const unitClass = typeof raw[2] === "string" ? raw[2].slice(0, 128) : "";
|
|
return { poolId, count, unitClass };
|
|
}
|
|
|
|
/**
|
|
* Applies one `reinforcement.spawn` / `reinforcement.refund` event to the
|
|
* pool balance (spawn subtracts, refund adds, no floor at zero). Never
|
|
* throws: a malformed payload or unknown pool is logged and skipped, and the
|
|
* game's next pull corrects its displayed value.
|
|
*/
|
|
export async function processReinforcementEvent(
|
|
payload: Payload,
|
|
raw: { eventId: string; type: string; payload: unknown },
|
|
req?: PayloadRequest,
|
|
): Promise<void> {
|
|
try {
|
|
const isSpawn = raw.type === "reinforcement.spawn";
|
|
const isRefund = raw.type === "reinforcement.refund";
|
|
if (!isSpawn && !isRefund) return;
|
|
|
|
const delta = parseReinforcementPayload(raw.payload);
|
|
if (!delta) {
|
|
payload.logger.warn(
|
|
`[Reinforcements] ${raw.type} event ${raw.eventId} has a malformed payload; skipping`,
|
|
);
|
|
return;
|
|
}
|
|
|
|
let pool: ReinforcementPool | null = null;
|
|
try {
|
|
pool = await payload.findByID({
|
|
collection: "reinforcement-pools",
|
|
id: delta.poolId,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
} catch {
|
|
pool = null;
|
|
}
|
|
if (!pool) {
|
|
payload.logger.warn(
|
|
`[Reinforcements] ${raw.type} event ${raw.eventId} references unknown pool ${delta.poolId}; skipping`,
|
|
);
|
|
return;
|
|
}
|
|
|
|
const before = pool.available ?? 0;
|
|
// Soft limits: overdraft is allowed, so no floor at zero.
|
|
const after = isSpawn ? before - delta.count : before + delta.count;
|
|
|
|
const headers = new Headers(req?.headers);
|
|
headers.set(REINFORCEMENT_SYSTEM_HEADER, "1");
|
|
await payload.update({
|
|
collection: "reinforcement-pools",
|
|
id: pool.id,
|
|
data: { available: after },
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
// Pass the marker through the local API req so the collection's
|
|
// afterChange hook skips its manual-edit audit for this write.
|
|
req: { headers } as Partial<PayloadRequest>,
|
|
});
|
|
|
|
await emitGameEvent(payload, {
|
|
type: isSpawn ? EventTypes.ReinforcementSpawn : EventTypes.ReinforcementRefund,
|
|
message: isSpawn
|
|
? `Zeus spawn spent ${delta.count} from reinforcement pool "${pool.name}"${
|
|
delta.unitClass ? ` (${delta.unitClass})` : ""
|
|
}: ${before} -> ${after}`
|
|
: `Curator deletion refunded ${delta.count} to reinforcement pool "${pool.name}"${
|
|
delta.unitClass ? ` (${delta.unitClass})` : ""
|
|
}: ${before} -> ${after}`,
|
|
targetCollection: "reinforcement-pools",
|
|
targetId: pool.id,
|
|
data: {
|
|
eventId: raw.eventId,
|
|
poolId: pool.id,
|
|
name: pool.name,
|
|
count: delta.count,
|
|
unitClass: delta.unitClass,
|
|
availableBefore: before,
|
|
availableAfter: after,
|
|
},
|
|
});
|
|
} catch (err) {
|
|
payload.logger.error(`[Reinforcements] Failed to apply event ${raw.eventId}: ${err}`);
|
|
}
|
|
}
|