Add classic items and fix initial showcase responses

Store and activate boosters, personal discounts, wagers, and universal
spares. Apply active personal discounts when pricing vehicle purchases.
Return the server time and default fields for every showcase type.

Add developer commands for inspecting game logs, strings, references,
callers, and disassembly.
This commit is contained in:
N0VAN0M4LY committed 2026-11-25 04:17:34 +08:00
1 parent 76670121a9
commit ae6958f291
21 files changed
+880 -35

No files matched your search

@@ -1,18 +1,23 @@
/**
* The profile as the char service hands it to the client: the saved profile plus the
* account data the char server keeps outside the profile.
* account data and state the char server keeps outside the profile.
*
* charDrivenNick: the client reads the player's nick from this top-level string when it
* loads a profile (name looked up at aces.exe 2.61.0.37 0x8fe0aa, value stored at 0x91874d).
* A profile without it clears the nick, and the online initialisation then fails with
* "[CONNECT] nick is empty" and error 0x81110017 (0x164e5cf), as seen in the lab runs.
* The nick belongs to the account, so it is added here rather than kept in the save.
*
* serverTime:i64: the char server's clock; without it the client
* takes the server time as 0 and every expiry and time limit is wrong.
*/
import { setParam, type BlkBlock } from '@wto/blk';
import type { Account } from '../accounts/account-store.ts';
import { serverTimeNow } from './server-time.ts';
export function clientProfile(saved: BlkBlock, account: Account): BlkBlock {
const profile = structuredClone(saved);
setParam(profile, 'charDrivenNick', { type: 'string', value: account.nick });
setParam(profile, 'serverTime', { type: 'int64', value: serverTimeNow() });
return profile;
}
@@ -0,0 +1,148 @@
/**
* The player's classic items in the profile (implementation notes; 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=<FILETIME> 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=<uid> ... } active boosters, any number
* itemActiveDiscounts{ itemUID:t=<uid> ... } active personal discounts
* itemActiveWagers{ itemUID:t=<uid> } 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;
}
@@ -0,0 +1,27 @@
/**
* Time as the char server and the client count it: Windows FILETIME,
* 100-nanosecond ticks since 1601-01-01 UTC. The profile's serverTime:i64 sets the client's
* clock (set_charserver_time, aces.exe 2.61.0.37 0x904400; get_charserver_time_sec 0x15533a0
* turns it back into Unix seconds), and item expiry times (items/<uid>/expiredTimeH) are
* absolute times in the same unit.
*/
export type ServerTime = bigint;
const TICKS_PER_MILLISECOND = 10_000n;
const UNIX_EPOCH = 116_444_736_000_000_000n;
export const TICKS_PER_HOUR = 36_000_000_000n;
export function serverTimeFromUnixMs(milliseconds: number): ServerTime {
return BigInt(Math.trunc(milliseconds)) * TICKS_PER_MILLISECOND + UNIX_EPOCH;
}
export function serverTimeNow(): ServerTime {
return serverTimeFromUnixMs(Date.now());
}
/** A whole or fractional number of hours as ticks. */
export function hoursToTicks(hours: number): bigint {
return BigInt(Math.round(hours * 3_600_000)) * TICKS_PER_MILLISECOND;
}