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
|
part of 'game_state.dart';
final _gameSaveStateLog = Logger('GameSaveState');
class GameSaveState {
String? _savedState;
String getState() {
final state = _savedState;
if (state == null) throw StateError('GameSaveState: no saved state');
return state;
}
void save(GameState gameState) {
_savedState = gameState.toString();
}
void load(GameState gameState) {
if (_savedState != null) {
try {
final data = json.decode(_savedState ?? '') as Map<String, dynamic>;
gameState._level = data['level'] as int;
gameState.scenario = data['scenario']; // as String;
if (data.containsKey('toastMessage')) {
gameState._toastMessage = data['toastMessage']; // as String;
}
if (data.containsKey('scenarioSectionsAdded')) {
final List<Object?> scenarioSectionsAdded =
data['scenarioSectionsAdded'] as List<Object?>;
gameState._scenarioSectionsAdded.clear();
for (final item in scenarioSectionsAdded) {
gameState._scenarioSectionsAdded.add(item as String);
}
}
if (data.containsKey('scenarioSpecialRules')) {
final scenarioSpecialRulesList = data['scenarioSpecialRules'] as List;
gameState._scenarioSpecialRules.clear();
for (Map<String, dynamic> item in scenarioSpecialRulesList) {
gameState._scenarioSpecialRules.add(SpecialRule.fromJson(item));
}
}
gameState._scenarioSectionsVersion++;
gameState._currentCampaign = data['currentCampaign'];
gameState._round = data['round'] as int;
if (data.containsKey('totalRounds')) {
gameState._totalRounds = data['totalRounds'] as int;
} else {
gameState._totalRounds = gameState._round;
}
final roundStateIdx = data['roundState'] as int?;
if (roundStateIdx != null &&
roundStateIdx >= 0 &&
roundStateIdx < RoundState.values.length) {
gameState._roundState = RoundState.values[roundStateIdx];
}
gameState._solo = data['solo'] as bool;
if (data.containsKey('autoScenarioLevel')) {
gameState._autoScenarioLevel = data['autoScenarioLevel'] as bool;
} else {
gameState._autoScenarioLevel = true;
}
if (data.containsKey("allyDeckInOGGloom")) {
gameState._allyDeckInOGGloom = data["allyDeckInOGGloom"] as bool;
} else {
gameState._allyDeckInOGGloom = true;
}
if (data.containsKey('difficulty')) {
gameState._difficulty = data['difficulty'] as int;
} else {
gameState._difficulty = 0;
}
//main list — update existing objects in-place
final list = data['currentList'] as List;
final List<ListItemData> newList = [];
for (Map<String, dynamic> item in list) {
if (item["noteRow"] == true) {
final String itemId = item["id"] as String;
final NoteRow? existing = gameState._currentList
.whereType<NoteRow>()
.firstWhereOrNull((e) => e.id == itemId);
if (existing != null) {
existing.updateFromJson(item);
newList.add(existing);
} else {
newList.add(NoteRow.fromJson(item));
}
} else if (item["characterClass"] != null) {
final String itemId = item["id"] as String;
final Character? existing = gameState._currentList
.whereType<Character>()
.firstWhereOrNull((e) => e.id == itemId);
if (existing != null) {
existing.updateFromJson(item);
newList.add(existing);
} else {
newList.add(Character.fromJson(item));
}
} else if (item["type"] != null) {
final String itemId = item["id"] as String;
final Monster? existing = gameState._currentList
.whereType<Monster>()
.firstWhereOrNull((e) => e.id == itemId);
if (existing != null) {
existing.updateFromJson(item);
newList.add(existing);
} else {
newList.add(Monster.fromJson(item));
}
}
}
gameState._currentList = newList;
gameState._notifyCurrentList();
final unlockedClassesList = data['unlockedClasses'] as List;
gameState._unlockedClasses.clear();
for (String item in unlockedClassesList) {
gameState._unlockedClasses.add(item);
}
gameState._unlockedClassesVersion++;
//ability decks — update existing instances in-place
final decks = data['currentAbilityDecks'] as List;
final List<MonsterAbilityState> newAbilityDecks = [];
for (Map<String, dynamic> item in decks) {
final String deckName = item["name"] as String;
// Prototype has the full initialized card set; used only for nr lookups.
final proto = MonsterAbilityState(deckName);
final List<MonsterAbilityCardModel> newDrawList = [];
final List drawPile = item["drawPile"] as List;
for (final Map<String, dynamic> cardItem in drawPile) {
final int nr = cardItem["nr"];
for (final card in proto._drawPile.getList()) {
if (card.nr == nr) {
newDrawList.add(card);
break;
}
}
}
final List<MonsterAbilityCardModel> newDiscardList = [];
for (final Map<String, dynamic> cardItem in item["discardPile"] as List) {
final int nr = cardItem["nr"];
for (final card in proto._drawPile.getList()) {
if (card.nr == nr) {
newDiscardList.add(card);
break;
}
}
}
// Reuse existing state; fall back to the prototype for monsters
// newly added from a remote device.
final state =
gameState._currentAbilityDecks.firstWhereOrNull(
(s) => s.name == deckName,
) ??
proto;
if (item.containsKey("lastRoundDrawn")) {
state._lastRoundDrawn = item["lastRoundDrawn"] as int;
}
state._drawPile.clear();
state._discardPile.clear();
state._drawPile.setList(newDrawList);
state._discardPile.setList(newDiscardList);
state.drawPileVersion++;
newAbilityDecks.add(state);
}
gameState._currentAbilityDecks.clear();
gameState._currentAbilityDecks.addAll(newAbilityDecks);
_loadModifierDeck('modifierDeck', data, gameState);
_loadModifierDeck('modifierDeckAllies', data, gameState);
_loadLootDeck(data, gameState);
if (data["sanctuaryDeck"] != null) {
gameState._sanctuaryDeck = SanctuaryDeck.fromJson(
data["sanctuaryDeck"],
);
}
//this is not really a setting, but a scenario command?
if (data["showAllyDeck"] != null) {
gameState._showAllyDeck = data["showAllyDeck"];
}
//////elements
final Map elementData = data['elementState'];
for (final element in Elements.values) {
final raw = elementData[element.index.toString()] as int?;
final idx =
(raw != null && raw >= 0 && raw < ElementState.values.length)
? raw
: ElementState.inert.index;
gameState._elementState[element] = ElementState.values[idx];
}
} catch (e, stack) {
// Deserialization failure: log always (not just debug) so it surfaces
// in release builds and can be caught by crash-reporting tools.
_gameSaveStateLog.warning('GameSaveState.load error: $e\n$stack');
}
}
}
void saveToDisk(GameState gameState) {
if (_savedState == null) {
save(gameState);
}
getIt<SaveStore>().write(_savedState!);
}
void loadFromDisk(GameState gameState) {
//have to call after init or element state overridden
final String? data = getIt<SaveStore>().read();
if (data != null) {
_savedState = data;
load(gameState);
} else {
save(gameState);
}
}
void loadFromData(String data, GameState gameState) {
//have to call after init or element state overridden
_savedState = data;
load(gameState);
}
void _loadLootDeck(Map<String, dynamic> data, GameState gameState) {
gameState._lootDeck.updateFromJson(data["lootDeck"]);
}
void _loadModifierDeck(
String identifier,
Map<String, dynamic> data,
GameState gameState,
) {
final modifierDeckData = data[identifier] as Map<String, dynamic>;
if (identifier == 'modifierDeck') {
gameState._modifierDeck.updateFromJson(modifierDeckData);
} else {
gameState._modifierDeckAllies.updateFromJson(modifierDeckData);
}
}
}
|