poe2-bot/src-old/game/GameController.ts
2026-02-13 01:12:11 -05:00

139 lines
3.5 KiB
TypeScript

import { WindowManager } from './WindowManager.js';
import { InputSender, VK } from './InputSender.js';
import { sleep, randomDelay } from '../util/sleep.js';
import { writeClipboard } from '../util/clipboard.js';
import { logger } from '../util/logger.js';
import type { Config } from '../types.js';
export class GameController {
private windowManager: WindowManager;
private inputSender: InputSender;
constructor(config: Config) {
this.windowManager = new WindowManager(config.poe2WindowTitle);
this.inputSender = new InputSender();
}
async focusGame(): Promise<boolean> {
const result = this.windowManager.focusWindow();
if (result) {
await sleep(300); // Wait for window to actually focus
}
return result;
}
isGameFocused(): boolean {
return this.windowManager.isGameFocused();
}
getWindowRect() {
return this.windowManager.getWindowRect();
}
async sendChat(message: string): Promise<void> {
logger.info({ message }, 'Sending chat message');
// Open chat
await this.inputSender.pressKey(VK.RETURN);
await randomDelay(100, 200);
// Clear any existing text
await this.inputSender.selectAll();
await sleep(50);
await this.inputSender.pressKey(VK.BACK);
await sleep(50);
// Type the message
await this.inputSender.typeText(message);
await randomDelay(50, 100);
// Send
await this.inputSender.pressKey(VK.RETURN);
await sleep(100);
}
async sendChatViaPaste(message: string): Promise<void> {
logger.info({ message }, 'Sending chat message via paste');
// Copy message to clipboard
writeClipboard(message);
await sleep(50);
// Open chat
await this.inputSender.pressKey(VK.RETURN);
await randomDelay(100, 200);
// Clear any existing text
await this.inputSender.selectAll();
await sleep(50);
await this.inputSender.pressKey(VK.BACK);
await sleep(50);
// Paste
await this.inputSender.paste();
await randomDelay(100, 200);
// Send
await this.inputSender.pressKey(VK.RETURN);
await sleep(100);
}
async goToHideout(): Promise<void> {
logger.info('Sending /hideout command');
await this.sendChatViaPaste('/hideout');
}
async ctrlRightClickAt(x: number, y: number): Promise<void> {
await this.inputSender.ctrlRightClick(x, y);
}
async moveMouseTo(x: number, y: number): Promise<void> {
await this.inputSender.moveMouse(x, y);
}
moveMouseInstant(x: number, y: number): void {
this.inputSender.moveMouseInstant(x, y);
}
async moveMouseFast(x: number, y: number): Promise<void> {
await this.inputSender.moveMouseFast(x, y);
}
async leftClickAt(x: number, y: number): Promise<void> {
await this.inputSender.leftClick(x, y);
}
async rightClickAt(x: number, y: number): Promise<void> {
await this.inputSender.rightClick(x, y);
}
async holdAlt(): Promise<void> {
await this.inputSender.keyDown(VK.MENU);
}
async releaseAlt(): Promise<void> {
await this.inputSender.keyUp(VK.MENU);
}
async pressEscape(): Promise<void> {
await this.inputSender.pressKey(VK.ESCAPE);
}
async openInventory(): Promise<void> {
logger.info('Opening inventory');
await this.inputSender.pressKey(VK.I);
await sleep(300);
}
async ctrlLeftClickAt(x: number, y: number): Promise<void> {
await this.inputSender.ctrlLeftClick(x, y);
}
async holdCtrl(): Promise<void> {
await this.inputSender.keyDown(VK.CONTROL);
}
async releaseCtrl(): Promise<void> {
await this.inputSender.keyUp(VK.CONTROL);
}
}