sistema de verguenza persistente

This commit is contained in:
2025-08-16 00:23:32 -06:00
parent 63eb9b2c7e
commit f56244aa62
4 changed files with 51 additions and 4 deletions

View File

@@ -365,11 +365,16 @@ adminRouter.post("/admin/shuffle-players", async (req: Request, res: Response) =
// Collect players with their full info
if (roomState.players && roomState.players.length > 0) {
roomState.players.forEach((player: any) => {
const uuid = player.uuid || player.sessionId;
const shame = Number(player.shameTokens || 0);
// Persist sticky shame for this UUID before clearing rooms
try { NameManager.getInstance().setShameTokens(uuid, shame); } catch {}
allPlayers.push({
uuid: player.uuid || player.sessionId, // Use actual UUID if available
uuid,
name: player.name,
color: player.color,
sessionId: player.sessionId
sessionId: player.sessionId,
shameTokens: shame
});
});
}

View File

@@ -229,6 +229,12 @@ export class GameRoom extends Room<GameState> {
const p2 = this.state.p2Id ? this.state.players.get(this.state.p2Id) : undefined;
if (p2) {
p2.shameTokens += 1;
// Persist sticky shame per UUID
try {
const { NameManager } = require("../utils/nameManager");
const u = (p2 as any)?.uuid;
if (u) NameManager.getInstance().setShameTokens(u, p2.shameTokens);
} catch {}
// Notify dashboard of token change
broadcastDashboardUpdate();
}
@@ -320,11 +326,17 @@ export class GameRoom extends Room<GameState> {
const playerColor = (options.playerColor && typeof options.playerColor === 'string') ? options.playerColor : "#667eea";
const player = this.state.addPlayer(client.sessionId, playerName);
// Persist selected color
// Persist selected color and restore sticky values
const p = this.state.players.get(client.sessionId);
if (p) {
p.color = playerColor;
if (uuid) (p as any).uuid = uuid;
if (uuid) {
(p as any).uuid = uuid;
try {
const { NameManager } = require("../utils/nameManager");
p.shameTokens = NameManager.getInstance().getShameTokens(uuid);
} catch {}
}
}
client.send("playerInfo", {
@@ -781,6 +793,13 @@ export class GameRoom extends Room<GameState> {
player.color = playerColor;
(player as any).uuid = uuid;
this.sessionToUuid.set(client.sessionId, uuid);
// Restore sticky shame tokens for this UUID
try {
const { NameManager } = require("../utils/nameManager");
const count = NameManager.getInstance().getShameTokens(uuid);
const p = this.state.players.get(client.sessionId);
if (p) p.shameTokens = count;
} catch {}
// Set role IDs
if (expectedRole === 'P1') {

View File

@@ -55,10 +55,15 @@ export class GameState extends Schema {
}
startGame(): void {
// Preserve sticky values across new game start as well
const shameSnapshot: Record<string, number> = {};
this.players.forEach((p, key) => { shameSnapshot[key] = p.shameTokens; });
this.gameStatus = GameStatus.PLAYING;
this.startTime = Date.now();
this.timeRemaining = 0;
this.resetAllPlayers();
this.players.forEach((p, key) => { p.shameTokens = shameSnapshot[key] || 0; });
// Initialize tokens by role
if (this.p1Id) {
const p1 = this.players.get(this.p1Id);
@@ -88,6 +93,10 @@ export class GameState extends Schema {
}
restartGame(): void {
// Preserve sticky values (e.g., shameTokens) across restarts
const shameSnapshot: Record<string, number> = {};
this.players.forEach((p, key) => { shameSnapshot[key] = p.shameTokens; });
this.gameStatus = GameStatus.WAITING;
this.timeRemaining = 0;
this.winner = "";
@@ -99,6 +108,9 @@ export class GameState extends Schema {
this.requestPavo = this.requestElote = 0;
this.offerActive = false;
this.resetAllPlayers();
// Restore preserved sticky values
this.players.forEach((p, key) => { p.shameTokens = shameSnapshot[key] || 0; });
}
private resetAllPlayers(): void {

View File

@@ -2,6 +2,7 @@ export class NameManager {
private static instance: NameManager;
private uuidToName: Map<string, string> = new Map();
private uuidToColor: Map<string, string> = new Map();
private uuidToShame: Map<string, number> = new Map();
// For shuffle functionality
private roomAssignments: Map<string, { roomId: string; role: 'P1' | 'P2' }> = new Map();
@@ -70,6 +71,16 @@ export class NameManager {
return Array.from(this.uuidToName.values());
}
// Sticky shame tokens per UUID
setShameTokens(uuid: string, count: number): void {
const n = Math.max(0, Math.floor(count || 0));
this.uuidToShame.set(uuid, n);
}
getShameTokens(uuid: string): number {
return this.uuidToShame.get(uuid) || 0;
}
// Current game room assignment (for reconnection by UUID)
setCurrentRoom(uuid: string, roomId: string): void {
if (!uuid || !roomId) return;