From 864eb6b1eacf679075eb63c007d3baec5f2fc1fb Mon Sep 17 00:00:00 2001 From: sdalvi10 Date: Thu, 6 Feb 2025 00:55:56 +0530 Subject: [PATCH] Added emergency sound to the clock tool --- .../model/clock/clock_tool_controller.dart | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) mode change 100644 => 100755 lib/src/model/clock/clock_tool_controller.dart diff --git a/lib/src/model/clock/clock_tool_controller.dart b/lib/src/model/clock/clock_tool_controller.dart old mode 100644 new mode 100755 index 4f559511df..259bd460c5 --- a/lib/src/model/clock/clock_tool_controller.dart +++ b/lib/src/model/clock/clock_tool_controller.dart @@ -12,20 +12,30 @@ part 'clock_tool_controller.g.dart'; @riverpod class ClockToolController extends _$ClockToolController { late final ChessClock _clock; + final Map _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(); }); @@ -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); @@ -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, @@ -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,