import puppeteer from 'puppeteer-core'; import { spawn } from 'node:child_process'; import net from 'node:net'; const preview = spawn('npx', ['vite','preview','--host','127.0.0.1','--port','4815','--strictPort'], { stdio:'ignore' }); process.on('exit', () => { try{preview.kill()}catch{} }); for (let i=0;i<50;i++){ const ok = await new Promise(r=>{const s=net.connect(4815,'127.0.0.1');s.once('connect',()=>{s.end();r(true)});s.once('error',()=>r(false))}); if(ok)break; await new Promise(r=>setTimeout(r,300)); } const browser = await puppeteer.launch({ executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', headless: true, args: ['--autoplay-policy=no-user-gesture-required','--mute-audio'] }); const page = await browser.newPage(); await page.setViewport({ width: 960, height: 560 }); page.on('pageerror', e => console.log('[pageerror]', (e.stack||e.message).slice(0,500))); await page.goto('http://127.0.0.1:4815/', { waitUntil: 'networkidle2' }); await page.waitForFunction('window.__game && window.__director', { timeout: 15000 }); await page.waitForFunction(() => window.__game.scene.isActive('TitleScene'), { timeout: 20000 }); await page.evaluate(() => { document.querySelector('#game input[type="text"]').closest('div').querySelector('button').click(); }); await page.waitForFunction(() => window.__game.scene.isActive('HouseScene'), { timeout: 15000 }); await new Promise(r=>setTimeout(r,600)); // 切到 PlazaScene(停其它地图场景) await page.evaluate(() => { const sm = window.__game.scene; for (const sc of sm.getScenes(true)) { const k = sc.scene.key; if (k !== 'UIScene' && k !== 'PlazaScene') sm.stop(k); } sm.start('PlazaScene', { spawn: 'default' }); }); await page.waitForFunction(() => window.__game.scene.isActive('PlazaScene'), { timeout: 9000 }); await new Promise(r=>setTimeout(r,600)); const dump1 = await page.evaluate(() => { const s = window.__game.scene.getScene('PlazaScene'); const u = window.__game.scene.getScene('UIScene'); return { day: window.__director.day, dialogueActive: window.__director.dialogueActive, uiActive: window.__game.scene.isActive('UIScene'), player: { x: s.player.x, y: s.player.y }, zones: s.interactZones.map(z => ({ id: z.id, x: z.x, y: z.y })).filter(z => z.id === 'noticeboard' || z.id.startsWith('flowerbed')), payload: u.payload ? u.payload.lines : null, }; }); console.log('dump1', JSON.stringify(dump1, null, 1)); // 传送到公告板旁并按 E await page.evaluate(() => { const s = window.__game.scene.getScene('PlazaScene'); const z = s.interactZones.find(z => z.id === 'noticeboard'); s.player.setPosition(z.x, z.y + 8); s.player.setVelocity(0, 0); }); await new Promise(r=>setTimeout(r,200)); await page.keyboard.press('KeyE'); await new Promise(r=>setTimeout(r,600)); const dump2 = await page.evaluate(() => { const s = window.__game.scene.getScene('PlazaScene'); const u = window.__game.scene.getScene('UIScene'); return { dialogueActive: window.__director.dialogueActive, payload: u.payload ? u.payload.lines : null, body: u.dlgBody ? u.dlgBody.text : null, prompt: u.promptText ? u.promptText.text : null, }; }); console.log('dump2', JSON.stringify(dump2, null, 1)); await browser.close();