-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
data_service.dart
631 lines (549 loc) · 24.4 KB
/
data_service.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import 'package:genshindb/domain/app_constants.dart';
import 'package:genshindb/domain/assets.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/enums/item_type.dart';
import 'package:genshindb/domain/extensions/iterable_extensions.dart';
import 'package:genshindb/domain/extensions/string_extensions.dart';
import 'package:genshindb/domain/models/entities.dart';
import 'package:genshindb/domain/models/models.dart';
import 'package:genshindb/domain/services/calculator_service.dart';
import 'package:genshindb/domain/services/data_service.dart';
import 'package:genshindb/domain/services/genshin_service.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
class DataServiceImpl implements DataService {
final GenshinService _genshinService;
final CalculatorService _calculatorService;
Box<CalculatorSession> _sessionBox;
Box<CalculatorItem> _calcItemBox;
Box<CalculatorCharacterSkill> _calcItemSkillBox;
Box<InventoryItem> _inventoryBox;
Box<InventoryUsedItem> _inventoryUsedItemsBox;
Box<GameCode> _gameCodesBox;
Box<GameCodeReward> _gameCodeRewardsBox;
Box<TierListItem> _tierListBox;
DataServiceImpl(this._genshinService, this._calculatorService);
Future<void> init() async {
await Hive.initFlutter();
_registerAdapters();
_sessionBox = await Hive.openBox<CalculatorSession>('calculatorSessions');
_calcItemBox = await Hive.openBox<CalculatorItem>('calculatorSessionsItems');
_calcItemSkillBox = await Hive.openBox<CalculatorCharacterSkill>('calculatorSessionsItemsSkills');
_inventoryBox = await Hive.openBox<InventoryItem>('inventory');
_inventoryUsedItemsBox = await Hive.openBox<InventoryUsedItem>('inventoryUsedItems');
_gameCodesBox = await Hive.openBox<GameCode>('gameCodes');
_gameCodeRewardsBox = await Hive.openBox<GameCodeReward>('gameCodeRewards');
_tierListBox = await Hive.openBox<TierListItem>('tierList');
await _gameCodesBox.clear();
}
@override
List<CalculatorSessionModel> getAllCalAscMatSessions() {
final sessions = _sessionBox.values.toList()..sort((x, y) => x.position.compareTo(y.position));
final result = <CalculatorSessionModel>[];
for (final session in sessions) {
result.add(getCalcAscMatSession(session.key as int));
}
return result;
}
@override
CalculatorSessionModel getCalcAscMatSession(int sessionKey) {
final session = _sessionBox.values.firstWhere((el) => el.key == sessionKey);
final sessionItems = <ItemAscensionMaterials>[];
final calcItems = _calcItemBox.values.where((el) => el.sessionKey == session.key).toList()..sort((x, y) => x.position.compareTo(y.position));
for (final calItem in calcItems) {
if (calItem.isCharacter) {
sessionItems.add(_buildForCharacter(calItem, calculatorItemKey: calItem.key as int, includeInventory: true));
continue;
}
if (calItem.isWeapon) {
sessionItems.add(_buildForWeapon(calItem, calculatorItemKey: calItem.key as int, includeInventory: true));
continue;
}
throw Exception('The provided item with key = ${calItem.key} is not neither a character nor weapon');
}
return CalculatorSessionModel(key: session.key as int, name: session.name, position: session.position, items: sessionItems);
}
@override
Future<int> createCalAscMatSession(String name, int position) {
final session = CalculatorSession(name, position);
return _sessionBox.add(session);
}
@override
Future<void> updateCalAscMatSession(int sessionKey, String name, int position, {bool redistributeMaterials = false}) async {
final session = _sessionBox.get(sessionKey);
session.name = name;
session.position = position;
await session.save();
if (redistributeMaterials) {
await redistributeAllInventoryMaterials();
}
}
@override
Future<void> deleteCalAscMatSession(int sessionKey) async {
await _sessionBox.delete(sessionKey);
final calItems = _calcItemBox.values.where((el) => el.sessionKey == sessionKey).toList();
for (final calItem in calItems) {
await deleteCalAscMatSessionItem(sessionKey, calItem.position);
}
}
@override
Future<void> addCalAscMatSessionItems(int sessionKey, List<ItemAscensionMaterials> items) async {
for (final item in items) {
await addCalAscMatSessionItem(sessionKey, item);
}
}
@override
Future<ItemAscensionMaterials> addCalAscMatSessionItem(int sessionKey, ItemAscensionMaterials item, {bool redistribute = true}) async {
final mappedItem = _toCalculatorItem(sessionKey, item);
final calculatorItemKey = await _calcItemBox.add(mappedItem);
final skills = item.skills.map((e) => CalculatorCharacterSkill(calculatorItemKey, e.key, e.currentLevel, e.desiredLevel, e.position)).toList();
await _calcItemSkillBox.addAll(skills);
if (!mappedItem.useMaterialsFromInventory || item.materials.isEmpty || !item.isActive) {
return item;
}
//Here we created a used inventory item for each material
for (final material in item.materials) {
final mat = _genshinService.getMaterialByImage(material.fullImagePath);
await _useItemFromInventory(calculatorItemKey, mat.key, ItemType.material, material.quantity);
}
if (!redistribute) {
return item;
}
//Since we added a new item, we need to redistribute
//the materials because the priority of this item could be higher than the others
await redistributeAllInventoryMaterials();
//Then we retrieve the used items created before
final usedInventoryItems = _inventoryUsedItemsBox.values
.where(
(el) => el.calculatorItemKey == calculatorItemKey && el.type == ItemType.material.index,
)
.toList();
//And finally update the material quantity based on the used inventory items
//This is quite similar to what the _considerMaterialsInInventory does
final updatedMaterials = item.materials.map((e) {
final material = _genshinService.getMaterialByImage(e.fullImagePath);
if (!usedInventoryItems.any((el) => el.itemKey == material.key)) {
return e;
}
final usedQuantity = usedInventoryItems
.firstWhere(
(el) => el.calculatorItemKey == calculatorItemKey && el.itemKey == material.key && el.type == ItemType.material.index,
orElse: () => null,
)
?.usedQuantity ??
e.quantity;
final remaining = e.quantity - usedQuantity;
return e.copyWith.call(quantity: remaining);
}).toList();
return item.copyWith.call(materials: updatedMaterials);
}
@override
Future<ItemAscensionMaterials> updateCalAscMatSessionItem(
int sessionKey,
int newItemPosition,
ItemAscensionMaterials item, {
bool redistribute = true,
}) async {
await deleteCalAscMatSessionItem(sessionKey, item.position, redistribute: false);
return addCalAscMatSessionItem(sessionKey, item.copyWith.call(position: newItemPosition), redistribute: redistribute);
}
@override
Future<void> deleteCalAscMatSessionItem(int sessionKey, int position, {bool redistribute = true}) async {
final calcItem = _calcItemBox.values.firstWhere((el) => el.sessionKey == sessionKey && el.position == position, orElse: () => null);
if (calcItem == null) {
return;
}
final calcItemKey = calcItem.key as int;
final skillsKeys = _calcItemSkillBox.values.where((el) => el.calculatorItemKey == calcItemKey).map((e) => e.key).toList();
await _calcItemSkillBox.deleteAll(skillsKeys);
//Make sure we delete the item before redistributing
await _calcItemBox.delete(calcItemKey);
await _clearUsedInventoryItems(calcItemKey, redistribute: redistribute);
}
@override
Future<void> addItemToInventory(String key, ItemType type, int quantity) {
if (isItemInInventory(key, type)) {
return Future.value();
}
return _inventoryBox.add(InventoryItem(key, quantity, type.index));
}
@override
Future<void> deleteItemFromInventory(String key, ItemType type) async {
final item = _getItemFromInventory(key, type);
if (item != null) {
await _inventoryBox.delete(item.key);
}
}
@override
List<CharacterCardModel> getAllCharactersInInventory() {
final characters = _inventoryBox.values
.where((el) => el.type == ItemType.character.index)
.map(
(e) => _genshinService.getCharacterForCard(e.itemKey),
)
.toList();
return characters..sort((x, y) => x.name.compareTo(y.name));
}
@override
List<MaterialCardModel> getAllMaterialsInInventory() {
final materials = _genshinService.getAllMaterialsForCard();
final inInventory = _inventoryBox.values.where((el) => el.type == ItemType.material.index).map((e) {
final material = _genshinService.getMaterialForCard(e.itemKey);
return material.copyWith.call(quantity: e.quantity);
}).toList();
final allMaterials = <MaterialCardModel>[];
for (final material in materials) {
if (inInventory.any((m) => m.key == material.key)) {
//The one in db has the quantity
final inDb = inInventory.firstWhere((m) => m.key == material.key);
final usedQuantity = getNumberOfItemsUsed(material.key, ItemType.material);
allMaterials.add(inDb.copyWith.call(usedQuantity: usedQuantity));
} else {
allMaterials.add(material);
}
}
return sortMaterialsByGrouping(allMaterials, SortDirectionType.asc);
}
@override
List<WeaponCardModel> getAllWeaponsInInventory() {
final weapons = _inventoryBox.values
.where((el) => el.type == ItemType.weapon.index)
.map(
(e) => _genshinService.getWeaponForCard(e.itemKey),
)
.toList();
return weapons..sort((x, y) => x.name.compareTo(y.name));
}
@override
Future<void> updateItemInInventory(String key, ItemType type, int quantity) async {
var item = _getItemFromInventory(key, type);
if (item == null) {
item = InventoryItem(key, quantity, type.index);
await _inventoryBox.add(item);
} else {
if (quantity == item.quantity) {
return;
}
//TODO: IF THE QUANTITY IS 0 SHOULD I DELETE THE ITEM ?
item.quantity = quantity;
await item.save();
}
await redistributeInventoryMaterial(key, quantity);
}
@override
bool isItemInInventory(String key, ItemType type) {
return _inventoryBox.values.any((el) => el.itemKey == key && el.type == type.index && el.quantity > 0);
}
int getNumberOfItemsUsed(String byItemKey, ItemType type) {
return _inventoryUsedItemsBox.values
.where((el) => el.itemKey == byItemKey && el.type == type.index)
.fold(0, (previousValue, element) => previousValue + element.usedQuantity);
}
@override
Future<void> redistributeAllInventoryMaterials() async {
//Here we just redistribute what we got based on what we have
//This method should only be called
final materialsInInventory = _inventoryBox.values.where((el) => el.type == ItemType.material.index && el.quantity > 0).toList();
for (final material in materialsInInventory) {
await redistributeInventoryMaterial(material.itemKey, material.quantity);
}
}
@override
Future<void> redistributeInventoryMaterial(String itemKey, int newQuantity) async {
var currentQuantity = newQuantity;
final sessions = _sessionBox.values.toList()..sort((x, y) => x.position.compareTo(y.position));
for (final session in sessions) {
final calcItems = _calcItemBox.values.where((el) => el.sessionKey == session.key).toList()..sort((x, y) => x.position.compareTo(y.position));
for (final calItem in calcItems) {
if (!calItem.useMaterialsFromInventory || !calItem.isActive) {
continue;
}
//If we hit this point, that means that itemKey COULD be being used, so we need to update the used values accordingly
final item = calItem.isCharacter ? _buildForCharacter(calItem) : _buildForWeapon(calItem);
final material = _genshinService.getMaterial(itemKey);
final desiredQuantityToUse = item.materials.firstWhere((el) => el.fullImagePath == material.fullImagePath, orElse: () => null)?.quantity ?? 0;
//Next, we check if there is a used item for this calculator item
var usedInInventory =
_inventoryUsedItemsBox.values.firstWhere((el) => el.calculatorItemKey == calItem.key && el.itemKey == itemKey, orElse: () => null);
//If no used item was found, lets check if this calc. item could benefit from this itemKey
if (usedInInventory == null) {
//If itemKey is not in the used materials, then this item does not use this material
if (!item.materials.any((el) => el.fullImagePath == material.fullImagePath)) {
continue;
}
//otherwise, since we don't have a used inventory item, we need to create one, that will later get updated
usedInInventory = InventoryUsedItem(calItem.key as int, itemKey, desiredQuantityToUse, ItemType.material.index);
await _inventoryUsedItemsBox.add(usedInInventory);
}
final available = currentQuantity - desiredQuantityToUse;
final canBeSatisfied = available >= 0;
//If we can satisfy the desired quantity, reduce the current quantity and also update the used qty in the inventory item
if (canBeSatisfied) {
currentQuantity -= desiredQuantityToUse;
usedInInventory.usedQuantity = desiredQuantityToUse;
await usedInInventory.save();
continue;
}
//If we ended up using all the available material,
//we must delete the previously used item since we cannot satisfy the required material quantity
if (currentQuantity == 0) {
await _inventoryUsedItemsBox.delete(usedInInventory.key);
continue;
}
//Finally, if we can't satisfy all the desired quantity, and we still have a little bit left
//then, use the remaining
usedInInventory.usedQuantity = currentQuantity;
await usedInInventory.save();
currentQuantity = 0;
}
}
}
@override
List<GameCodeModel> getAllGameCodes() {
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
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> 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);
}
@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
List<TierListRowModel> getTierList() {
final values = _tierListBox.values.toList()..sort((x, y) => x.position.compareTo(y.position));
return values.map((e) => TierListRowModel.row(tierText: e.text, charImgs: e.charsImgs, tierColor: e.color)).toList();
}
@override
Future<void> saveTierList(List<TierListRowModel> tierList) async {
await deleteTierList();
final toSave = tierList.mapIndex((e, i) => TierListItem(e.tierText, e.tierColor, i, e.charImgs)).toList();
await _tierListBox.addAll(toSave);
}
@override
Future<void> deleteTierList() async {
final keys = _tierListBox.values.map((e) => e.key);
await _tierListBox.deleteAll(keys);
}
void _registerAdapters() {
Hive.registerAdapter(CalculatorCharacterSkillAdapter());
Hive.registerAdapter(CalculatorItemAdapter());
Hive.registerAdapter(CalculatorSessionAdapter());
Hive.registerAdapter(InventoryItemAdapter());
Hive.registerAdapter(InventoryUsedItemAdapter());
Hive.registerAdapter(GameCodeAdapter());
Hive.registerAdapter(GameCodeRewardAdapter());
Hive.registerAdapter(TierListItemAdapter());
}
ItemAscensionMaterials _buildForCharacter(CalculatorItem item, {int calculatorItemKey, bool includeInventory = false}) {
final character = _genshinService.getCharacter(item.itemKey);
final translation = _genshinService.getCharacterTranslation(item.itemKey);
final skills = _calcItemSkillBox.values
.where((s) => s.calculatorItemKey == item.key)
.map((skill) => _buildCharacterSkill(item, skill, translation.skills.firstWhere((t) => t.key == skill.skillKey)))
.toList();
var materials = _calculatorService.getCharacterMaterialsToUse(
character,
item.currentLevel,
item.desiredLevel,
item.currentAscensionLevel,
item.desiredAscensionLevel,
skills,
);
if (item.useMaterialsFromInventory && item.isActive && includeInventory && calculatorItemKey != null) {
materials = _considerMaterialsInInventory(calculatorItemKey, materials);
}
return ItemAscensionMaterials.forCharacters(
key: item.itemKey,
name: translation.name,
image: Assets.getCharacterPath(character.image),
rarity: character.rarity,
materials: materials,
currentLevel: item.currentLevel,
desiredLevel: item.desiredLevel,
currentAscensionLevel: item.currentAscensionLevel,
desiredAscensionLevel: item.desiredAscensionLevel,
skills: skills,
isActive: item.isActive,
position: item.position,
isCharacter: item.isCharacter,
isWeapon: item.isWeapon,
useMaterialsFromInventory: item.useMaterialsFromInventory,
);
}
CharacterSkill _buildCharacterSkill(CalculatorItem item, CalculatorCharacterSkill skillInDb, TranslationCharacterSkillFile skill) {
final enableTuple = _calculatorService.isSkillEnabled(
skillInDb.currentLevel,
skillInDb.desiredLevel,
item.currentAscensionLevel,
item.desiredAscensionLevel,
minSkillLevel,
maxSkillLevel,
);
return CharacterSkill.skill(
name: skill.title,
currentLevel: skillInDb.currentLevel,
desiredLevel: skillInDb.desiredLevel,
isCurrentDecEnabled: enableTuple.item1,
isCurrentIncEnabled: enableTuple.item2,
isDesiredDecEnabled: enableTuple.item3,
isDesiredIncEnabled: enableTuple.item4,
position: skillInDb.position,
key: skillInDb.skillKey,
);
}
ItemAscensionMaterials _buildForWeapon(CalculatorItem item, {int calculatorItemKey, bool includeInventory = false}) {
final weapon = _genshinService.getWeapon(item.itemKey);
final translation = _genshinService.getWeaponTranslation(item.itemKey);
var materials = _calculatorService.getWeaponMaterialsToUse(
weapon,
item.currentLevel,
item.desiredLevel,
item.currentAscensionLevel,
item.desiredAscensionLevel,
);
if (item.useMaterialsFromInventory && item.isActive && includeInventory && calculatorItemKey != null) {
materials = _considerMaterialsInInventory(calculatorItemKey, materials);
}
return ItemAscensionMaterials.forWeapons(
key: item.itemKey,
name: translation.name,
image: weapon.fullImagePath,
rarity: weapon.rarity,
materials: materials,
currentLevel: item.currentLevel,
desiredLevel: item.desiredLevel,
currentAscensionLevel: item.currentAscensionLevel,
desiredAscensionLevel: item.desiredAscensionLevel,
skills: [],
isActive: item.isActive,
position: item.position,
isWeapon: item.isWeapon,
isCharacter: item.isCharacter,
useMaterialsFromInventory: item.useMaterialsFromInventory,
);
}
/// This method checks if the [calculatorItemKey] has used inventory items, it it does, it will update the quantity
/// of each used material passed, otherwise it will return the same material unchanged
///
/// Keep in mind that this method must be called in order based on the [calculatorItemKey]
List<ItemAscensionMaterialModel> _considerMaterialsInInventory(int calculatorItemKey, List<ItemAscensionMaterialModel> materials) {
return materials.map((e) {
final material = _genshinService.getMaterialByImage(e.fullImagePath);
if (!isItemInInventory(material.key, ItemType.material)) {
return e;
}
final usedQuantity = _inventoryUsedItemsBox.values
.firstWhere(
(el) => el.calculatorItemKey == calculatorItemKey && el.itemKey == material.key && el.type == ItemType.material.index,
orElse: () => null,
)
?.usedQuantity ??
0;
final remaining = e.quantity - usedQuantity;
return e.copyWith.call(quantity: remaining);
}).toList();
}
InventoryItem _getItemFromInventory(String key, ItemType type) {
return _inventoryBox.values.firstWhere((el) => el.itemKey == key && el.type == type.index, orElse: () => null);
}
Future<void> _useItemFromInventory(int calculatorItemKey, String itemKey, ItemType type, int quantityToUse) async {
if (!isItemInInventory(itemKey, type)) {
return;
}
final used = getNumberOfItemsUsed(itemKey, type);
final item = _getItemFromInventory(itemKey, type);
final available = item.quantity - used;
final toUse = available - quantityToUse < 0 ? available : quantityToUse;
if (toUse == 0) {
return;
}
final usedItem = InventoryUsedItem(calculatorItemKey, itemKey, toUse, type.index);
await _inventoryUsedItemsBox.add(usedItem);
}
Future<void> _clearUsedInventoryItemsInSession(int sessionKey) async {
final calcItems = _calcItemBox.values.where((el) => el.sessionKey == sessionKey).toList();
for (final calcItem in calcItems) {
if (calcItem.useMaterialsFromInventory) {
await _clearUsedInventoryItems(calcItem.key as int);
}
}
await redistributeAllInventoryMaterials();
}
Future<void> _clearUsedInventoryItems(int calculatorItemKey, {String onlyItemKey, bool redistribute = false}) async {
final usedItems = onlyItemKey.isNullEmptyOrWhitespace
? _inventoryUsedItemsBox.values.where((el) => el.calculatorItemKey == calculatorItemKey).map((e) => e.key).toList()
: _inventoryUsedItemsBox.values
.where((el) => el.calculatorItemKey == calculatorItemKey && el.itemKey == onlyItemKey)
.map((e) => e.key)
.toList();
await _inventoryUsedItemsBox.deleteAll(usedItems);
if (redistribute) {
await redistributeAllInventoryMaterials();
}
}
CalculatorItem _toCalculatorItem(int sessionKey, ItemAscensionMaterials item) {
return CalculatorItem(
sessionKey,
item.key,
item.position,
item.currentLevel,
item.desiredLevel,
item.currentAscensionLevel,
item.desiredAscensionLevel,
item.isCharacter,
item.isWeapon,
item.isActive,
item.useMaterialsFromInventory,
);
}
}