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
6 changes: 6 additions & 0 deletions .changes/help-back-to-game.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
app: patch
crate: none
---
## Player
- In the installed app, Back to Game in the Player's Manual now closes the manual and returns to your game, instead of opening a second copy of the game inside the manual's window.
6 changes: 4 additions & 2 deletions help-site/src/components/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
// tab the game opened (?from=game, src/ui/fromGame.ts) that button and the
// front page's Play button both read Back to game and close this tab instead
// of loading a second copy of the game; the note below says so when the
// browser keeps the tab open. In an installed app the button first checks the
// game still runs, and loads it here when it doesn't (backToOpenGame).
// browser keeps the tab open. In an installed app the manual on its own host
// opens in a browser view over the app, so the button closes that view; a
// /help build in the app's own window first checks the game still runs, and
// loads it here when it doesn't (backToOpenGame).
import Search from "@astrojs/starlight/components/Search.astro";
import ThemeSelect from "@astrojs/starlight/components/ThemeSelect.astro";
import TitleBar from "../../../docs/engine-site/src/components/TitleBar.astro";
Expand Down
14 changes: 12 additions & 2 deletions src/ui/fromGame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ describe("backToOpenGame", () => {
});

/** Click the shell's Back to game link in an installed app whose game does or doesn't answer. */
async function clickInstalled(gameAnswers: boolean): Promise<{ win: Window; e: MouseEvent; stillOpen: ReturnType<typeof vi.fn> }> {
async function clickInstalled(gameAnswers: boolean, pageAt: string = location.href): Promise<{ win: Window; e: MouseEvent; stillOpen: ReturnType<typeof vi.fn> }> {
mountFromGame();
const replace = vi.fn();
const win = { close: vi.fn(), closed: true, setTimeout: vi.fn(), document, location: { replace } } as unknown as Window;
const page = new URL(pageAt);
const win = { close: vi.fn(), closed: true, setTimeout: vi.fn(), document, location: { replace, href: page.href, origin: page.origin } } as unknown as Window;
const stillOpen = vi.fn(() => Promise.resolve(gameAnswers));
const link = document.querySelector<HTMLAnchorElement>("a[data-act=back-to-game]")!;
const e = new MouseEvent("click", { cancelable: true });
Expand All @@ -114,6 +115,15 @@ describe("backToOpenGame", () => {
expect(win.close).toHaveBeenCalledTimes(1);
expect(win.location.replace).not.toHaveBeenCalled();
});

it("closes the browser view the app opened for a manual on another host, without asking or loading the game there", async () => {
// The shell's link points at this test origin's "/", so a page on any other host sees the game elsewhere.
const { win, e, stillOpen } = await clickInstalled(false, "https://help.example.test/rooms/");
expect(e.defaultPrevented).toBe(true);
expect(stillOpen).not.toHaveBeenCalled();
expect(win.close).toHaveBeenCalledTimes(1);
expect(win.location.replace).not.toHaveBeenCalled();
});
});

/** A stand-in channel that records what it sends and lets a test deliver a message. */
Expand Down
18 changes: 15 additions & 3 deletions src/ui/fromGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,33 @@ const liveDeps: BackToGameDeps = { installed: isInstalledStandalone, stillOpen:
* button loads the game here instead (its tower was saved when the page went
* away, so the title screen offers Continue). When the game does answer, it
* is in another window and this one closes as in a browser tab.
*
* When the game lives on another host than this page (the manual at
* help.verticopolis.com), this window is never the app's own: the app opens
* an address outside its scope in a browser view over itself, and the
* presence check cannot reach a game on another origin. Loading the game here
* would start a second copy inside that view, so the page closes it instead,
* which brings back the app with its game.
*/
export function backToOpenGame(e: MouseEvent, win: Window = window, deps: BackToGameDeps = liveDeps): void {
e.preventDefault();
if (!deps.installed()) {
const link = e.currentTarget as HTMLAnchorElement | null;
const gameHref = link?.href || "/";
if (!deps.installed() || gameOnAnotherHost(gameHref, win)) {
closeOrSay(win);
return;
}
const link = e.currentTarget as HTMLAnchorElement | null;
const gameHref = link?.href || "/";
void deps.stillOpen().then((open) => {
if (open) closeOrSay(win);
else win.location.replace(gameHref);
});
}

/** True when `gameHref` points at another origin than this page. */
function gameOnAnotherHost(gameHref: string, win: Window): boolean {
return new URL(gameHref, win.location.href).origin !== win.location.origin;
}

/** Close this tab, or say why it stayed open. */
function closeOrSay(win: Window): void {
win.close();
Expand Down
Loading