Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added emergency sound to the clock tool #1409

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/src/model/clock/clock_tool_controller.dart
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@ part 'clock_tool_controller.g.dart';
@riverpod
class ClockToolController extends _$ClockToolController {
late final ChessClock _clock;
final Map<Side, bool> _hasPlayedLowTimeSound = {Side.white: false, Side.black: false};
late Duration _emergencyThreshold;

@override
ClockState build() {
const time = Duration(minutes: 10);
const increment = Duration.zero;
_emergencyThreshold = _calculateEmergencyThreshold(time);
const options = ClockOptions(
whiteTime: time,
blackTime: time,
whiteIncrement: increment,
blackIncrement: increment,
);

_clock = ChessClock(whiteTime: time, blackTime: time, onFlag: _onFlagged);

// Add listeners for both clocks
_clock.whiteTime.addListener(onClockEmergency);
_clock.blackTime.addListener(onClockEmergency);

ref.onDispose(() {
_clock.whiteTime.removeListener(onClockEmergency);
_clock.blackTime.removeListener(onClockEmergency);
_clock.dispose();
});

Expand All @@ -37,6 +47,22 @@ class ClockToolController extends _$ClockToolController {
);
}

// Emergency threshold is set to 10% of the total time
Duration _calculateEmergencyThreshold(Duration initialTime) {
return Duration(milliseconds: (initialTime.inMilliseconds * 0.1).round());
}

void onClockEmergency() {
final activeSide = state.activeSide;
if (_hasPlayedLowTimeSound[activeSide]!) return;
final activeSideTime =
activeSide == Side.white ? _clock.whiteTime.value : _clock.blackTime.value;
if (activeSideTime <= _emergencyThreshold) {
ref.read(soundServiceProvider).play(Sound.lowTime);
_hasPlayedLowTimeSound[activeSide] = true;
}
}

void _onFlagged() {
_clock.stop();
state = state.copyWith(flagged: _clock.activeSide);
Expand Down Expand Up @@ -78,6 +104,9 @@ class ClockToolController extends _$ClockToolController {

void updateOptions(TimeIncrement timeIncrement) {
final options = ClockOptions.fromTimeIncrement(timeIncrement);
_emergencyThreshold = _calculateEmergencyThreshold(Duration(seconds: timeIncrement.time));
_hasPlayedLowTimeSound[Side.white] = false;
_hasPlayedLowTimeSound[Side.black] = false;
_clock.setTimes(whiteTime: options.whiteTime, blackTime: options.blackTime);
state = state.copyWith(
options: options,
Expand Down Expand Up @@ -108,6 +137,9 @@ class ClockToolController extends _$ClockToolController {

void reset() {
_clock.setTimes(whiteTime: state.options.whiteTime, blackTime: state.options.whiteTime);
// Reset low time sound flags for both players
_hasPlayedLowTimeSound[Side.white] = false;
_hasPlayedLowTimeSound[Side.black] = false;
state = state.copyWith(
whiteTime: _clock.whiteTime,
blackTime: _clock.blackTime,
Expand Down