Skip to content

Commit e5af8ea

Browse files
authored
Merge pull request #2 from Foorack/copilot/refactor-lobby-join-logic
Refactor joinLobby to avoid top-level currentGameRunner variable
2 parents 2976da0 + 2509030 commit e5af8ea

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

src/client/ClientGameRunner.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,12 @@ export interface LobbyConfig {
6363
gameRecord?: GameRecord;
6464
}
6565

66-
// Module-level reference to the current game runner
67-
let currentGameRunner: ClientGameRunner | null = null;
68-
69-
export function shouldPreventUnload(): boolean {
70-
if (currentGameRunner) {
71-
return currentGameRunner.shouldPreventWindowClose();
72-
}
73-
return false;
74-
}
75-
7666
export function joinLobby(
7767
eventBus: EventBus,
7868
lobbyConfig: LobbyConfig,
7969
onPrestart: () => void,
8070
onJoin: () => void,
81-
): () => void {
71+
): () => boolean {
8272
console.log(
8373
`joining lobby: gameID: ${lobbyConfig.gameID}, clientID: ${lobbyConfig.clientID}`,
8474
);
@@ -88,6 +78,8 @@ export function joinLobby(
8878

8979
const transport = new Transport(lobbyConfig, eventBus);
9080

81+
let currentGameRunner: ClientGameRunner | null = null;
82+
9183
const onconnect = () => {
9284
console.log(`Joined game lobby ${lobbyConfig.gameID}`);
9385
transport.joinGame(0);
@@ -141,9 +133,13 @@ export function joinLobby(
141133
};
142134
transport.connect(onconnect, onmessage);
143135
return () => {
136+
if (currentGameRunner && currentGameRunner.shouldPreventWindowClose()) {
137+
return false;
138+
}
144139
console.log("leaving game");
145140
currentGameRunner = null;
146141
transport.leaveGame();
142+
return true;
147143
};
148144
}
149145

src/client/Main.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ServerConfig } from "../core/configuration/Config";
66
import { getServerConfigFromClient } from "../core/configuration/ConfigLoader";
77
import { UserSettings } from "../core/game/UserSettings";
88
import "./AccountModal";
9-
import { joinLobby, shouldPreventUnload } from "./ClientGameRunner";
9+
import { joinLobby } from "./ClientGameRunner";
1010
import { fetchCosmetics } from "./Cosmetics";
1111
import "./DarkModeButton";
1212
import { DarkModeButton } from "./DarkModeButton";
@@ -80,7 +80,7 @@ export interface JoinLobbyEvent {
8080
}
8181

8282
class Client {
83-
private gameStop: (() => void) | null = null;
83+
private gameStop: (() => boolean) | null = null;
8484
private eventBus: EventBus = new EventBus();
8585

8686
private usernameInput: UsernameInput | null = null;
@@ -154,16 +154,11 @@ class Client {
154154
this.publicLobby = document.querySelector("public-lobby") as PublicLobby;
155155

156156
window.addEventListener("beforeunload", (e) => {
157-
// Check if we should prevent unload (player is alive)
158-
if (shouldPreventUnload()) {
157+
if (this.gameStop && !this.gameStop()) {
159158
e.preventDefault();
160159
return "";
161160
}
162-
// Otherwise, cleanup the game normally
163161
console.log("Browser is closing");
164-
if (this.gameStop !== null) {
165-
this.gameStop();
166-
}
167162
});
168163

169164
document.addEventListener("join-lobby", this.handleJoinLobby.bind(this));

0 commit comments

Comments
 (0)