78 lines
3.3 KiB
TypeScript
78 lines
3.3 KiB
TypeScript
/**
|
|
* Multi-layer tuning merge helper with additive list operations (`$listOps`).
|
|
*
|
|
* Objects merge key-by-key. Plain arrays replace the base value (preserving existing player
|
|
* override behaviour). An object containing only `{ "$listOps": [...] }` applies incremental
|
|
* append/prepend/upsertBy/removeBy operations to the base array so multiple mods can add
|
|
* store packs, redeem codes, trophies, or preset clans without clobbering one another.
|
|
*/
|
|
|
|
export type TuningListOp =
|
|
| { readonly op: 'append'; readonly items: readonly unknown[] }
|
|
| { readonly op: 'prepend'; readonly items: readonly unknown[] }
|
|
| { readonly op: 'upsertBy'; readonly key: string; readonly item: Record<string, unknown> }
|
|
| { readonly op: 'removeBy'; readonly key: string; readonly value: unknown };
|
|
|
|
export function mergeTuningLayer(base: unknown, override: unknown): unknown {
|
|
if (isListOpsEnvelope(override)) {
|
|
const list = Array.isArray(base) ? [...base] : [];
|
|
return applyListOps(list, override.$listOps);
|
|
}
|
|
if (!isPlainObject(base) || !isPlainObject(override)) return override;
|
|
const merged: Record<string, unknown> = { ...base };
|
|
for (const [key, value] of Object.entries(override)) {
|
|
merged[key] = key in base ? mergeTuningLayer(base[key], value) : value;
|
|
}
|
|
return merged;
|
|
}
|
|
|
|
function applyListOps(base: unknown[], ops: readonly unknown[]): unknown[] {
|
|
const result = [...base];
|
|
for (const rawOp of ops) {
|
|
if (!isPlainObject(rawOp) || typeof rawOp.op !== 'string') {
|
|
throw new Error('invalid $listOps entry: expected object with string "op"');
|
|
}
|
|
switch (rawOp.op) {
|
|
case 'append':
|
|
if (!Array.isArray(rawOp.items)) throw new Error('$listOps append requires an "items" array');
|
|
result.push(...rawOp.items);
|
|
break;
|
|
case 'prepend':
|
|
if (!Array.isArray(rawOp.items)) throw new Error('$listOps prepend requires an "items" array');
|
|
result.unshift(...rawOp.items);
|
|
break;
|
|
case 'upsertBy': {
|
|
if (typeof rawOp.key !== 'string' || !isPlainObject(rawOp.item)) {
|
|
throw new Error('$listOps upsertBy requires string "key" and object "item"');
|
|
}
|
|
const matchValue = rawOp.item[rawOp.key];
|
|
const idx = result.findIndex((entry) => isPlainObject(entry) && entry[rawOp.key as string] === matchValue);
|
|
if (idx >= 0) result[idx] = mergeTuningLayer(result[idx], rawOp.item);
|
|
else result.push(rawOp.item);
|
|
break;
|
|
}
|
|
case 'removeBy': {
|
|
if (typeof rawOp.key !== 'string') throw new Error('$listOps removeBy requires string "key"');
|
|
for (let i = result.length - 1; i >= 0; i -= 1) {
|
|
const entry = result[i];
|
|
if (isPlainObject(entry) && entry[rawOp.key] === rawOp.value) result.splice(i, 1);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
throw new Error(`unsupported $listOps operation "${rawOp.op}"`);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function isListOpsEnvelope(value: unknown): value is { readonly $listOps: readonly unknown[] } {
|
|
if (!isPlainObject(value)) return false;
|
|
const keys = Object.keys(value);
|
|
return keys.length === 1 && keys[0] === '$listOps' && Array.isArray(value.$listOps);
|
|
}
|
|
|
|
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
}
|