-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
custom_build_bloc.dart
324 lines (293 loc) · 11.1 KB
/
custom_build_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
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
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:shiori/domain/app_constants.dart';
import 'package:shiori/domain/enums/enums.dart';
import 'package:shiori/domain/extensions/string_extensions.dart';
import 'package:shiori/domain/models/models.dart';
import 'package:shiori/domain/services/data_service.dart';
import 'package:shiori/domain/services/genshin_service.dart';
part 'custom_build_bloc.freezed.dart';
part 'custom_build_event.dart';
part 'custom_build_state.dart';
class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
final GenshinService _genshinService;
final DataService _dataService;
static int maxTitleLength = 40;
static int maxNoteLength = 100;
static int maxNumberOfNotes = 5;
static List<CharacterSkillType> validSkillTypes = [
CharacterSkillType.normalAttack,
CharacterSkillType.elementalSkill,
CharacterSkillType.elementalBurst,
];
static List<CharacterSkillType> excludedSkillTypes = [CharacterSkillType.others];
CustomBuildBloc(this._genshinService, this._dataService) : super(const CustomBuildState.loading()) {
on<CustomBuildEvent>(_handleEvent);
}
Future<void> _handleEvent(CustomBuildEvent event, Emitter<CustomBuildState> emit) async {
//todo: SHOULD I TRHOW ON INVALID REQUEST ?
final s = await event.map(
load: (e) async => _init(e.key),
characterChanged: (e) async => state.maybeMap(
loaded: (state) => _characterChanged(e, state),
orElse: () => state,
),
titleChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(title: e.newValue),
orElse: () => state,
),
roleChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(type: e.newValue),
orElse: () => state,
),
subRoleChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(subType: e.newValue),
orElse: () => state,
),
showOnCharacterDetailChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(showOnCharacterDetail: e.newValue),
orElse: () => state,
),
addWeapon: (e) async => state.maybeMap(
loaded: (state) => _addWeapon(e, state),
orElse: () => state,
),
deleteWeapon: (e) async => state.maybeMap(
loaded: (state) => _deleteWeapon(e, state),
orElse: () => state,
),
weaponOrderChanged: (e) async => state,
addArtifact: (e) async => state.maybeMap(
loaded: (state) => _addArtifact(e, state),
orElse: () => state,
),
saveChanges: (e) async => state.maybeMap(
loaded: (state) => _saveChanges(state),
orElse: () async => state,
),
reset: (e) async => state,
addNote: (e) async => state.maybeMap(
loaded: (state) => _addNote(e, state),
orElse: () => state,
),
deleteNote: (e) async => state.maybeMap(
loaded: (state) => _deleteNote(e, state),
orElse: () => state,
),
deleteWeapons: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(weapons: []),
orElse: () => state,
),
deleteArtifacts: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(artifacts: [], subStatsSummary: []),
orElse: () => state,
),
deleteSkillPriority: (e) async => state.maybeMap(
loaded: (state) => _deleteSkillPriority(e, state),
orElse: () => state,
),
addSkillPriority: (e) async => state.maybeMap(
loaded: (state) => _addSkillPriority(e, state),
orElse: () => state,
),
isRecommendedChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(isRecommended: e.newValue),
orElse: () => state,
),
addArtifactSubStats: (e) async => state.maybeMap(
loaded: (state) => _addArtifactSubStats(e, state),
orElse: () => state,
),
deleteArtifact: (e) async => state.maybeMap(
loaded: (state) => _deleteArtifact(e, state),
orElse: () => state,
),
);
emit(s);
}
CustomBuildState _init(int? key) {
if (key != null) {
final build = _dataService.customBuilds.getCustomBuild(key);
return CustomBuildState.loaded(
key: key,
title: build.title,
type: build.type,
subType: build.subType,
showOnCharacterDetail: build.showOnCharacterDetail,
isRecommended: build.isRecommended,
character: build.character,
weapons: build.weapons,
notes: build.notes,
skillPriorities: build.skillPriorities,
artifacts: build.artifacts..sort((x, y) => x.type.index.compareTo(y.type.index)),
subStatsSummary: _generateSubStatSummary(build.artifacts),
);
}
final character = _genshinService.getCharactersForCard().first;
return CustomBuildState.loaded(
title: '',
type: CharacterRoleType.dps,
subType: CharacterRoleSubType.none,
showOnCharacterDetail: true,
isRecommended: false,
character: character,
notes: [],
weapons: [],
artifacts: []..sort((x, y) => x.type.index.compareTo(y.type.index)),
skillPriorities: [],
subStatsSummary: [],
);
}
CustomBuildState _addNote(_AddNote e, _LoadedState state) {
if (e.note.isNullEmptyOrWhitespace || state.notes.length >= maxNumberOfNotes) {
return state;
}
final newNote = CustomBuildNoteModel(index: state.notes.length, note: e.note);
return state.copyWith.call(notes: [...state.notes, newNote]);
}
CustomBuildState _deleteNote(_DeleteNote e, _LoadedState state) {
if (e.index < 0 || e.index >= state.notes.length) {
return state;
}
final notes = [...state.notes];
notes.removeAt(e.index);
return state.copyWith.call(notes: notes);
}
CustomBuildState _addSkillPriority(_AddSkillPriority e, _LoadedState state) {
if (state.skillPriorities.contains(e.type) || !validSkillTypes.contains(e.type)) {
return state;
}
return state.copyWith.call(skillPriorities: [...state.skillPriorities, e.type]);
}
CustomBuildState _deleteSkillPriority(_DeleteSkillPriority e, _LoadedState state) {
if (e.index < 0 || e.index >= state.skillPriorities.length) {
return state;
}
final skillPriorities = [...state.skillPriorities];
skillPriorities.removeAt(e.index);
return state.copyWith.call(skillPriorities: skillPriorities);
}
CustomBuildState _characterChanged(_CharacterChanged e, _LoadedState state) {
if (state.character.key == e.newKey) {
return state;
}
final newCharacter = _genshinService.getCharacterForCard(e.newKey);
return state.copyWith.call(character: newCharacter);
}
CustomBuildState _addWeapon(_AddWeapon e, _LoadedState state) {
if (state.weapons.any((el) => el.key == e.key)) {
throw Exception('Weapons cannot be repeated');
}
final weapon = _genshinService.getWeaponForCard(e.key);
final weapons = [...state.weapons, weapon];
return state.copyWith.call(weapons: weapons);
}
CustomBuildState _deleteWeapon(_DeleteWeapon e, _LoadedState state) {
if (!state.weapons.any((el) => el.key == e.key)) {
return state;
}
final updated = [...state.weapons];
updated.removeWhere((el) => el.key == e.key);
return state.copyWith.call(weapons: updated);
}
CustomBuildState _addArtifact(_AddArtifact e, _LoadedState state) {
final fullArtifact = _genshinService.getArtifact(e.key);
final translation = _genshinService.getArtifactTranslation(e.key);
final img = _genshinService.getArtifactRelatedPart(fullArtifact.fullImagePath, fullArtifact.image, translation.bonus.length, e.type);
final updatedArtifacts = [...state.artifacts];
final old = state.artifacts.firstWhereOrNull((el) => el.type == e.type);
if (old != null) {
updatedArtifacts.removeWhere((el) => el.type == e.type);
final updatedSubStats = [...old.subStats]..removeWhere((el) => el == e.statType);
final updated = old.copyWith.call(
type: e.type,
image: img,
key: e.key,
rarity: fullArtifact.maxRarity,
statType: e.statType,
subStats: updatedSubStats,
);
updatedArtifacts.add(updated);
} else {
final newOne = CustomBuildArtifactModel(
type: e.type,
image: img,
key: e.key,
rarity: fullArtifact.maxRarity,
statType: e.statType,
subStats: [],
);
updatedArtifacts.add(newOne);
}
return state.copyWith.call(artifacts: updatedArtifacts..sort((x, y) => x.type.index.compareTo(y.type.index)));
}
CustomBuildState _addArtifactSubStats(_AddArtifactSubStats e, _LoadedState state) {
final artifact = state.artifacts.firstWhereOrNull((el) => el.type == e.type);
if (artifact == null) {
return state;
}
final possibleSubStats = getArtifactPossibleSubStats(artifact.statType);
if (e.subStats.any((s) => !possibleSubStats.contains(s))) {
throw Exception('One of the provided sub-stats is not valid');
}
final index = state.artifacts.indexOf(artifact);
final updated = artifact.copyWith.call(subStats: e.subStats);
final artifacts = [...state.artifacts];
artifacts.removeAt(index);
artifacts.insert(index, updated);
return state.copyWith.call(artifacts: artifacts, subStatsSummary: _generateSubStatSummary(artifacts));
}
CustomBuildState _deleteArtifact(_DeleteArtifact e, _LoadedState state) {
if (!state.artifacts.any((el) => el.type == e.type)) {
return state;
}
final updated = [...state.artifacts];
updated.removeWhere((el) => el.type == e.type);
return state.copyWith.call(artifacts: updated, subStatsSummary: _generateSubStatSummary(updated));
}
Future<CustomBuildState> _saveChanges(_LoadedState state) async {
if (state.key != null) {
await _dataService.customBuilds.updateCustomBuild(
state.key!,
state.title,
state.type,
state.subType,
state.showOnCharacterDetail,
state.isRecommended,
state.notes,
state.weapons.map((e) => e.key).toList(),
[],
[],
);
return _init(state.key);
}
final build = await _dataService.customBuilds.saveCustomBuild(
state.character.key,
state.title,
state.type,
state.subType,
state.showOnCharacterDetail,
state.isRecommended,
state.notes,
state.weapons.map((e) => e.key).toList(),
//TODO: THIS
[],
[],
);
return _init(build.key);
}
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();
}
}