summaryrefslogtreecommitdiff
path: root/server/lib
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-15 13:45:21 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-15 13:45:21 +0100
commit4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (patch)
tree94e6286e7e22e0467a61b99c18b9ab582fbbe656 /server/lib
parent3ebbdf6ffa16f751c82e3bd2e74294f4fe843155 (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/lib')
-rw-r--r--server/lib/net/app.dart50
1 files changed, 47 insertions, 3 deletions
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',