From a1329ec354867f4cc5ea3188d6920b43fe3ff07f Mon Sep 17 00:00:00 2001 From: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> Date: Sun, 8 May 2022 15:56:58 -0500 Subject: [PATCH] chore: set max score (#408) --- lib/game/bloc/game_bloc.dart | 13 +++++++--- test/game/bloc/game_bloc_test.dart | 39 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/game/bloc/game_bloc.dart b/lib/game/bloc/game_bloc.dart index 795d04e2..c63bf514 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); } + static const _maxScore = 9999999999; + void _onGameStarted(GameStarted _, Emitter emit) { emit(state.copyWith(status: GameStatus.playing)); } @@ -25,7 +27,10 @@ class GameBloc extends Bloc { } void _onRoundLost(RoundLost event, Emitter emit) { - final score = state.totalScore + state.roundScore * state.multiplier; + final score = math.min( + state.totalScore + state.roundScore * state.multiplier, + _maxScore, + ); final roundsLeft = math.max(state.rounds - 1, 0); emit( @@ -41,9 +46,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..2108b950 100644 --- a/test/game/bloc/game_bloc_test.dart +++ b/test/game/bloc/game_bloc_test.dart @@ -91,6 +91,28 @@ void main() { ], ); + blocTest( + "multiplier doesn't increase score above the max score", + build: GameBloc.new, + seed: () => const GameState( + totalScore: 9999999998, + roundScore: 1, + multiplier: 2, + rounds: 1, + bonusHistory: [], + status: GameStatus.playing, + ), + act: (bloc) => bloc.add(const RoundLost()), + expect: () => [ + isA() + ..having( + (state) => state.totalScore, + 'totalScore', + 9999999999, + ) + ], + ); + blocTest( 'resets multiplier when round is lost', build: GameBloc.new, @@ -167,6 +189,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', () {