makefuture / mp-relay / fake-peer.mjs
395 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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);

static mirror of HEAD · about · clone: git clone https://git.makefuture.ro/makefuture.git