74 lines
3.8 KiB
JavaScript
74 lines
3.8 KiB
JavaScript
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;
|
|
};
|
|
|
|
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]; })()`);
|
|
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);
|
|
}
|
|
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);
|
|
|
|
console.log(await ev(`(() => {
|
|
const gs = window.game.scene.getScene('GameScene');
|
|
const s = gs.actionBar.slots.find((x) => x.id === 'menu');
|
|
const inp = s.panel.input;
|
|
const ha = inp.hitArea;
|
|
return {
|
|
panelPos: [s.panel.x, s.panel.y],
|
|
slotPos: [s.slot.x, s.slot.y],
|
|
scrollFactor: [s.panel.scrollFactorX, s.panel.scrollFactorY],
|
|
enabled: inp.enabled,
|
|
interactive: !!inp,
|
|
hitArea: ha ? { x: ha.x, y: ha.y, w: ha.width, h: ha.height } : null,
|
|
styleBW: gs.actionBar.style.bw,
|
|
styleBH: gs.actionBar.style.bh,
|
|
activePointers: window.game.input.activePointer ? { x: window.game.input.activePointer.x, y: window.game.input.activePointer.y } : null,
|
|
worldPoint: null,
|
|
};
|
|
})()`));
|
|
|
|
// Now hover via CDP mouse move to the slot and see if pointerover fires
|
|
await ev(`(() => { const gs = window.game.scene.getScene('GameScene'); const s = gs.actionBar.slots.find((x) => x.id === 'menu'); window.__over = 0; s.panel.on('pointerover', () => window.__over++); window.__down = 0; s.panel.on('pointerdown', () => window.__down++); return true; })()`);
|
|
const [cx, cy] = await ev(toClient(1143.33, 680));
|
|
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: cx, y: cy });
|
|
await sleep(300);
|
|
console.log('hover count:', await ev(`window.__over`));
|
|
|
|
// check the pointer world coords phaser sees
|
|
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: cx - 5, y: cy });
|
|
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: cx, y: cy });
|
|
await sleep(200);
|
|
console.log('phaser pointer:', await ev(`(() => { const p = window.game.input.activePointer; return { x: p.x, y: p.y, worldX: p.worldX, worldY: p.worldY, position: [p.position.x, p.position.y] }; })()`));
|
|
|
|
// hit test manually
|
|
console.log(await ev(`(() => {
|
|
const gs = window.game.scene.getScene('GameScene');
|
|
const s = gs.actionBar.slots.find((x) => x.id === 'menu');
|
|
const p = window.game.input.activePointer;
|
|
try { return { hit: s.panel.input.hitTest(p) }; } catch(e) { return 'ERR ' + e.message; }
|
|
})()`));
|
|
|
|
cdp.close(); kill();
|