-
-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathretro_controller.dart
More file actions
620 lines (513 loc) · 20.1 KB
/
retro_controller.dart
File metadata and controls
620 lines (513 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import 'dart:async';
import 'dart:math';
import 'package:dartchess/dartchess.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart';
import 'package:lichess_mobile/src/model/analysis/common_analysis_state.dart';
import 'package:lichess_mobile/src/model/analysis/server_analysis_service.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
import 'package:lichess_mobile/src/model/common/eval.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/common/node.dart';
import 'package:lichess_mobile/src/model/common/service/move_feedback.dart';
import 'package:lichess_mobile/src/model/common/service/sound_service.dart';
import 'package:lichess_mobile/src/model/common/uci.dart';
import 'package:lichess_mobile/src/model/engine/evaluation_mixin.dart';
import 'package:lichess_mobile/src/model/engine/evaluation_preferences.dart';
import 'package:lichess_mobile/src/model/explorer/opening_explorer_preferences.dart';
import 'package:lichess_mobile/src/model/explorer/opening_explorer_repository.dart';
import 'package:lichess_mobile/src/model/game/exported_game.dart';
import 'package:lichess_mobile/src/model/game/game_repository_providers.dart';
import 'package:lichess_mobile/src/network/socket.dart';
import 'package:lichess_mobile/src/view/engine/engine_gauge.dart';
import 'package:logging/logging.dart';
part 'retro_controller.freezed.dart';
typedef RetroOptions = ({GameId id, Side initialSide});
final Logger _logger = Logger('RetroController');
@freezed
sealed class Mistake with _$Mistake {
const Mistake._();
const factory Mistake({
required ViewBranch branch,
@Default(IList<UCIMove>.empty()) IList<UCIMove> openingExplorerSolutions,
}) = _Mistake;
ViewBranch get userBranch => branch.children[0];
Move get userMove => userBranch.sanMove.move;
ViewBranch get serverBranch => branch.children[1];
Move get serverMove => serverBranch.sanMove.move;
bool isSolution(RetroCurrentNode node) =>
node.position == serverBranch.position ||
node.position.isCheckmate ||
// Any move that was found in the master's database is also considered a solution.
openingExplorerSolutions.contains(node.sanMove!.move.uci);
}
/// Depth needed to evaluate alternative solution moves.
const double _kEvalDepthThreshold = kDebugMode ? 12 : 18;
const Duration _kLowerDepthThresholdTime = Duration(seconds: 6);
/// If we don't reach [_kEvalDepthThreshold] after [_kLowerDepthThresholdTime], we lower the depth threshold to this value.
const double _kEvalDepthThresholdAfterLongEvalTime = _kEvalDepthThreshold - 4;
/// Threshold for considering a move a correct alternative solution.
///
/// When checking a move that is not the server solution,
/// consider it a correct move if the eval difference is above this threshold,
/// i.e. the move does not make the position significantly worse.
const double _kCorrectMovePovDiffThreshold = -0.04;
/// Threshold for considering a move a mistake due to how it affected the evaluation.
const double _kEvalSwingThreshold = 0.1;
/// A provider for [RetroController].
final retroControllerProvider = AsyncNotifierProvider.autoDispose
.family<RetroController, RetroState, RetroOptions>(
RetroController.new,
name: 'RetroControllerProvider',
);
class RetroController extends AsyncNotifier<RetroState> with EngineEvaluationMixin {
RetroController(this.options);
final RetroOptions options;
late Root _root;
late ExportedGame _game;
final Completer<void> _serverAnalysisCompleter = Completer<void>();
@override
@protected
late SocketClient socketClient;
@override
@protected
Node get positionTree => _root;
@override
Future<RetroState> build() async {
final serverAnalysisService = ref.watch(serverAnalysisServiceProvider);
ref.onDispose(() {
serverAnalysisService.lastAnalysisEvent.removeListener(_listenToServerAnalysisEvents);
});
socketClient = ref.watch(socketPoolProvider).open(AnalysisController.socketUri);
_game = await ref.watch(archivedGameProvider(options.id).future);
_root = _game.makeTree();
if (_game.serverAnalysis == null) {
final retroState = RetroState(
serverAnalysisAvailable: false,
mistakes: const IList.empty(),
currentMistakeIndex: 0,
feedback: RetroFeedback.findMove,
mainlinePath: _root.mainlinePath,
pov: options.initialSide,
currentNode: RetroCurrentNode.fromNode(_root),
variant: _game.meta.variant,
currentPath: UciPath.empty,
root: _root.view,
evaluationContext: EvaluationContext(
id: options.id,
variant: _game.meta.variant,
initialPosition: _root.position,
),
);
state = AsyncValue.data(retroState);
// Attach listener BEFORE possibly requesting analysis,
// so we don't miss the first progress event.
serverAnalysisService.lastAnalysisEvent.addListener(_listenToServerAnalysisEvents);
// Reuse an already available event immediately if it belongs to this game.
final existingEvent = serverAnalysisService.lastAnalysisEvent.value;
if (existingEvent != null && existingEvent.$1 == options.id) {
ServerAnalysisService.mergeOngoingAnalysis(_root, existingEvent.$2.tree);
final progress =
existingEvent.$2.evals.where((e) => e.hasEval).length / _root.mainline.length;
state = AsyncValue.data(state.requireValue.copyWith(serverAnalysisProgress: progress));
if (existingEvent.$2.isAnalysisComplete) {
if (!_serverAnalysisCompleter.isCompleted) {
_serverAnalysisCompleter.complete();
}
state = AsyncData(await _computeMistakes(options.initialSide));
socketClient.firstConnection.then((_) {
requestEval();
});
return state.requireValue;
}
}
// Only request analysis if this exact game is not already being analyzed.
if (serverAnalysisService.currentAnalysis.value != options.id) {
await serverAnalysisService.requestAnalysis(options.id);
}
unawaited(
_serverAnalysisCompleter.future.timeout(
kMaxWaitForServerAnalysis,
onTimeout: () {
_logger.warning(
'Server analysis did not finish within $kMaxWaitForServerAnalysis for game ${options.id}',
);
state = AsyncError(
Exception('Server analysis did not finish within $kMaxWaitForServerAnalysis'),
StackTrace.current,
);
},
),
);
return state.requireValue;
}
state = AsyncData(await _computeMistakes(options.initialSide));
socketClient.firstConnection.then((_) {
requestEval();
});
return state.requireValue;
}
Future<RetroState> _computeMistakes(Side side) async {
final mistakes = (await Future.wait(
_root.mainline.map((branch) async {
if (branch.position.turn != side || branch.children.isEmpty) {
return null;
}
final eval = branch.externalEval;
final newEval = branch.children.first.externalEval;
if (eval == null || newEval == null) {
return null;
}
final bigEvalSwing =
Eval.winningChancesPovDiff(side, eval, newEval).abs() > _kEvalSwingThreshold;
final lostEasyMate = eval.mate != null && newEval.mate == null && eval.mate!.abs() <= 3;
final hasSolution = branch.children.length > 1;
final isMistake = (bigEvalSwing || lostEasyMate) && hasSolution;
if (!isMistake) return null;
var openingExplorerSolutions = const IList<UCIMove>.empty();
final middlegame = _game.meta.division?.middlegame;
if (middlegame == null || branch.position.ply + 1 < middlegame) {
try {
final entry = await ref
.read(openingExplorerRepositoryProvider)
.getMasterDatabase(branch.position.fen, since: MasterDb.kEarliestYear);
final masterMovesPlayedMoreThanOnce = entry.moves.where(
(move) => move.white + move.draws + move.black > 1,
);
// If we find this move in a master's game, be generous and not consider it a mistake.
if (masterMovesPlayedMoreThanOnce.any(
(move) => move.uci == branch.children.first.sanMove.move.uci,
)) {
return null;
}
openingExplorerSolutions = masterMovesPlayedMoreThanOnce
.map((move) => move.uci)
.toIList();
} catch (e, st) {
_logger.warning(
'Failed to fetch opening explorer data for ply ${branch.position.ply}',
e,
st,
);
}
}
return Mistake(branch: branch.view, openingExplorerSolutions: openingExplorerSolutions);
}),
)).nonNulls.toIList();
return RetroState(
serverAnalysisAvailable: true,
mistakes: mistakes.toIList(),
currentMistakeIndex: 0,
feedback: mistakes.isNotEmpty ? RetroFeedback.findMove : RetroFeedback.done,
mainlinePath: _root.mainlinePath,
pov: side,
currentNode: RetroCurrentNode.fromNode(mistakes.firstOrNull?.branch.branch ?? _root),
lastMove: mistakes.firstOrNull?.branch.sanMove.move,
variant: _game.meta.variant,
root: _root.view,
evaluationContext: EvaluationContext(
id: options.id,
variant: _game.meta.variant,
initialPosition: _root.position,
),
currentPath: mistakes.isNotEmpty
? _root.mainlinePath.truncate(mistakes[0].branch.position.ply)
: UciPath.empty,
);
}
void onUserMove(Move move) {
if (!state.requireValue.currentPosition.isLegal(move)) return;
if (move case NormalMove() when isPromotionPawnMove(state.requireValue.currentPosition, move)) {
state = AsyncValue.data(state.requireValue.copyWith(promotionMove: move));
return;
}
final (newPath, isNewNode) = _root.addMoveAt(state.requireValue.currentPath, move);
if (newPath != null) {
_setPath(newPath);
}
}
void onPromotionSelection(Role? role) {
final state = this.state.value;
if (state == null) return;
if (role == null) {
this.state = AsyncValue.data(state.copyWith(promotionMove: null));
return;
}
final promotionMove = state.promotionMove;
if (promotionMove != null) {
final promotion = promotionMove.withPromotion(role);
onUserMove(promotion);
}
}
void userNext() {
_setPath(
state.requireValue.currentPath +
_root.nodeAt(state.requireValue.currentPath).children.first.id,
isNavigating: true,
);
}
void userPrevious() {
_setPath(state.requireValue.currentPath.penultimate, isNavigating: true);
}
void viewSolution() {
final currentMistake = state.value?.currentMistake;
if (currentMistake != null) {
onUserMove(currentMistake.serverMove);
state = AsyncValue.data(state.requireValue.copyWith(feedback: RetroFeedback.viewingSolution));
}
}
Future<void> flipSide() async {
state = AsyncValue.data(await _computeMistakes(state.requireValue.pov.opposite));
}
void restart() {
_showMistake(0);
}
void nextMistake() {
_showMistake(state.requireValue.currentMistakeIndex + 1);
}
void _showMistake(int index) {
final mistake = state.requireValue.mistakes.getOrNull(index);
final lastMistake = state.requireValue.mistakes.lastOrNull;
_setPath(
_root.mainlinePath.truncate(
mistake?.branch.position.ply ?? lastMistake?.branch.position.ply ?? _root.mainlinePath.size,
),
);
state = AsyncValue.data(
state.requireValue.copyWith(
currentMistakeIndex: index,
currentNode: RetroCurrentNode.fromNode(
mistake?.branch.branch ?? lastMistake?.branch.branch ?? _root,
),
feedback: mistake != null ? RetroFeedback.findMove : RetroFeedback.done,
),
);
}
void _setPath(
UciPath path, {
/// Whether the user is navigating through the moves (as opposed to playing a move).
bool isNavigating = false,
}) {
final state = this.state.value;
if (state == null) return;
final pathChange = state.currentPath != path;
final currentNode = _root.nodeAt(path);
final isForward = path.size > state.currentPath.size;
if (currentNode is Branch) {
// normal move feedback
if (!isNavigating && isForward) {
final isCheck = currentNode.sanMove.isCheck;
if (currentNode.sanMove.isCapture) {
ref.read(moveFeedbackServiceProvider).captureFeedback(state.variant, check: isCheck);
} else {
ref.read(moveFeedbackServiceProvider).moveFeedback(check: isCheck);
}
}
// if navigating, only sound feedback
else {
final soundService = ref.read(soundServiceProvider);
if (currentNode.sanMove.isCapture) {
soundService.playCaptureSound(state.variant);
} else {
soundService.play(Sound.move);
}
}
this.state = AsyncValue.data(
state.copyWith(
currentPath: path,
currentNode: RetroCurrentNode.fromNode(currentNode),
lastMove: currentNode.sanMove.move,
promotionMove: null,
root: isNavigating ? state.root : _root.view,
),
);
} else {
this.state = AsyncValue.data(
state.copyWith(
currentPath: path,
currentNode: RetroCurrentNode.fromNode(currentNode),
lastMove: null,
promotionMove: null,
root: isNavigating ? state.root : _root.view,
),
);
}
if (pathChange) {
this.state = AsyncValue.data(this.state.requireValue.copyWith(engineInThreatMode: false));
requestEval();
}
_updateFeedback();
}
void _onIncorrectMove() {
state = AsyncValue.data(state.requireValue.copyWith(feedback: RetroFeedback.incorrect));
userPrevious();
}
void _onCorrectMove() {
state = AsyncValue.data(state.requireValue.copyWith(feedback: RetroFeedback.correct));
}
@override
void onCurrentPathEvalChanged(bool isSameEvalString) {
_refreshCurrentNode(recomputeRootView: !isSameEvalString);
if (state.requireValue.feedback == RetroFeedback.evalMove) {
final eval = state.requireValue.currentNode.eval;
if (eval == null) return;
if (eval.depth >= _kEvalDepthThreshold ||
(eval.depth >= _kEvalDepthThresholdAfterLongEvalTime &&
state.requireValue.evalTime! > _kLowerDepthThresholdTime)) {
final diff = Eval.winningChancesPovDiff(
state.requireValue.pov,
eval,
state.requireValue.currentMistake!.branch.serverEval!,
);
if (diff > _kCorrectMovePovDiffThreshold) {
_onCorrectMove();
// We used unlimited search time during Feedback.evalMove,
// now restart evaluation with normal search time
requestEval();
} else {
_onIncorrectMove();
}
}
}
}
void _refreshCurrentNode({bool recomputeRootView = false}) {
state = AsyncData(
state.requireValue.copyWith(
currentNode: RetroCurrentNode.fromNode(_root.nodeAt(state.requireValue.currentPath)),
),
);
}
void _updateFeedback() {
final state = this.state.requireValue;
switch (state.feedback) {
case RetroFeedback.incorrect:
case RetroFeedback.findMove:
if (state.currentPosition.ply == state.currentMistake!.serverBranch.position.ply) {
if (state.currentMistake!.isSolution(state.currentNode)) {
_onCorrectMove();
} else if (state.currentPosition == state.currentMistake!.userBranch.position) {
Timer(const Duration(milliseconds: 500), () {
_onIncorrectMove();
});
} else {
this.state = AsyncValue.data(
state.copyWith(feedback: RetroFeedback.evalMove, evalRequestedAt: DateTime.now()),
);
// Be sure to get enough depth to evaluate the move properly
requestEval(goDeeper: true);
}
}
case _:
}
}
Future<void> _listenToServerAnalysisEvents() async {
if (!state.hasValue) return;
final event = ref.read(serverAnalysisServiceProvider).lastAnalysisEvent.value;
if (event != null && event.$1 == options.id) {
ServerAnalysisService.mergeOngoingAnalysis(_root, event.$2.tree);
final progress = event.$2.evals.where((e) => e.hasEval).length / _root.mainline.length;
state = AsyncValue.data(state.requireValue.copyWith(serverAnalysisProgress: progress));
if (event.$2.isAnalysisComplete) {
if (_serverAnalysisCompleter.isCompleted == false) {
_serverAnalysisCompleter.complete();
}
state = AsyncData(await _computeMistakes(options.initialSide));
requestEval();
}
}
}
}
enum RetroFeedback { findMove, evalMove, correct, incorrect, viewingSolution, done }
@freezed
sealed class RetroState
with _$RetroState, AnalysisExplosionMixin, EvaluationMixinState<RetroState>
implements CommonAnalysisState {
const RetroState._();
@override
RetroState withThreatMode(bool engineInThreatMode) =>
copyWith(engineInThreatMode: engineInThreatMode);
const factory RetroState({
required bool serverAnalysisAvailable,
/// Progress of server analysis for the whole game, from 0.0 to 1.0.
double? serverAnalysisProgress,
required IList<Mistake> mistakes,
required int currentMistakeIndex,
required RetroFeedback feedback,
required UciPath mainlinePath,
required Side pov,
required RetroCurrentNode currentNode,
required Variant variant,
required UciPath currentPath,
required EvaluationContext evaluationContext,
required ViewRoot root,
DateTime? evalRequestedAt,
Move? lastMove,
NormalMove? promotionMove,
@Default(false) bool engineInThreatMode,
}) = _RetroState;
@override
bool get alwaysRequestCloudEval => false;
bool get isSolving =>
feedback == RetroFeedback.findMove ||
feedback == RetroFeedback.incorrect ||
feedback == RetroFeedback.evalMove;
Duration? get evalTime =>
evalRequestedAt != null ? DateTime.now().difference(evalRequestedAt!) : null;
double get evalProgress => feedback == RetroFeedback.evalMove && currentNode.eval != null
? min(1.0, currentNode.eval!.depth / _kEvalDepthThreshold)
: 0.0;
@override
Position get currentPosition => currentNode.position;
@override
ViewRoot get analysisRoot => root;
@override
bool isEngineAvailable(EngineEvaluationPrefState prefs) => true;
bool get hasMistakes => mistakes.isNotEmpty;
Mistake? get currentMistake => mistakes.getOrNull(currentMistakeIndex);
EngineGaugeParams get engineGaugeParams => (
isLocalEngineAvailable: true,
orientation: pov,
position: currentPosition,
savedEval: currentNode.eval,
serverEval: currentNode.serverEval,
filters: (id: evaluationContext.id, path: currentPath),
);
bool get canGoNext => !isSolving && currentNode.hasChild;
bool get canGoBack => !isSolving && currentPath.size > UciPath.empty.size;
}
@freezed
sealed class RetroCurrentNode with _$RetroCurrentNode implements AnalysisCurrentNodeInterface {
const RetroCurrentNode._();
const factory RetroCurrentNode({
required Position position,
required bool isRoot,
required bool hasChild,
SanMove? sanMove,
ClientEval? eval,
ExternalEval? serverEval,
IList<int>? nags,
}) = _RetroCurrentNode;
factory RetroCurrentNode.fromNode(Node node) {
if (node is Branch) {
return RetroCurrentNode(
sanMove: node.sanMove,
position: node.position,
isRoot: node is Root,
eval: node.eval,
serverEval: node.externalEval,
nags: IList(node.nags),
hasChild: node.children.isNotEmpty,
);
} else {
return RetroCurrentNode(
position: node.position,
isRoot: node is Root,
eval: node.eval,
hasChild: node.children.isNotEmpty,
);
}
}
}