colores asignados al azar y simulador de jugadores en el dashboard

This commit is contained in:
2025-08-15 19:09:49 -06:00
parent 310fb3455a
commit 5e42eb7d54
5 changed files with 131 additions and 11 deletions

View File

@@ -2,6 +2,7 @@ import { Request, Response, Router } from "express";
import { matchMaker } from "colyseus";
import { GameRoom } from "./rooms/GameRoom";
import { NameManager } from "./utils/nameManager";
import { getAllowedUuidCount, listAllowedUuids } from "./utils/uuidRegistry";
// SSE connections storage
const sseClients = new Set<Response>();
@@ -432,6 +433,17 @@ adminRouter.post("/admin/shuffle-players", async (req: Request, res: Response) =
}
});
// UUID allowlist endpoint
adminRouter.get("/admin/uuids", async (req: Request, res: Response) => {
try {
const uuids = listAllowedUuids();
res.json({ count: getAllowedUuidCount(), uuids });
} catch (error) {
console.error("[AdminAPI] Error fetching UUIDs:", error);
res.status(500).json({ error: "Failed to fetch UUIDs" });
}
});
// SSE endpoint for real-time dashboard updates
adminRouter.get("/dashboard-stream", (req: Request, res: Response) => {
// Set SSE headers
@@ -558,4 +570,4 @@ function broadcastDashboardUpdate() {
sendDashboardUpdate();
}
export { adminRouter, broadcastDashboardUpdate };
export { adminRouter, broadcastDashboardUpdate };

View File

@@ -6,6 +6,23 @@ import { isUuidAllowed } from "../utils/uuidRegistry";
export class LobbyRoom extends Room<LobbyState> {
private updateInterval?: NodeJS.Timeout;
private sessionToUuid: Map<string, string> = new Map();
// Generate a random dark-ish color (for white backgrounds)
private randomDarkHex(): string {
const h = Math.floor(Math.random() * 360);
const s = 65 + Math.floor(Math.random() * 20); // 65% - 85%
const l = 30 + Math.floor(Math.random() * 10); // 30% - 40%
return this.hslToHex(h, s, l);
}
private hslToHex(h: number, s: number, l: number): string {
s /= 100; l /= 100;
const k = (n: number) => (n + h / 30) % 12;
const a = s * Math.min(l, 1 - l);
const f = (n: number) => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
const toHex = (x: number) => Math.round(255 * x).toString(16).padStart(2, '0');
return `#${toHex(f(0))}${toHex(f(8))}${toHex(f(4))}`;
}
onCreate(options: any) {
this.setState(new LobbyState());
@@ -80,10 +97,14 @@ export class LobbyRoom extends Room<LobbyState> {
// Add player temporarily to lobby state
const existingName = NameManager.getInstance().getPlayerName(options.uuid);
this.state.addPlayer(client.sessionId, existingName || "");
const existingColor = NameManager.getInstance().getPlayerColor(options.uuid);
if (existingColor) {
const p = this.state.players.get(client.sessionId);
if (p) p.color = existingColor;
const p = this.state.players.get(client.sessionId);
if (p) {
let color = NameManager.getInstance().getPlayerColor(options.uuid);
if (!color) {
color = this.randomDarkHex();
NameManager.getInstance().setPlayerColor(options.uuid, color);
}
p.color = color;
}
// Send welcome first
@@ -106,10 +127,14 @@ export class LobbyRoom extends Room<LobbyState> {
// Check if this UUID already has a name
const existingName = NameManager.getInstance().getPlayerName(options.uuid);
this.state.addPlayer(client.sessionId, existingName || "");
const existingColor = NameManager.getInstance().getPlayerColor(options.uuid);
if (existingColor) {
const p = this.state.players.get(client.sessionId);
if (p) p.color = existingColor;
const p = this.state.players.get(client.sessionId);
if (p) {
let color = NameManager.getInstance().getPlayerColor(options.uuid);
if (!color) {
color = this.randomDarkHex();
NameManager.getInstance().setPlayerColor(options.uuid, color);
}
p.color = color;
}
client.send("welcome", {

View File

@@ -62,3 +62,7 @@ export function isUuidAllowed(uuid: string | undefined | null): boolean {
export function getAllowedUuidCount(): number {
return allowedUuids.size;
}
export function listAllowedUuids(): string[] {
return Array.from(allowedUuids.values());
}