const WebSocket = require('ws') const DRAW_VEL = 2.38 const room = 'QA' + Math.floor(Math.random() * 1000) const base = 'ws://127.0.0.1:3000/ws?room=' + room function throwFor(ws, team, broom_x, broom_y, velocity = DRAW_VEL, curl = 0, friction = 1.0) { ws.send(JSON.stringify({ type: 'throw', team, broom_x, broom_y, velocity, curl, friction })) } function connect(name) { return new Promise((resolve, reject) => { const ws = new WebSocket(base) const messages = [] ws.on('message', (data) => { const msg = JSON.parse(data.toString()) messages.push(msg) console.log(`[${name}]`, msg.type, msg.type === 'game_state' ? ` turn=${msg.turn_team}` : '') if (msg.type === 'joined') { resolve({ ws, messages }) } }) ws.on('open', () => console.log(`[${name}] open`)) ws.on('error', (e) => { console.error(`[${name}] error`, e.message); reject(e) }) ws.on('close', (code) => console.log(`[${name}] close`, code)) }) } function waitFor(condFn, timeout = 5000) { const start = Date.now() return new Promise((resolve, reject) => { const check = () => { if (condFn()) return resolve(undefined) if (Date.now() - start > timeout) return reject(new Error('Timeout waiting')) setTimeout(check, 50) } check() }) } ;(async () => { const p1 = await connect('p1') await waitFor(() => p1.messages.some(m => m.type === 'game_state')) const state = p1.messages.find(m => m.type === 'game_state') console.log('Game state', { end: state.end, turn_team: state.turn_team, stones_remaining: state.stones_remaining, scoreboard: state.scoreboard, }) const turn = state.turn_team // DRAW_VEL + broom at house center keeps stone in play throwFor(p1.ws, turn, 0.0, 38.5, DRAW_VEL, 0, 1.0) await waitFor(() => p1.messages.some(m => m.type === 'trajectories'), 15000) await waitFor(() => p1.messages.slice(-1)[0]?.type === 'game_state', 15000) const final = p1.messages.slice(-1)[0] console.log('Final state after throw:', { type: final.type, turn_team: final.turn_team, stones: final.stones, stones_remaining: final.stones_remaining, }) p1.ws.close() process.exit(0) })().catch(err => { console.error(err) process.exit(1) })