Files
WT-Offline/packages/local-service/src/profile/profile-external-inventory.ts
T
N0VAN0M4LY b68f321b84 refactor(server): separate inventory, squadron, and web logic
Split inventory definition generation by category and select chest
contents deterministically. Move squadron queries, membership, and
lifecycle into focused modules and enforce role permissions.

Serve store and marketplace pages as static files with shared web
helpers. Share profile field readers and keep response encoding in
the protocol modules. Restrict cloud settings to registered blocks.

Type checking and the save CLI ran successfully. In-game acceptance
is pending.
2026-11-25 08:26:11 +08:00

110 lines
4.1 KiB
TypeScript

/**
* 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, setParam, type BlkBlock } from '@wto/blk';
import { intField, stringField } from './blk-fields.ts';
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 = stringField(child, 'itemId');
const itemDefId = intField(child, 'itemDefId');
const quantity = intField(child, 'quantity');
const timestamp = stringField(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 && intField(block, 'itemDefId') === itemDefId);
if (existing) {
const newQty = Math.max(0, intField(existing, 'quantity')) + quantity;
setParam(existing, 'quantity', { type: 'int', value: newQty });
setParam(existing, 'timestamp', { type: 'string', value: nowIso });
return { itemId: stringField(existing, 'itemId'), itemDefId, quantity: newQty, timestamp: nowIso };
}
const nextId = Math.max(1, intField(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 && stringField(block, 'itemId') === itemId);
if (idx < 0) return undefined;
const target = root.blocks[idx]!;
const itemDefId = intField(target, 'itemDefId');
const current = intField(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 };
}