// Scripted second player for manual curve testing. Not shipped anywhere.
// node fake-peer.mjs guest <CODE> [name] [seconds] join lobby curve-p-CODE
// node fake-peer.mjs host <CODE> [name] [seconds] ...and stream a scripted ball
// node fake-peer.mjs open <_> [name] [seconds] join the public table curve-open
import WebSocket from 'ws';
const [, , mode, code, botName = 'BOT', life = '25'] = process.argv;
const room = (mode === 'open' || mode === 'openhost') ? 'curve-open' : 'curve-p-' + code;
const ws = new WebSocket('ws://localhost:8090');
let id = null, hostId = null, states = 0, t0 = Date.now();
ws.on('open', () => {
ws.send(JSON.stringify({ t: 'join', map: room, name: botName, color: '#f0f' }));
});
ws.on('message', (raw) => {
const m = JSON.parse(String(raw));
if (m.t === 'welcome') id = m.id;
if (m.t === 'session_state') { hostId = m.hostId; console.log('host?', hostId === id); }
if (m.t === 'snapshot') console.log('snapshot others:', m.players.length);
if (m.t === 'joined') console.log('peer joined:', m.name);
if (m.t === 'departed') console.log('peer departed');
if (m.t === 'relay' && m.d && m.d.k === 'st') {
states++;
if (states % 20 === 1) console.log('st#' + states, JSON.stringify({ pts: [m.d.pa, m.d.pb], st: m.d.st, bz: m.d.b[2].toFixed(2) }));
}
if (m.t === 'relay' && m.d && m.d.k === 'ev') console.log('event', JSON.stringify(m.d));
});
// paddle wiggle at 14Hz so the relay sees a live opponent
setInterval(() => {
if (ws.readyState !== 1) return;
const t = (Date.now() - t0) / 1000;
ws.send(JSON.stringify({ t: 'update', x: Math.sin(t * 1.3) * 0.5, y: Math.cos(t * 0.9) * 0.3, z: 0, a: 0, s: 0 }));
}, 70);
if (mode === 'host' || mode === 'openhost') {
// Scripted "physics": ball bounces along z, scores a point every 6s.
let pa = 0, pb = 0, st = 'rally';
setInterval(() => {
if (ws.readyState !== 1) return;
const t = (Date.now() - t0) / 1000;
const z = 2.2 + 2.1 * Math.sin(t * 1.8);
ws.send(JSON.stringify({ t: 'relay', d: {
k: 'st', b: [Math.sin(t) * 0.6, Math.cos(t * 1.4) * 0.4, z, 0.5, 0.2, 2.4, 0.3, -0.2],
sp: 3.1, pa, pb, st, sv: 0, fs: 1, rl: (t | 0)
} }));
}, 100);
setInterval(() => {
if (ws.readyState !== 1) return;
pa++;
ws.send(JSON.stringify({ t: 'relay', d: { k: 'ev', pt: 1, w: 1 } }));
console.log('bot scored, now', pa, '-', pb);
}, 6000);
}
setTimeout(() => { console.log('bot exiting; states received:', states); process.exit(0); }, Number(life) * 1000);