summaryrefslogtreecommitdiff
path: root/server/lib/core/methods/character_methods.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/methods/character_methods.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/methods/character_methods.dart')
-rw-r--r--server/lib/core/methods/character_methods.dart393
1 files changed, 393 insertions, 0 deletions
diff --git a/server/lib/core/methods/character_methods.dart b/server/lib/core/methods/character_methods.dart
new file mode 100644
index 0000000..9c2705a
--- /dev/null
+++ b/server/lib/core/methods/character_methods.dart
@@ -0,0 +1,393 @@
+part of '../state/game_state.dart';
+
+// ignore_for_file: library_private_types_in_public_api
+
+class CharacterMethods {
+ static const int _kHailPerkIndex = 17;
+ static const int _kPainConduitPerkIndex = 16;
+ static const int _kPainConduitHealthBonus = 5;
+ static const int _kObjectiveInitiative = 99;
+ // Special summon base stats
+ static const int _kBeastHealth = 8;
+ static const int _kBeastMultiplier = 2;
+ static const int _kDomeHealth = 3;
+ static const int _kGlacierHealth = 7;
+ static const int _kJesterHealth = 5;
+ static const int _kSpecialSummonMove = 3;
+ static const int _kSpecialSummonAttack = 2;
+ static const int _kSpecialSummonRange = 0;
+ static const int _kSpecialSummonStandeeNr = 0;
+ static const int _kSpecialSummonInitiative = -1;
+
+ static void addPerk(
+ _StateModifier s,
+ Character character,
+ int index, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ final deck = character.characterState.modifierDeck;
+ final perksFH = character.characterClass.perksFH;
+ final useFHPerks =
+ character.characterState.useFHPerks && perksFH.isNotEmpty;
+ final perks = useFHPerks ? perksFH : character.characterClass.perks;
+ final perk = perks[index];
+ for (final item in perk.remove) {
+ final amount = deck.drawPileSize;
+ deck.removeCard(s, item);
+ if (deck.drawPileSize == amount) {
+ //must be a perk card
+
+ //find missing card from perk list
+
+ for (int i = 0; i < perks.length; i++) {
+ //check if other perk added the card previously
+ final adds = perks[i].add;
+ if (adds.contains(item)) {
+ //remove that perk card
+ String second = "";
+ if (adds.first != adds.last && item == adds.last) {
+ second = "-2"; //in case perk adds 2 different cards
+ }
+ deck.removeCard(s, "P$i$second");
+ if (deck.drawPileSize != amount) {
+ //found and removed
+ break;
+ }
+ }
+ }
+ }
+ }
+ for (final item in perk.add) {
+ CardType type = CardType.add;
+ if (item.endsWith("/ns2")) {
+ //nightshroud hack
+ type = CardType.multiply;
+ }
+ final id = GameMethods.perkGfxIdToCardId(item, perk, index);
+ deck.addCard(s, id, type);
+ }
+
+ final className = character.characterClass.name;
+ if (index == _kHailPerkIndex && className == "Hail") {
+ gs.modifierDeck.addHailSpecial(s);
+ }
+ if (index == _kPainConduitPerkIndex && className == "Pain Conduit") {
+ final level = character.characterState.level;
+ final healthByLevel = character.characterClass.healthByLevel;
+ if (level >= 1 && level <= healthByLevel.length) {
+ character.characterState._health =
+ healthByLevel[level - 1] + _kPainConduitHealthBonus;
+ character.characterState.setMaxHealth(
+ s,
+ character.characterState._health,
+ );
+ }
+ }
+ }
+
+ static void removePerk(
+ _StateModifier s,
+ Character character,
+ int index, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ final deck = character.characterState.modifierDeck;
+ final perksFH = character.characterClass.perksFH;
+ final useFHPerks =
+ character.characterState.useFHPerks && perksFH.isNotEmpty;
+ final perks = useFHPerks ? perksFH : character.characterClass.perks;
+ final perk = perks[index];
+ for (final item in perk.remove) {
+ if (item.startsWith("perks/")) {
+ //find id of perk: bull: could be several...
+ for (int i = 0; i < perks.length; i++) {
+ if (character.characterState.perkList[i] &&
+ perks[i].add.contains(item)) {
+ final id = GameMethods.perkGfxIdToCardId(item, perks[i], i);
+ deck.addCard(s, id, CardType.add);
+ break;
+ }
+ }
+ } else {
+ deck.addCard(s, item, CardType.add);
+ }
+ }
+ for (final item in perk.add) {
+ final id = GameMethods.perkGfxIdToCardId(item, perk, index);
+ deck.removeCard(s, id);
+ }
+
+ final className = character.characterClass.name;
+ if (index == _kHailPerkIndex && className == "Hail") {
+ gs.modifierDeck.removeHailSpecial(s);
+ }
+ if (index == _kPainConduitPerkIndex && className == "Pain Conduit") {
+ final state = character.characterState;
+ final characterClass = character.characterClass;
+ final level = state.level;
+ if (level >= 1 && level <= characterClass.healthByLevel.length) {
+ final int health = characterClass.healthByLevel[level - 1];
+ state.setMaxHealth(s, health);
+ state.setHealth(s, health);
+ }
+ }
+ }
+
+ static void setCharacterLevel(
+ _StateModifier s,
+ int level,
+ String characterId, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ Character? character;
+ for (final item in gs.currentList) {
+ if (item.id == characterId && item is Character) {
+ character = item;
+ break;
+ }
+ }
+ if (character != null) {
+ final healthByLevel = character.characterClass.healthByLevel;
+ if (healthByLevel.length < level) {
+ level = healthByLevel.length;
+ }
+ character.characterState.setFigureLevel(s, level);
+ character.characterState.setHealth(s, healthByLevel[level - 1]);
+
+ if (character.id == "Pain Conduit" &&
+ character.characterState.perkList[_kPainConduitPerkIndex]) {
+ character.characterState.setHealth(
+ s,
+ healthByLevel[level - 1] + _kPainConduitHealthBonus,
+ );
+ }
+
+ character.characterState.setMaxHealth(s, character.characterState.health);
+
+ //handle special summons
+ int health = 0;
+ int multiplier = 1;
+ String name = "";
+ if (character.id == "Beast Tyrant" || character.id == "Wildfury") {
+ //create the bear summon
+ health = _kBeastHealth;
+ multiplier = _kBeastMultiplier;
+ name = "Beast";
+ }
+ if (character.id == "D.O.M.E.") {
+ health = _kDomeHealth;
+ name = "Barrier";
+ }
+ if (character.id == "Glacial Torrent") {
+ health = _kGlacierHealth;
+ name = "Glacier";
+ }
+ if (character.id == "Jester Twins") {
+ //create the barrier as a summon
+ health = _kJesterHealth;
+ name = "Jester Twin";
+ }
+
+ final list = character.characterState.summonList;
+ if (list.isNotEmpty && list.first.name == name) {
+ int hp = health + character.characterState.level * multiplier;
+ list.first.setMaxHealth(s, hp);
+ list.first.setHealth(s, hp);
+ }
+ }
+
+ ScenarioMethods.applyDifficulty(s);
+ }
+
+ static void resetCharacter(_StateModifier s, Character item) {
+ item.characterState._initiative = 0;
+ final level = item.characterState.level;
+ item.characterState._health = item.characterClass.healthByLevel[level - 1];
+
+ if (item.characterClass.name == "Pain Conduit") {
+ if (item.characterState.perkList[_kPainConduitPerkIndex]) {
+ item.characterState._health += _kPainConduitHealthBonus;
+ }
+ }
+
+ item.characterState._maxHealth = item.characterState.health;
+ item.characterState._xp = 0;
+ item.characterState._conditions.clear();
+ item.characterState._chill = 0;
+ item.characterState._plague = 0;
+ item.characterState.modifierDeck._initDeck();
+ //reapply perks
+ final perksSetList = item.characterState.perkList;
+ final perks = item.characterState.useFHPerks
+ ? item.characterClass.perksFH
+ : item.characterClass.perks;
+ for (int i = 0; i < perks.length; i++) {
+ if (perksSetList[i]) {
+ addPerk(s, item, i);
+ }
+ }
+ //handle special summons
+ final summonList = item.characterState._summonList;
+ summonList.clear();
+ int health = 0;
+ int multiplier = 1;
+ String gfx = "";
+ String name = "";
+ if (item.id == "Beast Tyrant" || item.id == "Wildfury") {
+ //create the bear summon
+ health = _kBeastHealth;
+ multiplier = _kBeastMultiplier;
+ gfx = item.id == "Beast Tyrant" ? "beast" : "Beast v2";
+ name = "Beast";
+ }
+ if (item.id == "D.O.M.E.") {
+ health = _kDomeHealth;
+ gfx = "DOM barrier";
+ name = "Barrier";
+ }
+ if (item.id == "Glacial Torrent") {
+ health = _kGlacierHealth;
+ gfx = "GLA glacier";
+ name = "Glacier";
+ }
+ if (item.id == "Jester Twins") {
+ //create the barrier as a summon
+ health = _kJesterHealth;
+ gfx = "JES twin";
+ name = "Jester Twin";
+ }
+
+ if (name.isNotEmpty) {
+ MonsterInstance summon = MonsterInstance.summon(
+ _kSpecialSummonStandeeNr,
+ MonsterType.summon,
+ name,
+ health + level * multiplier,
+ _kSpecialSummonMove,
+ _kSpecialSummonAttack,
+ _kSpecialSummonRange,
+ gfx,
+ _kSpecialSummonInitiative,
+ );
+ summonList.add(summon);
+ }
+ item.characterState._notifySummonList();
+ }
+
+ static void removeCharacters(
+ _StateModifier s,
+ List<Character> characters, {
+ GameState? gameState,
+ }) {
+ List<ListItemData> newList = [];
+ final gs = gameState ?? getIt<GameState>();
+ for (final item in gs.currentList) {
+ if (item is Character) {
+ bool remove = false;
+ for (final name in characters) {
+ if (item.characterState.display == name.characterState.display) {
+ remove = true;
+ break;
+ }
+ }
+ if (!remove) {
+ newList.add(item);
+ }
+ } else {
+ newList.add(item);
+ }
+ }
+ gs._currentList = newList;
+ RoundMethods.updateForSpecialRules(s);
+ gs._notifyCurrentList();
+ }
+
+ static Character? createCharacter(
+ _StateModifier _,
+ String id,
+ String? edition,
+ String? display,
+ int level, {
+ GameData? gameData,
+ }) {
+ Character? character;
+ List<CharacterClass> characters = [];
+ final gd = gameData ?? getIt<GameData>();
+ final modelData = gd.modelData;
+ for (String key in modelData.keys) {
+ characters.addAll(modelData[key]?.characters ?? []);
+ }
+ for (CharacterClass characterClass in characters) {
+ if (characterClass.id == id &&
+ (edition == null || edition == characterClass.edition)) {
+ final characterState = CharacterState(id);
+ characterState._level = level;
+
+ if (GameMethods.isObjectiveOrEscort(characterClass)) {
+ characterState._initiative = _kObjectiveInitiative;
+ }
+ characterState._health = characterClass.healthByLevel[level - 1];
+ characterState._maxHealth = characterState.health;
+
+ if (display != null) {
+ characterState._display = display;
+ } else {
+ characterState._display = characterClass.name;
+ }
+ character = Character(characterState, characterClass);
+
+ //handle special summons
+ final summonList = character.characterState._summonList;
+ summonList.clear();
+ int health = 0;
+ int multiplier = 1;
+ String gfx = "";
+ String name = "";
+ if (character.id == "Beast Tyrant" || character.id == "Wildfury") {
+ //create the bear summon
+ health = _kBeastHealth;
+ multiplier = _kBeastMultiplier;
+ gfx = character.id == "Beast Tyrant" ? "beast" : "Beast v2";
+ name = "Beast";
+ }
+ if (character.id == "D.O.M.E.") {
+ health = _kDomeHealth;
+ gfx = "DOM barrier";
+ name = "Barrier";
+ }
+ if (character.id == "Glacial Torrent") {
+ health = _kGlacierHealth;
+ gfx = "GLA glacier";
+ name = "Glacier";
+ }
+ if (character.id == "Jester Twins") {
+ //create the barrier as a summon
+ health = _kJesterHealth;
+ gfx = "JES twin";
+ name = "Jester Twin";
+ }
+
+ if (name.isNotEmpty) {
+ MonsterInstance summon = MonsterInstance.summon(
+ _kSpecialSummonStandeeNr,
+ MonsterType.summon,
+ name,
+ health + level * multiplier,
+ _kSpecialSummonMove,
+ _kSpecialSummonAttack,
+ _kSpecialSummonRange,
+ gfx,
+ _kSpecialSummonInitiative,
+ );
+ summonList.add(summon);
+ }
+
+ break;
+ }
+ }
+ return character;
+ }
+}