Skip to content
Draft
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
9 changes: 6 additions & 3 deletions src/client/ClientGameRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,15 @@ export class ClientGameRunner {
* the window or navigate away during an active game session.
*
* @returns {boolean} `true` if the window close should be prevented
* (when the player is alive in the game), `false` otherwise
* (when the player is alive in the game or during spawn phase), `false` otherwise
* (when the player is not alive or doesn't exist)
*/
public shouldPreventWindowClose(): boolean {
// Show confirmation dialog if player is alive in the game
return !!this.myPlayer?.isAlive();
// gameView, not this.myPlayer, so it works before 1st click after spawn phase too
return (
(this.gameView.myPlayer()?.isAlive() ?? false) ||
this.gameView.inSpawnPhase()
);
}

private async saveGame(update: WinUpdate) {
Expand Down
14 changes: 12 additions & 2 deletions src/client/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,23 @@ class Client {
if (!this.gameStop()) {
console.info("Player is active, ask before leaving game");

// Rollback navigator history to stay in game on cancel
// Before calling confirm so dialog also shows on mobile
history.pushState(null, "", window.location.origin + "#refresh");
history.pushState(null, "", this.currentUrl);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, you are rolling navigation back even if the user confirms he wants to exit the game.
Am I right?


const isConfirmed = confirm(
translateText("help_modal.exit_confirmation"),
);

if (!isConfirmed) {
// Rollback navigator history
history.pushState(null, "", this.currentUrl);
// Rebuild stack:
// - prevent being put back to homepage when clicking back button again after cancelling
// - replaceState instead of 2x pushState for cleaner history
// Can't prevent browser blocking popState on clicking back button again without in-game click first
// - preventable by only having pushState this.currentUrl here, which breaks confirm on mobile
// history.replaceState(null, "", window.location.origin + "#refresh");
// history.pushState(null, "", this.currentUrl);
return;
}
}
Expand Down
Loading