import puppeteer from 'puppeteer-core';
const browser = await puppeteer.launch({
executablePath: '/usr/bin/chromium', headless: true,
args: ['--no-sandbox', '--enable-unsafe-swiftshader', '--use-angle=swiftshader',
'--autoplay-policy=no-user-gesture-required', '--window-size=960,540']
});
const page = await browser.newPage();
await page.setViewport({ width: 960, height: 540 });
page.on('pageerror', (e) => console.log('[err]', e.message));
const booted = new Promise((r) => page.on('console', (m) => m.text().includes('[boot]') && r()));
await page.goto('http://localhost:5199/', { waitUntil: 'networkidle2' });
await booted;
await new Promise((r) => setTimeout(r, 1200));
await page.mouse.click(480, 283);
await new Promise((r) => setTimeout(r, 2500));
// Клик в тайл на 2 шага к цели по диагонали (всегда на экране), у цели — точно в неё.
async function stepToward(tx, ty) {
const p = await page.evaluate((tx, ty) => {
const g = window.__game;
const sc = g.scenes.current;
const t = sc.player.currentTile();
const dx = Math.sign(tx - t.x);
const dy = Math.sign(ty - t.y);
const target = Math.abs(tx - t.x) <= 2 && Math.abs(ty - t.y) <= 2 ? { x: tx, y: ty }
: { x: t.x + dx * 2, y: t.y + dy * 2 };
const rect = document.querySelector('canvas').getBoundingClientRect();
const wr = g.renderer.worldRoot.position;
const wx = (target.x - target.y) * 16;
const wy = (target.x + target.y) * 8 + 8;
return { x: rect.left + ((wx + wr.x) / 480) * rect.width,
y: rect.top + ((wy + wr.y) / 270) * rect.height };
}, tx, ty);
await page.mouse.click(p.x, p.y);
}
async function walkTo(tx, ty) {
for (let i = 0; i < 40; i++) {
const at = await page.evaluate((tx, ty) => {
const t = window.__game.scenes.current?.player?.currentTile();
return t && t.x === tx && t.y === ty;
}, tx, ty);
if (at) return true;
await stepToward(tx, ty);
await new Promise((r) => setTimeout(r, 700));
}
return false;
}
await walkTo(2, 2);
await new Promise((r) => setTimeout(r, 1500));
const ok = await walkTo(4, 9);
const state = await page.evaluate(() => {
const g = window.__game;
return { loc: g.scenes.current.location.id, tile: g.scenes.current.player.currentTile(),
fog: g.scenes.current.fog?.alpha, mul: g.scenes.current.player.speedMul };
});
console.log(JSON.stringify(state), 'arrived:', ok);
await page.screenshot({ path: '/tmp/probe_hazard.png' });
await browser.close();