diff --git a/lib/game/bloc/game_bloc.dart b/lib/game/bloc/game_bloc.dart index 795d04e2..08afb383 100644 --- a/lib/game/bloc/game_bloc.dart +++ b/lib/game/bloc/game_bloc.dart @@ -16,6 +16,8 @@ class GameBloc extends Bloc { on(_onGameStarted); } + final _maxScore = 9999999999; + void _onGameStarted(GameStarted _, Emitter emit) { emit(state.copyWith(status: GameStatus.playing)); } @@ -41,9 +43,11 @@ class GameBloc extends Bloc { void _onScored(Scored event, Emitter emit) { if (state.status.isPlaying) { - emit( - state.copyWith(roundScore: state.roundScore + event.points), + final combinedScore = math.min( + state.totalScore + state.roundScore + event.points, + _maxScore, ); + emit(state.copyWith(roundScore: combinedScore - state.totalScore)); } } diff --git a/test/game/bloc/game_bloc_test.dart b/test/game/bloc/game_bloc_test.dart index 9e8c6354..74d852c1 100644 --- a/test/game/bloc/game_bloc_test.dart +++ b/test/game/bloc/game_bloc_test.dart @@ -167,6 +167,23 @@ void main() { ), ], ); + + blocTest( + "doesn't increase score above the max score", + build: GameBloc.new, + seed: () => const GameState( + totalScore: 9999999998, + roundScore: 0, + multiplier: 1, + rounds: 1, + bonusHistory: [], + status: GameStatus.playing, + ), + act: (bloc) => bloc.add(const Scored(points: 2)), + expect: () => [ + isA()..having((state) => state.roundScore, 'roundScore', 1) + ], + ); }); group('MultiplierIncreased', () {