/** * Bought modifications in the profile: * * modifications{ * us_m22_locust_tank_tool_kit{ count:i64=1 } * us_m22_locust_37mm_usa_m3_APC{ wpSpent:i=450 count:i64=150 } * us_m22_locust_new_tank_tracks{ enabled:b=no count:i64=1 } * } * * One block per bought modification, named _; having a block means the * modification is owned. The client reads a block's params strictly in this order and stops at * the first one that does not match (aces.exe 0x942740), so a record is always rewritten whole: * wpSpent:i goldSpent:i lastWpCost:i lastGoldCost:i persist:b rpEarned:i enabled:b count:i64 modLevel:i * Absent values are left out; enabled is only written when false (the default is enabled). */ import { ensureBlock, findBlock, type BlkBlock, type BlkParam } from '@wto/blk'; const MODIFICATIONS = 'modifications'; export interface ModificationRecord { readonly silverLionsSpent: number; readonly goldenEaglesSpent: number; readonly enabled: boolean; /** How many the player has: 1 for a plain modification, the number of rounds or uses for counted ones. */ readonly count: number; } export function recordName(vehicle: string, modification: string): string { return `${vehicle}_${modification}`; } export function modificationRecord(profile: BlkBlock, vehicle: string, modification: string): ModificationRecord | undefined { const block = findBlock(profile, MODIFICATIONS); const record = block && findBlock(block, recordName(vehicle, modification)); if (!record) return undefined; return { silverLionsSpent: intOf(record, 'wpSpent'), goldenEaglesSpent: intOf(record, 'goldSpent'), enabled: boolOf(record, 'enabled') ?? true, count: intOf(record, 'count'), }; } export function ownsModification(profile: BlkBlock, vehicle: string, modification: string): boolean { return modificationRecord(profile, vehicle, modification) !== undefined; } export function writeModificationRecord(profile: BlkBlock, vehicle: string, modification: string, record: ModificationRecord): void { const block = ensureBlock(ensureBlock(profile, MODIFICATIONS), recordName(vehicle, modification)); block.params = orderedParams(record); } function orderedParams(record: ModificationRecord): BlkParam[] { const params: BlkParam[] = []; if (record.silverLionsSpent) params.push({ name: 'wpSpent', value: { type: 'int', value: record.silverLionsSpent } }); if (record.goldenEaglesSpent) params.push({ name: 'goldSpent', value: { type: 'int', value: record.goldenEaglesSpent } }); if (!record.enabled) params.push({ name: 'enabled', value: { type: 'bool', value: false } }); if (record.count) params.push({ name: 'count', value: { type: 'int64', value: BigInt(record.count) } }); return params; } function intOf(block: BlkBlock, name: string): number { const value = block.params.find((param) => param.name === name)?.value; if (value?.type === 'int64') return Number(value.value); return value?.type === 'int' ? value.value : 0; } function boolOf(block: BlkBlock, name: string): boolean | undefined { const value = block.params.find((param) => param.name === name)?.value; return value?.type === 'bool' ? value.value : undefined; }