This commit is contained in:
Boki 2026-02-12 12:33:06 -05:00
parent 3d7a8aafdf
commit c1892230b7
13 changed files with 1111 additions and 993 deletions

View file

@ -23,6 +23,7 @@ export class TradeExecutor {
private tradeMonitor: TradeMonitor;
private inventoryManager: InventoryManager;
private config: Config;
private _onStateChange?: (state: string) => void;
constructor(
gameController: GameController,
@ -38,16 +39,25 @@ export class TradeExecutor {
this.config = config;
}
set onStateChange(cb: (state: string) => void) {
this._onStateChange = cb;
}
getState(): TradeState {
return this.state;
}
private setState(s: TradeState): void {
this.state = s;
this._onStateChange?.(s);
}
async executeTrade(trade: TradeInfo): Promise<boolean> {
const page = trade.page as Page;
try {
// Step 1: Click "Travel to Hideout" on the trade website
this.state = 'TRAVELING';
this.setState('TRAVELING');
logger.info({ searchId: trade.searchId }, 'Clicking Travel to Hideout...');
// Register listener BEFORE clicking, then click inside the callback
@ -65,11 +75,11 @@ export class TradeExecutor {
);
if (!arrived) {
logger.error('Timed out waiting for hideout arrival');
this.state = 'FAILED';
this.setState('FAILED');
return false;
}
this.state = 'IN_SELLERS_HIDEOUT';
this.setState('IN_SELLERS_HIDEOUT');
this.inventoryManager.setLocation(false);
logger.info('Arrived at seller hideout');
@ -89,19 +99,19 @@ export class TradeExecutor {
const stashPos = await this.inventoryManager.findAndClickNameplate('Stash');
if (!stashPos) {
logger.error('Could not find Stash nameplate in seller hideout');
this.state = 'FAILED';
this.setState('FAILED');
return false;
}
await sleep(1000); // Wait for stash to open
// Step 4: Scan stash and buy items
this.state = 'SCANNING_STASH';
this.setState('SCANNING_STASH');
logger.info('Scanning stash for items...');
await this.scanAndBuyItems();
// Step 5: Wait for more items
this.state = 'WAITING_FOR_MORE';
this.setState('WAITING_FOR_MORE');
logger.info(
{ waitMs: this.config.waitForMoreItemsMs },
'Waiting for seller to add more items...',
@ -112,7 +122,7 @@ export class TradeExecutor {
await this.scanAndBuyItems();
// Step 6: Go back to own hideout
this.state = 'GOING_HOME';
this.setState('GOING_HOME');
logger.info('Traveling to own hideout...');
await this.gameController.focusGame();
await sleep(300);
@ -128,15 +138,15 @@ export class TradeExecutor {
this.inventoryManager.setLocation(true);
// Step 7: Store items in stash
this.state = 'IN_HIDEOUT';
this.setState('IN_HIDEOUT');
await sleep(1000);
await this.storeItems();
this.state = 'IDLE';
this.setState('IDLE');
return true;
} catch (err) {
logger.error({ err }, 'Trade execution failed');
this.state = 'FAILED';
this.setState('FAILED');
// Try to recover by going home
try {
@ -148,7 +158,7 @@ export class TradeExecutor {
// Best-effort recovery
}
this.state = 'IDLE';
this.setState('IDLE');
return false;
}
}
@ -165,7 +175,7 @@ export class TradeExecutor {
// TODO: Implement item matching logic based on OCR text
// For now, we'll Ctrl+right-click at known grid positions
this.state = 'BUYING';
this.setState('BUYING');
// Check for price warning dialog after each buy
await this.checkPriceWarning();