diff --git a/src/client/Main.ts b/src/client/Main.ts index f4fb076e9..262b07ceb 100644 --- a/src/client/Main.ts +++ b/src/client/Main.ts @@ -246,6 +246,7 @@ class Client { private storeModal: StoreModal; private tokenLoginModal: TokenLoginModal; private matchmakingModal: MatchmakingModal; + private mostRecentJoinEvent: number; private turnstileTokenPromise: Promise<{ token: string; @@ -739,6 +740,7 @@ class Client { private async handleJoinLobby(event: CustomEvent) { const lobby = event.detail; + this.mostRecentJoinEvent = event.timeStamp; if (this.usernameInput && !this.usernameInput.validateOrShowError()) { return; } @@ -757,7 +759,7 @@ class Client { if (lobby.source !== "public") { this.updateJoinUrlForShare(lobby.gameID, config); } - this.lobbyHandle = joinLobby(this.eventBus, { + const newLobbyHandle = joinLobby(this.eventBus, { gameID: lobby.gameID, serverConfig: config, cosmetics: await getPlayerCosmeticsRefs(), @@ -768,6 +770,14 @@ class Client { gameRecord: lobby.gameRecord, }); + if (this.mostRecentJoinEvent !== event.timeStamp) { + newLobbyHandle.stop(true); + console.warn("Join requested, but was superseded"); + return; + } + + this.lobbyHandle = newLobbyHandle; + this.lobbyHandle.prestart.then(() => { console.log("Closing modals"); document.getElementById("settings-button")?.classList.add("hidden");