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(80); 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(80); } let menuUp = false; for (let i = 0; i < 120 && !menuUp; i++) { await sleep(500); menuUp = await ev(`!!(window.game && window.game.scene.isActive('MenuScene'))`); } if (!menuUp) throw new Error('no menu'); await sleep(4200); 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)`); } if (!gameUp) throw new Error('no game'); await sleep(1000); const [cx, cy] = await ev(toClient(1143.33, 680)); await cdp.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: cx, y: cy }); await sleep(250); console.log('input keys:', await ev(`Object.keys(window.game.input).join(', ')`)); console.log('input own:', await ev(`Object.getOwnPropertyNames(Object.getPrototypeOf(window.game.input)).join(', ')`)); console.log('interactiveObjects-ish:', await ev(`(() => { const i = window.game.input; return { iO: typeof i.interactionObjects, objs: typeof i.objects, list: typeof i.list, active: typeof i.activePointers, capture: i.captureTarget ? true : false }; })()`)); console.log(JSON.stringify(await ev(`(() => { const i = window.game.input; const list = i.interactionObjects ?? i.objects ?? i._objects ?? null; if (!list) return 'no list found'; const p = i.activePointer ?? i.activePointers?.[0]; const out = []; for (const o of list) { try { if (o.hitArea && o.hitTest(p)) out.push(o.name || o.type); } catch (e) {} } return { count: list.length, under: out }; })()`), null, 1)); cdp.close(); kill();