/** * The player's classic items in the profile (ItemStorage, aces.exe * 2.61.0.37 0x8f2de0): * * items{ * numTotalAddedItems:i=2 * item_0000019b2f3c1a40_2{ block name = the item's uid * id:t="booster_shop_wp_15_10mp" config/items.blk name * type:t="rateBooster" * expiredTimeH:i64= absolute expiry; missing = never * instCount:i64=1 pieces under this uid * ... per-type fields (wager: numWins numBattles wager; * personalDiscounts: purchasesCount purchasesMaxCount discountsDesc{}) * } * } * itemActiveBoosters{ itemUID:t= ... } active boosters, any number * itemActiveDiscounts{ itemUID:t= ... } active personal discounts * itemActiveWagers{ itemUID:t= } the current contract, at most one * * Every field is read by name (0x8f10d0 and the per-type readers), so order does not matter. * New uids follow the client's own pattern item_%016llx_%x (0x8f2c87): a time stamp and the * running count numTotalAddedItems. */ import { ensureBlock, findBlock, findParam, setParam, type BlkBlock, type BlkParam } from '@wto/blk'; import type { ServerTime } from './server-time.ts'; const ITEMS = 'items'; const ADDED_COUNT = 'numTotalAddedItems'; const ACTIVE_UID = 'itemUID'; export type ActiveList = 'itemActiveBoosters' | 'itemActiveDiscounts' | 'itemActiveWagers'; export const ACTIVE_LISTS: readonly ActiveList[] = ['itemActiveBoosters', 'itemActiveDiscounts', 'itemActiveWagers']; export interface OwnedItem { readonly uid: string; readonly id: string; readonly type: string; readonly count: number; readonly expiresAt?: ServerTime; } export interface NewItem { readonly id: string; readonly type: string; readonly count: number; readonly expiresAt?: ServerTime; /** Per-type fields written after the common ones. */ readonly params?: readonly BlkParam[]; readonly blocks?: readonly BlkBlock[]; } export function ownedItems(profile: BlkBlock): OwnedItem[] { return (findBlock(profile, ITEMS)?.blocks ?? []).flatMap((record) => { const item = itemOf(record); return item ? [item] : []; }); } export function ownedItem(profile: BlkBlock, uid: string): OwnedItem | undefined { const record = itemRecord(profile, uid); return record && itemOf(record); } /** The item's whole block, for per-type fields; undefined when the player does not have it. */ export function itemRecord(profile: BlkBlock, uid: string): BlkBlock | undefined { const items = findBlock(profile, ITEMS); return items && findBlock(items, uid); } /** Add an item under a new uid; returns the uid. */ export function addItem(profile: BlkBlock, item: NewItem, now: ServerTime): string { const items = ensureBlock(profile, ITEMS); const added = intOf(items, ADDED_COUNT) + 1; setParam(items, ADDED_COUNT, { type: 'int', value: added }); const uid = `item_${now.toString(16).padStart(16, '0')}_${added.toString(16)}`; items.blocks.push({ name: uid, params: [ { name: 'id', value: { type: 'string', value: item.id } }, { name: 'type', value: { type: 'string', value: item.type } }, ...(item.expiresAt !== undefined ? [expiryParam(item.expiresAt)] : []), countParam(item.count), ...(item.params ?? []), ], blocks: [...(item.blocks ?? [])], }); return uid; } export function setItemCount(profile: BlkBlock, uid: string, count: number): void { const record = itemRecord(profile, uid); if (record) setParam(record, 'instCount', countParam(count).value); } export function setItemExpiry(profile: BlkBlock, uid: string, expiresAt: ServerTime): void { const record = itemRecord(profile, uid); if (record) setParam(record, 'expiredTimeH', expiryParam(expiresAt).value); } /** Remove an item and every reference to it from the active lists. */ export function removeItem(profile: BlkBlock, uid: string): void { const items = findBlock(profile, ITEMS); if (items) items.blocks = items.blocks.filter((record) => record.name !== uid); for (const list of ACTIVE_LISTS) deactivate(profile, list, uid); } export function activeUids(profile: BlkBlock, list: ActiveList): string[] { return (findBlock(profile, list)?.params ?? []).flatMap((param) => param.name === ACTIVE_UID && param.value.type === 'string' ? [param.value.value] : []); } export function activate(profile: BlkBlock, list: ActiveList, uid: string): void { if (activeUids(profile, list).includes(uid)) return; ensureBlock(profile, list).params.push({ name: ACTIVE_UID, value: { type: 'string', value: uid } }); } export function deactivate(profile: BlkBlock, list: ActiveList, uid: string): void { const block = findBlock(profile, list); if (block) block.params = block.params.filter((param) => !(param.name === ACTIVE_UID && param.value.type === 'string' && param.value.value === uid)); } function itemOf(record: BlkBlock): OwnedItem | undefined { const id = stringOf(record, 'id'); const type = stringOf(record, 'type'); if (id === undefined || type === undefined) return undefined; const expiry = findParam(record, 'expiredTimeH')?.value; const expiresAt = expiry?.type === 'int64' && expiry.value >= 0n ? expiry.value : undefined; return { uid: record.name, id, type, count: intOf(record, 'instCount'), ...(expiresAt !== undefined ? { expiresAt } : {}) }; } function countParam(count: number): BlkParam { return { name: 'instCount', value: { type: 'int64', value: BigInt(count) } }; } function expiryParam(expiresAt: ServerTime): BlkParam { return { name: 'expiredTimeH', value: { type: 'int64', value: expiresAt } }; } function stringOf(block: BlkBlock, name: string): string | undefined { const value = findParam(block, name)?.value; return value?.type === 'string' ? value.value : undefined; } function intOf(block: BlkBlock, name: string): number { const value = findParam(block, name)?.value; if (value?.type === 'int64') return Number(value.value); return value?.type === 'int' ? value.value : 0; }