laatste versie

This commit is contained in:
martijn.diersmann@pm.me
2026-09-26 23:25:04 +02:00
parent 32c7696bda
commit 4bdac1e6e8
9 changed files with 267 additions and 84 deletions
+30 -5
View File
@@ -3,6 +3,7 @@
* Turbo Race - HTTP + WebSocket server
* Serveert de statische frontend en draait de spelengine.
* Host-eindpunt: verbind met ?host=<ADMIN_TOKEN> en druk op "s".
* De host is toeschouwer: rijdt NIET zelf mee, maar volgt de koplopers live.
*/
const path = require('path');
const http = require('http');
@@ -70,6 +71,20 @@ wss.on('connection', (ws) => {
try { msg = JSON.parse(buf.toString()); } catch { return; }
switch (msg.type) {
case 'join': {
const isHost = !!(msg.hostToken && ADMIN_TOKEN && String(msg.hostToken) === String(ADMIN_TOKEN));
if (isHost) {
// Host = toeschouwer: neemt geen spelerslot in, toont de koplopers.
ws.isHost = true;
send(ws, {
type: 'joined',
host: true,
spectator: true,
self: { name: 'Host', color: '#ffd93d' },
snapshot: game.snapshot(),
track: game.track,
});
break;
}
let player;
try {
player = game.addPlayer(msg.name, msg.color, msg.hostToken, ADMIN_TOKEN);
@@ -81,6 +96,7 @@ wss.on('connection', (ws) => {
type: 'joined',
id: player.id,
host: player.host,
spectator: false,
self: { name: player.name, color: player.color },
snapshot: game.snapshot(),
track: game.track,
@@ -92,20 +108,29 @@ wss.on('connection', (ws) => {
if (!id) break;
const p = game.players.get(id);
if (!p) break;
p.steer = Math.max(-1, Math.min(1, Number(msg.steer) || 0));
if (typeof msg.x === 'number') {
p.followX = Math.max(12, Math.min(408, Number(msg.x))); // vinger volgen
p.steer = 0;
} else {
p.steer = Math.max(-1, Math.min(1, Number(msg.steer) || 0));
p.followX = null; // toetsenbord
}
break;
}
case 'host_start': {
const id = sockToId.get(ws);
const p = id && game.players.get(id);
if (!p || !p.host) break;
const allowed = ws.isHost || (() => {
const id = sockToId.get(ws);
const p = id && game.players.get(id);
return !!(p && p.host);
})();
if (!allowed) break;
if (game.hostStart()) broadcast({ type: 'phase', phase: 'countdown', hostStart: true });
break;
}
case 'ready_again': {
const id = sockToId.get(ws);
const p = id && game.players.get(id);
if (!p) break;
if (!ws.isHost && !p) break;
if (game.readyAgain()) broadcast({ type: 'phase', phase: 'lobby', reset: true });
break;
}