From b884441ba0d10bdd3559748507a0991888f66194 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 19 Nov 2026 08:53:51 +0100 Subject: Phase 6: settings, saves, localization parity (M6) --- server/lib/core/action_handler.dart | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'server/lib/core/action_handler.dart') diff --git a/server/lib/core/action_handler.dart b/server/lib/core/action_handler.dart index 2c73bb1..5b3f8d4 100644 --- a/server/lib/core/action_handler.dart +++ b/server/lib/core/action_handler.dart @@ -89,10 +89,18 @@ class ActionHandler { saveState.load(_gameState); saveState.saveToDisk(_gameState); updateAllUI(); - _sendState('undo'); } lastEvent = const NoEvent(); + // The broadcast travels the undone action's description (as the + // reference's undo envelope does) but the *post-decrement* index: the + // reference reports the pre-decrement index because its clients keep + // local state and reconcile it; our stateless web clients take the + // wire index at face value, so it must equal the state position. + final undoneDescription = commandIndex < _commandDescriptions.length + ? _commandDescriptions[commandIndex] + : ''; commandIndex--; + if (saveState != null) _sendState('undo', description: undoneDescription); } } @@ -115,17 +123,17 @@ class ActionHandler { /// Broadcasts the current command index + description after an undo/redo, /// mirroring the reference app's server-side `StateEnvelope` send. - void _sendState(String action) { + /// [description] overrides the one at the current index (the undo path + /// sends the undone action's name; the redo path sends the redone one). + void _sendState(String action, {String? description}) { final idx = commandIndex; - if (idx >= 0 && idx < _commandDescriptions.length) { - _log.info( - 'server sends, $action index: $idx, description:${_commandDescriptions[idx]}', - ); - onAction?.call( - idx, - _commandDescriptions[idx], - const NoEvent().toJsonString(), - ); + // idx -1 is the baseline (empty history). The reference skips the send + // there because its clients keep local state; the stateless web clients + // only ever see what the server pushes, so the baseline goes out too. + if (idx >= -1 && idx < _commandDescriptions.length) { + final desc = description ?? (idx >= 0 ? _commandDescriptions[idx] : ''); + _log.info('server sends, $action index: $idx, description:$desc'); + onAction?.call(idx, desc, const NoEvent().toJsonString()); } } -- cgit v1.3.1