import type { SettlementConsumed } from "./allocationTypes"; import { DEFAULT_RECOVERY_POLICY, EXTRACTION_KINDS, INVENTORY_SNAPSHOT_ITEM_LIMIT, LOSS_REASONS, OPERATION_SETTLEMENT_VERSION, } from "./settlementTypes"; import type { ExtractionKind, ExtractionPayloadV1, InventoryLossPayloadV1, LogisticsExtractionLine, LossReason, ObjectivePayloadV1, OperationEndPayloadV1, ParseFailure, ParseResult, PlayerExtractionLine, RecoveryPolicy, SettlementFailureCode, } from "./settlementTypes"; /** * Boundary parsers for the settlement contract: untrusted effect payloads * are parsed into typed contract values exactly once, here. Anything the * parsers reject becomes a failed effect; the handlers never re-validate. */ function asRecord(raw: unknown): Record | null { return typeof raw === "object" && raw !== null && !Array.isArray(raw) ? (raw as Record) : null; } function positiveInt(value: unknown): number | null { return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : null; } function fail(code: SettlementFailureCode, message: string): ParseFailure { return { ok: false, code, message }; } function parseItems(raw: unknown): ParseResult { if (raw === undefined || raw === null) return { ok: true, value: [] }; if (!Array.isArray(raw)) return fail("malformed-payload", "items must be an array."); if (raw.length > INVENTORY_SNAPSHOT_ITEM_LIMIT) { return fail( "malformed-payload", `items exceeds the snapshot limit of ${INVENTORY_SNAPSHOT_ITEM_LIMIT} lines.`, ); } return { ok: true, value: raw }; } function parseOptionalId(p: Record, key: string): number | undefined { const value = p[key]; return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined; } /** Parse a logistics line: `{ resourceId, amount }`, both positive integers. */ export function parseLogisticsLine(raw: unknown): ParseResult { const line = asRecord(raw); const resourceId = positiveInt(line?.resourceId); const amount = positiveInt(line?.amount); if (!line || resourceId === null || amount === null) { return fail( "malformed-asset", "A logistics line must carry a positive integer resourceId and amount.", ); } return { ok: true, value: { resourceId, amount } }; } /** Parse a player line: `{ assetId, quantity }`, both positive integers. */ export function parsePlayerLine(raw: unknown): ParseResult { const line = asRecord(raw); const assetId = positiveInt(line?.assetId); const quantity = positiveInt(line?.quantity); if (!line || assetId === null || quantity === null) { return fail( "malformed-asset", "A player extraction line must carry a positive integer assetId and quantity.", ); } return { ok: true, value: { assetId, quantity } }; } export function parseExtractionPayload(raw: unknown): ParseResult { const p = asRecord(raw); if (!p) return fail("malformed-payload", "Extraction effect payload must be an object."); const operationId = typeof p.operationId === "string" ? p.operationId : "unknown"; const version = typeof p.version === "number" ? p.version : OPERATION_SETTLEMENT_VERSION; const items = parseItems(p.items); if (!items.ok) return items; let kind: ExtractionKind | undefined; if (p.kind !== undefined && p.kind !== null) { if (!EXTRACTION_KINDS.includes(p.kind as ExtractionKind)) { return fail("malformed-payload", `Unknown extraction kind "${String(p.kind)}".`); } kind = p.kind as ExtractionKind; } if (kind === undefined && items.value.length > 0) { return fail("malformed-payload", "Extraction kind is required when items are present."); } const actorId = parseOptionalId(p, "actorId"); const hqStructureId = parseOptionalId(p, "hqStructureId"); if (kind === "player" && actorId === undefined) { return fail("malformed-payload", "A player extraction requires a positive integer actorId."); } if (kind === "logistics" && hqStructureId === undefined) { return fail( "malformed-payload", "A logistics extraction requires a positive integer hqStructureId.", ); } let consumed: SettlementConsumed | undefined; if (p.consumed !== undefined && p.consumed !== null) { const c = asRecord(p.consumed); if (!c) return fail("malformed-payload", "consumed must be an object."); consumed = c as SettlementConsumed; } let recoveryPolicy: RecoveryPolicy = DEFAULT_RECOVERY_POLICY; if (p.recoveryPolicy !== undefined && p.recoveryPolicy !== null) { const rp = asRecord(p.recoveryPolicy); if (!rp || typeof rp.type !== "string") { return fail("malformed-payload", "recoveryPolicy must be an object with a type."); } recoveryPolicy = rp as unknown as RecoveryPolicy; } const reservationKey = typeof p.reservationKey === "string" && p.reservationKey ? p.reservationKey : undefined; return { ok: true, value: { operationId, version, kind, actorId, hqStructureId, reservationKey, items: items.value, consumed, recoveryPolicy, }, }; } export function parseInventoryLossPayload(raw: unknown): ParseResult { const p = asRecord(raw); if (!p) return fail("malformed-payload", "Inventory effect payload must be an object."); const operationId = typeof p.operationId === "string" ? p.operationId : "unknown"; const version = typeof p.version === "number" ? p.version : OPERATION_SETTLEMENT_VERSION; const items = parseItems(p.items); if (!items.ok) return items; let reason: LossReason | undefined; if (p.reason !== undefined && p.reason !== null) { if (!LOSS_REASONS.includes(p.reason as LossReason)) { return fail("malformed-payload", `Unknown inventory reason "${String(p.reason)}".`); } reason = p.reason as LossReason; } return { ok: true, value: { operationId, version, actorId: parseOptionalId(p, "actorId"), reason, items: items.value, }, }; } export function parseObjectivePayload(raw: unknown): ParseResult { const p = asRecord(raw); if (!p) return fail("malformed-payload", "Objective effect payload must be an object."); return { ok: true, value: { operationId: typeof p.operationId === "string" ? p.operationId : "unknown", actorId: parseOptionalId(p, "actorId"), objective: p.objective ?? null, }, }; } export function parseOperationEndPayload(raw: unknown): ParseResult { const p = asRecord(raw); if (!p) return fail("malformed-payload", "Operation end payload must be an object."); return { ok: true, value: { operationId: typeof p.operationId === "string" ? p.operationId : "unknown", actorId: parseOptionalId(p, "actorId"), outcome: typeof p.outcome === "string" ? p.outcome : undefined, zoneId: parseOptionalId(p, "zoneId"), factionId: parseOptionalId(p, "factionId"), missionId: parseOptionalId(p, "missionId"), }, }; }