working on crop

This commit is contained in:
Boki 2026-02-12 17:48:16 -05:00
parent 93e2234c4e
commit f74e3e1c85
12 changed files with 1135 additions and 220 deletions

View file

@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import { watch } from 'chokidar';
import { createReadStream, statSync } from 'fs';
import { createReadStream, statSync, openSync, readSync, closeSync } from 'fs';
import { createInterface } from 'readline';
import { logger } from '../util/logger.js';
@ -32,6 +32,8 @@ export class ClientLogWatcher extends EventEmitter {
try {
const stats = statSync(this.logPath);
this.fileOffset = stats.size;
// Read tail of log to determine current area before we start watching
this.detectCurrentArea(stats.size);
} catch {
logger.warn({ path: this.logPath }, 'Log file not found yet, will watch for creation');
this.fileOffset = 0;
@ -47,7 +49,39 @@ export class ClientLogWatcher extends EventEmitter {
this.readNewLines();
});
logger.info({ path: this.logPath }, 'Watching Client.txt for game events');
logger.info({ path: this.logPath, currentArea: this.currentArea || '(unknown)' }, 'Watching Client.txt for game events');
}
/** Read the last chunk of the log file to determine the current area. */
private detectCurrentArea(fileSize: number): void {
const TAIL_BYTES = 8192;
const start = Math.max(0, fileSize - TAIL_BYTES);
const buf = Buffer.alloc(Math.min(TAIL_BYTES, fileSize));
const fd = openSync(this.logPath, 'r');
try {
readSync(fd, buf, 0, buf.length, start);
} finally {
closeSync(fd);
}
const tail = buf.toString('utf-8');
const lines = tail.split(/\r?\n/);
// Walk backwards to find the most recent area transition
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i];
const sceneMatch = line.match(/\[SCENE\] Set Source \[(.+?)\]/);
if (sceneMatch && sceneMatch[1] !== '(null)') {
this.currentArea = sceneMatch[1];
logger.info({ area: this.currentArea }, 'Detected current area from log tail');
return;
}
const areaMatch = line.match(/You have entered (.+?)\.?$/);
if (areaMatch) {
this.currentArea = areaMatch[1];
logger.info({ area: this.currentArea }, 'Detected current area from log tail');
return;
}
}
}
private readNewLines(): void {