fix for reentering leaving lobby

This commit is contained in:
1brucben
2025-05-08 23:59:19 +02:00
parent c6e938d0e2
commit e8129e4a3d
4 changed files with 62 additions and 29 deletions
+4 -2
View File
@@ -80,12 +80,14 @@ export function joinLobby(
onPrestart(); onPrestart();
} }
if (message.type == "start") { if (message.type == "start") {
// Trigger prestart for singleplayer games
onPrestart(); onPrestart();
consolex.log(`lobby: game started: ${JSON.stringify(message)}`); consolex.log(`lobby: game started: ${JSON.stringify(message)}`);
onJoin(); onJoin();
// For multiplayer games, GameStartInfo is not known until game starts.
lobbyConfig.gameStartInfo = message.gameStartInfo; lobbyConfig.gameStartInfo = message.gameStartInfo;
// ✅ Add this line to ensure gameID is defined
lobbyConfig.gameID = message.gameStartInfo.gameID;
createClientGame( createClientGame(
lobbyConfig, lobbyConfig,
eventBus, eventBus,
+18 -5
View File
@@ -44,7 +44,7 @@ export interface JoinLobbyEvent {
class Client { class Client {
private gameStop: () => void; private gameStop: () => void;
private homepageWS: WebSocket | null = null;
private usernameInput: UsernameInput | null = null; private usernameInput: UsernameInput | null = null;
private flagInput: FlagInput | null = null; private flagInput: FlagInput | null = null;
private darkModeButton: DarkModeButton | null = null; private darkModeButton: DarkModeButton | null = null;
@@ -222,11 +222,10 @@ class Client {
} }
// Establish minimal WebSocket connection to track homepage view // Establish minimal WebSocket connection to track homepage view
try { try {
const ws = new WebSocket(`ws://${location.host}/w0`); this.homepageWS = new WebSocket(`ws://${location.host}/w0`);
this.homepageWS.onopen = () => {
ws.onopen = () => {
console.log("WebSocket opened: sending homepage_ping"); console.log("WebSocket opened: sending homepage_ping");
ws.send(JSON.stringify({ type: "homepage_ping" })); this.homepageWS?.send(JSON.stringify({ type: "homepage_ping" }));
}; };
} catch (e) { } catch (e) {
console.warn("Failed to connect to homepage tracking WebSocket", e); console.warn("Failed to connect to homepage tracking WebSocket", e);
@@ -243,6 +242,11 @@ class Client {
private async handleJoinLobby(event: CustomEvent) { private async handleJoinLobby(event: CustomEvent) {
const lobby = event.detail as JoinLobbyEvent; const lobby = event.detail as JoinLobbyEvent;
consolex.log(`joining lobby ${lobby.gameID}`); consolex.log(`joining lobby ${lobby.gameID}`);
if (this.homepageWS) {
this.homepageWS.close();
this.homepageWS = null;
}
if (this.gameStop != null) { if (this.gameStop != null) {
consolex.log("joining lobby, stopping existing game"); consolex.log("joining lobby, stopping existing game");
this.gameStop(); this.gameStop();
@@ -320,6 +324,15 @@ class Client {
this.gameStop(); this.gameStop();
this.gameStop = null; this.gameStop = null;
this.publicLobby.leaveLobby(); this.publicLobby.leaveLobby();
try {
const ws = new WebSocket(`ws://${location.host}/w0`);
ws.onopen = () => {
ws.send(JSON.stringify({ type: "homepage_ping" }));
};
} catch (e) {
console.warn("Failed to reconnect to homepage tracking WebSocket", e);
}
} }
} }
+13 -12
View File
@@ -15,7 +15,6 @@ import {
AllPlayersStats, AllPlayersStats,
ClientID, ClientID,
ClientIntentMessageSchema, ClientIntentMessageSchema,
ClientJoinMessageSchema,
ClientLogMessageSchema, ClientLogMessageSchema,
ClientMessageSchema, ClientMessageSchema,
ClientPingMessageSchema, ClientPingMessageSchema,
@@ -329,18 +328,20 @@ export class Transport {
} }
joinGame(numTurns: number) { joinGame(numTurns: number) {
if (!this.lobbyConfig.gameID) {
throw new Error("gameID is missing in lobbyConfig. Cannot join game.");
}
this.sendMsg( this.sendMsg(
JSON.stringify( JSON.stringify({
ClientJoinMessageSchema.parse({ type: "join",
type: "join", gameID: this.lobbyConfig.gameID,
gameID: this.lobbyConfig.gameID, clientID: this.lobbyConfig.clientID,
clientID: this.lobbyConfig.clientID, lastTurn: numTurns,
lastTurn: numTurns, persistentID: this.lobbyConfig.persistentID,
persistentID: this.lobbyConfig.persistentID, username: this.lobbyConfig.playerName,
username: this.lobbyConfig.playerName, flag: this.lobbyConfig.flag,
flag: this.lobbyConfig.flag, }),
}),
),
); );
} }
+27 -10
View File
@@ -271,9 +271,15 @@ export function startWorker() {
sendHomepageCount(); sendHomepageCount();
return; return;
} }
log.info("FULL JOIN MSG:", clientMsg);
if (clientMsg.type == "join") { if (
// Verify this worker should handle this game clientMsg.type === "join" &&
typeof clientMsg.gameID === "string" &&
typeof clientMsg.clientID === "string" &&
typeof clientMsg.username === "string" &&
typeof clientMsg.persistentID === "string"
) {
const expectedWorkerId = config.workerIndex(clientMsg.gameID); const expectedWorkerId = config.workerIndex(clientMsg.gameID);
if (expectedWorkerId !== workerId) { if (expectedWorkerId !== workerId) {
log.warn( log.warn(
@@ -282,28 +288,39 @@ export function startWorker() {
return; return;
} }
// Create client and add to game log.info(
`Processing join for gameID: ${clientMsg.gameID}, clientID: ${clientMsg.clientID}`,
);
const client = new Client( const client = new Client(
clientMsg.clientID, clientMsg.clientID,
clientMsg.persistentID, clientMsg.persistentID,
ip, ip,
clientMsg.username, clientMsg.username,
ws, ws,
clientMsg.flag, clientMsg.flag ?? "", // fallback if flag is missing
); );
homepageClients.delete(ws); // Remove from homepage tracker
sendHomepageCount(); // Update count immediately
const wasFound = gm.addClient( let wasFound = false;
client, try {
clientMsg.gameID, wasFound = gm.addClient(
clientMsg.lastTurn, client,
); clientMsg.gameID,
clientMsg.lastTurn ?? 0, // default to 0
);
} catch (err) {
log.error("gm.addClient threw error:", err);
}
if (!wasFound) { if (!wasFound) {
log.info( log.info(
`game ${clientMsg.gameID} not found on worker ${workerId}`, `game ${clientMsg.gameID} not found on worker ${workerId}`,
); );
// Handle game not found case
} }
return;
} }
// Handle other message types // Handle other message types