Skip to content

Commit

Permalink
Get the current opening by traversing the branch
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Sep 23, 2023
1 parent ea242f2 commit 5db7fc5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
25 changes: 16 additions & 9 deletions lib/src/model/analysis/analysis_ctrl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -206,6 +218,7 @@ class AnalysisCtrl extends _$AnalysisCtrl {
state = state.copyWith(
currentPath: path,
currentNode: state.root,
currentBranchOpening: opening,
lastMove: null,
);
}
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -278,7 +285,7 @@ class AnalysisCtrlState with _$AnalysisCtrlState {
required Side pov,
required EvaluationContext evaluationContext,
Move? lastMove,
(UciPath, Opening)? gameOpening,
Opening? currentBranchOpening,
}) = _AnalysisCtrlState;

IMap<String, ISet<String>> get validMoves =>
Expand Down
5 changes: 3 additions & 2 deletions lib/src/view/analysis/analysis_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5db7fc5

Please sign in to comment.