File size: 5,664 Bytes
11355a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * WebSocket handler that wires raw `ws` connections to the room manager.
 */

import { WebSocketServer, type WebSocket } from "ws";
import type { Server as HttpServer, IncomingMessage } from "http";
import { logger } from "../lib/logger.js";
import {
  makePlayerId,
  OWNER_PASSWORD,
  roomManager,
  VALID_DIFFICULTIES,
  VALID_LEVELS,
  VALID_GAME_MODES,
  type ClientToServer,
  type Difficulty,
  type EmoteKind,
  type GameMode,
  type Level,
  type PowerUpKind,
} from "./rooms.js";

const VALID_EMOTES: EmoteKind[] = ["wave", "thumbsup", "recycle", "smile", "cheer"];
function isEmote(x: string): x is EmoteKind { return (VALID_EMOTES as string[]).includes(x); }
function isLevel(x: string): x is Level { return (VALID_LEVELS as string[]).includes(x); }
function isDifficulty(x: string): x is Difficulty { return (VALID_DIFFICULTIES as string[]).includes(x); }
function isGameMode(x: string): x is GameMode { return (VALID_GAME_MODES as string[]).includes(x); }

const VALID_POWERUPS: PowerUpKind[] = ["magnet", "speed", "bonus"];
function isPowerUpKind(x: string): x is PowerUpKind { return (VALID_POWERUPS as string[]).includes(x); }

export function attachMultiplayer(server: HttpServer) {
  const wss = new WebSocketServer({ noServer: true });
  roomManager.start();

  server.on("upgrade", (request: IncomingMessage, socket, head) => {
    const url = request.url ?? "";
    if (!url.startsWith("/api/ws")) { socket.destroy(); return; }
    wss.handleUpgrade(request, socket, head, (ws) => { wss.emit("connection", ws, request); });
  });

  wss.on("connection", (ws: WebSocket, request: IncomingMessage) => {
    const url = new URL(request.url ?? "/", "http://localhost");
    const levelParam = url.searchParams.get("level") ?? "park";
    const nameParam = (url.searchParams.get("name") ?? "Player").slice(0, 20);
    const codeParam = url.searchParams.get("code");
    const diffParam = url.searchParams.get("difficulty") ?? "normal";
    const modeParam = url.searchParams.get("mode") ?? "speedrun";
    const level: Level = isLevel(levelParam) ? levelParam : "park";
    const difficulty: Difficulty = isDifficulty(diffParam) ? diffParam : "normal";
    const gameMode: GameMode = isGameMode(modeParam) ? modeParam : "speedrun";

    let room;
    if (codeParam) {
      const upper = codeParam.toUpperCase();
      if (upper === "NEW") {
        room = roomManager.createPrivate(level, difficulty, gameMode);
      } else {
        const existing = roomManager.joinByCode(upper);
        if (!existing) {
          ws.send(JSON.stringify({ type: "error", message: `No room with code "${upper}". Check the code and try again.` }));
          ws.close(); return;
        }
        room = existing;
      }
    } else {
      room = roomManager.ensurePublic(level, gameMode);
    }

    const playerId = makePlayerId();
    const color = room.pickColor();
    const player = {
      id: playerId,
      name: nameParam || "Player",
      color,
      x: (Math.random() - 0.5) * 4,
      z: (Math.random() - 0.5) * 4,
      rot: 0,
      score: 0,
      combo: 0,
      lastCollectAt: 0,
      lastEmoteAt: 0,
      socket: ws,
      alive: true,
    };
    room.addPlayer(player);

    ws.on("message", (raw) => {
      let msg: ClientToServer;
      try { msg = JSON.parse(raw.toString()); } catch { return; }
      switch (msg.type) {
        case "move":
          if (typeof msg.x === "number" && typeof msg.z === "number" && typeof msg.rot === "number"
            && Number.isFinite(msg.x) && Number.isFinite(msg.z) && Number.isFinite(msg.rot)) {
            room.handleMove(playerId, msg.x, msg.z, msg.rot);
          }
          break;
        case "collect":
          if (typeof msg.trashId === "string") room.handleCollect(playerId, msg.trashId);
          break;
        case "collectPowerUp":
          if (typeof msg.powerUpId === "string") room.handleCollectPowerUp(playerId, msg.powerUpId);
          break;
        case "emote":
          if (typeof msg.kind === "string" && isEmote(msg.kind)) room.handleEmote(playerId, msg.kind);
          break;
        case "ping":
          ws.send(JSON.stringify({ type: "pong", t: msg.t }));
          break;
        case "join": break;
        case "admin.identify":
          ws.send(JSON.stringify(msg.password === OWNER_PASSWORD
            ? { type: "adminAck", ok: true, message: "Owner mode unlocked." }
            : { type: "adminAck", ok: false, message: "Wrong owner password." }
          ));
          break;
        case "admin.endRound":
          if (msg.password === OWNER_PASSWORD) room.adminEndRound(msg.result === "won" ? "won" : "lost");
          break;
        case "admin.spawnTrash":
          if (msg.password === OWNER_PASSWORD && typeof msg.count === "number") room.adminSpawnTrash(msg.count);
          break;
        case "admin.spawnPowerUp":
          if (msg.password === OWNER_PASSWORD && typeof msg.kind === "string" && isPowerUpKind(msg.kind))
            room.adminSpawnPowerUp(msg.kind);
          break;
        case "admin.kick":
          if (msg.password === OWNER_PASSWORD && typeof msg.playerId === "string" && msg.playerId !== playerId)
            room.adminKick(msg.playerId);
          break;
        case "admin.setTimer":
          if (msg.password === OWNER_PASSWORD && typeof msg.seconds === "number") room.adminSetTimer(msg.seconds);
          break;
      }
    });

    ws.on("close", () => { room.removePlayer(playerId); roomManager.disposeIfEmpty(room); });
    ws.on("error", (err: Error) => { logger.warn({ err, playerId }, "WebSocket error"); });
  });

  logger.info("Multiplayer WebSocket server attached at /api/ws");
}