import { launch, CDP } from './cdpshot.mjs'; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const { url: wsUrl, kill } = await launch('http://localhost:8080/index.html', undefined, 1280, 720, 5000); const cdp = new CDP(wsUrl); await cdp.open(); await cdp.send('Runtime.enable'); const ev = async (expression) => { const r = await cdp.send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }); if (r.exceptionDetails) throw new Error('eval: ' + (r.exceptionDetails.exception?.description ?? r.exceptionDetails.text)); return r.result?.value; }; const toClient = (x, y) => `(() => { const c = window.game.canvas; const r = c.getBoundingClientRect(); const z = r.width / c.width; return [r.left + ${x} * z, r.top + ${y} * z]; })()`; async function click(cx, cy) { await cdp.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: cx, y: cy }); await sleep(70); await cdp.send('Input.dispatchMouseEvent', { type: 'mousePressed', x: cx, y: cy, button: 'left', clickCount: 1 }); await sleep(100); await cdp.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: cx, y: cy, button: 'left', clickCount: 1 }); await sleep(70); } let menuUp = false; for (let i = 0; i < 120 && !menuUp; i++) { await sleep(500); menuUp = await ev(`!!(window.game && window.game.scene.isActive('MenuScene'))`); } await sleep(4600); const mb = await ev(`(() => { const m = window.game.scene.getScene('MenuScene'); return [m.newGameBtn.x, m.newGameBtn.y]; })()`); await click(...(await ev(toClient(mb[0], mb[1])))); let gameUp = false; for (let i = 0; i < 240 && !gameUp; i++) { await sleep(500); gameUp = await ev(`!!(window.game && window.game.scene.isActive('GameScene') && window.game.scene.getScene('GameScene').ship)`); } await sleep(1500); console.log(JSON.stringify(await ev(`(() => { const gs = window.game.scene.getScene('GameScene'); return { deckEnabled: gs.actionBar.enabled, slots: gs.actionBar.slots.map((s) => [s.id, s.disabled === true, s.panel.input ? s.panel.input.enabled : null]), commPaused: !!(gs.comms && gs.comms.paused), commBox: !!(gs.comms && gs.comms.box), commBoxOpen: !!(gs.comms && gs.comms.box && gs.comms.box.alpha > 0.2), }; })()`), null, 1)); cdp.close(); kill();