Skip to content

Commit

Permalink
[Infrastructure] Updated the logic to save / retrieve the game codes …
Browse files Browse the repository at this point in the history
…from db
  • Loading branch information
Wolfteam committed Jun 3, 2021
1 parent 6e1878f commit 14f4bc9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 29 deletions.
78 changes: 61 additions & 17 deletions lib/infrastructure/data_service.dart
Expand Up @@ -20,7 +20,8 @@ class DataServiceImpl implements DataService {
Box<CalculatorCharacterSkill> _calcItemSkillBox;
Box<InventoryItem> _inventoryBox;
Box<InventoryUsedItem> _inventoryUsedItemsBox;
Box<UsedGameCode> _usedGameCodesBox;
Box<GameCode> _gameCodesBox;
Box<GameCodeReward> _gameCodeRewardsBox;
Box<TierListItem> _tierListBox;

DataServiceImpl(this._genshinService, this._calculatorService);
Expand All @@ -33,8 +34,10 @@ class DataServiceImpl implements DataService {
_calcItemSkillBox = await Hive.openBox<CalculatorCharacterSkill>('calculatorSessionsItemsSkills');
_inventoryBox = await Hive.openBox<InventoryItem>('inventory');
_inventoryUsedItemsBox = await Hive.openBox<InventoryUsedItem>('inventoryUsedItems');
_usedGameCodesBox = await Hive.openBox<UsedGameCode>('usedGameCodes');
_gameCodesBox = await Hive.openBox<GameCode>('gameCodes');
_gameCodeRewardsBox = await Hive.openBox<GameCodeReward>('gameCodeRewards');
_tierListBox = await Hive.openBox<TierListItem>('tierList');
await _gameCodesBox.clear();
}

@override
Expand Down Expand Up @@ -350,28 +353,68 @@ class DataServiceImpl implements DataService {

@override
List<GameCodeModel> getAllGameCodes() {
final usedCodes = getAllUsedGameCodes();
return _genshinService.getAllGameCodes().map((e) {
final isUsed = usedCodes.contains(e.code);
return GameCodeModel(code: e.code, isExpired: e.isExpired, isUsed: isUsed, rewards: e.rewards);
return _gameCodesBox.values.map((e) {
final rewards = _gameCodeRewardsBox.values.where((el) => el.gameCodeKey == e.key).map((reward) {
final material = _genshinService.getMaterial(reward.itemKey);
return ItemAscensionMaterialModel(quantity: reward.quantity, image: material.image, materialType: material.type);
}).toList();
return GameCodeModel(
code: e.code,
isExpired: e.isExpired,
expiredOn: e.expiredOn,
discoveredOn: e.discoveredOn,
isUsed: e.usedOn != null,
rewards: rewards,
region: e.region != null ? AppServerResetTimeType.values[e.region] : null,
);
}).toList();
}

@override
List<String> getAllUsedGameCodes() {
return _usedGameCodesBox.values.map((e) => e.code).toList();
Future<void> saveGameCodes(List<GameCodeModel> itemsFromApi) async {
final itemsOnDb = _gameCodesBox.values.toList();

for (final item in itemsFromApi) {
final gcOnDb = itemsOnDb.firstWhere((el) => el.code == item.code, orElse: () => null);
if (gcOnDb != null) {
gcOnDb.isExpired = item.isExpired;
gcOnDb.expiredOn = item.expiredOn;
gcOnDb.discoveredOn = item.discoveredOn;
gcOnDb.region = item.region?.index;
await gcOnDb.save();
await deleteAllGameCodeRewards(gcOnDb.key as int);
await saveGameCodeRewards(gcOnDb.key as int, item.rewards);
} else {
final gc = GameCode(item.code, null, item.discoveredOn, item.expiredOn, item.isExpired, item.region?.index);
await _gameCodesBox.add(gc);
//This line shouldn't be necessary, though for testing purposes I'll leave it here
await deleteAllGameCodeRewards(gc.key as int);
await saveGameCodeRewards(gc.key as int, item.rewards);
}
}
}

@override
Future<void> markCodeAsUsed(String code, {bool wasUsed = true}) async {
final usedGameCode = _usedGameCodesBox.values.firstWhere((el) => el.code == code, orElse: () => null);
if (usedGameCode != null) {
await _usedGameCodesBox.delete(usedGameCode.key);
}
Future<void> saveGameCodeRewards(int gameCodeKey, List<ItemAscensionMaterialModel> rewards) {
final rewardsToSave = rewards
.map(
(e) => GameCodeReward(gameCodeKey, _genshinService.getMaterialByImage(e.fullImagePath).key, e.quantity),
)
.toList();
return _gameCodeRewardsBox.addAll(rewardsToSave);
}

if (wasUsed) {
await _usedGameCodesBox.add(UsedGameCode(code, DateTime.now()));
}
@override
Future<void> deleteAllGameCodeRewards(int gameCodeKey) {
final keys = _gameCodeRewardsBox.values.where((el) => el.gameCodeKey == gameCodeKey).map((e) => e.key).toList();
return _gameCodeRewardsBox.deleteAll(keys);
}

@override
Future<void> markCodeAsUsed(String code, {bool wasUsed = true}) async {
final usedGameCode = _gameCodesBox.values.firstWhere((el) => el.code == code, orElse: () => null);
usedGameCode.usedOn = wasUsed ? DateTime.now() : null;
await usedGameCode.save();
}

@override
Expand Down Expand Up @@ -399,7 +442,8 @@ class DataServiceImpl implements DataService {
Hive.registerAdapter(CalculatorSessionAdapter());
Hive.registerAdapter(InventoryItemAdapter());
Hive.registerAdapter(InventoryUsedItemAdapter());
Hive.registerAdapter(UsedGameCodeAdapter());
Hive.registerAdapter(GameCodeAdapter());
Hive.registerAdapter(GameCodeRewardAdapter());
Hive.registerAdapter(TierListItemAdapter());
}

Expand Down
12 changes: 0 additions & 12 deletions lib/infrastructure/genshin_service.dart
Expand Up @@ -17,7 +17,6 @@ class GenshinServiceImpl implements GenshinService {
ArtifactsFile _artifactsFile;
MaterialsFile _materialsFile;
ElementsFile _elementsFile;
GameCodesFile _gameCodesFile;
MonstersFile _monstersFile;

GenshinServiceImpl(this._localeService);
Expand All @@ -30,7 +29,6 @@ class GenshinServiceImpl implements GenshinService {
initArtifacts(),
initMaterials(),
initElements(),
initGameCodes(),
initMonsters(),
initTranslations(languageType),
]);
Expand Down Expand Up @@ -71,13 +69,6 @@ class GenshinServiceImpl implements GenshinService {
_elementsFile = ElementsFile.fromJson(json);
}

@override
Future<void> initGameCodes() async {
final jsonStr = await rootBundle.loadString(Assets.gameCodesDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_gameCodesFile = GameCodesFile.fromJson(json);
}

@override
Future<void> initMonsters() async {
final jsonStr = await rootBundle.loadString(Assets.monstersDbPath);
Expand Down Expand Up @@ -426,9 +417,6 @@ class GenshinServiceImpl implements GenshinService {
return server.subtract(const Duration(days: 1));
}

@override
List<GameCodeFileModel> getAllGameCodes() => _gameCodesFile.gameCodes;

@override
List<String> getCharacterImgsUsingMaterial(String key) {
final material = getMaterial(key);
Expand Down
1 change: 1 addition & 0 deletions lib/infrastructure/infrastructure.dart
@@ -1,6 +1,7 @@
export 'package:genshindb/infrastructure/calculator_service.dart';
export 'package:genshindb/infrastructure/data_service.dart';
export 'package:genshindb/infrastructure/device_info_service.dart';
export 'package:genshindb/infrastructure/game_codes_service.dart';
export 'package:genshindb/infrastructure/genshin_service.dart';
export 'package:genshindb/infrastructure/locale_service.dart';
export 'package:genshindb/infrastructure/logging_service.dart';
Expand Down

0 comments on commit 14f4bc9

Please sign in to comment.