summaryrefslogtreecommitdiff
path: root/server/lib/core/commands/change_stat_commands
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/commands/change_stat_commands
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/commands/change_stat_commands')
-rw-r--r--server/lib/core/commands/change_stat_commands/change_bless_command.dart46
-rw-r--r--server/lib/core/commands/change_stat_commands/change_curse_command.dart46
-rw-r--r--server/lib/core/commands/change_stat_commands/change_empower_command.dart47
-rw-r--r--server/lib/core/commands/change_stat_commands/change_enfeeble_command.dart50
-rw-r--r--server/lib/core/commands/change_stat_commands/change_health_command.dart54
-rw-r--r--server/lib/core/commands/change_stat_commands/change_max_health_command.dart44
-rw-r--r--server/lib/core/commands/change_stat_commands/change_stat_command.dart92
-rw-r--r--server/lib/core/commands/change_stat_commands/change_xp_command.dart28
8 files changed, 407 insertions, 0 deletions
diff --git a/server/lib/core/commands/change_stat_commands/change_bless_command.dart b/server/lib/core/commands/change_stat_commands/change_bless_command.dart
new file mode 100644
index 0000000..94f83ae
--- /dev/null
+++ b/server/lib/core/commands/change_stat_commands/change_bless_command.dart
@@ -0,0 +1,46 @@
+import '../../game_methods.dart';
+import '../../state/game_state.dart';
+import 'change_stat_command.dart';
+import '../command_l10n.dart';
+
+class ChangeBlessCommand extends ChangeStatCommand {
+ ModifierDeck? deck;
+ ChangeBlessCommand(
+ super.change,
+ super.figureId,
+ super.ownerId, {
+ required super.gameState,
+ });
+ ChangeBlessCommand.deck(this.deck, {required GameState gameState})
+ : super(0, '', '', gameState: gameState);
+
+ @override
+ void execute() {
+ if (deck == null) {
+ deck = gameState.modifierDeck;
+ for (final item in gameState.currentList) {
+ if (item.id == ownerId) {
+ if (item is Monster &&
+ item.isAlly &&
+ (gameState.allyDeckInOGGloom ||
+ !GameMethods.isOgGloomEdition())) {
+ deck = gameState.modifierDeckAllies;
+ }
+ if (item is Character) {
+ deck = item.characterState.modifierDeck;
+ }
+ }
+ }
+ }
+
+ deck?.addRemovableValue(stateAccess, "bless", change);
+ }
+
+ @override
+ String describe() {
+ if (change > 0) {
+ return commandL10n.cmdAddBless;
+ }
+ return commandL10n.cmdRemoveBless;
+ }
+}
diff --git a/server/lib/core/commands/change_stat_commands/change_curse_command.dart b/server/lib/core/commands/change_stat_commands/change_curse_command.dart
new file mode 100644
index 0000000..4505711
--- /dev/null
+++ b/server/lib/core/commands/change_stat_commands/change_curse_command.dart
@@ -0,0 +1,46 @@
+import '../../game_methods.dart';
+import '../../state/game_state.dart';
+import 'change_stat_command.dart';
+import '../command_l10n.dart';
+
+class ChangeCurseCommand extends ChangeStatCommand {
+ ChangeCurseCommand(
+ super.change,
+ super.figureId,
+ super.ownerId, {
+ required super.gameState,
+ });
+ ChangeCurseCommand.deck(this.deck, {required GameState gameState})
+ : super(0, '', '', gameState: gameState);
+
+ ModifierDeck? deck;
+
+ @override
+ void execute() {
+ if (deck == null) {
+ deck = gameState.modifierDeck;
+ for (final item in gameState.currentList) {
+ if (item.id == ownerId) {
+ if (item is Monster &&
+ item.isAlly &&
+ (gameState.allyDeckInOGGloom ||
+ !GameMethods.isOgGloomEdition())) {
+ deck = gameState.modifierDeckAllies;
+ }
+ if (item is Character) {
+ deck = item.characterState.modifierDeck;
+ }
+ }
+ }
+ }
+ deck?.addRemovableValue(stateAccess, "curse", change);
+ }
+
+ @override
+ String describe() {
+ if (change > 0) {
+ return commandL10n.cmdAddCurse;
+ }
+ return commandL10n.cmdRemoveCurse;
+ }
+}
diff --git a/server/lib/core/commands/change_stat_commands/change_empower_command.dart b/server/lib/core/commands/change_stat_commands/change_empower_command.dart
new file mode 100644
index 0000000..e4a8a52
--- /dev/null
+++ b/server/lib/core/commands/change_stat_commands/change_empower_command.dart
@@ -0,0 +1,47 @@
+import '../../game_methods.dart';
+import '../../state/game_state.dart';
+import 'change_stat_command.dart';
+import '../command_l10n.dart';
+
+class ChangeEmpowerCommand extends ChangeStatCommand {
+ final String gfx;
+ ModifierDeck? deck;
+ ChangeEmpowerCommand(
+ super.change,
+ this.gfx,
+ super.figureId,
+ super.ownerId, {
+ required super.gameState,
+ });
+ ChangeEmpowerCommand.deck(this.deck, this.gfx, {required GameState gameState})
+ : super(0, '', '', gameState: gameState);
+
+ @override
+ void execute() {
+ if (deck == null) {
+ deck = gameState.modifierDeck;
+ for (final item in gameState.currentList) {
+ if (item.id == ownerId) {
+ if (item is Monster &&
+ item.isAlly &&
+ (gameState.allyDeckInOGGloom ||
+ !GameMethods.isOgGloomEdition())) {
+ deck = gameState.modifierDeckAllies;
+ }
+ if (item is Character) {
+ deck = item.characterState.modifierDeck;
+ }
+ }
+ }
+ }
+ deck?.addRemovableValue(stateAccess, gfx, change);
+ }
+
+ @override
+ String describe() {
+ if (change > 0) {
+ return commandL10n.cmdAddEmpower;
+ }
+ return commandL10n.cmdRemoveEmpower;
+ }
+}
diff --git a/server/lib/core/commands/change_stat_commands/change_enfeeble_command.dart b/server/lib/core/commands/change_stat_commands/change_enfeeble_command.dart
new file mode 100644
index 0000000..ecb9f64
--- /dev/null
+++ b/server/lib/core/commands/change_stat_commands/change_enfeeble_command.dart
@@ -0,0 +1,50 @@
+import '../../game_methods.dart';
+import '../../state/game_state.dart';
+import 'change_stat_command.dart';
+import '../command_l10n.dart';
+
+class ChangeEnfeebleCommand extends ChangeStatCommand {
+ ModifierDeck? deck;
+ final String gfx;
+ ChangeEnfeebleCommand(
+ super.change,
+ this.gfx,
+ super.figureId,
+ super.ownerId, {
+ required super.gameState,
+ });
+ ChangeEnfeebleCommand.deck(
+ this.deck,
+ this.gfx, {
+ required GameState gameState,
+ }) : super(0, '', '', gameState: gameState);
+
+ @override
+ void execute() {
+ if (deck == null) {
+ deck = gameState.modifierDeck;
+ for (final item in gameState.currentList) {
+ if (item.id == ownerId) {
+ if (item is Monster &&
+ item.isAlly &&
+ (gameState.allyDeckInOGGloom ||
+ !GameMethods.isOgGloomEdition())) {
+ deck = gameState.modifierDeckAllies;
+ }
+ if (item is Character) {
+ deck = item.characterState.modifierDeck;
+ }
+ }
+ }
+ }
+ deck?.addRemovableValue(stateAccess, gfx, change);
+ }
+
+ @override
+ String describe() {
+ if (change > 0) {
+ return commandL10n.cmdAddEnfeeble;
+ }
+ return commandL10n.cmdRemoveEnfeeble;
+ }
+}
diff --git a/server/lib/core/commands/change_stat_commands/change_health_command.dart b/server/lib/core/commands/change_stat_commands/change_health_command.dart
new file mode 100644
index 0000000..8d6b6f4
--- /dev/null
+++ b/server/lib/core/commands/change_stat_commands/change_health_command.dart
@@ -0,0 +1,54 @@
+import '../../game_event.dart';
+import '../../game_methods.dart';
+import '../../state/game_state.dart';
+import 'change_stat_command.dart';
+import '../command_l10n.dart';
+
+class ChangeHealthCommand extends ChangeStatCommand {
+ ChangeHealthCommand(
+ super.change,
+ super.figureId,
+ super.ownerId, {
+ required super.gameState,
+ });
+
+ @override
+ void execute() {
+ FigureState? figure = GameMethods.getFigure(ownerId, figureId);
+ if (figure != null) {
+ final previousValue = figure.health;
+ if (previousValue + change < 0) {
+ //no negative values
+ figure.setHealth(stateAccess, 0);
+ } else {
+ figure.setHealth(stateAccess, figure.health + change);
+ }
+ final newValue = figure.health;
+ if (previousValue <= 0 && newValue > 0) {
+ //un death
+ gameState.notifyListeners();
+ }
+
+ if (newValue <= 0) {
+ handleDeath();
+ }
+ }
+ }
+
+ @override
+ GameEvent get event => HealthChangedEvent(figureId, ownerId ?? '', change);
+
+ @override
+ String describe() {
+ if (change > 0) {
+ //TODO: looks bad
+ return commandL10n.cmdIncreaseHealth(figureId, change);
+ }
+ FigureState? figure = GameMethods.getFigure(ownerId, figureId);
+ if (figure == null || figure.health <= 0) {
+ return commandL10n.cmdKill(ownerId ?? '');
+ }
+ //TODO: incorrect for character summons
+ return commandL10n.cmdDecreaseHealth(ownerId ?? '', -change);
+ }
+}
diff --git a/server/lib/core/commands/change_stat_commands/change_max_health_command.dart b/server/lib/core/commands/change_stat_commands/change_max_health_command.dart
new file mode 100644
index 0000000..70b26d9
--- /dev/null
+++ b/server/lib/core/commands/change_stat_commands/change_max_health_command.dart
@@ -0,0 +1,44 @@
+import '../../game_methods.dart';
+import '../../state/game_state.dart';
+import 'change_stat_command.dart';
+import '../command_l10n.dart';
+
+class ChangeMaxHealthCommand extends ChangeStatCommand {
+ ChangeMaxHealthCommand(
+ super.change,
+ super.figureId,
+ super.ownerId, {
+ required super.gameState,
+ });
+
+ @override
+ void execute() {
+ final FigureState? figure = GameMethods.getFigure(ownerId, figureId);
+ if (figure == null) return;
+
+ int newValue = figure.maxHealth + change;
+ if (newValue <= 0) {
+ //can't lower maxvalue less than 1
+ return;
+ }
+
+ figure.setMaxHealth(stateAccess, newValue);
+
+ //lower health if max health lowers
+ if (figure.maxHealth < figure.health) {
+ figure.setHealth(stateAccess, figure.maxHealth);
+ }
+ //if health same as max health, then let health follow?
+ if (figure.maxHealth - change == figure.health) {
+ figure.setHealth(stateAccess, figure.maxHealth);
+ }
+ }
+
+ @override
+ String describe() {
+ if (change > 0) {
+ return commandL10n.cmdIncreaseMaxHealth(ownerId ?? '');
+ }
+ return commandL10n.cmdDecreaseMaxHealth(ownerId ?? '');
+ }
+}
diff --git a/server/lib/core/commands/change_stat_commands/change_stat_command.dart b/server/lib/core/commands/change_stat_commands/change_stat_command.dart
new file mode 100644
index 0000000..3f94375
--- /dev/null
+++ b/server/lib/core/commands/change_stat_commands/change_stat_command.dart
@@ -0,0 +1,92 @@
+import '../../enums.dart';
+import '../../game_methods.dart';
+import '../../state/game_state.dart';
+import '../command_l10n.dart';
+
+abstract class ChangeStatCommand extends Command {
+ final String? ownerId;
+ int change;
+ final String figureId;
+ // Public so subclasses in other files can access it
+ final GameState gameState;
+
+ ChangeStatCommand(
+ this.change,
+ this.figureId,
+ this.ownerId, {
+ required this.gameState,
+ });
+
+ void setChange(int change) {
+ this.change = change;
+ }
+
+ void handleDeath() {
+ for (final item in gameState.currentList) {
+ if (item is Monster) {
+ _handleMonsterDeath(item);
+ } else if (item is Character) {
+ _handleCharacterDeath(item);
+ }
+ }
+ }
+
+ void _handleMonsterDeath(Monster monster) {
+ final instances = monster.monsterInstances;
+ final deadIndex = instances.indexWhere((i) => i.health == 0);
+ if (deadIndex == -1) return;
+
+ final int deadStandeeNr = instances[deadIndex].standeeNr;
+ final newList = List<MonsterInstance>.from(instances)..removeAt(deadIndex);
+ monster.setMonsterInstances(stateAccess, newList);
+ // A note tied to this specific defeated standee is deleted with it.
+ RoundMethods.removeNoteRowsForStandee(
+ stateAccess,
+ monster.id,
+ deadStandeeNr,
+ gameState: gameState,
+ );
+
+ if (monster.monsterInstances.isEmpty) {
+ monster.setActive(stateAccess, false);
+ if (gameState.roundState == RoundState.chooseInitiative) {
+ RoundMethods.sortCharactersFirst(stateAccess);
+ }
+ _notifyUpdateList();
+ }
+ }
+
+ void _handleCharacterDeath(Character character) {
+ if (character.characterState.health <= 0) {
+ gameState.notifyListeners();
+ }
+ _handleSummonDeath(character);
+ }
+
+ void _handleSummonDeath(Character character) {
+ for (final instance in character.characterState.summonList) {
+ if (instance.health == 0 &&
+ !GameMethods.summonDoesNotDie(character.id, instance.name)) {
+ character.characterState.removeSummon(stateAccess, instance);
+ if (character.characterState.summonList.isEmpty) {
+ _notifyUpdateList();
+ } else {
+ gameState.notifyListeners();
+ }
+ break;
+ }
+ }
+ }
+
+ /// The reference app delayed this list update by 600 ms while a turn was in
+ /// play so the removal animation could finish; server-side the update is
+ /// immediate.
+ void _notifyUpdateList() {
+ gameState.notifyListeners();
+ }
+
+ @override
+ String describe() {
+ return commandL10n.cmdChangeStat;
+ }
+}
diff --git a/server/lib/core/commands/change_stat_commands/change_xp_command.dart b/server/lib/core/commands/change_stat_commands/change_xp_command.dart
new file mode 100644
index 0000000..423b16d
--- /dev/null
+++ b/server/lib/core/commands/change_stat_commands/change_xp_command.dart
@@ -0,0 +1,28 @@
+import '../../game_methods.dart';
+import '../../state/game_state.dart';
+import 'change_stat_command.dart';
+import '../command_l10n.dart';
+
+class ChangeXPCommand extends ChangeStatCommand {
+ ChangeXPCommand(
+ super.change,
+ super.figureId,
+ super.ownerId, {
+ required super.gameState,
+ });
+
+ @override
+ void execute() {
+ final figure = GameMethods.getFigure(ownerId, figureId);
+ if (figure is! CharacterState) return;
+ figure.setXp(stateAccess, figure.xp + change);
+ }
+
+ @override
+ String describe() {
+ if (change > 0) {
+ return commandL10n.cmdIncreaseXp(figureId, change);
+ }
+ return commandL10n.cmdDecreaseXp(figureId, change.abs());
+ }
+}