feat(inventory): add external inventory, marketplace, and supply chests

Define external inventory items and support GJN purchases, recycling,
and supply chest drops.
This commit is contained in:
N0VAN0M4LY committed 2026-11-25 07:19:07 +08:00
1 parent 129d796095
commit 415f78e761
27 files changed
+2538 -15

No files matched your search

@@ -0,0 +1,118 @@
/**
* External inventory items stored in the player's save.
*
* Stored under top-level block `extInventory` in `profile.blk` (ignored by aces.exe's profile
* reader 0x919060), keeping every player's save self-contained in a single directory:
*
* extInventory{
* nextItemId:i=4
* item{ itemId:t="1" itemDefId:i=50221 quantity:i=1 timestamp:t="2026-11-25T00:00:00Z" }
* }
*/
import { ensureBlock, findBlock, findParam, setParam, type BlkBlock } from '@wto/blk';
const EXT_INVENTORY = 'extInventory';
const NEXT_ID = 'nextItemId';
const ITEM_BLOCK = 'item';
export interface StoredExternalItem {
readonly itemId: string;
readonly itemDefId: number;
readonly quantity: number;
readonly timestamp: string;
}
export function externalInventoryItems(profile: BlkBlock): StoredExternalItem[] {
const root = findBlock(profile, EXT_INVENTORY);
if (!root) return [];
const items: StoredExternalItem[] = [];
for (const child of root.blocks) {
if (child.name !== ITEM_BLOCK) continue;
const itemId = stringOf(child, 'itemId');
const itemDefId = intOf(child, 'itemDefId');
const quantity = intOf(child, 'quantity');
const timestamp = stringOf(child, 'timestamp') || new Date().toISOString();
if (itemId && itemDefId > 0 && quantity > 0) {
items.push({ itemId, itemDefId, quantity, timestamp });
}
}
return items;
}
export function externalItemByUid(profile: BlkBlock, itemId: string): StoredExternalItem | undefined {
return externalInventoryItems(profile).find((item) => item.itemId === itemId);
}
/**
* Add quantity to an existing stack of the same itemDefId, or allocate a new itemId stack.
* Returns the updated or newly created item record.
*/
export function grantExternalItem(
profile: BlkBlock,
itemDefId: number,
quantity: number,
nowIso = new Date().toISOString(),
): StoredExternalItem {
const root = ensureBlock(profile, EXT_INVENTORY);
const existing = root.blocks.find((block) => block.name === ITEM_BLOCK && intOf(block, 'itemDefId') === itemDefId);
if (existing) {
const newQty = Math.max(0, intOf(existing, 'quantity')) + quantity;
setParam(existing, 'quantity', { type: 'int', value: newQty });
setParam(existing, 'timestamp', { type: 'string', value: nowIso });
return { itemId: stringOf(existing, 'itemId'), itemDefId, quantity: newQty, timestamp: nowIso };
}
const nextId = Math.max(1, intOf(root, NEXT_ID) + 1);
setParam(root, NEXT_ID, { type: 'int', value: nextId });
const itemId = String(nextId);
root.blocks.push({
name: ITEM_BLOCK,
params: [
{ name: 'itemId', value: { type: 'string', value: itemId } },
{ name: 'itemDefId', value: { type: 'int', value: itemDefId } },
{ name: 'quantity', value: { type: 'int', value: quantity } },
{ name: 'timestamp', value: { type: 'string', value: nowIso } },
],
blocks: [],
});
return { itemId, itemDefId, quantity, timestamp: nowIso };
}
/**
* Consume up to `quantity` from a specific `itemId` stack. Returns undefined if the player
* does not hold enough in that stack; otherwise returns the post-consumption quantity (0 when
* removed).
*/
export function consumeExternalItem(
profile: BlkBlock,
itemId: string,
quantity: number,
nowIso = new Date().toISOString(),
): { readonly itemDefId: number; readonly remaining: number; readonly timestamp: string } | undefined {
if (quantity <= 0) return undefined;
const root = findBlock(profile, EXT_INVENTORY);
if (!root) return undefined;
const idx = root.blocks.findIndex((block) => block.name === ITEM_BLOCK && stringOf(block, 'itemId') === itemId);
if (idx < 0) return undefined;
const target = root.blocks[idx]!;
const itemDefId = intOf(target, 'itemDefId');
const current = intOf(target, 'quantity');
if (current < quantity) return undefined;
const remaining = current - quantity;
if (remaining === 0) {
root.blocks.splice(idx, 1);
} else {
setParam(target, 'quantity', { type: 'int', value: remaining });
setParam(target, 'timestamp', { type: 'string', value: nowIso });
}
return { itemDefId, remaining, timestamp: nowIso };
}
function stringOf(block: BlkBlock, name: string): string {
const param = findParam(block, name)?.value;
return param?.type === 'string' ? param.value : '';
}
function intOf(block: BlkBlock, name: string): number {
const param = findParam(block, name)?.value;
return param?.type === 'int' ? param.value : param?.type === 'int64' ? Number(param.value) : 0;
}
@@ -0,0 +1,58 @@
/**
* Trophy open counts and UserLog entries in the profile:
* - `trophies{ <trophyId>{ count:i=<openCount> } }` (aces.exe 0x92d16b, 0x92d4df);
* - `userlogs{ <16-hex-id>{ type:i=<EULT> time:i=<unixSec> roomId:i64=0 body{ ... } } }`
* (UserLog::serialize 0x95f160, entry fields 0x95ec30; EULT_OPEN_TROPHY = 30 at 0x1a5599b).
*/
import { ensureBlock, findBlock, findParam, setParam, type BlkBlock, type BlkParam } from '@wto/blk';
const TROPHIES = 'trophies';
const USERLOGS = 'userlogs';
const MAX_USERLOGS = 80;
export const EULT_OPEN_TROPHY = 30;
export const EULT_BUY_TROPHY = 20;
export function trophyOpenCount(profile: BlkBlock, trophyId: string): number {
const trophies = findBlock(profile, TROPHIES);
const entry = trophies && findBlock(trophies, trophyId);
const param = entry && findParam(entry, 'count')?.value;
return param?.type === 'int' ? param.value : 0;
}
export function incrementTrophyOpenCount(profile: BlkBlock, trophyId: string, delta = 1): number {
const entry = ensureBlock(ensureBlock(profile, TROPHIES), trophyId);
const next = trophyOpenCount(profile, trophyId) + delta;
setParam(entry, 'count', { type: 'int', value: next });
return next;
}
export interface NewUserLogEntry {
readonly type: number;
readonly timeSec: number;
readonly bodyParams: readonly BlkParam[];
readonly bodyBlocks?: readonly BlkBlock[];
}
export function appendUserLog(profile: BlkBlock, entry: NewUserLogEntry): string {
const logs = ensureBlock(profile, USERLOGS);
const seq = logs.blocks.length + 1;
const idHex = ((BigInt(entry.timeSec) * 1000n) + BigInt(seq)).toString(16).padStart(16, '0');
logs.blocks.push({
name: idHex,
params: [
{ name: 'type', value: { type: 'int', value: entry.type } },
{ name: 'time', value: { type: 'int', value: entry.timeSec } },
{ name: 'roomId', value: { type: 'int64', value: 0n } },
],
blocks: [{
name: 'body',
params: [...entry.bodyParams],
blocks: [...(entry.bodyBlocks ?? [])],
}],
});
if (logs.blocks.length > MAX_USERLOGS) {
logs.blocks.splice(0, logs.blocks.length - MAX_USERLOGS);
}
return idHex;
}