asks what game you want Three queued items. The last matters most. A RELEASE NO LONGER DESTROYS EVERY GAME IN PROGRESS. Four consecutive releases killed every game on the box, one of them a release that changed only how the board is drawn. The reasoning behind the refusal was always right — a move legal under old rules may not be legal under new ones, and half-replaying a save is worse than refusing it. The TEST was wrong: it compared engineVersion for exact equality, and that stamp is the package version, which moves for a CSS fix. Whether a save still replays has an exact answer, so it is now asked directly. loadGame reads the file and judges nothing; tryResumeSession replays the intents and reports the first one the engine refuses. A save stamped with a version this server has never run resumes fine provided its moves replay — verified against a file hand-stamped 0.4.9-ancient. One that genuinely does not replay is still refused, but the log names the move rather than two version strings: "move 3 of 8 (localOps.choose) is rejected by the current rules with OPTION_ALREADY_CHOSEN". fromMultiplayerSave had to stop lying first. It has always stopped at the first unacceptable intent and done so in silence, which was survivable only because the version gate meant a doomed replay was never attempted. Now that the replay IS the check, it returns where it stopped and why. Deliberately not done: resuming a partly-replayable game at its last good move. That silently rewinds a game to a position nobody played to while every browser holding a later Frame carries on unaware. Refusing leaves the file intact, so putting the previous version back still recovers it. EMPLOYEE ROTATION IS IMPLEMENTED, SISTER TRAINS IS DELETED. Two of the four optional-rule flags were read by nothing at all. Employee Rotation is four lines in advance.ts, because the seat/player split (D9) exists for precisely this rule: seating is the only thing that moves, so Revenue, hands, the Superintendent and whose turn it is travel with the player, and the Office, district, grid and any trains standing in it stay with the chair. Inheriting the district you move into is the point of the rule, not a side effect. "Left" is seat + 1, matching playerLeftOf. Sister Trains is deleted rather than built: Q9 records that the Second Section card supersedes it, and that card exists, so the flag was a toggle for a rule the game no longer has. THE LOBBY ASKS WHAT GAME YOU WANT TO PLAY. Creating a game asked for a name, a mode and a table size; every other dial was hardcoded. A Game settings block now carries the same set the solitaire dialog does — seed, starting hand, the three revenue rates, Days, the combined-Revenue floor, both collision caps, the opponent-card toggle — plus the three surviving optional rules. Mode and table size set the defaults and everything stays editable. The seed is honoured, so a game can be reproduced or compared. Verified: 682 tests pass (679 + 3). The rotation tests were mutation-checked both ways — disabling the rotation and turning the table the wrong way each fail the suite. Live: a save stamped 0.4.9-ancient resumed, an injected illegal move was refused by name, and a create with every dial set to a non-default value came back out of game.json with all of them intact, including seed 777. Two of my own assertions were wrong on the way and the tests caught them: the Fedora legitimately passes at Stage 12 (§5) so it cannot be compared against its own earlier value, and dispatchUsedToday is cleared at every Day boundary so it cannot mark a district.
108 lines
4.6 KiB
TypeScript
108 lines
4.6 KiB
TypeScript
/**
|
|
* Process bootstrap — Phase 2 §12 step 8, extended for Phase 3 (§12 steps 14-15) load-on-start and
|
|
* Phase 4 (§12 steps 17-20) to resume every saved game and lobby, not just one.
|
|
*
|
|
* Run with: node src/server/index.ts
|
|
*
|
|
* Env-configured, no config file — matches how the rest of this project's dev-side tooling reads
|
|
* `process.env` directly (`scripts/build-web.ts`'s `BUILD_DIST_DIR`).
|
|
*/
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
import { dirname, join, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { startServer } from './http.ts';
|
|
import { gameDir, loadGame, readIndex, readLobby, readSessions } from './persistence.ts';
|
|
import { tryResumeSession } from './session.ts';
|
|
import type { GameSession } from './session.ts';
|
|
import type { Lobby, PlayerSession } from './lobby.ts';
|
|
|
|
const port = Number(process.env['PORT'] ?? 8081);
|
|
const bindAddress = process.env['BIND_ADDRESS'] ?? '0.0.0.0';
|
|
const joinSecret = process.env['JOIN_SECRET'];
|
|
/**
|
|
* Optional, unlike `JOIN_SECRET`: a server with no administrator is a perfectly good server, and
|
|
* refusing to boot without one would break every existing deployment and every dev run. Unset
|
|
* simply means the admin routes are not there (`http.ts`), which is the safe default — the
|
|
* capability has to be granted, never merely left ungated.
|
|
*/
|
|
const adminSecret = process.env['ADMIN_SECRET'];
|
|
const distDir = resolve(process.env['DIST_DIR'] ?? 'dist');
|
|
const dataDir = resolve(process.env['DATA_DIR'] ?? 'data');
|
|
|
|
if (!joinSecret) {
|
|
console.error('JOIN_SECRET must be set — a server-wide secret, passed out of band (D14).');
|
|
process.exit(1);
|
|
}
|
|
|
|
// `package.json`'s version IS `engineVersion` (§12 step 15) — the same reading `scripts/build-web.ts`'s
|
|
// `buildStamp()` already does, just from `src/server/` rather than the repo root script directory.
|
|
const root = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
const engineVersion = (JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')) as { version: string }).version;
|
|
|
|
const initialGames = new Map<string, GameSession>();
|
|
const initialLobbies = new Map<string, Lobby>();
|
|
const initialSessions = new Map<string, PlayerSession>();
|
|
|
|
const index = await readIndex(dataDir);
|
|
// Said before the loop, not after it: replaying is the reason a restart pauses before the port
|
|
// opens, and a log that only reports each game once it is done gives no warning of how much is
|
|
// still to come.
|
|
const resumable = index.filter((e) => e.status !== 'finished').length;
|
|
if (resumable > 0) console.log(`Resuming ${resumable} saved game(s)…`);
|
|
for (const entry of index) {
|
|
const sessions = await readSessions(dataDir, entry.gameId);
|
|
for (const s of sessions) initialSessions.set(s.token, s);
|
|
|
|
if (entry.status === 'lobby') {
|
|
const lobby = await readLobby(dataDir, entry.gameId);
|
|
if (lobby) initialLobbies.set(entry.gameId, lobby);
|
|
continue;
|
|
}
|
|
|
|
const loaded = await loadGame(gameDir(dataDir, entry.gameId));
|
|
if (loaded.found) {
|
|
const resumed = tryResumeSession(loaded.saved);
|
|
if (resumed.ok) {
|
|
initialGames.set(entry.gameId, resumed.session);
|
|
console.log(`Resumed ${entry.gameId} (${entry.gameCode}) — ${loaded.saved.history.length} intents replayed.`);
|
|
} else {
|
|
/**
|
|
* The save does not replay under these rules, which is the only thing that has ever actually
|
|
* mattered — and now the only thing asked. Says which move it choked on, because "some
|
|
* version differs" was never enough to act on: the file is left untouched, so an operator who
|
|
* wants the game back can put the previous version on and finish it.
|
|
*/
|
|
const f = resumed.failure;
|
|
console.error(
|
|
`Refusing to resume ${entry.gameId} (${entry.gameCode}): move ${f.stoppedAt + 1} of ${f.of} ` +
|
|
`(${f.intent}) is rejected by the current rules with ${f.code}. Saved under engine ` +
|
|
`version ${loaded.storedVersion}, this server is running ${engineVersion}. The file is ` +
|
|
`left untouched.`,
|
|
);
|
|
}
|
|
}
|
|
// `entry.status === 'finished'` games are not resumed into memory at all — nothing plays them
|
|
// forward, and their files stay on disk for post-game replay (`lobby-and-sessions.md` §6).
|
|
}
|
|
|
|
startServer({
|
|
port,
|
|
bindAddress,
|
|
joinSecret,
|
|
adminSecret,
|
|
distDir,
|
|
dataDir,
|
|
engineVersion,
|
|
initialGames,
|
|
initialLobbies,
|
|
initialSessions,
|
|
});
|
|
console.log(
|
|
`Station Master multiplayer server on ${bindAddress}:${port}, serving ${distDir} — ` +
|
|
`${initialGames.size} game(s) and ${initialLobbies.size} lobby(ies) resumed.`,
|
|
);
|
|
if (!adminSecret) {
|
|
console.log('ADMIN_SECRET is unset — the /api/games administration routes are disabled.');
|
|
}
|