-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
genshin_service.dart
1398 lines (1221 loc) · 50 KB
/
genshin_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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'dart:convert';
import 'dart:math';
import 'package:collection/collection.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:shiori/domain/app_constants.dart';
import 'package:shiori/domain/assets.dart';
import 'package:shiori/domain/enums/enums.dart';
import 'package:shiori/domain/extensions/double_extensions.dart';
import 'package:shiori/domain/extensions/string_extensions.dart';
import 'package:shiori/domain/models/models.dart';
import 'package:shiori/domain/services/genshin_service.dart';
import 'package:shiori/domain/services/locale_service.dart';
import 'package:shiori/domain/utils/date_utils.dart';
class GenshinServiceImpl implements GenshinService {
final LocaleService _localeService;
late CharactersFile _charactersFile;
late WeaponsFile _weaponsFile;
late TranslationFile _translationFile;
late ArtifactsFile _artifactsFile;
late MaterialsFile _materialsFile;
late ElementsFile _elementsFile;
late MonstersFile _monstersFile;
late GadgetsFile _gadgetsFile;
late FurnitureFile _furnitureFile;
late BannerHistoryFile _bannerHistoryFile;
GenshinServiceImpl(this._localeService);
@override
Future<void> init(AppLanguageType languageType) async {
await Future.wait([
initCharacters(),
initWeapons(),
initArtifacts(),
initMaterials(),
initElements(),
initMonsters(),
initGadgets(),
initFurniture(),
initBannerHistory(),
initTranslations(languageType),
]);
}
@override
Future<void> initCharacters() async {
final jsonStr = await rootBundle.loadString(Assets.charactersDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_charactersFile = CharactersFile.fromJson(json);
}
@override
Future<void> initWeapons() async {
final jsonStr = await rootBundle.loadString(Assets.weaponsDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_weaponsFile = WeaponsFile.fromJson(json);
}
@override
Future<void> initArtifacts() async {
final jsonStr = await rootBundle.loadString(Assets.artifactsDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_artifactsFile = ArtifactsFile.fromJson(json);
}
@override
Future<void> initMaterials() async {
final jsonStr = await rootBundle.loadString(Assets.materialsDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_materialsFile = MaterialsFile.fromJson(json);
}
@override
Future<void> initElements() async {
final jsonStr = await rootBundle.loadString(Assets.elementsDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_elementsFile = ElementsFile.fromJson(json);
}
@override
Future<void> initMonsters() async {
final jsonStr = await rootBundle.loadString(Assets.monstersDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_monstersFile = MonstersFile.fromJson(json);
}
@override
Future<void> initGadgets() async {
final jsonStr = await rootBundle.loadString(Assets.gadgetsDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_gadgetsFile = GadgetsFile.fromJson(json);
assert(
_gadgetsFile.gadgets.map((e) => e.key).toSet().length == _gadgetsFile.gadgets.length,
'All the gadgets keys must be unique',
);
}
@override
Future<void> initFurniture() async {
final jsonStr = await rootBundle.loadString(Assets.furnitureDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_furnitureFile = FurnitureFile.fromJson(json);
assert(
_furnitureFile.furniture.map((e) => e.key).toSet().length == _furnitureFile.furniture.length,
'All the furniture keys must be unique',
);
}
@override
Future<void> initBannerHistory() async {
final jsonStr = await rootBundle.loadString(Assets.bannerHistoryDbPath);
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_bannerHistoryFile = BannerHistoryFile.fromJson(json);
}
@override
Future<void> initTranslations(AppLanguageType languageType) async {
final transJsonStr = await rootBundle.loadString(Assets.getTranslationPath(languageType));
final transJson = jsonDecode(transJsonStr) as Map<String, dynamic>;
_translationFile = TranslationFile.fromJson(transJson);
}
@override
List<CharacterCardModel> getCharactersForCard() {
return _charactersFile.characters.map((e) => _toCharacterForCard(e)).toList();
}
@override
CharacterFileModel getCharacter(String key) {
return _charactersFile.characters.firstWhere((element) => element.key == key);
}
@override
List<CharacterFileModel> getCharactersForBirthday(DateTime date) {
return _charactersFile.characters.where((char) {
if (char.isComingSoon) {
return false;
}
if (char.birthday.isNullEmptyOrWhitespace) {
return false;
}
final charBirthday = _localeService.getCharBirthDate(char.birthday);
return charBirthday.day == date.day && charBirthday.month == date.month;
}).toList();
}
@override
List<TierListRowModel> getDefaultCharacterTierList(List<int> colors) {
assert(colors.length == 7);
final sssTier = _charactersFile.characters
.where((char) => !char.isComingSoon && char.tier == 'sss')
.map((char) => ItemCommon(char.key, Assets.getCharacterPath(char.image)))
.toList();
final ssTier = _charactersFile.characters
.where((char) => !char.isComingSoon && char.tier == 'ss')
.map((char) => ItemCommon(char.key, Assets.getCharacterPath(char.image)))
.toList();
final sTier = _charactersFile.characters
.where((char) => !char.isComingSoon && char.tier == 's')
.map((char) => ItemCommon(char.key, Assets.getCharacterPath(char.image)))
.toList();
final aTier = _charactersFile.characters
.where((char) => !char.isComingSoon && char.tier == 'a')
.map((char) => ItemCommon(char.key, Assets.getCharacterPath(char.image)))
.toList();
final bTier = _charactersFile.characters
.where((char) => !char.isComingSoon && char.tier == 'b')
.map((char) => ItemCommon(char.key, Assets.getCharacterPath(char.image)))
.toList();
final cTier = _charactersFile.characters
.where((char) => !char.isComingSoon && char.tier == 'c')
.map((char) => ItemCommon(char.key, Assets.getCharacterPath(char.image)))
.toList();
final dTier = _charactersFile.characters
.where((char) => !char.isComingSoon && char.tier == 'd')
.map((char) => ItemCommon(char.key, Assets.getCharacterPath(char.image)))
.toList();
return <TierListRowModel>[
TierListRowModel.row(tierText: 'SSS', tierColor: colors.first, items: sssTier),
TierListRowModel.row(tierText: 'SS', tierColor: colors[1], items: ssTier),
TierListRowModel.row(tierText: 'S', tierColor: colors[2], items: sTier),
TierListRowModel.row(tierText: 'A', tierColor: colors[3], items: aTier),
TierListRowModel.row(tierText: 'B', tierColor: colors[4], items: bTier),
TierListRowModel.row(tierText: 'C', tierColor: colors[5], items: cTier),
TierListRowModel.row(tierText: 'D', tierColor: colors.last, items: dTier),
];
}
@override
List<WeaponCardModel> getWeaponsForCard() {
return _weaponsFile.weapons.map((e) => _toWeaponForCard(e)).toList();
}
@override
WeaponFileModel getWeapon(String key) {
return _weaponsFile.weapons.firstWhere((element) => element.key == key);
}
@override
List<ItemCommon> getCharacterForItemsUsingWeapon(String key) {
final weapon = getWeapon(key);
final items = <ItemCommon>[];
for (final char in _charactersFile.characters.where((el) => !el.isComingSoon)) {
for (final build in char.builds) {
final isBeingUsed = build.weaponKeys.contains(weapon.key);
if (isBeingUsed && !items.any((el) => el.key == char.key)) {
items.add(ItemCommon(char.key, Assets.getCharacterPath(char.image)));
}
}
}
return items;
}
@override
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
ArtifactCardModel getArtifactForCard(String key) {
final artifact = _artifactsFile.artifacts.firstWhere((a) => a.key == key);
return _toArtifactForCard(artifact);
}
@override
ArtifactFileModel getArtifact(String key) {
return _artifactsFile.artifacts.firstWhere((a) => a.key == key);
}
@override
List<ItemCommon> getCharacterForItemsUsingArtifact(String key) {
final artifact = getArtifact(key);
final items = <ItemCommon>[];
for (final char in _charactersFile.characters.where((el) => !el.isComingSoon)) {
for (final build in char.builds) {
final isBeingUsed = build.artifacts.any((a) => a.oneKey == artifact.key || a.multiples.any((m) => m.key == artifact.key));
if (isBeingUsed && !items.any((el) => el.key == char.key)) {
items.add(ItemCommon(char.key, Assets.getCharacterPath(char.image)));
}
}
}
return items;
}
@override
TranslationCharacterFile getCharacterTranslation(String key) {
return _translationFile.characters.firstWhere((element) => element.key == key);
}
@override
TranslationWeaponFile getWeaponTranslation(String key) {
return _translationFile.weapons.firstWhere((element) => element.key == key);
}
@override
TranslationArtifactFile getArtifactTranslation(String key) {
return _translationFile.artifacts.firstWhere((t) => t.key == key);
}
@override
TranslationMaterialFile getMaterialTranslation(String key) {
return _translationFile.materials.firstWhere((t) => t.key == key);
}
@override
TranslationMonsterFile getMonsterTranslation(String key) {
return _translationFile.monsters.firstWhere((el) => el.key == key);
}
@override
List<TodayCharAscensionMaterialsModel> getCharacterAscensionMaterials(int day) {
final iterable = day == DateTime.sunday
? _materialsFile.talents.where((t) => t.days.isNotEmpty && t.level == 0)
: _materialsFile.talents.where((t) => t.days.contains(day) && t.level == 0);
return iterable.map((e) {
final translation = _translationFile.materials.firstWhere((t) => t.key == e.key);
final characters = <ItemCommon>[];
for (final char in _charactersFile.characters) {
if (char.isComingSoon) {
continue;
}
final normalAscMaterial = char.ascensionMaterials.expand((m) => m.materials).where((m) => m.key == e.key).isNotEmpty ||
char.talentAscensionMaterials.expand((m) => m.materials).where((m) => m.key == e.key).isNotEmpty;
//The travelers have different ascension materials, that's why we do the following
var specialAscMaterial = false;
if (char.multiTalentAscensionMaterials != null) {
final keyword = e.key;
final materials = char.multiTalentAscensionMaterials!
.expand((m) => m.materials)
.expand((m) => m.materials)
.where((m) => m.type == MaterialType.talents)
.map((e) => e.key)
.toSet()
.toList();
specialAscMaterial = materials.any((m) => m == keyword);
}
final materialIsBeingUsed = normalAscMaterial || specialAscMaterial;
if (materialIsBeingUsed && !characters.any((el) => el.key == char.key)) {
characters.add(ItemCommon(char.key, Assets.getCharacterPath(char.image)));
}
}
return e.isFromBoss
? TodayCharAscensionMaterialsModel.fromBoss(
key: e.key,
name: translation.name,
image: Assets.getMaterialPath(e.image, e.type),
bossName: translation.bossName,
characters: characters,
)
: TodayCharAscensionMaterialsModel.fromDays(
key: e.key,
name: translation.name,
image: Assets.getMaterialPath(e.image, e.type),
characters: characters,
days: e.days,
);
}).toList();
}
@override
List<TodayWeaponAscensionMaterialModel> getWeaponAscensionMaterials(int day) {
final iterable = day == DateTime.sunday
? _materialsFile.weaponPrimary.where((t) => t.level == 0)
: _materialsFile.weaponPrimary.where((t) => t.days.contains(day) && t.level == 0);
return iterable.map((e) {
final translation = _translationFile.materials.firstWhere((t) => t.key == e.key);
final weapons = <ItemCommon>[];
for (final weapon in _weaponsFile.weapons) {
final materialIsBeingUsed = weapon.ascensionMaterials.expand((m) => m.materials).where((m) => m.key == e.key).isNotEmpty;
if (materialIsBeingUsed) {
weapons.add(ItemCommon(weapon.key, weapon.fullImagePath));
}
}
return TodayWeaponAscensionMaterialModel(
key: e.key,
days: e.days,
name: translation.name,
image: Assets.getMaterialPath(e.image, e.type),
weapons: weapons,
);
}).toList();
}
@override
List<ElementCardModel> getElementDebuffs() {
return _elementsFile.debuffs.map(
(e) {
final translation = _translationFile.debuffs.firstWhere((t) => t.key == e.key);
final reaction = ElementCardModel(name: translation.name, effect: translation.effect, image: e.fullImagePath);
return reaction;
},
).toList()
..sort((x, y) => x.name.compareTo(y.name));
}
@override
List<ElementReactionCardModel> getElementReactions() {
return _elementsFile.reactions.map(
(e) {
final translation = _translationFile.reactions.firstWhere((t) => t.key == e.key);
final reaction = ElementReactionCardModel.withImages(
name: translation.name,
effect: translation.effect,
principal: e.principalImages,
secondary: e.secondaryImages,
);
return reaction;
},
).toList()
..sort((x, y) => x.name.compareTo(y.name));
}
@override
List<ElementReactionCardModel> getElementResonances() {
return _elementsFile.resonance.map(
(e) {
final translation = _translationFile.resonance.firstWhere((t) => t.key == e.key);
final reaction = e.hasImages
? ElementReactionCardModel.withImages(
name: translation.name,
effect: translation.effect,
principal: e.principalImages,
secondary: e.secondaryImages,
)
: ElementReactionCardModel.withoutImages(
name: translation.name,
effect: translation.effect,
description: translation.description,
);
return reaction;
},
).toList();
}
@override
List<MaterialCardModel> getAllMaterialsForCard() {
return _materialsFile.materials.where((el) => el.isReadyToBeUsed).map((e) => _toMaterialForCard(e)).toList();
}
@override
MaterialFileModel getMaterial(String key) {
return _materialsFile.materials.firstWhere((m) => m.key == key);
}
@override
MaterialFileModel getMaterialByImage(String image) {
return _materialsFile.materials.firstWhere((m) => m.fullImagePath == image);
}
@override
List<MaterialFileModel> getMaterials(MaterialType type, {bool onlyReadyToBeUsed = true}) {
if (onlyReadyToBeUsed) {
return _materialsFile.materials.where((m) => m.type == type && m.isReadyToBeUsed).toList();
}
return _materialsFile.materials.where((m) => m.type == type).toList();
}
@override
MaterialFileModel getMoraMaterial() {
return _materialsFile.materials.firstWhere((el) => el.type == MaterialType.currency && el.key == 'mora');
}
@override
String getMaterialImg(String key) {
return _materialsFile.materials.firstWhere((m) => m.key == key).fullImagePath;
}
@override
int getServerDay(AppServerResetTimeType type) {
return getServerDate(type).weekday;
}
@override
DateTime getServerDate(AppServerResetTimeType type) {
final now = DateTime.now();
final nowUtc = now.toUtc();
DateTime server;
// According to this page, the server reset happens at 4 am
// https://game8.co/games/Genshin-Impact/archives/301599
switch (type) {
case AppServerResetTimeType.northAmerica:
server = nowUtc.subtract(const Duration(hours: 5));
break;
case AppServerResetTimeType.europe:
server = nowUtc.add(const Duration(hours: 1));
break;
case AppServerResetTimeType.asia:
server = nowUtc.add(const Duration(hours: 8));
break;
default:
throw Exception('Invalid server reset type');
}
if (server.hour >= serverResetHour) {
return server;
}
return server.subtract(const Duration(days: 1));
}
@override
Duration getDurationUntilServerResetDate(AppServerResetTimeType type) {
final serverDate = getServerDate(type);
//Here the utc part is important, otherwise the difference will be calculated using the local time
final serverResetDate = DateTime.utc(serverDate.year, serverDate.month, serverDate.day, serverResetHour);
final dateToUse = serverDate.isBefore(serverResetDate) ? serverDate : serverDate.subtract(const Duration(days: 1));
return serverResetDate.difference(dateToUse);
}
@override
List<ItemCommon> getCharacterForItemsUsingMaterial(String key) {
final material = getMaterial(key);
final imgs = <ItemCommon>[];
for (final char in _charactersFile.characters.where((c) => !c.isComingSoon)) {
final multiTalentAscensionMaterials =
(char.multiTalentAscensionMaterials?.expand((e) => e.materials).expand((e) => e.materials) ?? <ItemAscensionMaterialFileModel>[]).toList();
final ascensionMaterial = char.ascensionMaterials.expand((e) => e.materials).toList();
final talentMaterial = char.talentAscensionMaterials.expand((e) => e.materials).toList();
final materials = multiTalentAscensionMaterials + ascensionMaterial + talentMaterial;
final allMaterials = _getMaterialsToUse(materials);
if (allMaterials.any((m) => m.key == material.key)) {
imgs.add(ItemCommon(char.key, Assets.getCharacterPath(char.image)));
}
}
return imgs;
}
@override
List<ItemCommon> getWeaponForItemsUsingMaterial(String key) {
final material = getMaterial(key);
final items = <ItemCommon>[];
for (final weapon in _weaponsFile.weapons) {
final materials = weapon.craftingMaterials + weapon.ascensionMaterials.expand((e) => e.materials).toList();
final allMaterials = _getMaterialsToUse(materials);
if (allMaterials.any((m) => m.key == material.key)) {
items.add(ItemCommon(weapon.key, weapon.fullImagePath));
}
}
return items;
}
@override
MaterialCardModel getMaterialForCard(String key) {
final material = _materialsFile.materials.firstWhere((m) => m.key == key);
return _toMaterialForCard(material);
}
@override
CharacterCardModel getCharacterForCard(String key) {
final character = _charactersFile.characters.firstWhere((el) => el.key == key);
return _toCharacterForCard(character);
}
@override
WeaponCardModel getWeaponForCard(String key) {
final weapon = _weaponsFile.weapons.firstWhere((el) => el.key == key);
return _toWeaponForCard(weapon);
}
@override
List<String> getUpcomingCharactersKeys() => _charactersFile.characters.where((el) => el.isComingSoon).map((e) => e.key).toList();
@override
List<String> getUpcomingWeaponsKeys() => _weaponsFile.weapons.where((el) => el.isComingSoon).map((e) => e.key).toList();
@override
List<String> getUpcomingKeys() => getUpcomingCharactersKeys() + getUpcomingWeaponsKeys();
@override
MonsterFileModel getMonster(String key) {
return _monstersFile.monsters.firstWhere((el) => el.key == key);
}
@override
List<MonsterCardModel> getAllMonstersForCard() {
return _monstersFile.monsters.map((e) => _toMonsterForCard(e)).toList();
}
@override
MonsterCardModel getMonsterForCard(String key) {
final monster = _monstersFile.monsters.firstWhere((el) => el.key == key);
return _toMonsterForCard(monster);
}
@override
List<MonsterFileModel> getMonsters(MonsterType type) {
return _monstersFile.monsters.where((el) => el.type == type).toList();
}
List<MaterialFileModel> _getMaterialsToUse(
List<ItemAscensionMaterialFileModel> materials, {
List<MaterialType> ignore = const [MaterialType.currency],
}) {
final mp = <String, MaterialFileModel>{};
for (final item in materials) {
final material = getMaterial(item.key);
if (!ignore.contains(item.type)) {
mp[item.key] = material;
}
}
return mp.values.toList();
}
@override
List<ItemCommon> getRelatedMonsterToMaterialForItems(String key) {
final items = <ItemCommon>[];
for (final monster in _monstersFile.monsters) {
if (!monster.drops.any((el) => el.type == MonsterDropType.material && el.key == key)) {
continue;
}
items.add(ItemCommon(monster.key, monster.fullImagePath));
}
return items;
}
@override
List<ItemCommon> getRelatedMonsterToArtifactForItems(String key) {
final items = <ItemCommon>[];
for (final monster in _monstersFile.monsters) {
if (!monster.drops.any((el) => el.type == MonsterDropType.artifact && key == el.key)) {
continue;
}
items.add(ItemCommon(monster.key, monster.fullImagePath));
}
return items;
}
@override
List<MaterialFileModel> getAllMaterialsThatCanBeObtainedFromAnExpedition() {
return _materialsFile.materials.where((el) => el.canBeObtainedFromAnExpedition).toList();
}
@override
List<MaterialFileModel> getAllMaterialsThatHaveAFarmingRespawnDuration() {
return _materialsFile.materials.where((el) => el.farmingRespawnDuration != null).toList();
}
@override
String getItemImageFromNotificationType(
String itemKey,
AppNotificationType notificationType, {
AppNotificationItemType? notificationItemType,
}) {
switch (notificationType) {
case AppNotificationType.resin:
case AppNotificationType.expedition:
case AppNotificationType.realmCurrency:
final material = getMaterial(itemKey);
return material.fullImagePath;
case AppNotificationType.furniture:
final furniture = getFurniture(itemKey);
return furniture.fullImagePath;
case AppNotificationType.gadget:
final gadget = getGadget(itemKey);
return gadget.fullImagePath;
case AppNotificationType.farmingArtifacts:
final artifact = getArtifact(itemKey);
return artifact.fullImagePath;
case AppNotificationType.farmingMaterials:
final material = getMaterial(itemKey);
return material.fullImagePath;
case AppNotificationType.weeklyBoss:
final monsters = getMonster(itemKey);
return monsters.fullImagePath;
case AppNotificationType.custom:
case AppNotificationType.dailyCheckIn:
return getItemImageFromNotificationItemType(itemKey, notificationItemType!);
default:
throw Exception('The provided notification type = $notificationType is not valid');
}
}
@override
String getItemImageFromNotificationItemType(String itemKey, AppNotificationItemType notificationItemType) {
switch (notificationItemType) {
case AppNotificationItemType.character:
final character = getCharacter(itemKey);
return character.fullImagePath;
case AppNotificationItemType.weapon:
final weapon = getWeapon(itemKey);
return weapon.fullImagePath;
case AppNotificationItemType.artifact:
final artifact = getArtifact(itemKey);
return artifact.fullImagePath;
case AppNotificationItemType.monster:
final monster = getMonster(itemKey);
return monster.fullImagePath;
case AppNotificationItemType.material:
final material = getMaterial(itemKey);
return material.fullImagePath;
default:
throw Exception('The provided notification item type = $notificationItemType');
}
}
@override
List<GadgetFileModel> getAllGadgetsForNotifications() {
return _gadgetsFile.gadgets.where((el) => el.cooldownDuration != null).toList();
}
@override
GadgetFileModel getGadget(String key) {
return _gadgetsFile.gadgets.firstWhere((m) => m.key == key);
}
@override
FurnitureFileModel getDefaultFurnitureForNotifications() {
return _furnitureFile.furniture.first;
}
@override
FurnitureFileModel getFurniture(String key) {
return _furnitureFile.furniture.firstWhere((m) => m.key == key);
}
@override
DateTime getNextDateForWeeklyBoss(AppServerResetTimeType type) {
final durationUntilServerReset = getDurationUntilServerResetDate(type);
var finalDate = DateTime.now().add(durationUntilServerReset);
while (finalDate.weekday != DateTime.monday) {
finalDate = finalDate.add(const Duration(days: 1));
}
return finalDate;
}
@override
List<CharacterSkillStatModel> getCharacterSkillStats(List<CharacterFileSkillStatModel> skillStats, List<String> statsTranslations) {
final stats = <CharacterSkillStatModel>[];
if (skillStats.isEmpty || statsTranslations.isEmpty) {
return stats;
}
final statExp = RegExp('(?<={).+?(?=})');
final maxLevel = skillStats.first.values.length;
for (var i = 1; i <= maxLevel; i++) {
final stat = CharacterSkillStatModel(level: i, descriptions: []);
for (final translation in statsTranslations) {
// "Curación continua|{param3}% Max HP + {param4}",
final splitted = translation.split('|');
if (splitted.isEmpty || splitted.length != 2) {
continue;
}
final desc = splitted.first;
var toReplace = splitted[1];
final matches = statExp.allMatches(toReplace);
for (final match in matches) {
final val = match.group(0);
final statValues = skillStats.firstWhereOrNull((el) => el.key == val);
if (statValues == null) {
continue;
}
if (statValues.values.length - 1 < i - 1) {
continue;
}
final statValue = statValues.values[i - 1];
toReplace = toReplace.replaceFirst('{$val}', '$statValue');
}
stat.descriptions.add('$desc|$toReplace');
}
stats.add(stat);
}
return stats;
}
@override
List<ArtifactCardBonusModel> getArtifactBonus(TranslationArtifactFile translation) {
final bonus = <ArtifactCardBonusModel>[];
var pieces = translation.bonus.length == 2 ? 2 : 1;
for (var i = 1; i <= translation.bonus.length; i++) {
final item = ArtifactCardBonusModel(pieces: pieces, bonus: translation.bonus[i - 1]);
bonus.add(item);
pieces += 2;
}
return bonus;
}
@override
List<String> getArtifactRelatedParts(String fullImagePath, String image, int bonus) {
var imageWithoutExt = image.split('.png').first;
imageWithoutExt = imageWithoutExt.substring(0, imageWithoutExt.length - 1);
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'));
}
@override
List<StatType> generateSubStatSummary(List<CustomBuildArtifactModel> artifacts) {
final weightMap = <StatType, int>{};
for (final artifact in artifacts) {
int weight = artifact.subStats.length;
for (var i = 0; i < artifact.subStats.length; i++) {
final subStat = artifact.subStats[i];
final ifAbsent = weightMap.containsKey(subStat) ? i : weight;
weightMap.update(subStat, (value) => value + weight, ifAbsent: () => ifAbsent);
weight--;
}
}
final sorted = weightMap.entries.sorted((a, b) => b.value.compareTo(a.value));
return sorted.map((e) => e.key).toList();
}
@override
List<double> getBannerHistoryVersions(SortDirectionType type) {
final versions = _bannerHistoryFile.banners.map((el) => el.version).toSet().toList();
switch (type) {
case SortDirectionType.asc:
return versions..sort((x, y) => x.compareTo(y));
case SortDirectionType.desc:
return versions..sort((x, y) => y.compareTo(x));
}
}
@override
List<BannerHistoryItemModel> getBannerHistory(BannerHistoryItemType type) {
final banners = <BannerHistoryItemModel>[];
final itemVersionsMap = <String, List<double>>{};
final allVersions = getBannerHistoryVersions(SortDirectionType.asc);
final filteredBanners = _bannerHistoryFile.banners.where((el) => el.type == type).toList();
for (final banner in filteredBanners) {
for (final key in banner.itemKeys) {
final alreadyAdded = banners.any((el) => el.key == key);
switch (banner.type) {
case BannerHistoryItemType.character:
if (!alreadyAdded) {
final char = getCharacterForCard(key);
final item = BannerHistoryItemModel(
versions: [],
image: char.image,
name: char.name,
key: key,
type: banner.type,
rarity: char.stars,
);
banners.add(item);
}
break;
case BannerHistoryItemType.weapon:
if (!alreadyAdded) {
final weapon = getWeaponForCard(key);
final bannerItem = BannerHistoryItemModel(
versions: [],
image: weapon.image,
name: weapon.name,
key: key,
type: banner.type,
rarity: weapon.rarity,
);
banners.add(bannerItem);
}
break;
default:
throw Exception('The provided banner type = ${banner.type} is not mapped');
}
if (!alreadyAdded) {
itemVersionsMap[key] = [banner.version];
} else {
itemVersionsMap.update(key, (value) => [...value, banner.version]);
}
}
}
for (var i = 0; i < banners.length; i++) {
final current = banners[i];
final values = itemVersionsMap.entries.firstWhere((el) => el.key == current.key).value;
final updated = current.copyWith.call(versions: _getBannerVersionsForItem(allVersions, values));
banners.removeAt(i);
banners.insert(i, updated);
}
return banners;
}
@override
List<BannerHistoryPeriodModel> getBanners(double version) {
final banners = _bannerHistoryFile.banners
.where((el) => el.version == version)
.map(
(e) => BannerHistoryPeriodModel(
from: e.from,
until: e.until,
type: e.type,
version: e.version,
items: e.itemKeys.map((key) {
String? imagePath;
int? rarity;
ItemType? type;
switch (e.type) {
case BannerHistoryItemType.character:
final character = getCharacter(key);
rarity = character.rarity;
imagePath = character.fullImagePath;
type = ItemType.character;
break;
case BannerHistoryItemType.weapon:
final weapon = getWeapon(key);
rarity = weapon.rarity;
imagePath = weapon.fullImagePath;
type = ItemType.weapon;
break;
default:
throw Exception('Banner history item type = ${e.type} is not valid');
}
return ItemCommonWithRarityAndType(key, imagePath, rarity, type);
}).toList(),
),
)
.toList()
..sort((x, y) => x.from.compareTo(y.from));
if (banners.isEmpty) {
throw Exception('Banners associated to version = $version were not found');
}
return banners;
}
@override
List<ItemReleaseHistoryModel> getItemReleaseHistory(String itemKey) {
final history = _bannerHistoryFile.banners
.where((el) => el.itemKeys.contains(itemKey))
.map((e) => ItemReleaseHistoryModel(version: e.version, dates: [ItemReleaseHistoryDatesModel(from: e.from, until: e.until)]))
.toList();
if (history.isEmpty) {
throw Exception('There is no banner history associated to itemKey = $itemKey');
}
return history.groupListsBy((el) => el.version).entries.map((e) {
//with the multi banners, we need to group the dates to avoid showing up repeated ones
final dates = e.value
.expand((el) => el.dates)
.groupListsBy((d) => '${d.from}__${d.until}')
.values
.map((e) => ItemReleaseHistoryDatesModel(from: e.first.from, until: e.first.until))
.toList();
return ItemReleaseHistoryModel(version: e.key, dates: dates);
}).toList()
..sort((x, y) => x.version.compareTo(y.version));
}
@override
List<ChartTopItemModel> getTopCharts(ChartType type) {
final fiveStars = [
ChartType.topFiveStarCharacterMostReruns,
ChartType.topFiveStarCharacterLeastReruns,
ChartType.topFiveStarWeaponMostReruns,
ChartType.topFiveStarWeaponLeastReruns,
];
final stars = fiveStars.contains(type) ? 5 : 4;
final mostRerunsTypes = [
ChartType.topFiveStarCharacterMostReruns,
ChartType.topFourStarCharacterMostReruns,
ChartType.topFiveStarWeaponMostReruns,
ChartType.topFourStarWeaponMostReruns,
];
final mostReruns = mostRerunsTypes.contains(type);
switch (type) {
case ChartType.topFiveStarCharacterMostReruns:
case ChartType.topFourStarCharacterMostReruns:
case ChartType.topFiveStarCharacterLeastReruns:
case ChartType.topFourStarCharacterLeastReruns:
final characters = getCharactersForCard().where((el) => el.stars == stars).map((e) => ItemCommonWithName(e.key, e.image, e.name)).toList();
return _getTopCharts(mostReruns, type, BannerHistoryItemType.character, characters);
case ChartType.topFiveStarWeaponMostReruns:
case ChartType.topFourStarWeaponMostReruns:
case ChartType.topFiveStarWeaponLeastReruns:
case ChartType.topFourStarWeaponLeastReruns:
final weapons = getWeaponsForCard().where((el) => el.rarity == stars).map((e) => ItemCommonWithName(e.key, e.image, e.name)).toList();
return _getTopCharts(mostReruns, type, BannerHistoryItemType.weapon, weapons);
default:
throw Exception('Type = $type is not valid in the getTopCharts method');
}
}
@override
List<ChartBirthdayMonthModel> getCharacterBirthdaysForCharts() {
final grouped = _charactersFile.characters
.where((char) => !char.isComingSoon && !char.birthday.isNullEmptyOrWhitespace)
.groupListsBy((char) => _localeService.getCharBirthDate(char.birthday).month)
.entries;
final birthdays = grouped
.map(
(e) => ChartBirthdayMonthModel(