Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/client/ClientGameRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,12 @@ export interface LobbyConfig {
gameRecord?: GameRecord;
}

// Module-level reference to the current game runner
let currentGameRunner: ClientGameRunner | null = null;

export function shouldPreventUnload(): boolean {
if (currentGameRunner) {
return currentGameRunner.shouldPreventWindowClose();
}
return false;
}

export function joinLobby(
eventBus: EventBus,
lobbyConfig: LobbyConfig,
onPrestart: () => void,
onJoin: () => void,
): () => void {
): () => boolean {
console.log(
`joining lobby: gameID: ${lobbyConfig.gameID}, clientID: ${lobbyConfig.clientID}`,
);
Expand All @@ -88,6 +78,8 @@ export function joinLobby(

const transport = new Transport(lobbyConfig, eventBus);

let currentGameRunner: ClientGameRunner | null = null;

const onconnect = () => {
console.log(`Joined game lobby ${lobbyConfig.gameID}`);
transport.joinGame(0);
Expand Down Expand Up @@ -141,9 +133,13 @@ export function joinLobby(
};
transport.connect(onconnect, onmessage);
return () => {
if (currentGameRunner && currentGameRunner.shouldPreventWindowClose()) {
return false;
}
console.log("leaving game");
currentGameRunner = null;
transport.leaveGame();
return true;
};
}

Expand Down
11 changes: 3 additions & 8 deletions src/client/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ServerConfig } from "../core/configuration/Config";
import { getServerConfigFromClient } from "../core/configuration/ConfigLoader";
import { UserSettings } from "../core/game/UserSettings";
import "./AccountModal";
import { joinLobby, shouldPreventUnload } from "./ClientGameRunner";
import { joinLobby } from "./ClientGameRunner";
import { fetchCosmetics } from "./Cosmetics";
import "./DarkModeButton";
import { DarkModeButton } from "./DarkModeButton";
Expand Down Expand Up @@ -80,7 +80,7 @@ export interface JoinLobbyEvent {
}

class Client {
private gameStop: (() => void) | null = null;
private gameStop: (() => boolean) | null = null;
private eventBus: EventBus = new EventBus();

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

window.addEventListener("beforeunload", (e) => {
// Check if we should prevent unload (player is alive)
if (shouldPreventUnload()) {
if (this.gameStop && !this.gameStop()) {
e.preventDefault();
return "";
}
// Otherwise, cleanup the game normally
console.log("Browser is closing");
if (this.gameStop !== null) {
this.gameStop();
}
});

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