From 5db7fc5462898aad8b75eb5a07faf2a65a656491 Mon Sep 17 00:00:00 2001 From: Vincent Velociter Date: Sat, 23 Sep 2023 11:51:54 +0200 Subject: [PATCH] Get the current opening by traversing the branch --- lib/src/model/analysis/analysis_ctrl.dart | 25 ++++++++++++++-------- lib/src/view/analysis/analysis_screen.dart | 5 +++-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/src/model/analysis/analysis_ctrl.dart b/lib/src/model/analysis/analysis_ctrl.dart index c3e39fa91d..d16d15f58b 100644 --- a/lib/src/model/analysis/analysis_ctrl.dart +++ b/lib/src/model/analysis/analysis_ctrl.dart @@ -160,13 +160,24 @@ class AnalysisCtrl extends _$AnalysisCtrl { _startEngineEval(); } + /// Gets the node and maybe the associated branch opening at the given path. + (Node, Opening?) _nodeOpeningAt(Node node, UciPath path, [Opening? opening]) { + if (path.isEmpty) return (node, opening); + final child = node.childById(path.head!); + if (child != null) { + return _nodeOpeningAt(child, path.tail, child.opening ?? opening); + } else { + return (node, opening); + } + } + void _setPath( UciPath path, { bool isNewNode = false, bool replaying = false, }) { final pathChange = state.currentPath != path; - final currentNode = _root.nodeAt(path); + final (currentNode, opening) = _nodeOpeningAt(_root, path); if (currentNode is Branch) { if (!replaying) { @@ -198,6 +209,7 @@ class AnalysisCtrl extends _$AnalysisCtrl { currentPath: path, currentNode: currentNode.view, lastMove: currentNode.sanMove.move, + currentBranchOpening: opening, // root view is only used to display move list, so we need to // recompute the root view only when a new node is added root: isNewNode ? _root.view : state.root, @@ -206,6 +218,7 @@ class AnalysisCtrl extends _$AnalysisCtrl { state = state.copyWith( currentPath: path, currentNode: state.root, + currentBranchOpening: opening, lastMove: null, ); } @@ -228,15 +241,9 @@ class AnalysisCtrl extends _$AnalysisCtrl { if (opening != null) { _root.updateAt(path, (node) => node.opening = opening); - AnalysisCtrlState newState = state; if (state.currentPath == path) { - newState = newState.copyWith(currentNode: _root.nodeAt(path).view); - } - if (state.gameOpening == null || path.size > state.gameOpening!.$1.size) { - newState = newState.copyWith(gameOpening: (path, opening)); + state = state.copyWith(currentNode: _root.nodeAt(path).view); } - - state = newState; } } @@ -278,7 +285,7 @@ class AnalysisCtrlState with _$AnalysisCtrlState { required Side pov, required EvaluationContext evaluationContext, Move? lastMove, - (UciPath, Opening)? gameOpening, + Opening? currentBranchOpening, }) = _AnalysisCtrlState; IMap> get validMoves => diff --git a/lib/src/view/analysis/analysis_screen.dart b/lib/src/view/analysis/analysis_screen.dart index 636519428f..4c985a4a05 100644 --- a/lib/src/view/analysis/analysis_screen.dart +++ b/lib/src/view/analysis/analysis_screen.dart @@ -284,8 +284,9 @@ class _Opening extends ConsumerWidget { final nodeOpening = ref.watch(ctrlProvider.select((s) => s.currentNode.opening)); - final gameOpening = ref.watch(ctrlProvider.select((s) => s.gameOpening)); - final opening = nodeOpening ?? gameOpening?.$2; + final branchOpening = + ref.watch(ctrlProvider.select((s) => s.currentBranchOpening)); + final opening = nodeOpening ?? branchOpening; return opening != null ? Container( height: kEvalGaugeSize,