summaryrefslogtreecommitdiff
path: root/server/lib/core/state/character_state.dart
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-12 00:28:50 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-12 00:28:50 +0100
commit808294adad3f378ad8d66b698acf8df7f49c2302 (patch)
treecfd11a1395a8ad482311969f86abbbad97e7e0b4 /server/lib/core/state/character_state.dart
parentf6955e34489c79f6d5b764cb6a39b1387366b433 (diff)
Phase 1: headless game engine on the server (M1)
Complete mechanical port of the reference app's game engine to server/lib/core/ so later phases are UI-only: - models/ (7), state/ (GameState + 18 part files), methods/ (7), game_methods, 79 command files, enums, card_stack, stat_calculator, action_handler (index, 250-deep history, save-per-action, undo/redo), disk-based game_data, l10n, translation_service, service_locator - ValueNotifier/ChangeNotifier removed in favor of plain fields with a minimal GameState listener hook for the Phase 2 broadcast layer; shared_preferences replaced by the SaveStore abstraction (InMemory for tests, File-backed for bin/main.dart) - command registry: 82 entries mapping wire names to constructors with arg validation; live-object args (loot/ability cards, figure lists) travel as id/name/nr and resolve against live state, enums by name, injected singletons never serialize - line_builder ported to Phase 1 scope: stat_applier + minimal LineBuilder (isElement, tokens); widget rendering deferred to Phase 4 - ported test suite: 81 test files, 562 tests green (command tests 1:1 with the reference, resource tests, registry smoke tests) - new headless integration test drives a full round: scenario setup, characters + standee, secret-initiative gate, draw, every figure's turn (incl. undo/redo), next round, round 2, round 3 - PORTING.md: the conventions contract used for the port - bin/main.dart boots the engine (file save store, assets/data) before serving; pubspec gains built_collection, collection, get_it, intl - dart format drift on pre-existing net/test files from the current formatter (whitespace only) dart analyze warning-free; dart test 562/562.
Diffstat (limited to 'server/lib/core/state/character_state.dart')
-rw-r--r--server/lib/core/state/character_state.dart251
1 files changed, 251 insertions, 0 deletions
diff --git a/server/lib/core/state/character_state.dart b/server/lib/core/state/character_state.dart
new file mode 100644
index 0000000..848bc1c
--- /dev/null
+++ b/server/lib/core/state/character_state.dart
@@ -0,0 +1,251 @@
+part of 'game_state.dart';
+
+// ignore_for_file: library_private_types_in_public_api
+
+class CharacterState extends FigureState {
+ static const int _kMaxPerks = 18;
+
+ String _display = "";
+ final List<bool> _perkList = List.filled(_kMaxPerks, false);
+ int _perkListVersion = 0;
+ bool _useFHPerks = false;
+
+ int _initiative = 0;
+ int _xp = 0;
+ final List<MonsterInstance> _summonList = [];
+ BuiltList<MonsterInstance> _summonListNotifier = BuiltList.of([]);
+ final ModifierDeck _modifierDeck;
+
+ String get display => _display;
+ int get initiative => _initiative;
+ int get xp => _xp;
+ BuiltList<MonsterInstance> get summonList => BuiltList.of(_summonList);
+ BuiltList<MonsterInstance> get summonListNotifier => _summonListNotifier;
+ ModifierDeck get modifierDeck => _modifierDeck;
+
+ void _notifySummonList() {
+ // UI-only in the reference app
+ }
+
+ /// Public guarded notify — allows commands to trigger the summon list
+ /// update at the correct time (e.g. after a 600 ms death animation).
+ void notifySummonList(_StateModifier _) {
+ _notifySummonList();
+ }
+
+ BuiltList<bool> get perkList => BuiltList.of(_perkList);
+ int get perkListVersion => _perkListVersion;
+ bool get useFHPerks => _useFHPerks;
+
+ CharacterState(final String id) : _modifierDeck = ModifierDeck(id);
+
+ static ModifierDeck _deckFromJson(String id, Map<String, dynamic> json) {
+ if (json.containsKey("modifierDeck")) {
+ return ModifierDeck.fromJson(id, json["modifierDeck"]);
+ }
+ return ModifierDeck(id);
+ }
+
+ CharacterState.fromSave(final String id, Map<String, dynamic> json)
+ : _modifierDeck = ModifierDeck(id) {
+ _level = json["level"];
+ _display = json['display'];
+ if (json.containsKey("useFHPerks")) {
+ _useFHPerks = json["useFHPerks"];
+ }
+ if (json.containsKey("perkList")) {
+ final perks = json["perkList"];
+ int i = 0;
+ for (bool item in perks) {
+ if (i >= _perkList.length) break;
+ _perkList[i] = item;
+ i++;
+ }
+ }
+ }
+
+ CharacterState.fromJson(final String id, Map<String, dynamic> json)
+ : _modifierDeck = _deckFromJson(id, json) {
+ _initiative = json['initiative'];
+ _xp = json['xp'];
+ _chill = json['chill'];
+ _health = json["health"];
+ _level = json["level"];
+ _maxHealth = json["maxHealth"];
+ _display = json['display'];
+
+ final summons = json["summonList"];
+ for (final item in summons) {
+ _summonList.add(MonsterInstance.fromJson(item));
+ }
+ _notifySummonList();
+
+ //todo: automate check json.containsKey for everything: the nr of update bugs...
+ if (json.containsKey("perkList")) {
+ final perks = json["perkList"];
+ int i = 0;
+ for (bool item in perks) {
+ if (i >= _perkList.length) break;
+ _perkList[i] = item;
+ i++;
+ }
+ }
+ if (json.containsKey("useFHPerks")) {
+ _useFHPerks = json["useFHPerks"];
+ }
+
+ final condis = json["conditions"];
+ for (int item in condis) {
+ if (item >= 0 && item < Condition.values.length) {
+ _conditions.add(Condition.values[item]);
+ }
+ }
+
+ if (json.containsKey("conditionsAddedThisTurn")) {
+ final condis2 = json["conditionsAddedThisTurn"];
+ for (int item in condis2) {
+ if (item >= 0 && item < Condition.values.length) {
+ _conditionsAddedThisTurn.add(Condition.values[item]);
+ }
+ }
+ }
+ if (json.containsKey("conditionsAddedPreviousTurn")) {
+ final condis3 = json["conditionsAddedPreviousTurn"];
+ for (int item in condis3) {
+ if (item >= 0 && item < Condition.values.length) {
+ _conditionsAddedPreviousTurn.add(Condition.values[item]);
+ }
+ }
+ }
+ }
+
+ /// Updates all fields in-place from [json]. Preserves object identity.
+ void updateFromJson(String _, Map<String, dynamic> json) {
+ _initiative = json['initiative'] as int;
+ _xp = json['xp'] as int;
+ _chill = json['chill'] as int;
+ _health = json["health"] as int;
+ _level = json["level"] as int;
+ _maxHealth = json["maxHealth"] as int;
+ _display = json['display'] as String;
+ _plague = 0; // not serialised — reset to match fromJson behaviour
+
+ _summonList.clear();
+ for (final item in json["summonList"]) {
+ _summonList.add(MonsterInstance.fromJson(item as Map<String, dynamic>));
+ }
+ _notifySummonList();
+
+ for (int i = 0; i < _perkList.length; i++) {
+ _perkList[i] = false;
+ }
+ if (json.containsKey("perkList")) {
+ int i = 0;
+ for (bool item in json["perkList"]) {
+ if (i >= _perkList.length) break;
+ _perkList[i] = item;
+ i++;
+ }
+ }
+ _perkListVersion++;
+ _useFHPerks = json.containsKey("useFHPerks")
+ ? json["useFHPerks"] as bool
+ : false;
+
+ // Assign a new list instance.
+ final newConditions = <Condition>[];
+ for (int item in json["conditions"]) {
+ if (item >= 0 && item < Condition.values.length) {
+ newConditions.add(Condition.values[item]);
+ }
+ }
+ _conditions = newConditions;
+
+ if (json.containsKey("modifierDeck")) {
+ _modifierDeck.updateFromJson(
+ json["modifierDeck"] as Map<String, dynamic>,
+ );
+ } else {
+ _modifierDeck.resetToDefault();
+ }
+
+ _conditionsAddedThisTurn.clear();
+ if (json.containsKey("conditionsAddedThisTurn")) {
+ for (int item in json["conditionsAddedThisTurn"]) {
+ if (item >= 0 && item < Condition.values.length) {
+ _conditionsAddedThisTurn.add(Condition.values[item]);
+ }
+ }
+ }
+ _conditionsAddedPreviousTurn.clear();
+ if (json.containsKey("conditionsAddedPreviousTurn")) {
+ for (int item in json["conditionsAddedPreviousTurn"]) {
+ if (item >= 0 && item < Condition.values.length) {
+ _conditionsAddedPreviousTurn.add(Condition.values[item]);
+ }
+ }
+ }
+ }
+
+ void flipPerk(_StateModifier _, int index) {
+ _perkList[index] = !_perkList[index];
+ _perkListVersion++;
+ }
+
+ void setDisplay(_StateModifier _, String value) {
+ _display = value;
+ }
+
+ void setInitiative(_StateModifier _, int value) {
+ _initiative = value;
+ }
+
+ void setXp(_StateModifier _, int value) {
+ _xp = max(value, 0);
+ }
+
+ void addSummon(_StateModifier _, MonsterInstance summon) {
+ _summonList.add(summon);
+ _notifySummonList();
+ }
+
+ /// Removes [summon] from the list.
+ /// The caller is responsible for calling [notifySummonList] at the
+ /// appropriate time (e.g. after a 600 ms death animation).
+ void removeSummon(_StateModifier _, MonsterInstance summon) {
+ _summonList.remove(summon);
+ }
+
+ String toSave() {
+ return '{'
+ '"level": ${level}, '
+ '"useFHPerks": ${jsonEncode(useFHPerks)}, '
+ '"display": ${jsonEncode(display)}, '
+ '"perkList": ${_perkList.toString()} '
+ '}';
+ }
+
+ Map<String, dynamic> toJson() => {
+ 'initiative': initiative,
+ 'health': health,
+ 'maxHealth': maxHealth,
+ 'level': level,
+ 'xp': xp,
+ 'chill': chill,
+ 'display': display,
+ 'modifierDeck': _modifierDeck.toJson(),
+ 'summonList': _summonList.map((s) => s.toJson()).toList(),
+ 'useFHPerks': useFHPerks,
+ 'perkList': List<bool>.of(_perkList),
+ 'conditions': _conditions.map((c) => c.index).toList(),
+ 'conditionsAddedThisTurn': _conditionsAddedThisTurn
+ .map((c) => c.index)
+ .toList(),
+ 'conditionsAddedPreviousTurn': _conditionsAddedPreviousTurn
+ .map((c) => c.index)
+ .toList(),
+ };
+
+ @override
+ String toString() => json.encode(toJson());
+}