Skip to content

Commit

Permalink
Added an option to change the weapon's level
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Feb 20, 2022
1 parent ce737cc commit 4bddf0e
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 47 deletions.
41 changes: 35 additions & 6 deletions lib/application/custom_build/custom_build_bloc.dart
Expand Up @@ -93,6 +93,10 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
loaded: (state) => _weaponRefinementChanged(e, state),
orElse: () => state,
),
weaponStatChanged: (e) async => state.maybeMap(
loaded: (state) => _weaponStatChanged(e, state),
orElse: () => state,
),
weaponsOrderChanged: (e) async => state.maybeMap(
loaded: (state) => _weaponsOrderChanged(e, state),
orElse: () => state,
Expand Down Expand Up @@ -257,21 +261,27 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
throw Exception('Cannot add more than = $maxNumberOfWeapons weapons to the state');
}

final weapon = _genshinService.getWeaponForCard(e.key);
final weapon = _genshinService.getWeapon(e.key);
final translation = _genshinService.getWeaponTranslation(e.key);
if (state.character.weaponType != weapon.type) {
throw Exception('Type = ${weapon.type} is not valid for character = ${state.character.key}');
}

if (weapon.stats.isEmpty) {
throw Exception('Weapon = ${e.key} does not have any stat');
}

final stat = weapon.stats.last;
final newOne = CustomBuildWeaponModel(
key: e.key,
index: state.weapons.length,
refinement: getWeaponMaxRefinementLevel(weapon.rarity) <= 0 ? 0 : 1,
name: weapon.name,
image: weapon.image,
name: translation.name,
image: weapon.fullImagePath,
rarity: weapon.rarity,
baseAtk: weapon.baseAtk,
subStatType: weapon.subStatType,
subStatValue: weapon.subStatValue,
subStatType: weapon.secondaryStat,
stat: stat,
stats: weapon.stats,
);
final weapons = [...state.weapons, newOne];
return state.copyWith.call(weapons: weapons, readyForScreenshot: false);
Expand Down Expand Up @@ -315,6 +325,25 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
return state.copyWith.call(weapons: weapons, readyForScreenshot: false);
}

CustomBuildState _weaponStatChanged(_WeaponStatChanged e, _LoadedState state) {
final current = state.weapons.firstWhereOrNull((el) => el.key == e.key);
if (current == null) {
throw Exception('Weapon = ${e.key} does not exist in the state');
}

if (current.stat == e.newValue) {
return state;
}

final index = state.weapons.indexOf(current);
final weapons = [...state.weapons];
weapons.removeAt(index);

final updated = current.copyWith.call(stat: e.newValue);
weapons.insert(index, updated);
return state.copyWith.call(weapons: weapons, readyForScreenshot: false);
}

CustomBuildState _deleteWeapon(_DeleteWeapon e, _LoadedState state) {
if (!state.weapons.any((el) => el.key == e.key)) {
throw Exception('Weapon = ${e.key} does not exist');
Expand Down
5 changes: 5 additions & 0 deletions lib/application/custom_build/custom_build_event.dart
Expand Up @@ -34,6 +34,11 @@ class CustomBuildEvent with _$CustomBuildEvent {
required int newValue,
}) = _WeaponRefinementChanged;

const factory CustomBuildEvent.weaponStatChanged({
required String key,
required WeaponFileStatModel newValue,
}) = _WeaponStatChanged;

const factory CustomBuildEvent.weaponsOrderChanged({required List<SortableItem> weapons}) = _WeaponsOrderChanged;

const factory CustomBuildEvent.deleteWeapon({required String key}) = _DeleteWeapon;
Expand Down
@@ -1,5 +1,6 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:shiori/domain/enums/enums.dart';
import 'package:shiori/domain/models/models.dart';

part 'custom_build_weapon_model.freezed.dart';

Expand All @@ -10,10 +11,10 @@ class CustomBuildWeaponModel with _$CustomBuildWeaponModel {
required int index,
required int rarity,
required int refinement,
required double baseAtk,
required StatType subStatType,
required double subStatValue,
required String name,
required String image,
required WeaponFileStatModel stat,
required List<WeaponFileStatModel> stats,
}) = _CustomBuildWeaponModel;
}
Expand Up @@ -16,5 +16,18 @@ class CustomBuildWeapon extends HiveObject {
@HiveField(3)
int refinement;

CustomBuildWeapon(this.buildItemKey, this.weaponKey, this.index, this.refinement);
@HiveField(4, defaultValue: -1)
int level;

@HiveField(5, defaultValue: false)
bool isAnAscension;

CustomBuildWeapon(
this.buildItemKey,
this.weaponKey,
this.index,
this.refinement,
this.level,
this.isAnAscension,
);
}
16 changes: 9 additions & 7 deletions lib/infrastructure/persistence/custom_builds_data_service.dart
Expand Up @@ -174,7 +174,7 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {
}

Future<List<CustomBuildWeapon>> _saveWeapons(int buildKey, List<CustomBuildWeaponModel> weapons) async {
final buildWeapons = weapons.map((e) => CustomBuildWeapon(buildKey, e.key, e.index, e.refinement)).toList();
final buildWeapons = weapons.map((e) => CustomBuildWeapon(buildKey, e.key, e.index, e.refinement, e.stat.level, e.stat.isAnAscension)).toList();
await _weaponsBox.addAll(buildWeapons);
return buildWeapons;
}
Expand Down Expand Up @@ -264,17 +264,19 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {
isRecommended: build.isRecommended,
character: character,
weapons: buildWeapons.map((e) {
final weapon = _genshinService.getWeaponForCard(e.weaponKey);
final weapon = _genshinService.getWeapon(e.weaponKey);
final translation = _genshinService.getWeaponTranslation(e.weaponKey);
final stat = e.level <= 0 ? weapon.stats.last : weapon.stats.firstWhere((el) => el.level == e.level && el.isAnAscension == e.isAnAscension);
return CustomBuildWeaponModel(
key: e.weaponKey,
index: e.index,
refinement: e.refinement,
name: weapon.name,
image: weapon.image,
name: translation.name,
image: weapon.fullImagePath,
rarity: weapon.rarity,
baseAtk: weapon.baseAtk,
subStatType: weapon.subStatType,
subStatValue: weapon.subStatValue,
subStatType: weapon.secondaryStat,
stat: stat,
stats: weapon.stats,
);
}).toList()
..sort((x, y) => x.index.compareTo(y.index)),
Expand Down
Expand Up @@ -6,13 +6,13 @@ import 'package:shiori/domain/app_constants.dart';
import 'package:shiori/domain/extensions/iterable_extensions.dart';
import 'package:shiori/domain/models/models.dart';
import 'package:shiori/generated/l10n.dart';
import 'package:shiori/presentation/shared/ascension_level.dart';
import 'package:shiori/presentation/shared/bottom_sheets/common_bottom_sheet.dart';
import 'package:shiori/presentation/shared/bottom_sheets/common_button_bar.dart';
import 'package:shiori/presentation/shared/bottom_sheets/right_bottom_sheet.dart';
import 'package:shiori/presentation/shared/dialogs/number_picker_dialog.dart';
import 'package:shiori/presentation/shared/loading.dart';

import 'ascension_level.dart';
import 'skill_item.dart';

const _sessionKey = 'sessionKey';
Expand Down Expand Up @@ -121,9 +121,15 @@ class AddEditItemBottomSheet extends StatelessWidget {
),
),
Text(s.currentAscension, textAlign: TextAlign.center, style: theme.textTheme.subtitle2),
AscensionLevel(isCurrentLevel: true, level: state.currentAscensionLevel),
AscensionLevel(
level: state.currentAscensionLevel,
onSave: (newValue) => _ascensionLevelChanged(newValue, true, context),
),
Text(s.desiredAscension, textAlign: TextAlign.center, style: theme.textTheme.subtitle2),
AscensionLevel(isCurrentLevel: false, level: state.desiredAscensionLevel),
AscensionLevel(
level: state.desiredAscensionLevel,
onSave: (newValue) => _ascensionLevelChanged(newValue, false, context),
),
...state.skills
.mapIndex(
(e, index) => SkillItem(
Expand Down Expand Up @@ -201,9 +207,15 @@ class AddEditItemBottomSheet extends StatelessWidget {
),
),
Text(s.currentAscension, textAlign: TextAlign.center, style: theme.textTheme.subtitle2),
AscensionLevel(isCurrentLevel: true, level: state.currentAscensionLevel),
AscensionLevel(
level: state.currentAscensionLevel,
onSave: (newValue) => _ascensionLevelChanged(newValue, true, context),
),
Text(s.desiredAscension, textAlign: TextAlign.center, style: theme.textTheme.subtitle2),
AscensionLevel(isCurrentLevel: false, level: state.desiredAscensionLevel),
AscensionLevel(
level: state.desiredAscensionLevel,
onSave: (newValue) => _ascensionLevelChanged(newValue, false, context),
),
...state.skills
.mapIndex(
(e, index) => SkillItem(
Expand Down Expand Up @@ -246,6 +258,13 @@ class AddEditItemBottomSheet extends StatelessWidget {
context.read<CalculatorAscMaterialsItemBloc>().add(event);
});
}

void _ascensionLevelChanged(int newValue, bool isCurrentLevel, BuildContext context) {
final event = isCurrentLevel
? CalculatorAscMaterialsItemEvent.currentAscensionLevelChanged(newValue: newValue)
: CalculatorAscMaterialsItemEvent.desiredAscensionLevelChanged(newValue: newValue);
context.read<CalculatorAscMaterialsItemBloc>().add(event);
}
}

class _UseMaterialsFromInventoryToggleButton extends StatelessWidget {
Expand Down

0 comments on commit 4bddf0e

Please sign in to comment.