Consume raw arma-sync-events into a validated, idempotent operation ledger: contract v1, reject/dead-letter with zero effects, effect keys with unique-constraint race handling, and a staged-activation flag hook. Registers the operations permission domains. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
87 lines
No EOL
2.6 KiB
TypeScript
87 lines
No EOL
2.6 KiB
TypeScript
import {
|
|
OPERATION_EVENT_VERSION,
|
|
SUPPORTED_OPERATION_EVENT_TYPES,
|
|
type OperationEventError,
|
|
type OperationEventStatus,
|
|
type OperationEventType,
|
|
type RawSyncEvent,
|
|
} from "./types";
|
|
|
|
export interface ValidationResult {
|
|
ok: boolean;
|
|
status: OperationEventStatus;
|
|
error?: OperationEventError;
|
|
operationId?: string;
|
|
version?: number;
|
|
}
|
|
|
|
/**
|
|
* Validate a raw sync event against the operation event contract.
|
|
*
|
|
* - Unknown type -> dead-letter (code `unsupported-type`), so the original
|
|
* type string is preserved on the record.
|
|
* - Supported type with a malformed payload -> rejected (code `invalid-payload`).
|
|
* - Supported type with a version above the current contract -> rejected
|
|
* (code `unsupported-version`); the record is retained but derives no effects.
|
|
* - Otherwise -> validated.
|
|
*/
|
|
export function validateOperationEvent(raw: RawSyncEvent): ValidationResult {
|
|
if (!SUPPORTED_OPERATION_EVENT_TYPES.includes(raw.type as OperationEventType)) {
|
|
return {
|
|
ok: false,
|
|
status: "dead-letter",
|
|
error: { code: "unsupported-type", message: `Unsupported event type "${raw.type}".` },
|
|
};
|
|
}
|
|
|
|
if (raw.type === "player.kill") {
|
|
// Kill tuple: [victimName, victimSteamId, killerName, killerSteamId, ...].
|
|
if (!Array.isArray(raw.payload) || raw.payload.length < 6 || !raw.payload[3]) {
|
|
return {
|
|
ok: false,
|
|
status: "rejected",
|
|
error: {
|
|
code: "invalid-payload",
|
|
message: "player.kill payload must be a kill tuple with a killer Steam ID.",
|
|
},
|
|
};
|
|
}
|
|
return { ok: true, status: "validated", operationId: "unknown", version: 1 };
|
|
}
|
|
|
|
const payload = raw.payload;
|
|
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
|
|
return {
|
|
ok: false,
|
|
status: "rejected",
|
|
error: {
|
|
code: "invalid-payload",
|
|
message: `${raw.type} payload must be a JSON object.`,
|
|
},
|
|
};
|
|
}
|
|
|
|
const p = payload as Record<string, unknown>;
|
|
const operationId =
|
|
typeof p.operationId === "string" && p.operationId
|
|
? p.operationId
|
|
: typeof p.operation === "string" && p.operation
|
|
? p.operation
|
|
: "unknown";
|
|
const version = typeof p.version === "number" ? p.version : 1;
|
|
|
|
if (version > OPERATION_EVENT_VERSION) {
|
|
return {
|
|
ok: false,
|
|
status: "rejected",
|
|
error: {
|
|
code: "unsupported-version",
|
|
message: `Event version ${version} exceeds supported version ${OPERATION_EVENT_VERSION}.`,
|
|
},
|
|
operationId,
|
|
version,
|
|
};
|
|
}
|
|
|
|
return { ok: true, status: "validated", operationId, version };
|
|
} |