finished up grid detection.

This commit is contained in:
Boki 2026-02-10 17:29:37 -05:00
parent 41d174195e
commit 1246884be9
11 changed files with 1037 additions and 19 deletions

View file

@ -6,6 +6,7 @@ import { fileURLToPath } from 'url';
import { logger } from '../util/logger.js';
import type { BotController } from './BotController.js';
import type { ScreenReader } from '../game/ScreenReader.js';
import { GRID_LAYOUTS } from '../game/GridReader.js';
import type { GameController } from '../game/GameController.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@ -150,6 +151,81 @@ export class DashboardServer {
}
});
this.app.post('/api/debug/hideout', async (_req, res) => {
if (!this.debug) { res.status(503).json({ error: 'Debug not available' }); return; }
try {
await this.debug.gameController.focusGame();
await this.debug.gameController.goToHideout();
this.broadcastLog('info', 'Sent /hideout command');
res.json({ ok: true });
} catch (err) {
logger.error({ err }, 'Debug hideout failed');
res.status(500).json({ error: 'Hideout command failed' });
}
});
// Click first text, then wait for second text to appear and click it
this.app.post('/api/debug/click-then-click', async (req, res) => {
if (!this.debug) { res.status(503).json({ error: 'Debug not available' }); return; }
const { first, second, timeout = 5000 } = req.body as { first: string; second: string; timeout?: number };
if (!first || !second) { res.status(400).json({ error: 'Missing first/second' }); return; }
try {
// Click the first target
const pos1 = await this.debug.screenReader.findTextOnScreen(first);
if (!pos1) {
this.broadcastLog('warn', `"${first}" not found on screen`);
res.json({ ok: true, found: false, step: 'first' });
return;
}
await this.debug.gameController.focusGame();
await this.debug.gameController.leftClickAt(pos1.x, pos1.y);
this.broadcastLog('info', `Clicked "${first}" at (${pos1.x}, ${pos1.y}), waiting for "${second}"...`);
// Poll OCR until second text appears
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
const pos2 = await this.debug.screenReader.findTextOnScreen(second);
if (pos2) {
await this.debug.gameController.leftClickAt(pos2.x, pos2.y);
this.broadcastLog('info', `Clicked "${second}" at (${pos2.x}, ${pos2.y})`);
res.json({ ok: true, found: true, position: pos2 });
return;
}
}
this.broadcastLog('warn', `"${second}" not found after clicking "${first}" (timed out)`);
res.json({ ok: true, found: false, step: 'second' });
} catch (err) {
logger.error({ err }, 'Debug click-then-click failed');
res.status(500).json({ error: 'Click-then-click failed' });
}
});
// Grid scan with calibrated positions
this.app.post('/api/debug/grid-scan', async (req, res) => {
if (!this.debug) { res.status(503).json({ error: 'Debug not available' }); return; }
const { layout: layoutName } = req.body as { layout: string };
if (!GRID_LAYOUTS[layoutName]) { res.status(400).json({ error: `Unknown grid layout: ${layoutName}` }); return; }
try {
const result = await this.debug.screenReader.grid.scan(layoutName);
const imageBuffer = await this.debug.screenReader.captureRegion(result.layout.region);
const imageBase64 = imageBuffer.toString('base64');
const r = result.layout.region;
this.broadcastLog('info',
`Grid scan (${layoutName}): ${result.layout.cols}x${result.layout.rows} at (${r.x},${r.y}) ${r.width}x${r.height}${result.occupied.length} occupied cells`);
res.json({
ok: true,
occupied: result.occupied,
cols: result.layout.cols,
rows: result.layout.rows,
image: imageBase64,
region: result.layout.region,
});
} catch (err) {
logger.error({ err }, 'Debug grid-scan failed');
res.status(500).json({ error: 'Grid scan failed' });
}
});
this.app.post('/api/debug/find-and-click', async (req, res) => {
if (!this.debug) { res.status(503).json({ error: 'Debug not available' }); return; }
const { text } = req.body as { text: string };