Skip to content

Commit

Permalink
Merge branch 'julien4215-coordinate-training-adjustement'
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Oct 11, 2024
2 parents ac8b526 + de176dc commit c3db887
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 152 deletions.
4 changes: 2 additions & 2 deletions lib/src/model/common/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import 'package:lichess_mobile/l10n/l10n.dart';

/// Represents the choice of a side as a player: white, black or random.
enum SideChoice {
random,
white,
random,
black;

String label(AppLocalizations l10n) => switch (this) {
SideChoice.random => l10n.randomColor,
SideChoice.white => l10n.white,
SideChoice.random => l10n.randomColor,
SideChoice.black => l10n.black,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:math';

import 'package:dartchess/dartchess.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/common/game.dart';
import 'package:lichess_mobile/src/model/coordinate_training/coordinate_training_preferences.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'coordinate_training_controller.freezed.dart';
Expand All @@ -23,7 +25,12 @@ class CoordinateTrainingController extends _$CoordinateTrainingController {
ref.onDispose(() {
_updateTimer?.cancel();
});
return const CoordinateTrainingState();
final sideChoice = ref.watch(
coordinateTrainingPreferencesProvider.select((value) => value.sideChoice),
);
return CoordinateTrainingState(
orientation: _getOrientation(sideChoice),
);
}

void startTraining(Duration? timeLimit) {
Expand Down Expand Up @@ -53,15 +60,34 @@ class CoordinateTrainingController extends _$CoordinateTrainingController {

void _finishTraining() {
// TODO save score in local storage here (and display high score and/or average score in UI)

stopTraining();
final orientation = _getOrientation(
ref.read(coordinateTrainingPreferencesProvider).sideChoice,
);
_updateTimer?.cancel();
state = CoordinateTrainingState(
lastGuess: state.lastGuess,
lastScore: state.score,
orientation: orientation,
);
}

void stopTraining() {
void abortTraining() {
final orientation = _getOrientation(
ref.read(coordinateTrainingPreferencesProvider).sideChoice,
);
_updateTimer?.cancel();
state = const CoordinateTrainingState();
state = CoordinateTrainingState(orientation: orientation);
}

Side _getOrientation(SideChoice choice) => switch (choice) {
SideChoice.white => Side.white,
SideChoice.black => Side.black,
SideChoice.random => _randomSide(),
};

/// Generate a random side
Side _randomSide() => Side.values[Random().nextInt(Side.values.length)];

/// Generate a random square that is not the same as the [previous] square
Square _randomCoord({Square? previous}) {
while (true) {
Expand Down Expand Up @@ -100,6 +126,8 @@ class CoordinateTrainingState with _$CoordinateTrainingState {
@Default(null) Duration? timeLimit,
@Default(null) Duration? elapsed,
@Default(null) Guess? lastGuess,
required Side orientation,
@Default(null) int? lastScore,
}) = _CoordinateTrainingState;

bool get trainingActive => elapsed != null;
Expand Down
Loading

0 comments on commit c3db887

Please sign in to comment.