import 'dart:convert'; import 'package:x_haven_server/core/commands/load_character_save_command.dart'; import 'package:x_haven_server/core/commands/load_save_command.dart'; import 'package:x_haven_server/core/service_locator.dart'; import 'enums.dart'; import 'settings_store.dart'; import 'state/game_state.dart'; /// Server-side settings (Phase 6 split, PLAN.md section 4). /// /// Only the **game-relevant** settings live here — everything the engine /// reads: initiative gating, standee tracking/spawning, condition expiry, /// the loot deck, the style, the reminder flags, AMD visibility, the /// OG-Gloom hazard-terrain calculation, and [locale] (the command /// descriptions are localized server-side via `command_l10n`). /// /// The reference app's pure-UI settings (list/bar scaling, fullscreen, /// dark mode, the soft numpad, no-calculations display, the health wheel, /// sections-in-main-view, character-AMD display, custom-content display, /// scenario-name display, the power-mode display behaviour) are per-browser /// preferences now and live in the web client's `localStorage` /// (`web/src/lib/settings.ts`) — they no longer cross the wire. class Settings { bool noInit = true; bool noStandees = false; bool randomStandees = false; bool expireConditions = true; bool autoAddStandees = true; bool autoAddSpawns = true; bool hideLootDeck = false; Style style = Style.original; bool showBattleGoalReminder = true; bool showReminders = true; bool showAmdDeck = true; bool fhHazTerrainCalcInOGGloom = true; /// The locale the command descriptions are generated in (the reference /// app's `settings.locale`, ported with `command_l10n`). String locale = 'en'; /// Multi-save game states: save name → serialized [GameState]. final Map saves = {}; /// Character saves: `"$saveName\n${character.id}"` → `Character.toSave()`. /// The `\n`-joined key is the reference app's save-id format (the save /// name is human-visible, the character id is not). final Map characterSaves = {}; // --- save / load ---------------------------------------------------------- /// Loads the named save into the game state (an undoable command). void loadSave(String saveName, {GameState? gameState}) { final String? save = saves[saveName]; if (save != null) { final gs = gameState ?? getIt(); gs.action(LoadSaveCommand(saveName, save, gameState: gs)); } } /// Stores a snapshot of the current game state under [saveName]. void saveState(String saveName, {GameState? gameState}) { saves[saveName] = (gameState ?? getIt()).toString(); saveToDisk(); } void deleteSave(String saveName) { saves.remove(saveName); saveToDisk(); } /// Loads the named character save (an undoable command). void loadCharacterSave(String saveId, {GameState? gameState}) { final String? save = characterSaves[saveId]; if (save != null) { final gs = gameState ?? getIt(); final name = saveId.split('\n').first; gs.action(LoadCharacterSaveCommand(name, save, gameState: gs)); } } void saveCharacterState(String saveName, Character character) { characterSaves['$saveName\n${character.id}'] = character.toSave(); saveToDisk(); } void deleteCharacterSave(String saveId) { characterSaves.remove(saveId); saveToDisk(); } // --- persistence ----------------------------------------------------------- /// Writes the settings to disk (the reference app's `saveToDisk` — called /// after every change, so a restart restores the full settings + save list). void saveToDisk() { getIt().write(toString()); } /// Restores the settings from disk (no-op when nothing was stored). void loadFromDisk() { final stored = getIt().read(); if (stored == null) return; try { loadFromData(stored); } catch (_) { // A corrupt settings file must not take the server down — start from // the defaults (the reference app fails the same lookup silently). } } // --- wire payload ------------------------------------------------------------ /// The payload the server sends to every client (the `settings` message /// and the `welcome` field): the game-relevant fields plus the save *names* /// and character-save entries. The save **content** never crosses the wire /// — it can be large, and only the server needs it. Map forClient() { return { 'noInit': noInit, 'noStandees': noStandees, 'randomStandees': randomStandees, 'expireConditions': expireConditions, 'autoAddStandees': autoAddStandees, 'autoAddSpawns': autoAddSpawns, 'hideLootDeck': hideLootDeck, 'style': style.index, 'showBattleGoalReminder': showBattleGoalReminder, 'showReminders': showReminders, 'showAmdDeck': showAmdDeck, 'fhHazTerrainCalcInOGGloom': fhHazTerrainCalcInOGGloom, 'locale': locale, 'saves': saves.keys.toList(), 'characterSaves': [ for (final entry in characterSaves.entries) { 'name': entry.key.split('\n').first, 'characterId': entry.key.split('\n').last, }, ], }; } // --- disk format -------------------------------------------------------------- /// Applies a settings JSON previously produced by [toString]. /// /// Ported from the reference app's `loadFromDisk` field parsing, minus the /// client-only fields (Phase 6 split). Unknown fields are ignored, so a /// file written by the pre-split server still loads. void loadFromData(String state) { final data = jsonDecode(state); if (data is! Map) return; if (data["noInit"] != null) { noInit = data["noInit"] as bool; } if (data["noStandees"] != null) { noStandees = data["noStandees"] as bool; } if (data["randomStandees"] != null) { randomStandees = data["randomStandees"] as bool; } if (data["expireConditions"] != null) { expireConditions = data["expireConditions"] as bool; } if (data["autoAddStandees"] != null) { autoAddStandees = data["autoAddStandees"] as bool; } if (data["autoAddSpawns"] != null) { autoAddSpawns = data["autoAddSpawns"] as bool; } if (data["hideLootDeck"] != null) { hideLootDeck = data["hideLootDeck"] as bool; } if (data["style"] != null) { style = Style.values[data["style"] as int]; } if (data["showBattleGoalReminder"] != null) { showBattleGoalReminder = data["showBattleGoalReminder"] as bool; } if (data["showReminders"] != null) { showReminders = data["showReminders"] as bool; } if (data["showAmdDeck"] != null) { showAmdDeck = data["showAmdDeck"] as bool; } if (data["fhHazTerrainCalcInOGGloom"] != null) { fhHazTerrainCalcInOGGloom = data["fhHazTerrainCalcInOGGloom"] as bool; } if (data["locale"] != null) { locale = data["locale"] as String; } if (data["saves"] != null) { final map = data["saves"] as Map; for (final key in map.keys) { saves[key] = map[key] as String; } } if (data["characterSaves"] != null) { final map = data["characterSaves"] as Map; for (final key in map.keys) { characterSaves[key] = map[key] as String; } } } @override String toString() { return '{' '"noInit": $noInit, ' '"noStandees": $noStandees, ' '"randomStandees": $randomStandees, ' '"expireConditions": $expireConditions, ' '"autoAddStandees": $autoAddStandees, ' '"autoAddSpawns": $autoAddSpawns, ' '"hideLootDeck": $hideLootDeck, ' '"style": ${style.index}, ' '"showBattleGoalReminder": $showBattleGoalReminder, ' '"showReminders": $showReminders, ' '"showAmdDeck": $showAmdDeck, ' '"fhHazTerrainCalcInOGGloom": $fhHazTerrainCalcInOGGloom, ' '"locale": "$locale", ' '"saves": ${jsonEncode(saves)}, ' '"characterSaves": ${jsonEncode(characterSaves)} ' '}'; } }