2024-10-26 13:05:56 -03:00
|
|
|
"use strict";
|
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
|
exports.PlayerService = void 0;
|
|
|
|
|
const electron_1 = require("electron");
|
|
|
|
|
const openai_service_1 = require("./openai.service");
|
|
|
|
|
class PlayerService {
|
|
|
|
|
constructor() {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] Initializing');
|
2024-10-26 13:05:56 -03:00
|
|
|
this.openAIService = new openai_service_1.OpenAIService();
|
|
|
|
|
}
|
|
|
|
|
async executeBasicCode(code) {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] executeBasicCode called with:', code);
|
2024-10-26 13:05:56 -03:00
|
|
|
const lines = code.split('\n');
|
|
|
|
|
for (const line of lines) {
|
|
|
|
|
if (line.trim().startsWith('REM') || line.trim() === '')
|
|
|
|
|
continue;
|
|
|
|
|
const match = line.match(/^\d+\s+(\w+)\s+"([^"]+)"(?:\s+"([^"]+)")?/);
|
|
|
|
|
if (!match)
|
|
|
|
|
continue;
|
|
|
|
|
const [_, command, identifier, value] = match;
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] Executing command:', { command, identifier, value });
|
2024-10-26 13:05:56 -03:00
|
|
|
await this.executeCommand(command, identifier, value);
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 500));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async executeCommand(command, identifier, value) {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] executeCommand called with:', { command, identifier, value });
|
2024-10-26 13:05:56 -03:00
|
|
|
const screenshotPath = await this.captureScreen();
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] Screen captured at:', screenshotPath);
|
2024-10-26 13:05:56 -03:00
|
|
|
const analysis = await this.openAIService.analyzeScreen(screenshotPath);
|
|
|
|
|
const element = analysis.elements.find(e => e.identifier === identifier);
|
|
|
|
|
if (!element)
|
|
|
|
|
throw new Error(`Element not found: ${identifier}`);
|
|
|
|
|
const centerX = element.bounds.x + element.bounds.width / 2;
|
|
|
|
|
const centerY = element.bounds.y + element.bounds.height / 2;
|
|
|
|
|
switch (command) {
|
|
|
|
|
case 'CLICK':
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] Simulating click at:', { centerX, centerY });
|
2024-10-26 13:05:56 -03:00
|
|
|
await this.simulateClick(centerX, centerY);
|
|
|
|
|
break;
|
|
|
|
|
case 'TYPE':
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] Simulating type:', { centerX, centerY, value });
|
2024-10-26 13:05:56 -03:00
|
|
|
await this.simulateClick(centerX, centerY);
|
|
|
|
|
await this.simulateTyping(value || '');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async captureScreen() {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] captureScreen called');
|
2024-10-26 13:05:56 -03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
electron_1.ipcMain.once('screen-captured', (_, screenshotPath) => {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] Screen captured event received:', screenshotPath);
|
2024-10-26 13:05:56 -03:00
|
|
|
resolve(screenshotPath);
|
|
|
|
|
});
|
|
|
|
|
electron_1.ipcMain.emit('capture-screen');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
async simulateClick(x, y) {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] simulateClick called with:', { x, y });
|
2024-10-26 13:05:56 -03:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
electron_1.ipcMain.once('click-completed', () => {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] Click completed');
|
2024-10-26 13:05:56 -03:00
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
electron_1.ipcMain.emit('simulate-click', { x, y });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
async simulateTyping(text) {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] simulateTyping called with:', text);
|
2024-10-26 13:05:56 -03:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
electron_1.ipcMain.once('typing-completed', () => {
|
2024-10-26 21:21:51 -03:00
|
|
|
console.log('[PlayerService] Typing completed');
|
2024-10-26 13:05:56 -03:00
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
electron_1.ipcMain.emit('simulate-typing', { text });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
exports.PlayerService = PlayerService;
|