-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
calculator_asc_materials_bloc.dart
272 lines (239 loc) · 10.4 KB
/
calculator_asc_materials_bloc.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
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:genshindb/domain/app_constants.dart';
import 'package:genshindb/domain/assets.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/models/models.dart';
import 'package:genshindb/domain/services/genshin_service.dart';
import 'package:genshindb/domain/services/telemetry_service.dart';
part 'calculator_asc_materials_bloc.freezed.dart';
part 'calculator_asc_materials_event.dart';
part 'calculator_asc_materials_state.dart';
class CalculatorAscMaterialsBloc extends Bloc<CalculatorAscMaterialsEvent, CalculatorAscMaterialsState> {
final GenshinService _genshinService;
final TelemetryService _telemetryService;
_InitialState get currentState => state as _InitialState;
CalculatorAscMaterialsBloc(
this._genshinService,
this._telemetryService,
) : super(const CalculatorAscMaterialsState.initial(items: [], summary: []));
@override
Stream<CalculatorAscMaterialsState> mapEventToState(
CalculatorAscMaterialsEvent event,
) async* {
final s = await event.map(
init: (_) async => const CalculatorAscMaterialsState.initial(items: [], summary: []),
addCharacter: (e) async {
await _telemetryService.trackCalculatorItemAscMaterialLoaded(e.key);
final char = _genshinService.getCharacter(e.key);
final translation = _genshinService.getCharacterTranslation(e.key);
final items = [
...currentState.items,
ItemAscensionMaterials.forCharacters(
key: e.key,
image: Assets.getCharacterPath(char.image),
name: translation.name,
rarity: char.rarity,
materials: _getCharacterMaterialsToUse(char, e.currentAscensionLevel, e.desiredAscensionLevel, e.skills),
currentLevel: e.currentLevel,
desiredLevel: e.desiredLevel,
skills: e.skills,
desiredAscensionLevel: e.desiredAscensionLevel,
currentAscensionLevel: e.currentAscensionLevel,
)
];
return currentState.copyWith.call(items: items, summary: _generateSummary(items.expand((i) => i.materials).toList()));
},
addWeapon: (e) async {
await _telemetryService.trackCalculatorItemAscMaterialLoaded(e.key);
final weapon = _genshinService.getWeapon(e.key);
final translation = _genshinService.getWeaponTranslation(e.key);
final items = [
...currentState.items,
ItemAscensionMaterials.forWeapons(
key: e.key,
image: weapon.fullImagePath,
name: translation.name,
rarity: weapon.rarity,
materials: _getWeaponMaterialsToUse(weapon, e.currentAscensionLevel, e.desiredAscensionLevel),
currentLevel: e.currentLevel,
desiredLevel: e.desiredLevel,
desiredAscensionLevel: e.desiredAscensionLevel,
currentAscensionLevel: e.currentAscensionLevel,
)
];
return currentState.copyWith.call(items: items, summary: _generateSummary(items.expand((i) => i.materials).toList()));
},
removeItem: (e) async {
final items = [...currentState.items];
items.removeAt(e.index);
return currentState.copyWith.call(items: items, summary: _generateSummary(items.expand((i) => i.materials).toList()));
},
updateCharacter: (e) async {
final currentChar = currentState.items.elementAt(e.index);
final char = _genshinService.getCharacter(currentChar.key);
final updatedChar = currentChar.copyWith.call(
materials: _getCharacterMaterialsToUse(char, e.currentAscensionLevel, e.desiredAscensionLevel, e.skills),
currentLevel: e.currentLevel,
desiredLevel: e.desiredLevel,
skills: e.skills,
desiredAscensionLevel: e.desiredAscensionLevel,
currentAscensionLevel: e.currentAscensionLevel,
);
return _updateItem(e.index, updatedChar);
},
updateWeapon: (e) async {
final currentWeapon = currentState.items.elementAt(e.index);
final weapon = _genshinService.getWeapon(currentWeapon.key);
final updatedWeapon = currentWeapon.copyWith.call(
materials: _getWeaponMaterialsToUse(weapon, e.currentAscensionLevel, e.desiredAscensionLevel),
currentLevel: e.currentLevel,
desiredLevel: e.desiredLevel,
desiredAscensionLevel: e.desiredAscensionLevel,
currentAscensionLevel: e.currentAscensionLevel,
);
return _updateItem(e.index, updatedWeapon);
},
);
yield s;
}
ItemAscensionMaterials getItem(int index) {
return currentState.items.elementAt(index);
}
List<AscensionMaterialsSummary> _generateSummary(List<ItemAscensionMaterialModel> current) {
final flattened = _flatMaterialsList(current);
final summary = <AscensionMaterialSummaryType, List<MaterialSummary>>{};
for (var i = 0; i < flattened.length; i++) {
final item = flattened[i];
final material = _genshinService.getMaterialByImage(item.fullImagePath);
MaterialSummary newValue;
AscensionMaterialSummaryType key;
if (material.isFromBoss) {
key = AscensionMaterialSummaryType.worldBoss;
newValue = MaterialSummary.fromBoss(
key: material.key,
materialType: item.materialType,
fullImagePath: item.fullImagePath,
quantity: item.quantity,
);
} else if (material.days.isNotEmpty) {
key = AscensionMaterialSummaryType.day;
newValue = MaterialSummary.fromDays(
key: material.key,
materialType: item.materialType,
fullImagePath: item.fullImagePath,
quantity: item.quantity,
days: material.days,
);
} else {
switch (material.type) {
case MaterialType.common:
key = AscensionMaterialSummaryType.common;
break;
case MaterialType.local:
key = AscensionMaterialSummaryType.local;
break;
case MaterialType.currency:
key = AscensionMaterialSummaryType.currency;
break;
case MaterialType.weapon:
case MaterialType.weaponPrimary:
key = AscensionMaterialSummaryType.common;
break;
case MaterialType.elemental:
case MaterialType.jewels:
case MaterialType.talents:
case MaterialType.others:
case MaterialType.ingredient:
key = AscensionMaterialSummaryType.others;
break;
}
newValue = MaterialSummary.others(
key: material.key,
materialType: material.type,
fullImagePath: material.fullImagePath,
quantity: item.quantity,
);
}
if (summary.containsKey(key)) {
summary[key].add(newValue);
} else {
summary.putIfAbsent(key, () => [newValue]);
}
summary[key].sort((x, y) => x.key.compareTo(y.key));
}
return summary.entries.map((entry) => AscensionMaterialsSummary(type: entry.key, materials: entry.value)).toList();
}
List<ItemAscensionMaterialModel> _getCharacterMaterialsToUse(
CharacterFileModel char,
int currentAscensionLevel,
int desiredAscensionLevel,
List<CharacterSkill> skills,
) {
final ascensionMaterials =
char.ascensionMaterials.where((m) => m.rank > currentAscensionLevel && m.rank <= desiredAscensionLevel).expand((e) => e.materials).toList();
final skillMaterials = <ItemAscensionMaterialModel>[];
if (char.talentAscensionMaterials.isNotEmpty) {
for (final skill in skills) {
final materials = char.talentAscensionMaterials
.where((m) => m.level > skill.currentLevel && m.level <= skill.desiredLevel)
.expand((m) => m.materials)
.toList();
skillMaterials.addAll(materials);
}
} else if (char.multiTalentAscensionMaterials != null && char.multiTalentAscensionMaterials.isNotEmpty) {
//The traveler has different materials depending on the skill, that's why we need to retrieve the right amount for the provided skill
//Also, we are assuming that the skill's order are fixed
var talentNumber = 1;
for (final skill in skills) {
final materials = char.multiTalentAscensionMaterials
.where((mt) => mt.number == talentNumber)
.expand((mt) => mt.materials)
.where((m) => m.level > skill.currentLevel && m.level <= skill.desiredLevel)
.expand((m) => m.materials)
.toList();
skillMaterials.addAll(materials);
talentNumber++;
}
}
return _flatMaterialsList(ascensionMaterials + skillMaterials);
}
List<ItemAscensionMaterialModel> _getWeaponMaterialsToUse(
WeaponFileModel weapon,
int currentAscensionLevel,
int desiredAscensionLevel,
) {
final materials = weapon.ascensionMaterials
.where((m) => m.level > _mapToWeaponLevel(currentAscensionLevel) && m.level <= _mapToWeaponLevel(desiredAscensionLevel))
.expand((m) => m.materials)
.toList();
return _flatMaterialsList(materials);
}
List<ItemAscensionMaterialModel> _flatMaterialsList(List<ItemAscensionMaterialModel> current) {
final materials = <ItemAscensionMaterialModel>[];
for (final image in current.map((e) => e.fullImagePath).toSet().toList()) {
final item = current.firstWhere((m) => m.fullImagePath == image);
final int quantity = current.where((m) => m.fullImagePath == image).map((e) => e.quantity).fold(0, (previous, current) => previous + current);
materials.add(item.copyWith.call(quantity: quantity));
}
return materials;
}
CalculatorAscMaterialsState _updateItem(int index, ItemAscensionMaterials updatedItem) {
final items = [...currentState.items];
items.removeAt(index);
items.insert(index, updatedItem);
return currentState.copyWith.call(items: items, summary: _generateSummary(items.expand((i) => i.materials).toList()));
}
int _mapToWeaponLevel(int val) {
switch (val) {
//Here we consider the 0, because otherwise we will always start from a current level of 1, and sometimes, we want to know the whole thing
//(from 1 to 10 with 1 inclusive)
case 0:
return 0;
default:
final entry = itemAscensionLevelMap.entries.firstWhere((kvp) => kvp.key == val);
return entry.value;
}
}
}