reconexion establecida

This commit is contained in:
2025-08-11 18:36:24 -06:00
parent e5e5ad452a
commit 9a44afc81a
6 changed files with 100 additions and 79 deletions

View File

@@ -21,6 +21,8 @@ export class GameRoom extends Room<GameState> {
onCreate(options: any) {
this.setState(new GameState());
this.state.roomId = this.roomId;
// Expose status via metadata for lobby listing
this.setMetadata({ gameStatus: 'waiting' });
// Variant selection (both players can change)
this.onMessage("setVariant", (client, variant: string) => {
@@ -207,6 +209,11 @@ export class GameRoom extends Room<GameState> {
onJoin(client: Client, options: any) {
console.log(`[GameRoom] ${client.sessionId} joined room ${this.roomId} with name: ${options.playerName}`);
// Prevent new joins if game already started or two players are registered
if (this.state.gameStatus !== GameStatus.WAITING || this.state.players.size >= 2) {
try { client.leave(1000); } catch {}
return;
}
// Use the playerName passed from the lobby - don't generate a new one!
const playerName = options.playerName || "player";
@@ -256,6 +263,13 @@ export class GameRoom extends Room<GameState> {
player.connected = true;
}
// Send player info so client can rehydrate local session state
client.send("playerInfo", {
sessionId: client.sessionId,
name: player?.name || "player",
roomId: this.roomId
});
if (this.state.gameStatus === GameStatus.PAUSED && this.getConnectedPlayersCount() === 2) {
this.state.resumeGame();
}
@@ -274,6 +288,7 @@ export class GameRoom extends Room<GameState> {
private startGame() {
console.log(`[GameRoom] Starting demo game in room ${this.roomId}`);
this.state.startGame();
this.setMetadata({ gameStatus: 'playing' });
// G2: Force offer by default when starting game
if (this.state.currentVariant === 'G2') {
this.state.forcedByP2 = true;
@@ -287,11 +302,13 @@ export class GameRoom extends Room<GameState> {
console.log(`[GameRoom] Pausing game in room ${this.roomId}`);
this.state.pauseGame();
this.broadcast("gamePaused");
this.setMetadata({ gameStatus: 'paused' });
}
private endGame() {
console.log(`[GameRoom] Demo game ended in room ${this.roomId}`);
this.broadcast("gameEnd", {});
this.setMetadata({ gameStatus: 'finished' });
}
private resolveP2Action() {
@@ -352,6 +369,7 @@ export class GameRoom extends Room<GameState> {
this.state.restartGame();
this.broadcast("gameRestart");
this.setMetadata({ gameStatus: 'waiting' });
if (this.state.players.size === 2) {
setTimeout(() => this.startGame(), 500);

View File

@@ -148,7 +148,8 @@ export class LobbyRoom extends Room<LobbyState> {
// Verify the room exists and is available
const rooms = await matchMaker.query({ roomId });
if (rooms.length === 0 || rooms[0].clients >= 2) {
const status = rooms[0]?.metadata?.gameStatus || "waiting";
if (rooms.length === 0 || rooms[0].clients >= 2 || status !== "waiting") {
throw new Error("Room not available");
}
@@ -175,7 +176,7 @@ export class LobbyRoom extends Room<LobbyState> {
const rooms = await matchMaker.query({ name: "game" });
const availableRooms = rooms
.filter(room => !room.locked && room.clients < 2)
.filter(room => (room.metadata?.gameStatus || "waiting") === "waiting" && room.clients < 2)
.map(room => new AvailableRoom(
room.roomId,
room.clients,