File size: 10,885 Bytes
d0a5946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0820aaf
d0a5946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0820aaf
d0a5946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0820aaf
 
d0a5946
 
 
 
 
 
 
 
 
 
0820aaf
d0a5946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0820aaf
 
 
d0a5946
0820aaf
d0a5946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/** Lobby + match WebSocket client (racing state sync). */

import { PeerManager } from './peers.js';

const STATE_HZ = 30;
const STATE_INTERVAL = 1 / STATE_HZ;
const LOBBY_POLL_MS = 1000;
const HEARTBEAT_MS = 3000;

function wsUrl(spaceUrl, mode, lobbyId, username, role = 'play') {
  const base = (spaceUrl || window.location.origin).replace(/\/$/, '');
  const u = new URL(base);
  u.protocol = u.protocol === 'https:' ? 'wss:' : 'ws:';
  u.pathname = `/ws/match/${encodeURIComponent(mode)}/${encodeURIComponent(lobbyId)}`;
  u.search = `user=${encodeURIComponent(username)}&role=${encodeURIComponent(role)}`;
  return u.toString();
}

export class NetClient {
  constructor({ scene, onStatus, onMatchStart, onMatchEnd, onLobbyUpdate, onMatchConnectionLost }) {
    this.scene = scene;
    this.onStatus = onStatus || (() => {});
    this.onMatchStart = onMatchStart || (() => {});
    this.onMatchEnd = onMatchEnd || (() => {});
    this.onLobbyUpdate = onLobbyUpdate || (() => {});
    this.onMatchConnectionLost = onMatchConnectionLost || (() => {});

    this.username = null;
    this.avatarUrl = null;
    this.spaceUrl = null;
    this.playAllowed = true;
    this.host = 'local';
    this.mode = null;
    this.lobbyId = null;
    this.seat = null;
    this.side = null;
    this.matchId = null;
    this.inMatch = false;
    this.ws = null;
    this.peers = new PeerManager(scene);
    this.board = null;
    this._pollTimer = null;
    this._hbTimer = null;
    this._stateAcc = 0;
    this._spawnIndex = 0;
    this.players = [];
    this.spectating = false;
    this.carId = null;
  }

  async initLocal() {
    const cfg = await fetch('/api/config').then((r) => r.json());
    this.spaceUrl = cfg.spaceUrl || '';
    this.username = cfg.username || null;
    this.host = cfg.host || 'local';
    this.playAllowed = cfg.host === 'space' ? false : cfg.playAllowed !== false;
    this.avatarUrl = cfg.avatarUrl || (this.username && !String(this.username).startsWith('guest-')
      ? `https://huggingface.co/avatars/${encodeURIComponent(this.username)}`
      : null);
    if (!this.username) {
      return { ok: false, error: cfg.authError || 'No player identity' };
    }
    if (this.host === 'local' && String(this.username).startsWith('guest-')) {
      return { ok: false, error: 'Local play requires HF_TOKEN (guest ids are spectate-only on Space)' };
    }
    if (this.playAllowed && cfg.hasToken === false && this.host !== 'space') {
      return { ok: false, error: cfg.authError || 'Set HF_TOKEN in .env.local' };
    }
    return {
      ok: true,
      username: this.username,
      avatarUrl: this.avatarUrl,
      spaceUrl: this.spaceUrl || window.location.origin,
      playAllowed: this.playAllowed,
      host: this.host,
    };
  }

  _lobbyBase() {
    return '';
  }

  async _fetchLobbies() {
    const res = await fetch(`${this._lobbyBase()}/api/lobbies`);
    if (!res.ok) throw new Error(`Lobby server ${res.status}`);
    const board = await res.json();
    if (!board?.mvp && !board?.sandbox) throw new Error('Bad lobby payload');
    return board;
  }

  startLobbyPoll(mode) {
    this.mode = mode;
    this.stopLobbyPoll();
    const tick = async () => {
      try {
        const board = await this._fetchLobbies();
        this.board = board;
        this.onLobbyUpdate(board, mode, null);
      } catch (err) {
        this.onStatus(`Lobby poll failed: ${err.message}`);
        this.onLobbyUpdate(null, mode, err.message || String(err));
      }
    };
    tick();
    this._pollTimer = setInterval(tick, LOBBY_POLL_MS);
    this._hbTimer = setInterval(() => {
      if (!this.username) return;
      fetch(`${this._lobbyBase()}/api/lobbies/heartbeat`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ username: this.username }),
      }).catch(() => {});
    }, HEARTBEAT_MS);
  }

  stopLobbyPoll() {
    if (this._pollTimer) clearInterval(this._pollTimer);
    if (this._hbTimer) clearInterval(this._hbTimer);
    this._pollTimer = null;
    this._hbTimer = null;
  }

  async claim(mode, lobbyId, seat) {
    if (!this.playAllowed) throw new Error('Play disabled here — spectate only');
    const res = await fetch(`/api/lobbies/${encodeURIComponent(mode)}/${encodeURIComponent(lobbyId)}/claim`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        username: this.username,
        seat,
        avatarUrl: this.avatarUrl || null,
      }),
    });
    let data;
    try {
      data = await res.json();
    } catch {
      throw new Error(`claim failed (${res.status})`);
    }
    if (!res.ok || !data.ok) throw new Error(data.error || `claim failed (${res.status})`);
    this.mode = mode;
    this.lobbyId = lobbyId;
    this.seat = seat;
    this.side = seatSide(seat);
    this.matchId = data.matchId || null;
    return data;
  }

  async leaveLobby() {
    this.disconnectMatch();
    if (!this.username) return;
    await fetch(`${this._lobbyBase()}/api/lobbies/leave`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ username: this.username }),
    }).catch(() => {});
    this.lobbyId = null;
    this.seat = null;
  }

  connectMatch() {
    if (!this.playAllowed || this.host === 'space') {
      this.onStatus('Play disabled here — spectate only');
      return;
    }
    if (!this.lobbyId || !this.mode) return;
    this._openMatchWs('play');
  }

  connectSpectate(mode, lobbyId) {
    if (!mode || !lobbyId || !this.username) return;
    this.mode = mode;
    this.lobbyId = lobbyId;
    this.seat = null;
    this.side = 'ffa';
    this.spectating = true;
    this._openMatchWs('spectate');
  }

  _openMatchWs(role) {
    if (this.host === 'space' || !this.playAllowed) role = 'spectate';
    this.disconnectMatch(false);
    this.spectating = role === 'spectate';
    const url = wsUrl(this.spaceUrl, this.mode, this.lobbyId, this.username, role);
    this.onStatus(this.spectating ? 'Connecting as spectator…' : 'Connecting to race…');
    const ws = new WebSocket(url);
    this.ws = ws;

    ws.onopen = () => {
      this.onStatus(this.spectating ? 'Waiting for race (spectate)…' : 'Waiting for race start…');
      this.send({ type: 'ready', username: this.username });
    };

    ws.onmessage = (ev) => {
      let msg;
      try {
        msg = JSON.parse(ev.data);
      } catch {
        return;
      }
      this._onMessage(msg);
    };

    ws.onclose = () => {
      if (this.ws !== ws) return;
      const wasInMatch = this.inMatch;
      const wasSpectating = this.spectating;
      this.ws = null;
      this.inMatch = false;
      this.onStatus('Match connection closed');
      if (wasInMatch || wasSpectating) {
        this.onMatchConnectionLost({ wasInMatch, wasSpectating });
      }
    };

    ws.onerror = () => {
      this.onStatus('Match WebSocket error');
    };
  }

  disconnectMatch(clearPeers = true) {
    const ws = this.ws;
    this.ws = null;
    this.inMatch = false;
    this.matchId = null;
    this.spectating = false;
    if (ws) {
      try {
        ws.close();
      } catch {
        // ignore
      }
    }
    if (clearPeers) this.peers.clear();
  }

  send(msg) {
    if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
    if (this.spectating && msg?.type && msg.type !== 'ping' && msg.type !== 'ready') return;
    this.ws.send(JSON.stringify(msg));
  }

  _onMessage(msg) {
    const type = msg.type;
    if (type === 'waiting') {
      this.onStatus('In lobby — waiting for race start…');
      return;
    }
    if (type === 'error') {
      this.onStatus(msg.error || 'Server error');
      if (/seat|seated|rejoin|lobby reset|match did not start/i.test(msg.error || '')) {
        try {
          this.ws?.close();
        } catch {
          // ignore
        }
      }
      return;
    }
    if (type === 'match_start') {
      this.inMatch = true;
      this.matchId = msg.matchId;
      this.players = msg.players || [];
      this.spectating =
        !!msg.spectating || this.spectating || !this.playAllowed || this.host === 'space';
      this.seat = this.spectating ? null : (msg.seat || this.seat);
      this.side = this.spectating ? 'ffa' : seatSide(this.seat);
      const me = (this.players || []).find((p) => p.username === this.username);
      this.carId = this.spectating ? null : (me?.carId || null);
      const hideSelf = this.spectating ? null : this.username;
      this.peers.syncPlayers(this.players, hideSelf);
      this._spawnIndex = this.spectating ? 0 : spawnIndexFor(this.seat, this.mode, this.players);
      this.onMatchStart({
        mode: msg.mode || this.mode,
        seat: this.seat,
        side: this.side,
        players: this.players,
        spawnIndex: this._spawnIndex,
        spectating: this.spectating,
        carId: this.carId,
      });
      this.onStatus(this.spectating ? `Spectating ${this.mode} · ${this.lobbyId}` : `Race live — ${this.mode}`);
      return;
    }
    if (type === 'player_joined' || type === 'player_left') {
      this.players = msg.players || this.players;
      this.peers.syncPlayers(this.players, this.spectating ? null : this.username);
      return;
    }
    if (type === 'state' && msg.from) {
      this.peers.applyState(msg.from, msg);
      return;
    }
    if (type === 'match_end') {
      this.onMatchEnd(msg);
      this.onStatus(`Race over — ${msg.winner || 'finish'}`);
    }
  }

  /** Call each frame while in a race */
  update(delta, car) {
    if (!this.inMatch) return;
    this.peers.update(delta);
    if (this.spectating || !car) return;

    this._stateAcc += delta;
    if (this._stateAcc >= STATE_INTERVAL) {
      this._stateAcc = 0;
      const p = car.position;
      this.send({
        type: 'state',
        pos: [p.x, p.y, p.z],
        rot: [car.yaw, 0, 0],
        speed: car.speed || 0,
        avatarUrl: this.avatarUrl || undefined,
      });
    }
  }
}

function seatSide(seat) {
  if (!seat) return 'ffa';
  const s = String(seat);
  if (s.startsWith('X-')) return 'X';
  if (s.startsWith('Y-')) return 'Y';
  return 'ffa';
}

export function spawnIndexFor(seat, mode, players) {
  if (mode === 'mvp') {
    const m = String(seat).match(/^P-(\d+)$/i);
    if (m) return Math.max(0, Math.min(7, Number(m[1]) - 1));
  }
  // sandbox / fallback: index among seated players
  const names = (players || []).map((p) => p.username).sort();
  const me = (players || []).find((p) => p.seat === seat)?.username;
  const idx = Math.max(0, names.indexOf(me));
  return idx % 8;
}

/** Grid offsets until real track start line exists */
export const SPAWN_OFFSETS = [
  { x: -6, z: 0 },
  { x: -2, z: 0 },
  { x: 2, z: 0 },
  { x: 6, z: 0 },
  { x: -6, z: -6 },
  { x: -2, z: -6 },
  { x: 2, z: -6 },
  { x: 6, z: -6 },
];