v0.5.2 — the splash's multiplayer door opens, and knows whether it should
The "Play multiplayer" door on index.html had sat disabled, labelled
"Coming soon", since before the server existed — Phases 2 through 4 built
a working lobby and nothing ever linked to it. Loading the site landed on
the same solitaire splash whether a real multiplayer server was behind it
or not, with no visible way in. Found packaging Phase 6 for StartOS.
The door is now a live link to ./play.html?lobby, and main.ts's start()
routes ?lobby straight to the lobby screen — the same showScreen('lobby');
runLobby(beginRemote) the in-game Multiplayer button already used —
instead of dealing a solitaire game first.
GET /api/health is new, and exists to be failed. The same dist/ ships both
served by src/server/ and uploaded as flat files by deploy-web.ts, and the
bundle is identical either way (D4), so the page cannot know from its own
build which it is; every other route 404s an unknown path exactly as a
static host does, so nothing distinguished them. The splash probes it on
load and closes the door when nothing names itself in reply.
The door starts open and only ever closes, deliberately: a wrong "no
server" is the bug above again — invisible, and it strands a player who
does have one — while a wrong "there is one" costs a click and a lobby
that says it cannot connect. The reply must name itself rather than merely
return 200, or a host answering every path with its index page would pass.
Verified: tsc clean; 659 tests pass (656 + 3); /api/health exercised live
against a running server — 200 with the right body, unauthenticated, while
an unknown path and a wrong method both still 404, which is what makes the
probe discriminate at all.
The probe's own test was vacuous on the first attempt — both its "closes"
cases reached close() through the .catch arm, so deleting the body-naming
check outright still passed. Caught by mutating splash.ts and re-running;
the test now covers all three closing routes and fails without the check.
This commit is contained in:
@@ -26,3 +26,46 @@ if (heroImage && lightbox) {
|
||||
if (e instanceof KeyboardEvent && e.key === 'Escape') close();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Is a Station Master server actually behind this page?
|
||||
*
|
||||
* The same `dist/` ships two ways — served by `src/server/`, or uploaded as flat files with no
|
||||
* server at all (`scripts/deploy-web.ts`) — and the bundle is identical in both, so the only
|
||||
* honest way to answer is to ask. `/api/health` is the one route that exists to be failed.
|
||||
*
|
||||
* THE DOOR STARTS OPEN AND ONLY EVER CLOSES. Getting this wrong in the "no server" direction is
|
||||
* the bug this whole change exists to fix: a disabled door is invisible and leaves a player who
|
||||
* DOES have a server with no way in, and nothing on screen to explain it. Getting it wrong the
|
||||
* other way costs a click and a lobby that says it cannot reach a server — which is legible, and
|
||||
* recoverable. So a slow or flaky probe leaves the door alone; only a definite answer closes it.
|
||||
*/
|
||||
const mpDoor = document.getElementById('door-multiplayer');
|
||||
if (mpDoor) {
|
||||
const close = (): void => {
|
||||
mpDoor.classList.add('disabled');
|
||||
mpDoor.removeAttribute('href');
|
||||
const go = document.getElementById('door-multiplayer-go');
|
||||
if (go) go.textContent = 'Requires a Station Master server';
|
||||
const blurb = document.getElementById('door-multiplayer-blurb');
|
||||
if (blurb) {
|
||||
blurb.textContent =
|
||||
'Play with your friends across the internet, each running your own office area within the ' +
|
||||
'entire division. This copy of the game is a plain website with no server behind it, so ' +
|
||||
'there is nowhere to host a table — multiplayer needs a Station Master server.';
|
||||
}
|
||||
};
|
||||
|
||||
// Relative, never absolute: the page must work at whatever address it is reached by, and the
|
||||
// server is always the origin that served it (D16).
|
||||
void fetch('./api/health')
|
||||
.then((r) => (r.ok ? (r.json() as Promise<unknown>) : null))
|
||||
.then((body) => {
|
||||
// A static host that answers unknown paths with 200 and its own index page would sail past
|
||||
// an `r.ok` check, so the body has to name itself before the door is believed.
|
||||
const named =
|
||||
typeof body === 'object' && body !== null && (body as { service?: unknown }).service === 'station-master';
|
||||
if (!named) close();
|
||||
})
|
||||
.catch(() => close());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user