sistema de verguenza persistente
This commit is contained in:
@@ -365,11 +365,16 @@ adminRouter.post("/admin/shuffle-players", async (req: Request, res: Response) =
|
|||||||
// Collect players with their full info
|
// Collect players with their full info
|
||||||
if (roomState.players && roomState.players.length > 0) {
|
if (roomState.players && roomState.players.length > 0) {
|
||||||
roomState.players.forEach((player: any) => {
|
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({
|
allPlayers.push({
|
||||||
uuid: player.uuid || player.sessionId, // Use actual UUID if available
|
uuid,
|
||||||
name: player.name,
|
name: player.name,
|
||||||
color: player.color,
|
color: player.color,
|
||||||
sessionId: player.sessionId
|
sessionId: player.sessionId,
|
||||||
|
shameTokens: shame
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,6 +229,12 @@ export class GameRoom extends Room<GameState> {
|
|||||||
const p2 = this.state.p2Id ? this.state.players.get(this.state.p2Id) : undefined;
|
const p2 = this.state.p2Id ? this.state.players.get(this.state.p2Id) : undefined;
|
||||||
if (p2) {
|
if (p2) {
|
||||||
p2.shameTokens += 1;
|
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
|
// Notify dashboard of token change
|
||||||
broadcastDashboardUpdate();
|
broadcastDashboardUpdate();
|
||||||
}
|
}
|
||||||
@@ -320,11 +326,17 @@ export class GameRoom extends Room<GameState> {
|
|||||||
const playerColor = (options.playerColor && typeof options.playerColor === 'string') ? options.playerColor : "#667eea";
|
const playerColor = (options.playerColor && typeof options.playerColor === 'string') ? options.playerColor : "#667eea";
|
||||||
|
|
||||||
const player = this.state.addPlayer(client.sessionId, playerName);
|
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);
|
const p = this.state.players.get(client.sessionId);
|
||||||
if (p) {
|
if (p) {
|
||||||
p.color = playerColor;
|
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", {
|
client.send("playerInfo", {
|
||||||
@@ -781,6 +793,13 @@ export class GameRoom extends Room<GameState> {
|
|||||||
player.color = playerColor;
|
player.color = playerColor;
|
||||||
(player as any).uuid = uuid;
|
(player as any).uuid = uuid;
|
||||||
this.sessionToUuid.set(client.sessionId, 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
|
// Set role IDs
|
||||||
if (expectedRole === 'P1') {
|
if (expectedRole === 'P1') {
|
||||||
|
|||||||
@@ -55,10 +55,15 @@ export class GameState extends Schema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
startGame(): void {
|
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.gameStatus = GameStatus.PLAYING;
|
||||||
this.startTime = Date.now();
|
this.startTime = Date.now();
|
||||||
this.timeRemaining = 0;
|
this.timeRemaining = 0;
|
||||||
this.resetAllPlayers();
|
this.resetAllPlayers();
|
||||||
|
this.players.forEach((p, key) => { p.shameTokens = shameSnapshot[key] || 0; });
|
||||||
// Initialize tokens by role
|
// Initialize tokens by role
|
||||||
if (this.p1Id) {
|
if (this.p1Id) {
|
||||||
const p1 = this.players.get(this.p1Id);
|
const p1 = this.players.get(this.p1Id);
|
||||||
@@ -88,6 +93,10 @@ export class GameState extends Schema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
restartGame(): void {
|
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.gameStatus = GameStatus.WAITING;
|
||||||
this.timeRemaining = 0;
|
this.timeRemaining = 0;
|
||||||
this.winner = "";
|
this.winner = "";
|
||||||
@@ -99,6 +108,9 @@ export class GameState extends Schema {
|
|||||||
this.requestPavo = this.requestElote = 0;
|
this.requestPavo = this.requestElote = 0;
|
||||||
this.offerActive = false;
|
this.offerActive = false;
|
||||||
this.resetAllPlayers();
|
this.resetAllPlayers();
|
||||||
|
|
||||||
|
// Restore preserved sticky values
|
||||||
|
this.players.forEach((p, key) => { p.shameTokens = shameSnapshot[key] || 0; });
|
||||||
}
|
}
|
||||||
|
|
||||||
private resetAllPlayers(): void {
|
private resetAllPlayers(): void {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ export class NameManager {
|
|||||||
private static instance: NameManager;
|
private static instance: NameManager;
|
||||||
private uuidToName: Map<string, string> = new Map();
|
private uuidToName: Map<string, string> = new Map();
|
||||||
private uuidToColor: Map<string, string> = new Map();
|
private uuidToColor: Map<string, string> = new Map();
|
||||||
|
private uuidToShame: Map<string, number> = new Map();
|
||||||
|
|
||||||
// For shuffle functionality
|
// For shuffle functionality
|
||||||
private roomAssignments: Map<string, { roomId: string; role: 'P1' | 'P2' }> = new Map();
|
private roomAssignments: Map<string, { roomId: string; role: 'P1' | 'P2' }> = new Map();
|
||||||
@@ -70,6 +71,16 @@ export class NameManager {
|
|||||||
return Array.from(this.uuidToName.values());
|
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)
|
// Current game room assignment (for reconnection by UUID)
|
||||||
setCurrentRoom(uuid: string, roomId: string): void {
|
setCurrentRoom(uuid: string, roomId: string): void {
|
||||||
if (!uuid || !roomId) return;
|
if (!uuid || !roomId) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user