summaryrefslogtreecommitdiff
path: root/server/lib/core/action_handler.dart
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/core/action_handler.dart')
-rw-r--r--server/lib/core/action_handler.dart30
1 files changed, 19 insertions, 11 deletions
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());
}
}