Skip to content
Open
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
6 changes: 6 additions & 0 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3076,6 +3076,10 @@
"none": "You don't have an active subscription.",
"title": "Your Subscription"
},
"tab_attention": {
"pick_spawn": "Choose your spawn!",
"starting": "Game starting!"
},
"team_colors": {
"blue": "Blue",
"bot": "Tribes",
Expand Down Expand Up @@ -3380,6 +3384,8 @@
"steam_lobby_links_label": "Open Lobby Links in Steam",
"swap_direction": "Swap Rocket Direction",
"swap_direction_desc": "Toggle rocket launch direction (up/down).",
"tab_attention_desc": "Flash the tab title and icon when the game is starting or you still need to pick a spawn while this tab is in the background.",
"tab_attention_label": "Flash Tab When Game Starts",
"tab_audio": "Audio",
"tab_display": "Display",
"tab_gameplay": "Gameplay",
Expand Down
59 changes: 58 additions & 1 deletion src/client/ClientGameRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { ALL_UNIT_TYPES, UnitState } from "./render/types";
import { versionedPathForMismatchedGame } from "./ServerList";
import { audioMixer, initAudioMixer } from "./sound/AudioMixer";
import { SoundManager } from "./sound/SoundManager";
import { tabAttention, type TabAttentionOwner } from "./TabAttention";
import { reportGameError } from "./Telemetry";
import {
loadCachedTerrainMap,
Expand Down Expand Up @@ -126,6 +127,10 @@ export interface LobbyConfig {
// Host only: the play token the lobby was created under, used for the
// first join so the host joins as the creator (see createLobby).
creatorToken?: string;
// Who owns this lobby's tab flash. Main passes the same token to
// tabAttention.request("starting"), so leaving this lobby only ever clears
// its own flash, never a newer lobby's.
tabAttentionOwner?: TabAttentionOwner;
}

export interface JoinLobbyResult {
Expand All @@ -141,6 +146,9 @@ export function joinLobby(
// Mutable clientID state — assigned by server (multiplayer) or derived from gameStartInfo (singleplayer)
let clientID: ClientID | undefined;

// Tab-flash ownership for this lobby; see LobbyConfig.tabAttentionOwner.
const attentionOwner: TabAttentionOwner = lobbyConfig.tabAttentionOwner ?? {};
lobbyConfig.tabAttentionOwner = attentionOwner;
let resolvePrestart: () => void;
let resolveJoin: () => void;
const prestartPromise = new Promise<void>((r) => (resolvePrestart = r));
Expand All @@ -155,6 +163,9 @@ export function joinLobby(
const transport = new Transport(lobbyConfig, eventBus);

let currentGameRunner: ClientGameRunner | null = null;
// Set once this lobby has been left, so a load that was still pending
// doesn't start a runner for it.
let lobbyStopped = false;

const onconnect = async () => {
// Drop the tag if the ownership check failed; the server re-checks anyway.
Expand Down Expand Up @@ -307,13 +318,21 @@ export function joinLobby(
terrainMapFileLoader,
)
.then((r) => {
if (lobbyStopped) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// The lobby was left while this was loading. Dispose the runner
// without starting it; its release only touches this lobby's flash.
r.stop();
return;
}
currentGameRunner = r;
r.start();
})
.catch((e) => {
console.error("error creating client game", e);

currentGameRunner = null;
// The game never started, so stop this lobby's "starting" tab flash.
tabAttention.release(attentionOwner);

const startingModal = document.querySelector(
"game-starting-modal",
Expand Down Expand Up @@ -452,10 +471,14 @@ export function joinLobby(
return false;
}
console.log("leaving game");
lobbyStopped = true;
if (currentGameRunner) {
currentGameRunner.stop();
currentGameRunner = null;
} else {
// No runner yet to release the flash. Only this lobby's own flash is
// cleared: a no-op if it never asked, or if a newer lobby has since.
tabAttention.release(attentionOwner);
transport.leaveGame();
}
return true;
Expand Down Expand Up @@ -1118,6 +1141,7 @@ export class ClientGameRunner {
this.updateHealth();
this.webglBuilder?.update(this.gameView);
this.renderer.tick();
this.updateTabAttention();
if (gu.tickExecutionDuration !== undefined) {
this.metrics?.recordTickExecution(gu.tickExecutionDuration);
}
Expand Down Expand Up @@ -1285,7 +1309,38 @@ export class ClientGameRunner {
void this.transport.rejoinGame(0);
}

// Flash the tab while the player still has to pick a spawn and has switched
// to another tab; the flashing ends once they spawn or the phase is over.
private updateTabAttention(): void {
const config = this.gameView.config();
const canPickSpawn =
this.gameView.inSpawnPhase() &&
!config.isRandomSpawn() &&
!config.isReplay() &&
!config.isIntentionalSpectator();
const owner = this.attentionOwner();
if (!canPickSpawn) {
tabAttention.release(owner);
return;
}
const me = this.gameView.myPlayer();
// Before the player shows up in the view, leave any "starting" flash alone.
if (me === null) return;
if (me.hasSpawned()) {
tabAttention.release(owner);
} else {
tabAttention.request("spawn", owner);
}
}

// The lobby's own token (set by joinLobby), so this runner only ever starts
// or clears its own lobby's flash.
private attentionOwner(): TabAttentionOwner {
return (this.lobby.tabAttentionOwner ??= {});
}

public stop() {
tabAttention.release(this.attentionOwner());
if (this.healthTimer !== null) clearInterval(this.healthTimer);
this.healthTimer = null;
document.removeEventListener("visibilitychange", this.updateHealth);
Expand All @@ -1302,11 +1357,13 @@ export class ClientGameRunner {
// disposals around it.
this.input.destroy();
this.disposeRenderer?.();
// Terminate the worker even if the runner never started (a lobby left while
// it was loading), otherwise nothing else would ever stop it. Idempotent.
this.worker.cleanup();
if (!this.isActive) return;

this.isActive = false;
this.metrics?.stop();
this.worker.cleanup();
this.transport.leaveGame();
if (this.connectionCheckInterval) {
clearInterval(this.connectionCheckInterval);
Expand Down
8 changes: 8 additions & 0 deletions src/client/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ import {
import "./UserMeBroadcast";
import { classicReplayHref } from "./replay/ReplayEntry";
import { parseReplayViewerHash } from "./replay/ReplayViewerRoute";
import { tabAttention } from "./TabAttention";
// The UsernameInput class is used as a type; retain this for registration.
// oxlint-disable-next-line import/no-duplicates -- Required custom-element registration.
import "./UsernameInput";
Expand Down Expand Up @@ -1961,7 +1962,11 @@ class Client {
void showInGameAlert(translateText("error_modal.game_client_failed"));
return;
}
// This join's tab-flash owner: a join that gets superseded can then only
// ever release its own flash, never the newer join's.
const attentionOwner = {};
const newLobbyHandle = joinLobby(this.eventBus, {
tabAttentionOwner: attentionOwner,
gameID: lobby.gameID,
cosmetics: await getPlayerCosmeticsRefs({
verified: resolvedName.verified,
Expand Down Expand Up @@ -2005,6 +2010,9 @@ class Client {
// through the wait (e.g. the featured-stream panel) hide at this point instead of on join.
this.menuTornDown = true;
document.dispatchEvent(new CustomEvent("game-starting"));
// Flash the tab if the player has switched away while waiting; the game
// runner takes over (and stops it) once the spawn phase is under way.
tabAttention.request("starting", attentionOwner);
// Earliest point the lobby is provably closed: the server has stopped
// broadcasting lobby_info and refuses new seats, so the shell must stop
// advertising this as joinable even though terrain is still loading.
Expand Down
Loading
Loading