From 808294adad3f378ad8d66b698acf8df7f49c2302 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 12 Nov 2026 00:28:50 +0100 Subject: 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. --- server/lib/core/methods/monster_methods.dart | 403 +++++++++++++++++++++++++++ 1 file changed, 403 insertions(+) create mode 100644 server/lib/core/methods/monster_methods.dart (limited to 'server/lib/core/methods/monster_methods.dart') diff --git a/server/lib/core/methods/monster_methods.dart b/server/lib/core/methods/monster_methods.dart new file mode 100644 index 0000000..c0743b3 --- /dev/null +++ b/server/lib/core/methods/monster_methods.dart @@ -0,0 +1,403 @@ +part of '../state/game_state.dart'; + +// ignore_for_file: library_private_types_in_public_api + +class MonsterMethods { + static const int _kMinLevel = 0; + static const int _kMaxLevel = 7; + static const int _kMinCharacters = 2; + static const int _kMaxCharacters = 4; + static const int _kCharIndexOffset = 2; + static const int _kTrailingCommaLength = 2; + + static void showAllyDeck(_StateModifier _, {GameState? gameState}) { + final gs = gameState ?? getIt(); + gs._showAllyDeck = true; + } + + static void hideAllyDeck(_StateModifier _, {GameState? gameState}) { + final gs = gameState ?? getIt(); + gs._showAllyDeck = false; + } + + static Monster? createMonster( + _StateModifier _, + String name, + int? level, + bool isAlly, { + GameState? gameState, + GameData? gameData, + }) { + final gd = gameData ?? getIt(); + Map monsters = {}; + final modelData = gd.modelData; + for (String key in modelData.keys) { + monsters.addAll(modelData[key]?.monsters ?? {}); + } + level ??= (gameState ?? getIt()).level; + return Monster(name, level, isAlly); + } + + static void removeMonsters( + _StateModifier _, + List items, { + GameState? gameState, + }) { + List deckIds = []; + List newList = []; + final gs = gameState ?? getIt(); + for (final item in gs.currentList) { + if (item is Monster) { + bool remove = false; + for (final name in items) { + if (item.id == name.id) { + remove = true; + deckIds.add(item.type.deck); + } + } + if (!remove) { + newList.add(item); + } + } else { + newList.add(item); + } + } + + gs._currentList = newList; + + for (final deck in deckIds) { + bool removeDeck = true; + for (final item in gs.currentList) { + if (item is Monster) { + if (item.type.deck == deck) { + removeDeck = false; + } + } + } + + if (removeDeck) { + for (final item in gs.currentAbilityDecks) { + if (item.name == deck) { + gs._currentAbilityDecks.remove(item); + break; + } + } + } + } + + gs._notifyCurrentList(); + } + + static void executeAddStandee( + _StateModifier s, + final int nr, + final SummonData? summon, + final MonsterType type, + final String ownerId, + final bool addAsSummon, { + GameState? gameState, + }) { + final gs = gameState ?? getIt(); + MonsterInstance instance; + Monster? monster; + if (summon == null) { + for (final item in gs.currentList) { + if (item.id == ownerId && item is Monster) { + monster = item; + monster._isActive = true; + break; + } + } + if (monster == null) return; + instance = MonsterInstance(nr, type, addAsSummon, monster); + } else { + instance = MonsterInstance.summon( + summon.standeeNr, + type, + summon.name, + summon.health, + summon.move, + summon.attack, + summon.range, + summon.gfx, + gs.round, + ); + } + + List monsterList = []; + Character? character; + //find list + if (monster != null) { + monsterList = monster._monsterInstances; + } else { + for (final item in gs.currentList) { + if (item.id == ownerId) { + character = item as Character; + monsterList = character.characterState._summonList; + break; + } + } + } + + //make sure summons can not have same gfx and nr: + if (instance.standeeNr != 0 && summon != null) { + bool ok = false; + while (!ok) { + ok = true; + for (final item in monsterList) { + if (item.standeeNr == instance.standeeNr) { + if (item.gfx == instance.gfx) { + //can not have same gfx and nr + instance = MonsterInstance.summon( + instance.standeeNr + 1, + type, + summon.name, + summon.health, + summon.move, + summon.attack, + summon.range, + summon.gfx, + gs.round, + ); + ok = false; + } + } + } + } + } + + monsterList.add(instance); + if (monster != null) { + RoundMethods.sortMonsterInstances(s, monsterList); + monster._notifyMonsterInstances(); + } else { + character?.characterState._notifySummonList(); + } + if (monsterList.length == 1 && monster != null) { + //first added + final roundState = gs.roundState; + if (roundState == RoundState.chooseInitiative) { + RoundMethods.sortCharactersFirst(s); + } else if (roundState == RoundState.playTurns) { + DeckMethods.drawAbilityCardFromInactiveDeck(s); + RoundMethods.sortItemToPlace( + s, + monster.id, + GameMethods.getInitiative(monster), + ); //need to only sort this one item to place + } + } + } + + static void addStandee( + int? nr, + Monster data, + MonsterType type, + bool addAsSummon, { + GameState? gameState, + }) { + final gs = gameState ?? getIt(); + if (nr != null) { + gs.action( + AddStandeeCommand(nr, null, data.id, type, addAsSummon, gameState: gs), + ); + } else { + //add first un added nr + for (int i = 1; i <= data.type.count; i++) { + bool added = false; + for (final item in data.monsterInstances) { + if (item.standeeNr == i) { + added = true; + break; + } + } + if (!added) { + gs.action( + AddStandeeCommand( + i, + null, + data.id, + type, + addAsSummon, + gameState: gs, + ), + ); + return; + } + } + } + } + + static void addMonster( + _StateModifier s, + String monster, + List specialRules, { + GameState? gameState, + }) { + int levelAdjust = 0; + Set alliedMonsters = {}; + for (final rule in specialRules) { + if (rule.name == monster || rule.name == "Enemies") { + if (rule.type == "LevelAdjust") { + levelAdjust = rule.level; + } + } + if (rule.type == "Allies") { + for (String item in rule.list.cast()) { + alliedMonsters.add(item); + } + } + } + + final gs = gameState ?? getIt(); + bool add = true; + for (final item in gs.currentList) { + //don't add duplicates + if (item.id == monster) { + add = false; + break; + } + } + if (add) { + bool isAlly = false; + if (alliedMonsters.contains(monster)) { + isAlly = true; + } + + final munster = createMonster( + s, + monster, + (gs.level + levelAdjust).clamp(_kMinLevel, _kMaxLevel), + isAlly, + ); + if (munster != null) { + gs._currentList.add(munster); + } + } + } + + static String autoAddStandees( + _StateModifier stateModifier, + List roomMonsterData, + String initMessage, { + GameState? gameState, + Settings? settings, + }) { + final gs = gameState ?? getIt(); + //handle room data + int characterIndex = + GameMethods.getCurrentCharacterAmount().clamp( + _kMinCharacters, + _kMaxCharacters, + ) - + _kCharIndexOffset; + for (int i = 0; i < roomMonsterData.length; i++) { + final roomMonsters = roomMonsterData[i]; + addMonster(stateModifier, roomMonsters.name, gs._scenarioSpecialRules); + } + bool addSorted = gs.currentCampaign == "Buttons and Bugs"; + final s = settings ?? getIt(); + if (!s.noStandees && s.autoAddStandees) { + if (s.randomStandees || addSorted) { + if (initMessage.isNotEmpty && !addSorted) { + initMessage += "\n"; + } + for (int i = 0; i < roomMonsterData.length; i++) { + List normals = []; + List elites = []; + final roomMonsters = roomMonsterData[i]; + Monster data = + gs.currentList.firstWhereOrNull( + (element) => element.id == roomMonsters.name, + ) + as Monster; + + int eliteAmount = roomMonsters.elite[characterIndex]; + int normalAmount = roomMonsters.normal[characterIndex]; + + bool isBoss = false; + if (data.type.levels.first.boss != null) { + isBoss = true; + } + + for (int i = 0; i < eliteAmount; i++) { + int randomNr = GameMethods.getRandomStandee(data); + if (randomNr != 0) { + elites.add(randomNr); + executeAddStandee( + stateModifier, + randomNr, + null, + MonsterType.elite, + data.id, + false, + ); + } + } + + for (int i = 0; i < normalAmount; i++) { + int randomNr = GameMethods.getRandomStandee(data); + if (addSorted) { + randomNr = GameMethods.getNextAvailableBnBStandee(data); + } + if (randomNr != 0) { + normals.add(randomNr); + executeAddStandee( + stateModifier, + randomNr, + null, + isBoss ? MonsterType.boss : MonsterType.normal, + data.id, + false, + ); + } + } + + if (!addSorted && (elites.isNotEmpty || normals.isNotEmpty)) { + elites.sort(); + normals.sort(); + if (i != 0) { + initMessage += "\n"; + } + initMessage += "${data.type.display} added - "; + + if (elites.isNotEmpty) { + initMessage += "Elite: "; + for (int i = 0; i < elites.length; i++) { + initMessage += "${elites[i]}, "; + if (i == elites.length - 1) { + initMessage = initMessage.substring( + 0, + initMessage.length - _kTrailingCommaLength, + ); + } + } + } + if (normals.isNotEmpty) { + if (!isBoss) { + if (elites.isNotEmpty) { + initMessage += ", "; + } + initMessage += "Normal: "; + } + for (int i = 0; i < normals.length; i++) { + initMessage += "${normals[i]}, "; + if (i == normals.length - 1) { + initMessage = initMessage.substring( + 0, + initMessage.length - _kTrailingCommaLength, + ); + } + } + } + } + } + } else { + if (roomMonsterData.isNotEmpty) { + gs.pendingAutoAddDialog = roomMonsterData; + } + } + } + return initMessage; + } +} -- cgit v1.3.1