Vehicles gain a virtual-block disallowedCargo array (asset/vehicle/resource item + optional amount cap, 0 forbids outright). createShipment consults findDisallowedCargo() before deducting any origin stock and rejects matching cargo lines cleanly. Includes Payload migration + 7 integration tests.
75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
/**
|
|
* Pure helpers for vehicle virtual-cargo rules.
|
|
*
|
|
* Kept free of Payload/DB access so the enforcement seam in
|
|
* `createShipment` can be reasoned about (and unit-tested) in isolation.
|
|
*/
|
|
|
|
/** A shipment cargo line, as passed to `createShipment`. */
|
|
export interface CargoLine {
|
|
relationTo: string;
|
|
value: number | { id: number } | null;
|
|
amount: number;
|
|
}
|
|
|
|
/** A single entry on a vehicle template's `disallowedCargo` array. */
|
|
export interface DisallowedCargoEntry {
|
|
item: { relationTo: string; value: number | { id: number } } | number | null;
|
|
amount?: number | null;
|
|
}
|
|
|
|
/**
|
|
* Resolve a disallowed-cargo entry's item to a comparable `(relationTo, id)`
|
|
* pair, tolerating the raw relationship shape stored on the vehicle template.
|
|
*/
|
|
function normalizedItem(entry: DisallowedCargoEntry): { relationTo: string; id: number } | null {
|
|
const item = entry.item;
|
|
if (!item || typeof item !== "object") return null;
|
|
const relationTo = item.relationTo;
|
|
const raw = (item as { value?: number | { id?: number } | null }).value;
|
|
const id = typeof raw === "number" ? raw : raw?.id;
|
|
if (relationTo == null || id == null) return null;
|
|
return { relationTo, id };
|
|
}
|
|
|
|
export interface DisallowedMatch {
|
|
relationTo: string;
|
|
itemId: number;
|
|
/** Amount the entry permits (0 = full ban). */
|
|
cap: number;
|
|
}
|
|
|
|
/**
|
|
* Return the first cargo line that a vehicle template forbids, or `null` when
|
|
* the cargo is permitted.
|
|
*
|
|
* A line matches a `disallowedCargo` entry when the `(relationTo, itemId)` pair
|
|
* is equal. An entry with `amount === 0` (the default) forbids the item
|
|
* entirely; an entry with `amount > 0` only forbids quantities above that cap.
|
|
*/
|
|
export function findDisallowedCargo(
|
|
disallowedCargo: DisallowedCargoEntry[] | null | undefined,
|
|
cargo: CargoLine[],
|
|
): DisallowedMatch | null {
|
|
if (!disallowedCargo || disallowedCargo.length === 0 || !cargo.length) return null;
|
|
|
|
for (const line of cargo) {
|
|
const itemId =
|
|
typeof line.value === "number" ? line.value : (line.value as { id: number } | null)?.id;
|
|
if (itemId == null) continue;
|
|
|
|
const match = disallowedCargo.find((entry) => {
|
|
const resolved = normalizedItem(entry);
|
|
return resolved && resolved.relationTo === line.relationTo && resolved.id === itemId;
|
|
});
|
|
|
|
if (!match) continue;
|
|
|
|
const cap = match.amount ?? 0;
|
|
if (cap === 0 || line.amount > cap) {
|
|
return { relationTo: line.relationTo, itemId, cap };
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|