diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2026-11-15 13:45:21 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2026-11-15 13:45:21 +0100 |
| commit | 4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (patch) | |
| tree | 94e6286e7e22e0467a61b99c18b9ab582fbbe656 /server | |
| parent | 3ebbdf6ffa16f751c82e3bd2e74294f4fe843155 (diff) | |
Phase 3: web UI core loop (M3)
Browser game UI on top of the Phase 2 protocol: server-driven, input-only
clients with per-connection secret visibility.
Web (Svelte 5):
- Layout: TopBar (element buttons with use/imbue long-press, title, menu),
MainList (character/monster/note rows), BottomBar (draw/next-round +
level widget with T/H/X/C values).
- CharacterRow with the secret initiative input: own value editable,
server-masked values render as ??, revealed on draw.
- Per-figure StatusMenu: health/max/xp/bless/curse/empower/enfeeble
steppers, condition grid, turn-done — all server commands, player-gated.
- Menu dialogs: select scenario, add section, add/remove character,
set level/difficulty/solo/auto-level, add/remove monster, note row,
action log with rollback, settings (style theme + language).
- Player vs spectator UI gated on the live seat table; reconnecting
banner, error toasts, connection status plumbing.
- i18n: English ported, locale store with localStorage persistence.
- Design tokens + the three style themes (frosthaven/gloomhaven/original).
- Unit tests: level values, game logic (elements/conditions/art/stats),
i18n, the reactive player store, the data client (sections keyed by
scenario number), plus the existing connection/protocol/state tests.
Server:
- Serve the engine's data JSON at /data/ and artwork at /images/ so the
client menus and rows read the same assets the engine loads.
Diffstat (limited to 'server')
| -rw-r--r-- | server/bin/main.dart | 3 | ||||
| -rw-r--r-- | server/lib/net/app.dart | 50 | ||||
| -rw-r--r-- | server/test/net/app_test.dart | 9 | ||||
| -rw-r--r-- | server/test/net/ws_test_helpers.dart | 1 |
4 files changed, 60 insertions, 3 deletions
diff --git a/server/bin/main.dart b/server/bin/main.dart index 7571960..f864828 100644 --- a/server/bin/main.dart +++ b/server/bin/main.dart @@ -40,6 +40,9 @@ Future<void> main(List<String> args) async { port: port, staticDir: staticDir, gameServer: gameServer, + // The client's menus and artwork read the same assets the engine loads. + dataDir: 'assets/data/', + imageDir: 'assets/images/', ); stdout ..writeln('X-Haven server listening on http://localhost:${server.port}') diff --git a/server/lib/net/app.dart b/server/lib/net/app.dart index 21a1b81..a38d5c0 100644 --- a/server/lib/net/app.dart +++ b/server/lib/net/app.dart @@ -19,13 +19,30 @@ import 'ws_upgrade.dart'; final _log = Logger('server'); /// Builds the HTTP handler for the game server. -Handler createHandler({required String staticDir, required GameServer gameServer}) { +/// +/// [dataDir] (e.g. `assets/data/`) is served read-only at `/data/` when +/// non-null: the client's menus read the campaign/scenario/character/section +/// JSON from it (the same files the engine loads). [imageDir] (e.g. +/// `assets/images/`) is served at `/images/` when non-null — character and +/// monster artwork for the main list. +Handler createHandler({ + required String staticDir, + required GameServer gameServer, + String? dataDir, + String? imageDir, +}) { final router = Router(); router.get('/ws', createWebSocketUpgradeHandler(gameServer)); router.get('/healthz', (Request request) => Response.ok( jsonEncode({'status': 'ok'}), headers: {'content-type': 'application/json; charset=utf-8'}, )); + if (dataDir != null) { + router.mount('/data/', _createStaticHandler(dataDir, plain: true)); + } + if (imageDir != null) { + router.mount('/images/', _createStaticHandler(imageDir, plain: true)); + } // Session endpoint (PLAN.md section 3.3): returns the caller's persistent // session id, setting the `x-haven-session` cookie when the caller has // none yet. Browsers then carry the cookie on every same-origin request, @@ -53,8 +70,19 @@ Future<HttpServer> startServer({ required int port, required String staticDir, required GameServer gameServer, + String? dataDir, + String? imageDir, }) { - return shelf_io.serve(createHandler(staticDir: staticDir, gameServer: gameServer), '0.0.0.0', port); + return shelf_io.serve( + createHandler( + staticDir: staticDir, + gameServer: gameServer, + dataDir: dataDir, + imageDir: imageDir, + ), + '0.0.0.0', + port, + ); } /// The session id from [request]'s cookie, when well-formed. @@ -71,9 +99,17 @@ String? _validSessionIdFrom(Request request) { return null; } -Handler _createStaticHandler(String staticDir) { +/// Serves [staticDir] as static files. When [plain] is true (data/image +/// directories) it is plain static serving with a cache header and a plain +/// 404; otherwise (the SPA build) a 404 on an extensionless path falls back +/// to `index.html` so client-side routes resolve. +Handler _createStaticHandler(String staticDir, {bool plain = false}) { final directory = Directory(staticDir); if (!directory.existsSync()) { + if (plain) { + _log.info('data dir $staticDir not found; serving 404'); + return (Request request) => Response.notFound('not found'); + } _log.info('static dir $staticDir not found; serving stub page'); return (Request request) => Response.ok( '<!doctype html><meta charset="utf-8">' @@ -85,6 +121,14 @@ Handler _createStaticHandler(String staticDir) { 'with <code>--static-dir ../web/dist</code>.</p>', ); } + if (plain) { + // Plain static serving (no SPA fallback) with long-lived caching — + // the JSON and images are immutable build inputs. + return createStaticHandler( + directory.path, + maxAge: const Duration(days: 1), + ); + } final staticHandler = createStaticHandler( directory.path, defaultDocument: 'index.html', diff --git a/server/test/net/app_test.dart b/server/test/net/app_test.dart index 4a02c15..a37a8e5 100644 --- a/server/test/net/app_test.dart +++ b/server/test/net/app_test.dart @@ -42,6 +42,15 @@ void main() { expect(jsonDecode(body), {'status': 'ok'}); }); + test('serves the game data JSON at /data/', () async { + final body = await httpGet( + server.httpUrl.replace(path: '/data/editions/editions.json'), + ); + final decoded = jsonDecode(body) as Map<String, dynamic>; + expect(decoded['editions'], isA<List>()); + expect(decoded['editions'], contains('Jaws of the Lion')); + }); + test('session endpoint mints a session and sets the cookie', () async { final client = HttpClient(); addTearDown(client.close); diff --git a/server/test/net/ws_test_helpers.dart b/server/test/net/ws_test_helpers.dart index cb8af67..1903567 100644 --- a/server/test/net/ws_test_helpers.dart +++ b/server/test/net/ws_test_helpers.dart @@ -63,6 +63,7 @@ class TestGameServer { port: 0, staticDir: 'nonexistent-static-dir', gameServer: instance.gameServer, + dataDir: dataDir, ); return instance; } |
