File size: 20,996 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
 * Multiplayer game rooms for EcoClean 3D — v2 with game modes + new levels.
 */

import type { WebSocket } from "ws";
import { logger } from "../lib/logger.js";

export type Level = "park" | "beach" | "city" | "nightcity" | "arctic" | "jungle";
export type TrashType = "plastic" | "metal" | "organic" | "paper";
export type Difficulty = "easy" | "normal" | "hard";
export type PowerUpKind = "magnet" | "speed" | "bonus";
export type GameMode = "speedrun" | "percent100" | "powerup";

export const WORLD_HALF = 28;
export const COLLECT_RADIUS_SQ = 4.5;
export const POSITION_BROADCAST_HZ = 12;
export const END_HOLD_SEC = 8;
export const BASE_TRASH_POINTS = 10;
export const COMBO_WINDOW_MS = 4000;
export const COMBO_MAX = 5;
export const EMOTE_COOLDOWN_MS = 1500;
export const POWERUP_LIFETIME_SEC = 25;
export const POWERUP_EFFECT_SEC = 8;
export const POWERUP_BONUS_POINTS = 50;
export const POWERUP_PICKUP_RADIUS_SQ = 4;

export const DIFFICULTY_CONFIG: Record<
  Difficulty,
  { roundDuration: number; trashCount: number; winThreshold: number; label: string }
> = {
  easy:   { roundDuration: 180, trashCount: 24, winThreshold: 0.6, label: "Easy" },
  normal: { roundDuration: 120, trashCount: 32, winThreshold: 0.8, label: "Normal" },
  hard:   { roundDuration: 90,  trashCount: 44, winThreshold: 0.9, label: "Hard" },
};

/** Per-mode overrides applied on top of difficulty settings. */
export const MODE_CONFIG: Record<GameMode, {
  winThresholdOverride: number | null;
  roundDurationScale: number;
  trashCountScale: number;
  powerUpSpawnIntervalSec: number;
  maxActivePowerUps: number;
  label: string;
}> = {
  speedrun: {
    winThresholdOverride: null,  // uses difficulty threshold
    roundDurationScale: 0.7,     // 70% of normal time = hectic
    trashCountScale: 1.3,
    powerUpSpawnIntervalSec: 22,
    maxActivePowerUps: 3,
    label: "Speedrun",
  },
  percent100: {
    winThresholdOverride: 1.0,   // must collect EVERYTHING
    roundDurationScale: 2.0,     // generous time
    trashCountScale: 0.8,
    powerUpSpawnIntervalSec: 18,
    maxActivePowerUps: 4,
    label: "100% Clean",
  },
  powerup: {
    winThresholdOverride: null,
    roundDurationScale: 1.0,
    trashCountScale: 1.0,
    powerUpSpawnIntervalSec: 6,  // power-ups rain down
    maxActivePowerUps: 8,
    label: "Power-Up Party",
  },
};

export const VALID_LEVELS: Level[] = ["park", "beach", "city", "nightcity", "arctic", "jungle"];
export const VALID_DIFFICULTIES: Difficulty[] = ["easy", "normal", "hard"];
export const VALID_GAME_MODES: GameMode[] = ["speedrun", "percent100", "powerup"];

export const OWNER_PASSWORD = "Slayers";

/** Public room code from level + mode. */
export function publicCode(level: Level, mode: GameMode): string {
  const lv: Record<Level, string> = {
    park: "PK", beach: "BC", city: "CT", nightcity: "NK", arctic: "AC", jungle: "JG",
  };
  const md: Record<GameMode, string> = { speedrun: "SR", percent100: "PC", powerup: "PU" };
  return `${lv[level]}-${md[mode]}`;
}

const RESERVED = new Set<string>(
  VALID_LEVELS.flatMap(l => VALID_GAME_MODES.map(m => publicCode(l, m)))
);

function generateCode(): string {
  const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
  let s = "";
  for (let i = 0; i < 4; i++) s += alphabet[Math.floor(Math.random() * alphabet.length)];
  return s;
}

export type EmoteKind = "wave" | "thumbsup" | "recycle" | "smile" | "cheer";
const VALID_EMOTES: EmoteKind[] = ["wave", "thumbsup", "recycle", "smile", "cheer"];

export type ClientToServer =
  | { type: "join"; level: Level; name: string; color: string }
  | { type: "move"; x: number; z: number; rot: number }
  | { type: "collect"; trashId: string }
  | { type: "collectPowerUp"; powerUpId: string }
  | { type: "emote"; kind: EmoteKind }
  | { type: "ping"; t: number }
  | { type: "admin.endRound"; password: string; result: "won" | "lost" }
  | { type: "admin.spawnTrash"; password: string; count: number }
  | { type: "admin.spawnPowerUp"; password: string; kind: PowerUpKind }
  | { type: "admin.kick"; password: string; playerId: string }
  | { type: "admin.setTimer"; password: string; seconds: number }
  | { type: "admin.identify"; password: string };

export type ServerToClient =
  | {
      type: "init";
      selfId: string;
      level: Level;
      difficulty: Difficulty;
      gameMode: GameMode;
      code: string;
      isPublic: boolean;
      roundId: number;
      timeLeft: number;
      roundDuration: number;
      winThreshold: number;
      status: "playing" | "ended";
      players: SerializedPlayer[];
      trash: SerializedTrash[];
      powerUps: SerializedPowerUp[];
      mvpId: string | null;
    }
  | { type: "playerJoined"; player: SerializedPlayer }
  | { type: "playerLeft"; id: string }
  | { type: "positions"; players: { id: string; x: number; z: number; rot: number }[] }
  | {
      type: "trashCollected";
      trashId: string; trashType: TrashType; byId: string; byName: string;
      score: number; pointsGained: number; combo: number; comboExpiresInMs: number; totalCollected: number;
    }
  | { type: "powerUpSpawned"; powerUp: SerializedPowerUp }
  | {
      type: "powerUpCollected";
      powerUpId: string; kind: PowerUpKind; byId: string; byName: string;
      score: number; effectMs: number; bonusPoints: number;
    }
  | { type: "powerUpExpired"; powerUpId: string }
  | {
      type: "roundEnd";
      result: "won" | "lost"; cleanedPct: number;
      scores: { id: string; name: string; color: string; score: number }[];
      mvpId: string | null; restartIn: number;
    }
  | {
      type: "roundStart";
      roundId: number; trash: SerializedTrash[]; powerUps: SerializedPowerUp[];
      timeLeft: number; roundDuration: number; mvpId: string | null; gameMode: GameMode;
    }
  | { type: "emote"; playerId: string; kind: EmoteKind }
  | { type: "kicked"; reason: string }
  | { type: "adminAck"; ok: boolean; message: string }
  | { type: "pong"; t: number }
  | { type: "error"; message: string };

interface Player {
  id: string; name: string; color: string;
  x: number; z: number; rot: number;
  score: number; combo: number; lastCollectAt: number; lastEmoteAt: number;
  socket: WebSocket; alive: boolean;
}

interface TrashItem {
  id: string; type: TrashType; x: number; z: number; collected: boolean;
}

interface PowerUp {
  id: string; kind: PowerUpKind; x: number; z: number; expiresAt: number; collected: boolean;
}

interface SerializedPlayer {
  id: string; name: string; color: string; x: number; z: number; rot: number; score: number;
}
interface SerializedTrash { id: string; type: TrashType; x: number; z: number; }
interface SerializedPowerUp { id: string; kind: PowerUpKind; x: number; z: number; }

const PLAYER_COLORS = [
  "#ff7043","#42a5f5","#ab47bc","#ffca28","#26a69a",
  "#ec407a","#7e57c2","#5c6bc0","#66bb6a","#8d6e63",
];

function pickTrashType(level: Level): TrashType {
  const r = Math.random();
  if (level === "beach") {
    if (r < 0.55) return "plastic"; if (r < 0.75) return "metal"; if (r < 0.9) return "paper"; return "organic";
  }
  if (level === "city" || level === "nightcity") {
    if (r < 0.4) return "plastic"; if (r < 0.65) return "paper"; if (r < 0.85) return "metal"; return "organic";
  }
  if (level === "arctic") {
    if (r < 0.5) return "plastic"; if (r < 0.75) return "metal"; if (r < 0.9) return "paper"; return "organic";
  }
  if (level === "jungle") {
    if (r < 0.5) return "organic"; if (r < 0.75) return "plastic"; if (r < 0.9) return "paper"; return "metal";
  }
  // park
  if (r < 0.4) return "plastic"; if (r < 0.6) return "organic"; if (r < 0.8) return "paper"; return "metal";
}

function randomTrashPos(): { x: number; z: number } {
  for (let a = 0; a < 10; a++) {
    const x = (Math.random() * 2 - 1) * WORLD_HALF;
    const z = (Math.random() * 2 - 1) * WORLD_HALF;
    if (Math.abs(x) > 2 || Math.abs(z) > 2) return { x, z };
  }
  return { x: WORLD_HALF * Math.random(), z: WORLD_HALF * Math.random() };
}

function generateTrash(level: Level, count: number): TrashItem[] {
  const items: TrashItem[] = [];
  for (let i = 0; i < count; i++) {
    const { x, z } = randomTrashPos();
    items.push({
      id: `t-${Date.now().toString(36)}-${i}-${Math.random().toString(36).slice(2,6)}`,
      type: pickTrashType(level), x, z, collected: false,
    });
  }
  return items;
}

const POWERUP_KINDS: PowerUpKind[] = ["magnet", "speed", "bonus"];
function pickPowerUpKind(): PowerUpKind { return POWERUP_KINDS[Math.floor(Math.random() * POWERUP_KINDS.length)]; }
let nextPowerUpSeq = 0;
function makePowerUp(kind?: PowerUpKind, lifetimeSec = POWERUP_LIFETIME_SEC): PowerUp {
  const { x, z } = randomTrashPos();
  return {
    id: `pu-${Date.now().toString(36)}-${nextPowerUpSeq++}`,
    kind: kind ?? pickPowerUpKind(), x, z,
    expiresAt: Date.now() + lifetimeSec * 1000, collected: false,
  };
}

function serializePlayer(p: Player): SerializedPlayer {
  return { id: p.id, name: p.name, color: p.color, x: p.x, z: p.z, rot: p.rot, score: p.score };
}
function serializeTrash(t: TrashItem): SerializedTrash { return { id: t.id, type: t.type, x: t.x, z: t.z }; }
function serializePowerUp(p: PowerUp): SerializedPowerUp { return { id: p.id, kind: p.kind, x: p.x, z: p.z }; }

function send(socket: WebSocket, msg: ServerToClient) {
  if (socket.readyState === socket.OPEN) socket.send(JSON.stringify(msg));
}

class Room {
  level: Level;
  difficulty: Difficulty;
  gameMode: GameMode;
  code: string;
  isPublic: boolean;
  players = new Map<string, Player>();
  trash: TrashItem[] = [];
  powerUps: PowerUp[] = [];
  status: "playing" | "ended" = "playing";
  result: "won" | "lost" | null = null;
  timeLeft: number;
  roundDuration: number;
  trashCount: number;
  winThreshold: number;
  powerUpSpawnIntervalSec: number;
  maxActivePowerUps: number;
  roundId = 1;
  endHold = 0;
  usedColors = new Set<string>();
  mvpId: string | null = null;
  nextPowerUpAt: number;

  constructor(level: Level, code: string, isPublic: boolean, difficulty: Difficulty, gameMode: GameMode = "speedrun") {
    this.level = level;
    this.code = code;
    this.isPublic = isPublic;
    this.difficulty = difficulty;
    this.gameMode = gameMode;
    const dc = DIFFICULTY_CONFIG[difficulty];
    const mc = MODE_CONFIG[gameMode];
    this.roundDuration = Math.round(dc.roundDuration * mc.roundDurationScale);
    this.trashCount = Math.max(8, Math.round(dc.trashCount * mc.trashCountScale));
    this.winThreshold = mc.winThresholdOverride ?? dc.winThreshold;
    this.powerUpSpawnIntervalSec = mc.powerUpSpawnIntervalSec;
    this.maxActivePowerUps = mc.maxActivePowerUps;
    this.timeLeft = this.roundDuration;
    this.trash = generateTrash(level, this.trashCount);
    this.nextPowerUpAt = Date.now() + 8000;
  }

  pickColor(): string {
    for (const c of PLAYER_COLORS) if (!this.usedColors.has(c)) { this.usedColors.add(c); return c; }
    return `hsl(${Math.floor(Math.random() * 360)}, 65%, 55%)`;
  }
  freeColor(c: string) { this.usedColors.delete(c); }

  broadcast(msg: ServerToClient, exceptId?: string) {
    for (const p of this.players.values()) {
      if (p.id === exceptId) continue;
      send(p.socket, msg);
    }
  }

  addPlayer(player: Player) {
    this.players.set(player.id, player);
    send(player.socket, {
      type: "init",
      selfId: player.id, level: this.level, difficulty: this.difficulty, gameMode: this.gameMode,
      code: this.code, isPublic: this.isPublic, roundId: this.roundId,
      timeLeft: Math.ceil(this.timeLeft), roundDuration: this.roundDuration,
      winThreshold: this.winThreshold, status: this.status,
      players: Array.from(this.players.values()).map(serializePlayer),
      trash: this.trash.filter(t => !t.collected).map(serializeTrash),
      powerUps: this.powerUps.filter(p => !p.collected).map(serializePowerUp),
      mvpId: this.mvpId,
    });
    this.broadcast({ type: "playerJoined", player: serializePlayer(player) }, player.id);
    logger.info({ level: this.level, difficulty: this.difficulty, gameMode: this.gameMode, playerId: player.id, total: this.players.size }, "Player joined room");
  }

  removePlayer(id: string) {
    const p = this.players.get(id); if (!p) return;
    this.players.delete(id); this.freeColor(p.color);
    this.broadcast({ type: "playerLeft", id });
    logger.info({ level: this.level, playerId: id, total: this.players.size }, "Player left room");
  }

  handleMove(id: string, x: number, z: number, rot: number) {
    const p = this.players.get(id); if (!p) return;
    p.x = Math.max(-WORLD_HALF, Math.min(WORLD_HALF, x));
    p.z = Math.max(-WORLD_HALF, Math.min(WORLD_HALF, z));
    p.rot = rot;
  }

  handleCollect(id: string, trashId: string) {
    if (this.status !== "playing") return;
    const p = this.players.get(id); if (!p) return;
    const item = this.trash.find(t => t.id === trashId);
    if (!item || item.collected) return;
    const dx = item.x - p.x; const dz = item.z - p.z;
    if (dx * dx + dz * dz > COLLECT_RADIUS_SQ * 4) return;
    item.collected = true;
    const now = Date.now();
    p.combo = (now - p.lastCollectAt <= COMBO_WINDOW_MS) ? Math.min(COMBO_MAX, p.combo + 1) : 1;
    p.lastCollectAt = now;
    const pointsGained = BASE_TRASH_POINTS * p.combo;
    p.score += pointsGained;
    const totalCollected = this.trash.filter(t => t.collected).length;
    this.broadcast({
      type: "trashCollected", trashId, trashType: item.type, byId: id, byName: p.name,
      score: p.score, pointsGained, combo: p.combo, comboExpiresInMs: COMBO_WINDOW_MS, totalCollected,
    });
    if (totalCollected / this.trash.length >= this.winThreshold) this.endRound("won");
  }

  handleCollectPowerUp(id: string, powerUpId: string) {
    if (this.status !== "playing") return;
    const p = this.players.get(id); if (!p) return;
    const pu = this.powerUps.find(x => x.id === powerUpId);
    if (!pu || pu.collected) return;
    const dx = pu.x - p.x; const dz = pu.z - p.z;
    if (dx * dx + dz * dz > POWERUP_PICKUP_RADIUS_SQ * 4) return;
    pu.collected = true;
    let bonusPoints = 0;
    if (pu.kind === "bonus") { bonusPoints = POWERUP_BONUS_POINTS; p.score += bonusPoints; }
    this.broadcast({
      type: "powerUpCollected", powerUpId, kind: pu.kind, byId: id, byName: p.name,
      score: p.score, effectMs: pu.kind === "bonus" ? 0 : POWERUP_EFFECT_SEC * 1000, bonusPoints,
    });
  }

  handleEmote(id: string, kind: EmoteKind) {
    const p = this.players.get(id); if (!p) return;
    if (!VALID_EMOTES.includes(kind)) return;
    const now = Date.now();
    if (now - p.lastEmoteAt < EMOTE_COOLDOWN_MS) return;
    p.lastEmoteAt = now;
    this.broadcast({ type: "emote", playerId: id, kind });
  }

  endRound(result: "won" | "lost") {
    if (this.status === "ended") return;
    this.status = "ended"; this.result = result; this.endHold = END_HOLD_SEC;
    const cleaned = this.trash.filter(t => t.collected).length;
    const cleanedPct = Math.round((cleaned / this.trash.length) * 100);
    const scores = Array.from(this.players.values())
      .map(p => ({ id: p.id, name: p.name, color: p.color, score: p.score }))
      .sort((a, b) => b.score - a.score);
    this.mvpId = scores[0] && scores[0].score > 0 ? scores[0].id : null;
    this.broadcast({ type: "roundEnd", result, cleanedPct, scores, mvpId: this.mvpId, restartIn: END_HOLD_SEC });
    logger.info({ level: this.level, gameMode: this.gameMode, result, cleanedPct, mvpId: this.mvpId }, "Round ended");
  }

  startNewRound() {
    this.roundId += 1; this.status = "playing"; this.result = null;
    this.timeLeft = this.roundDuration;
    this.trash = generateTrash(this.level, this.trashCount);
    this.powerUps = []; this.nextPowerUpAt = Date.now() + 8000;
    for (const p of this.players.values()) { p.score = 0; p.combo = 0; p.lastCollectAt = 0; }
    this.broadcast({
      type: "roundStart", roundId: this.roundId,
      trash: this.trash.map(serializeTrash), powerUps: [],
      timeLeft: this.timeLeft, roundDuration: this.roundDuration,
      mvpId: this.mvpId, gameMode: this.gameMode,
    });
    logger.info({ level: this.level, difficulty: this.difficulty, gameMode: this.gameMode, roundId: this.roundId }, "Round started");
  }

  tickPowerUps() {
    if (this.status !== "playing") return;
    const now = Date.now();
    for (const pu of this.powerUps) {
      if (!pu.collected && now > pu.expiresAt) {
        pu.collected = true;
        this.broadcast({ type: "powerUpExpired", powerUpId: pu.id });
      }
    }
    this.powerUps = this.powerUps.filter(pu => !pu.collected);
    if (this.players.size > 0 && now >= this.nextPowerUpAt && this.powerUps.length < this.maxActivePowerUps) {
      const pu = makePowerUp();
      this.powerUps.push(pu);
      this.broadcast({ type: "powerUpSpawned", powerUp: serializePowerUp(pu) });
      this.nextPowerUpAt = now + this.powerUpSpawnIntervalSec * 1000;
    }
  }

  tick(dt: number) {
    if (this.status === "playing") {
      this.timeLeft -= dt;
      if (this.timeLeft <= 0) { this.timeLeft = 0; this.endRound("lost"); }
      this.tickPowerUps();
    } else {
      this.endHold -= dt;
      if (this.endHold <= 0) this.startNewRound();
    }
  }

  adminEndRound(result: "won" | "lost") { if (this.status !== "playing") return; this.endRound(result); }

  adminSpawnTrash(count: number) {
    const safeCount = Math.max(1, Math.min(50, Math.floor(count)));
    const newItems = generateTrash(this.level, safeCount);
    this.trash.push(...newItems);
    this.trashCount = this.trash.length;
    for (const player of this.players.values()) {
      send(player.socket, {
        type: "init", selfId: player.id, level: this.level, difficulty: this.difficulty, gameMode: this.gameMode,
        code: this.code, isPublic: this.isPublic, roundId: this.roundId,
        timeLeft: Math.ceil(this.timeLeft), roundDuration: this.roundDuration,
        winThreshold: this.winThreshold, status: this.status,
        players: Array.from(this.players.values()).map(serializePlayer),
        trash: this.trash.filter(t => !t.collected).map(serializeTrash),
        powerUps: this.powerUps.filter(p => !p.collected).map(serializePowerUp),
        mvpId: this.mvpId,
      });
    }
  }

  adminSpawnPowerUp(kind: PowerUpKind) {
    if (this.status !== "playing") return;
    const pu = makePowerUp(kind);
    this.powerUps.push(pu);
    this.broadcast({ type: "powerUpSpawned", powerUp: serializePowerUp(pu) });
  }

  adminKick(playerId: string): boolean {
    const p = this.players.get(playerId); if (!p) return false;
    send(p.socket, { type: "kicked", reason: "Removed by an owner." });
    try { p.socket.close(); } catch { /* ignore */ }
    return true;
  }

  adminSetTimer(seconds: number) {
    if (this.status !== "playing") return;
    this.timeLeft = Math.max(5, Math.min(600, Math.floor(seconds)));
  }

  broadcastPositions() {
    if (this.players.size === 0) return;
    const players = Array.from(this.players.values()).map(p => ({
      id: p.id, x: +p.x.toFixed(2), z: +p.z.toFixed(2), rot: +p.rot.toFixed(2),
    }));
    this.broadcast({ type: "positions", players });
  }
}

class RoomManager {
  rooms = new Map<string, Room>();
  positionTimer: NodeJS.Timeout | null = null;
  tickTimer: NodeJS.Timeout | null = null;
  lastTick = Date.now();

  ensurePublic(level: Level, gameMode: GameMode): Room {
    const code = publicCode(level, gameMode);
    let room = this.rooms.get(code);
    if (!room) {
      room = new Room(level, code, true, "normal", gameMode);
      this.rooms.set(code, room);
      logger.info({ code, level, gameMode }, "Created public room");
    }
    return room;
  }

  joinByCode(code: string): Room | null {
    return this.rooms.get(code.toUpperCase()) ?? null;
  }

  createPrivate(level: Level, difficulty: Difficulty, gameMode: GameMode): Room {
    let code: string;
    do { code = generateCode(); } while (this.rooms.has(code) || RESERVED.has(code));
    const room = new Room(level, code, false, difficulty, gameMode);
    this.rooms.set(code, room);
    logger.info({ code, level, difficulty, gameMode }, "Created private room");
    return room;
  }

  disposeIfEmpty(room: Room): void {
    if (room.isPublic) return;
    if (room.players.size > 0) return;
    this.rooms.delete(room.code);
    logger.info({ code: room.code }, "Destroyed empty private room");
  }

  start() {
    if (this.tickTimer) return;
    this.lastTick = Date.now();
    this.tickTimer = setInterval(() => {
      const now = Date.now(); const dt = (now - this.lastTick) / 1000; this.lastTick = now;
      for (const room of this.rooms.values()) room.tick(dt);
    }, 1000);
    this.positionTimer = setInterval(() => {
      for (const room of this.rooms.values()) room.broadcastPositions();
    }, Math.round(1000 / POSITION_BROADCAST_HZ));
  }
}

export const roomManager = new RoomManager();
let nextPlayerId = 1;
export function makePlayerId(): string { return `p${nextPlayerId++}`; }