inventory types
This commit is contained in:
parent
cf5d944fd1
commit
3d7a8aafdf
9 changed files with 532 additions and 369 deletions
79
src/index.ts
79
src/index.ts
|
|
@ -7,6 +7,7 @@ import { ClientLogWatcher } from './log/ClientLogWatcher.js';
|
|||
import { TradeExecutor } from './executor/TradeExecutor.js';
|
||||
import { ScrapExecutor } from './executor/ScrapExecutor.js';
|
||||
import { TradeQueue } from './executor/TradeQueue.js';
|
||||
import { InventoryManager } from './inventory/InventoryManager.js';
|
||||
import { BotController } from './dashboard/BotController.js';
|
||||
import { DashboardServer } from './dashboard/DashboardServer.js';
|
||||
import { ConfigStore } from './dashboard/ConfigStore.js';
|
||||
|
|
@ -55,47 +56,69 @@ program
|
|||
// Initialize bot controller with config store
|
||||
const bot = new BotController(store);
|
||||
|
||||
// Start dashboard
|
||||
// 1. Start dashboard
|
||||
const dashboard = new DashboardServer(bot, port);
|
||||
await dashboard.start();
|
||||
|
||||
// Initialize game components
|
||||
// 2. Create game components
|
||||
const screenReader = new ScreenReader();
|
||||
|
||||
const gameController = new GameController(config);
|
||||
dashboard.setDebugDeps({ screenReader, gameController });
|
||||
|
||||
// Go to hideout on startup
|
||||
dashboard.broadcastLog('info', 'Sending /hideout command...');
|
||||
await gameController.focusGame();
|
||||
await gameController.goToHideout();
|
||||
bot.state = 'IN_HIDEOUT';
|
||||
dashboard.broadcastStatus();
|
||||
dashboard.broadcastLog('info', 'In hideout, ready to trade');
|
||||
|
||||
// 3. Start logWatcher BEFORE /hideout so we can wait for area transition
|
||||
const logWatcher = new ClientLogWatcher(config.poe2LogPath);
|
||||
await logWatcher.start();
|
||||
logWatcher.currentArea = 'Hideout'; // We just sent /hideout on startup
|
||||
dashboard.broadcastLog('info', 'Watching Client.txt for game events');
|
||||
|
||||
// 4. Start tradeMonitor
|
||||
const tradeMonitor = new TradeMonitor(config);
|
||||
await tradeMonitor.start(`http://localhost:${port}`);
|
||||
dashboard.broadcastLog('info', 'Browser launched');
|
||||
|
||||
// 5. Create InventoryManager
|
||||
const inventoryManager = new InventoryManager(gameController, screenReader, logWatcher, config);
|
||||
|
||||
// 6. /hideout + waitForAreaTransition
|
||||
dashboard.broadcastLog('info', 'Sending /hideout command...');
|
||||
await gameController.focusGame();
|
||||
const arrivedHome = await inventoryManager.waitForAreaTransition(
|
||||
config.travelTimeoutMs,
|
||||
() => gameController.goToHideout(),
|
||||
);
|
||||
if (arrivedHome) {
|
||||
inventoryManager.setLocation(true);
|
||||
logWatcher.currentArea = 'Hideout';
|
||||
} else {
|
||||
// Assume we're already in hideout if timeout (e.g. already there)
|
||||
inventoryManager.setLocation(true);
|
||||
logWatcher.currentArea = 'Hideout';
|
||||
logger.warn('Timed out waiting for hideout transition on startup (may already be in hideout)');
|
||||
}
|
||||
bot.state = 'IN_HIDEOUT';
|
||||
dashboard.broadcastStatus();
|
||||
dashboard.broadcastLog('info', 'In hideout, ready to trade');
|
||||
|
||||
// 7. Clear leftover inventory items to stash
|
||||
dashboard.broadcastLog('info', 'Checking inventory for leftover items...');
|
||||
await inventoryManager.clearToStash();
|
||||
dashboard.broadcastLog('info', 'Inventory cleared');
|
||||
|
||||
// 8. Create executors with shared InventoryManager
|
||||
const executor = new TradeExecutor(
|
||||
gameController,
|
||||
screenReader,
|
||||
logWatcher,
|
||||
tradeMonitor,
|
||||
inventoryManager,
|
||||
config,
|
||||
);
|
||||
|
||||
// 9. Create tradeQueue
|
||||
const tradeQueue = new TradeQueue(executor, config);
|
||||
|
||||
// Track running scrap executors per link ID
|
||||
const scrapExecutors = new Map<string, ScrapExecutor>();
|
||||
|
||||
// Activate a link based on its mode
|
||||
// 10. Activate a link based on its mode
|
||||
const activateLink = async (link: TradeLink) => {
|
||||
try {
|
||||
if (link.mode === 'scrap') {
|
||||
|
|
@ -103,15 +126,15 @@ program
|
|||
const scrapExec = new ScrapExecutor(
|
||||
gameController,
|
||||
screenReader,
|
||||
logWatcher,
|
||||
tradeMonitor,
|
||||
inventoryManager,
|
||||
config,
|
||||
);
|
||||
scrapExecutors.set(link.id, scrapExec);
|
||||
dashboard.broadcastLog('info', `Scrap loop started: ${link.name || link.label}`);
|
||||
dashboard.broadcastStatus();
|
||||
// Run in background (don't await — it's an infinite loop)
|
||||
scrapExec.runScrapLoop(link.url).catch((err) => {
|
||||
scrapExec.runScrapLoop(link.url, link.postAction).catch((err) => {
|
||||
logger.error({ err, linkId: link.id }, 'Scrap loop error');
|
||||
dashboard.broadcastLog('error', `Scrap loop failed: ${link.name || link.label}`);
|
||||
scrapExecutors.delete(link.id);
|
||||
|
|
@ -140,7 +163,7 @@ program
|
|||
await tradeMonitor.pauseSearch(id);
|
||||
};
|
||||
|
||||
// Load all saved + CLI links (only activate ones marked active)
|
||||
// 11. Load all saved + CLI links (only activate ones marked active)
|
||||
for (const url of allUrls) {
|
||||
const link = bot.addLink(url);
|
||||
if (link.active) {
|
||||
|
|
@ -186,6 +209,15 @@ program
|
|||
}
|
||||
});
|
||||
|
||||
// When link postAction changes, restart executor if active
|
||||
bot.on('link-postaction-changed', async (data: { id: string; postAction: string; link: TradeLink }) => {
|
||||
if (data.link.active) {
|
||||
await deactivateLink(data.id);
|
||||
await activateLink(data.link);
|
||||
dashboard.broadcastLog('info', `Post-action changed to ${data.postAction}: ${data.link.name || data.id}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Wire up events: when new listings appear, queue them for trading
|
||||
tradeMonitor.on('new-listings', (data: { searchId: string; itemIds: string[]; page: Page }) => {
|
||||
if (bot.isPaused) {
|
||||
|
|
@ -216,17 +248,8 @@ program
|
|||
|
||||
// Forward executor state changes to dashboard
|
||||
const stateInterval = setInterval(() => {
|
||||
// Feed inventory state from active scrap executors
|
||||
let inventorySet = false;
|
||||
for (const [, scrapExec] of scrapExecutors) {
|
||||
const inv = scrapExec.getInventoryState();
|
||||
if (inv) {
|
||||
bot.setInventory(inv);
|
||||
inventorySet = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!inventorySet) bot.setInventory(undefined);
|
||||
// Feed inventory state from shared InventoryManager
|
||||
bot.setInventory(inventoryManager.getInventoryState());
|
||||
|
||||
// Check live trade executor state
|
||||
const execState = executor.getState();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue