From 6a1ac8c38523d649533aef9e81a577ea3ba38906 Mon Sep 17 00:00:00 2001 From: balladyna <91394225+balladyna@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:06:08 +0200 Subject: [PATCH] Feature: First Gen second pair inheritance This feature introduces IV button management for monsters in the second pair of the First Generation. The logic for enabling and disabling the fourth monster's button considers values from the preceding monster and those from the previous pair. This ensures logical consistency and refined user interaction. List of Changes: - in FirstGenCubit added setIVStateIndexThree() and setIVStateIndexFour() methods for managing the IV buttons of the third and the fourth monster - in FirstGenCubit extracted setIVStateIndexOne() -> _getSingleBlockedIVButtonMap() and setIVStateIndexTwo() -> _getSingleUnblockedIVButtonMap() for generating ivButtonMap, which are responsible for enabling and disabling ivButtons in the sliding panel depending on the user-selected IV. These methods are used in setIVStateIndexThree() and setIVStateIndexFour(). --- .../first_gen/first_gen_cubit.dart | 76 +++++++++--- .../second_gen/second_gen_cubit.dart | 4 +- .../first_gen_widgets/first_gen_widget.dart | 4 +- pubspec.yaml | 2 +- .../first_gen/first_gen_cubit_test.dart | 109 ++++++++++++++++++ 5 files changed, 175 insertions(+), 20 deletions(-) diff --git a/lib/blocks/pages/genealogical_tree/first_gen/first_gen_cubit.dart b/lib/blocks/pages/genealogical_tree/first_gen/first_gen_cubit.dart index 1664e75..aa394ed 100644 --- a/lib/blocks/pages/genealogical_tree/first_gen/first_gen_cubit.dart +++ b/lib/blocks/pages/genealogical_tree/first_gen/first_gen_cubit.dart @@ -37,18 +37,15 @@ class FirstGenCubit extends Cubit { } void setIVStateIndexOne() { - Map ivButtonsMap = Map.from(_ivButtonsModel.ivButtonsMap); - List currentIVColorList = firstGenModel.firstGenMap[FirstGenIndex.one]!.ivColorList; + Map ivButtonsMap = _initializeIVButtonsMap(); if (firstGenModel.hasNonDefaultIVColor(FirstGenIndex.one)) { - for (IVColor key in ivButtonsMap.keys) { - ivButtonsMap[key] = currentIVColorList.contains(key); - } + ivButtonsMap = _getSingledUnblockedIVButtonMap(FirstGenIndex.one); } emit(FirstGenIVButtonsState(ivButtonMap: ivButtonsMap)); } void setIVStateIndexTwo() { - Map ivButtonsMap = _updateMapFromPreviousMonster(FirstGenIndex.one); + Map ivButtonsMap = _getSingleBlockedIVButtonMap(FirstGenIndex.one); List currentIVColorList = firstGenModel.firstGenMap[FirstGenIndex.two]!.ivColorList; if (firstGenModel.hasNonDefaultIVColor(FirstGenIndex.two)) { for (IVColor key in ivButtonsMap.keys) { @@ -58,11 +55,44 @@ class FirstGenCubit extends Cubit { emit(FirstGenIVButtonsState(ivButtonMap: ivButtonsMap)); } + void setIVStateIndexThree() { + Map ivButtonsMap = _initializeIVButtonsMap(); + if (firstGenModel.hasNonDefaultIVColor(FirstGenIndex.three)) { + ivButtonsMap = _getSingledUnblockedIVButtonMap(FirstGenIndex.three); + } + emit(FirstGenIVButtonsState(ivButtonMap: ivButtonsMap)); + } + + void setIVStateIndexFour() { + Map ivButtonsMap = _initializeIVButtonsMap(); + + if (firstGenModel.hasNonDefaultIVColor(FirstGenIndex.four)) { + ivButtonsMap = _getSingledUnblockedIVButtonMap(FirstGenIndex.four); + } else { + Map> ivColorMap = _getIVColorMap(); + + bool previousIVColorBool = _hasPreviousIVColor(FirstGenIndex.three); + + if (previousIVColorBool) { + for (IVColor key in ivButtonsMap.keys) { + ivButtonsMap[key] = !ivColorMap.values.any((List ivColorList) => ivColorList.contains(key)); + } + } else { + for (IVColor key in ivButtonsMap.keys) { + ivButtonsMap[key] = ivColorMap.entries + .where((MapEntry> entry) => entry.key.value < FirstGenIndex.three.value) + .any((MapEntry> entry) => entry.value.contains(key)); + } + } + } + emit(FirstGenIVButtonsState(ivButtonMap: ivButtonsMap)); + } + Map getIVButtonsState() { if (state is FirstGenIVButtonsState) { return (state as FirstGenIVButtonsState).ivButtonMap; } else { - return _initializeIVButtons(); + return _initializeIVButtonsMap(); } } @@ -100,7 +130,7 @@ class FirstGenCubit extends Cubit { bool _isMonsterEnabled(FirstGenIndex firstGenIndex) { if (firstGenIndex.value == 1) { return true; - } else if (firstGenIndex.value > 2) { + } else if (firstGenIndex.value > 4) { return false; } @@ -111,7 +141,22 @@ class FirstGenCubit extends Cubit { return firstGenModel.hasNonDefaultIVColor(previousIndex); } - Map _updateMapFromPreviousMonster(FirstGenIndex firstGenIndex) { + bool _hasPreviousIVColor(FirstGenIndex firstGenIndex) { + Map> ivColorMap = _getIVColorMap(); + List femaleIVColorList = ivColorMap[firstGenIndex]!; + bool hasIVColorBool = ivColorMap.entries + .where((MapEntry> entry) => entry.key.value < firstGenIndex.value) + .any((MapEntry> entry) => entry.value.contains(femaleIVColorList[0])); + return hasIVColorBool; + } + + Map> _getIVColorMap() { + Map> ivColorMap = firstGenModel.firstGenMap.map( + (FirstGenIndex firstGenIndex, MonsterModel monsterModel) => MapEntry>(firstGenIndex, monsterModel.ivColorList)); + return ivColorMap; + } + + Map _getSingleBlockedIVButtonMap(FirstGenIndex firstGenIndex) { Map ivButtonsMap = Map.from(_ivButtonsModel.ivButtonsMap); List previousMonsterIVList = firstGenModel.firstGenMap[firstGenIndex]!.ivColorList; for (IVColor key in ivButtonsMap.keys) { @@ -120,13 +165,16 @@ class FirstGenCubit extends Cubit { return ivButtonsMap; } - Map> _getIVColorMap() { - Map> ivColorMap = firstGenModel.firstGenMap.map( - (FirstGenIndex firstGenIndex, MonsterModel monsterModel) => MapEntry>(firstGenIndex, monsterModel.ivColorList)); - return ivColorMap; + Map _getSingledUnblockedIVButtonMap(FirstGenIndex firstGenIndex) { + Map ivButtonsMap = Map.from(_ivButtonsModel.ivButtonsMap); + List currentIVColorList = firstGenModel.firstGenMap[firstGenIndex]!.ivColorList; + for (IVColor key in ivButtonsMap.keys) { + ivButtonsMap[key] = currentIVColorList.contains(key); + } + return ivButtonsMap; } - Map _initializeIVButtons() { + Map _initializeIVButtonsMap() { Map ivButtonMap = Map.from(_ivButtonsModel.ivButtonsMap); return ivButtonMap; } diff --git a/lib/blocks/pages/genealogical_tree/second_gen/second_gen_cubit.dart b/lib/blocks/pages/genealogical_tree/second_gen/second_gen_cubit.dart index cf6ade4..e2abd8a 100644 --- a/lib/blocks/pages/genealogical_tree/second_gen/second_gen_cubit.dart +++ b/lib/blocks/pages/genealogical_tree/second_gen/second_gen_cubit.dart @@ -90,9 +90,7 @@ class SecondGenCubit extends Cubit { _firstGenCubit.firstGenModel.firstGenMap[fatherFirstGenIndex]!.ivColorList[0] ]; - bool isAutoFilledBool = secondGenModel.secondGenMap[secondGenIndex]!.isAutoFilledBool; - - if (parentsList.contains(IVColor.defaultColor) || isAutoFilledBool) { + if (parentsList.contains(IVColor.defaultColor)) { secondGenModel.setDefaultValues(secondGenIndex); } else { secondGenModel.inheritIVFromParents(secondGenIndex, parentsList); diff --git a/lib/views/pages/genealogical_tree_page/first_gen_widgets/first_gen_widget.dart b/lib/views/pages/genealogical_tree_page/first_gen_widgets/first_gen_widget.dart index 095c3b3..1c8bdca 100644 --- a/lib/views/pages/genealogical_tree_page/first_gen_widgets/first_gen_widget.dart +++ b/lib/views/pages/genealogical_tree_page/first_gen_widgets/first_gen_widget.dart @@ -28,8 +28,8 @@ class _FirstGenWidgetState extends State { final Map buttonMethods = { FirstGenIndex.one: firstGenCubit.setIVStateIndexOne, FirstGenIndex.two: firstGenCubit.setIVStateIndexTwo, - FirstGenIndex.three: () {}, - FirstGenIndex.four: () {}, + FirstGenIndex.three: firstGenCubit.setIVStateIndexThree, + FirstGenIndex.four: firstGenCubit.setIVStateIndexFour, FirstGenIndex.five: () {}, FirstGenIndex.six: () {}, FirstGenIndex.seven: () {}, diff --git a/pubspec.yaml b/pubspec.yaml index 3d56d3e..e9f37a3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: breeder description: "The Breeder App is designed to assist users in breeding monsters with perfect combat characteristics." publish_to: 'none' # Remove this line if you wish to publish to pub.dev -version: 0.0.5 +version: 0.0.6 environment: sdk: ">=3.2.3" diff --git a/test/unit/blocs/genealogical_tree/first_gen/first_gen_cubit_test.dart b/test/unit/blocs/genealogical_tree/first_gen/first_gen_cubit_test.dart index d3b17e7..6c12bcb 100644 --- a/test/unit/blocs/genealogical_tree/first_gen/first_gen_cubit_test.dart +++ b/test/unit/blocs/genealogical_tree/first_gen/first_gen_cubit_test.dart @@ -308,6 +308,115 @@ Future main() async { }); }); + group('Test of FirstGenCubit.setIVStateIndexThree()', () { + // Act + setUpAll(() { + actualFirstGenCubit = FirstGenCubit(); + }); + + test('Should emit [FirstGenIVButtonsState] with [ivButtonMap where all IVColors has true values] when [third monster IV is not filled with IV]', + () { + //Arrange + Map expectedIVButtonMap = Map.from(ivButtonsModel.ivButtonsMap); + + AFirstGenState expectedFirstGenState = FirstGenIVButtonsState(ivButtonMap: expectedIVButtonMap); + + // Act + actualFirstGenCubit.setIVStateIndexThree(); + + // Assert + expect(actualFirstGenCubit.state, expectedFirstGenState); + }); + + test( + 'Should emit [FirstGenIVButtonsState] with [ivButtonMap where only atkColor has true value] when [user choose attack IV for third monster]', + () { + //Arrange + late Map expectedIVButtonMap = { + for (IVColor ivColor in IVColor.values.where((IVColor ivColor) => ivColor != IVColor.defaultColor)) ivColor: false + }; + + expectedIVButtonMap[IVColor.atkColor] = true; + AFirstGenState expectedFirstGenState = FirstGenIVButtonsState(ivButtonMap: expectedIVButtonMap); + + // Act + actualFirstGenCubit.setIVColors(FirstGenIndex.three, IVColor.atkColor); + actualFirstGenCubit.setIVStateIndexThree(); + + // Assert + expect(actualFirstGenCubit.state, expectedFirstGenState); + }); + }); + + group('Test of FirstGenCubit.setIVStateIndexFour()', () { + // Act + setUpAll(() { + actualFirstGenCubit = FirstGenCubit(); + }); + + test( + 'Should emit [FirstGenIVButtonsState] with [ivButtonMap where atkColor and hpColor has false value] when [third monster is filled with attack IV and previous pair contain hp IV and attack IV] and [fourth monster is not filled with IV]', + () { + //Arrange + late Map expectedIVButtonMap = { + for (IVColor ivColor in IVColor.values.where((IVColor ivColor) => ivColor != IVColor.defaultColor)) ivColor: true + }; + + expectedIVButtonMap[IVColor.hpColor] = false; + expectedIVButtonMap[IVColor.atkColor] = false; + + AFirstGenState expectedFirstGenState = FirstGenIVButtonsState(ivButtonMap: expectedIVButtonMap); + + actualFirstGenCubit + ..setIVColors(FirstGenIndex.one, IVColor.atkColor) + ..setIVColors(FirstGenIndex.two, IVColor.hpColor) + ..setIVColors(FirstGenIndex.three, IVColor.atkColor) + ..setIVStateIndexFour(); + + expect(actualFirstGenCubit.state, expectedFirstGenState); + }); + + test( + 'Should emit [FirstGenIVButtonsState] with [ivButtonMap where only atkColor and hpColor has true value] when [third monster is filled with speed IV and previous pair contain hp IV and attack IV] and [fourth monster is not filled with IV]', + () { + //Arrange + late Map expectedIVButtonMap = { + for (IVColor ivColor in IVColor.values.where((IVColor ivColor) => ivColor != IVColor.defaultColor)) ivColor: false + }; + + expectedIVButtonMap[IVColor.hpColor] = true; + expectedIVButtonMap[IVColor.atkColor] = true; + + AFirstGenState expectedFirstGenState = FirstGenIVButtonsState(ivButtonMap: expectedIVButtonMap); + + actualFirstGenCubit + ..resetMonsterToDefaultIVColors(FirstGenIndex.three) + ..setIVColors(FirstGenIndex.three, IVColor.speedColor) + ..setIVStateIndexFour(); + + expect(actualFirstGenCubit.state, expectedFirstGenState); + }); + + test( + 'Should emit [FirstGenIVButtonsState] with [ivButtonMap where only atkColor has true value] when [user choose attack IV for fourth monster]', + () { + //Arrange + late Map expectedIVButtonMap = { + for (IVColor ivColor in IVColor.values.where((IVColor ivColor) => ivColor != IVColor.defaultColor)) ivColor: false + }; + + expectedIVButtonMap[IVColor.atkColor] = true; + AFirstGenState expectedFirstGenState = FirstGenIVButtonsState(ivButtonMap: expectedIVButtonMap); + + // Act + actualFirstGenCubit.setIVColors(FirstGenIndex.four, IVColor.atkColor); + actualFirstGenCubit.setIVStateIndexFour(); + + // Assert + expect(actualFirstGenCubit.state, expectedFirstGenState); + }); + }); + group('Tests of FirstGenCubit.resetMonsterToDefaultIVColors()', () { // Act setUpAll(() {