Skip to content

Commit

Permalink
[Infrastructure] Added some logic to retrieve custom build's data
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jan 1, 2022
1 parent db06e79 commit e05b5c4
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
84 changes: 84 additions & 0 deletions lib/infrastructure/data_service.dart
Expand Up @@ -29,6 +29,7 @@ class DataServiceImpl implements DataService {
late Box<GameCode> _gameCodesBox;
late Box<GameCodeReward> _gameCodeRewardsBox;
late Box<TierListItem> _tierListBox;
late Box<CustomBuild> _customBuildsBox;

late Box<NotificationCustom> _notificationsCustomBox;
late Box<NotificationExpedition> _notificationsExpeditionBox;
Expand Down Expand Up @@ -67,6 +68,7 @@ class DataServiceImpl implements DataService {
_gameCodesBox = await Hive.openBox<GameCode>('gameCodes');
_gameCodeRewardsBox = await Hive.openBox<GameCodeReward>('gameCodeRewards');
_tierListBox = await Hive.openBox<TierListItem>('tierList');
_customBuildsBox = await Hive.openBox<CustomBuild>('customBuilds');

_notificationsCustomBox = await Hive.openBox('notificationsCustom');
_notificationsExpeditionBox = await Hive.openBox('notificationsExpedition');
Expand Down Expand Up @@ -1237,6 +1239,71 @@ class DataServiceImpl implements DataService {
return _updateNotification(item, item.title, item.body, item.note, item.showNotification);
}

@override
List<CustomBuildModel> getAllCustomBuilds() {
return _customBuildsBox.values.map(_mapToCustomBuildModel).toList()..sort((x, y) => x.character.name.compareTo(y.character.name));
}

@override
CustomBuildModel getCustomBuild(int key) {
final build = _customBuildsBox.values.firstWhere((e) => e.key == key);
return _mapToCustomBuildModel(build);
}

@override
Future<CustomBuildModel> saveCustomBuild(
String charKey,
String title,
CharacterRoleType type,
CharacterRoleSubType subType,
bool showOnCharacterDetail,
List<String> weaponKeys,
Map<String, int> artifacts,
List<CharacterSkillType> talentPriority,
) async {
final build = CustomBuild(
charKey,
showOnCharacterDetail,
title,
type.index,
subType.index,
weaponKeys,
artifacts,
talentPriority.map((e) => e.index).toList(),
);
await _customBuildsBox.add(build);
return _mapToCustomBuildModel(build);
}

@override
Future<CustomBuildModel> updateCustomBuild(
int key,
String title,
CharacterRoleType type,
CharacterRoleSubType subType,
bool showOnCharacterDetail,
List<String> weaponKeys,
Map<String, int> artifacts,
List<CharacterSkillType> talentPriority,
) async {
final build = _customBuildsBox.get(key)!;
build.title = title;
build.roleType = type.index;
build.roleSubType = subType.index;
build.showOnCharacterDetail = showOnCharacterDetail;
build.weaponKeys = weaponKeys;
build.artifacts = artifacts;
build.talentPriority = talentPriority.map((e) => e.index).toList();

await build.save();
return _mapToCustomBuildModel(build);
}

@override
Future<void> deleteCustomBuild(int key) async {
await _customBuildsBox.delete(key);
}

void _registerAdapters() {
Hive.registerAdapter(CalculatorCharacterSkillAdapter());
Hive.registerAdapter(CalculatorItemAdapter());
Expand All @@ -1255,6 +1322,7 @@ class DataServiceImpl implements DataService {
Hive.registerAdapter(NotificationRealmCurrencyAdapter());
Hive.registerAdapter(NotificationResinAdapter());
Hive.registerAdapter(NotificationWeeklyBossAdapter());
Hive.registerAdapter(CustomBuildAdapter());
}

ItemAscensionMaterials _buildForCharacter(CalculatorItem item, {int? calculatorItemKey, bool includeInventory = false}) {
Expand Down Expand Up @@ -1566,4 +1634,20 @@ class DataServiceImpl implements DataService {
await hiveObject.save();
return getNotification(hiveObject.key as int, AppNotificationType.values[notification.type]);
}

CustomBuildModel _mapToCustomBuildModel(CustomBuild build) {
final character = _genshinService.getCharacterForCard(build.characterKey);
final weapons = build.weaponKeys.map((e) => _genshinService.getWeaponForCard(e)).toList();
// final artifacts = build.artifactKeys.map((e) => _genshinService.getArtifactForCard(e)).toList();
return CustomBuildModel(
key: build.key as int,
title: build.title,
type: CharacterRoleType.values[build.roleType],
subType: CharacterRoleSubType.values[build.roleSubType],
showOnCharacterDetail: build.showOnCharacterDetail,
character: character,
weapons: weapons,
artifacts: [],
);
}
}
37 changes: 33 additions & 4 deletions lib/infrastructure/genshin_service.dart
Expand Up @@ -204,8 +204,14 @@ class GenshinServiceImpl implements GenshinService {
}

@override
List<ArtifactCardModel> getArtifactsForCard() {
return _artifactsFile.artifacts.map((e) => _toArtifactForCard(e)).toList();
List<ArtifactCardModel> getArtifactsForCard({ArtifactType? type}) {
return _artifactsFile.artifacts.map((e) => _toArtifactForCard(e, type: type)).where((e) {
//if a type was provided and it is different that crown, then return only the ones with more than 1 bonus
if (type != null && type != ArtifactType.crown) {
return e.bonus.length > 1;
}
return true;
}).toList();
}

@override
Expand Down Expand Up @@ -757,6 +763,21 @@ class GenshinServiceImpl implements GenshinService {
return bonus == 1 ? [fullImagePath] : artifactOrder.map((e) => Assets.getArtifactPath('$imageWithoutExt$e.png')).toList();
}

@override
String getArtifactRelatedPart(String fullImagePath, String image, int bonus, ArtifactType type) {
if (bonus == 1 && type != ArtifactType.crown) {
throw Exception('Invalid artifact type');
}

if (bonus == 1) {
return fullImagePath;
}

final imgs = getArtifactRelatedParts(fullImagePath, image, bonus);
final order = getArtifactOrder(type);
return imgs.firstWhere((el) => el.endsWith('$order.png'));
}

CharacterCardModel _toCharacterForCard(CharacterFileModel character) {
final translation = getCharacterTranslation(character.key);

Expand Down Expand Up @@ -809,16 +830,24 @@ class GenshinServiceImpl implements GenshinService {
);
}

ArtifactCardModel _toArtifactForCard(ArtifactFileModel artifact) {
ArtifactCardModel _toArtifactForCard(ArtifactFileModel artifact, {ArtifactType? type}) {
final translation = _translationFile.artifacts.firstWhere((t) => t.key == artifact.key);
final bonus = getArtifactBonus(translation);
return ArtifactCardModel(
final mapped = ArtifactCardModel(
key: artifact.key,
name: translation.name,
image: artifact.fullImagePath,
rarity: artifact.maxRarity,
bonus: bonus,
);

//only search for other images if the artifact has more than 1 bonus effect
if (type != null && bonus.length > 1) {
final img = getArtifactRelatedPart(artifact.fullImagePath, artifact.image, bonus.length, type);
return mapped.copyWith.call(image: img);
}

return mapped;
}

MaterialCardModel _toMaterialForCard(MaterialFileModel material) {
Expand Down

0 comments on commit e05b5c4

Please sign in to comment.