Skip to content

Commit

Permalink
Go to analysis from game position
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Oct 10, 2023
1 parent 7f850e6 commit 83597d1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/src/model/analysis/analysis_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AnalysisOptions with _$AnalysisOptions {
required String initialFen,
required int initialPly,
required IList<Move> moves,
int? initialMoveCursor,
LightOpening? opening,
}) = _AnalysisOptions;
}
Expand Down Expand Up @@ -61,6 +62,7 @@ class AnalysisController extends _$AnalysisController {
int ply = options.initialPly;
Position position = initialPosition;
Node current = _root;
UciPath path = UciPath.empty;
for (final move in options.moves) {
final (newPos, san) = position.playToSan(move);
position = newPos;
Expand All @@ -73,9 +75,15 @@ class AnalysisController extends _$AnalysisController {
);
current.addChild(nextNode);
current = nextNode;
if (options.initialMoveCursor != null &&
ply <= options.initialMoveCursor!) {
path = path + nextNode.id;
}
}

final currentPath = _root.mainlinePath;
final currentPath =
options.initialMoveCursor == null ? _root.mainlinePath : path;
final currentNode = _root.nodeAt(currentPath);

// don't use ref.watch here: we don't want to invalidate state when the
// analysis preferences change
Expand All @@ -100,7 +108,7 @@ class AnalysisController extends _$AnalysisController {
initialPath: UciPath.empty,
currentPath: currentPath,
root: _root.view,
currentNode: current.view,
currentNode: currentNode.view,
pov: options.orientation,
evaluationContext: evalContext,
contextOpening: options.opening,
Expand Down
1 change: 1 addition & 0 deletions lib/src/model/game/game_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ class GameState with _$GameState {
.where((e) => e.sanMove != null)
.map((e) => e.sanMove!.move),
),
initialMoveCursor: stepCursor,
orientation: game.youAre ?? Side.white,
id: game.meta.id,
);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/view/game/archived_game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,9 @@ class _BottomBar extends ConsumerWidget {
semanticsLabel: context.l10n.gameAnalysis,
onPressed: ref.read(gameCursorProvider(gameData.id)).hasValue
? () {
final game = ref
final (game, cursor) = ref
.read(gameCursorProvider(gameData.id))
.requireValue
.$1;
.requireValue;

pushPlatformRoute(
context,
Expand All @@ -261,6 +260,7 @@ class _BottomBar extends ConsumerWidget {
.where((e) => e.sanMove != null)
.map((e) => e.sanMove!.move),
),
initialMoveCursor: cursor,
orientation: orientation,
id: gameData.id,
opening: gameData.opening,
Expand Down

0 comments on commit 83597d1

Please sign in to comment.