1
0
Fork 0
polaris-task-force/src/collections/server/ArmaSyncEvents.ts
Z8MB1E 73cae3defd feat(arma): sync in-game shop money events to bank accounts
Route the game's `money.delta` events into the web banking system so a
player's personal bank account tracks their in-game wallet.

- New `processMoneyEvent` (src/lib/arma-bridge/money.ts) applies a parsed
  money delta to the player's personal account: debits go to the unit
  treasury (or leave if no treasury), refunds hit the treasury first and
  fall back to a plain deposit, and rewards/grants deposit directly.
- Wire the handler into ArmaSyncEvents and add a `POST /api/arma/v1/balances`
  route (get_balances) that returns personal balances for Steam IDs, omitting
  unlinked IDs so the game keeps its own stored value.
- Events dedupe by serverId+eventId so a retried delivery never
  double-charges, and the whole path never throws.
2026-10-01 10:04:32 -04:00

53 lines
2.5 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 { 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);
}
},
],
},
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 },
],
};