diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2026-11-12 00:28:50 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2026-11-12 00:28:50 +0100 |
| commit | 808294adad3f378ad8d66b698acf8df7f49c2302 (patch) | |
| tree | cfd11a1395a8ad482311969f86abbbad97e7e0b4 /server/lib/core/models | |
| parent | f6955e34489c79f6d5b764cb6a39b1387366b433 (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/models')
| -rw-r--r-- | server/lib/core/models/campaign.dart | 72 | ||||
| -rw-r--r-- | server/lib/core/models/character_class.dart | 128 | ||||
| -rw-r--r-- | server/lib/core/models/color.dart | 16 | ||||
| -rw-r--r-- | server/lib/core/models/monster.dart | 207 | ||||
| -rw-r--r-- | server/lib/core/models/monster_ability.dart | 113 | ||||
| -rw-r--r-- | server/lib/core/models/room.dart | 64 | ||||
| -rw-r--r-- | server/lib/core/models/scenario.dart | 314 | ||||
| -rw-r--r-- | server/lib/core/models/summon_model.dart | 57 |
8 files changed, 971 insertions, 0 deletions
diff --git a/server/lib/core/models/campaign.dart b/server/lib/core/models/campaign.dart new file mode 100644 index 0000000..0a393d3 --- /dev/null +++ b/server/lib/core/models/campaign.dart @@ -0,0 +1,72 @@ +import 'dart:collection'; +import 'dart:core'; + +import 'package:collection/collection.dart'; +import 'package:x_haven_server/core/models/character_class.dart'; +import 'package:x_haven_server/core/models/monster.dart'; +import 'package:x_haven_server/core/models/monster_ability.dart'; +import 'package:x_haven_server/core/models/room.dart'; +import 'package:x_haven_server/core/models/scenario.dart'; + +class CampaignModel { + // ignore: prefer-match-file-name, file name reflects campaign data domain + const CampaignModel({ + required this.edition, + required this.monsterAbilities, + required this.monsters, + required this.characters, + required this.scenarios, + }); + final String edition; + final List<MonsterAbilityDeckModel> monsterAbilities; + final Map<String, MonsterModel> monsters; + final List<CharacterClass> characters; + final Map<String, ScenarioModel> scenarios; + + factory CampaignModel.fromJson( + Map<String, dynamic> data, + List<RoomsModel> roomsData, + ) { + // note the explicit cast to String + // this is required if robust lint rules are enabled + final edition = data['edition'] as String; + final monsterAbilities = (data['monsterAbilities'] as List<Object?>) + .cast<Map<String, dynamic>>(); + List<MonsterAbilityDeckModel> deckDataList = []; + for (final item in monsterAbilities) { + deckDataList.add(MonsterAbilityDeckModel.fromJson(item, edition)); + } + + Map<String, MonsterModel> monsterMap = HashMap(); + final monsters = data['monsters'] as Map<dynamic, dynamic>; + for (String key in monsters.keys) { + monsterMap[key] = MonsterModel.fromJson(monsters[key], key, edition); + } + + List<CharacterClass> characterDataList = []; + final classes = (data['classes'] as List<Object?>) + .cast<Map<String, dynamic>>(); + for (final item in classes) { + characterDataList.add(CharacterClass.fromJson(item)); + } + + Map<String, ScenarioModel> scenarioMap = HashMap(); + final scenarios = data['scenarios'] as Map<dynamic, dynamic>; + for (String key in scenarios.keys) { + //find right room if exists + + RoomsModel? rooms = roomsData.firstWhereOrNull( + (element) => element.scenarioName == key.substring(1).split(" ").first, + ); + scenarioMap[key] = ScenarioModel.fromJson(key, scenarios[key], rooms); + } + + return CampaignModel( + edition: edition, + monsterAbilities: deckDataList, + monsters: monsterMap, + characters: characterDataList, + scenarios: scenarioMap, + ); + } +} diff --git a/server/lib/core/models/character_class.dart b/server/lib/core/models/character_class.dart new file mode 100644 index 0000000..12f4285 --- /dev/null +++ b/server/lib/core/models/character_class.dart @@ -0,0 +1,128 @@ +import 'package:x_haven_server/core/models/color.dart'; +import 'package:x_haven_server/core/models/summon_model.dart'; + +class CharacterClass { + final String id; + final String name; + final String edition; + final List<int> healthByLevel; + final Color color; + final Color colorSecondary; + final bool hidden; + final List<SummonModel> summons; + final List<PerkModel> perks; + final List<PerkModel> perksFH; + + const CharacterClass( + this.id, + this.name, + this.healthByLevel, + this.edition, + this.color, + this.colorSecondary, + this.hidden, + this.summons, + this.perks, + this.perksFH, + ); + + factory CharacterClass.fromJson(Map<String, dynamic> data) { + final edition = data['edition'] as String; + final name = data['name'] as String; + String id = name; //default to name if no id + if (data.containsKey('id')) { + id = data['id'] as String; + } + bool hidden = false; + if (data.containsKey('hidden')) { + hidden = data['hidden'] as bool; + } + //TODO: color array + gradiantType support + final healthByLevel = (data['health'] as List<Object?>).cast<int>(); + int radix = 16; + final colorValue = data['color']; + Color color = (colorValue is List<Object?>) + ? Color(int.parse(colorValue.first as String, radix: radix)) + : Color(int.parse(colorValue as String, radix: radix)); + + Color colorSecondary = color; + if (data.containsKey("colorSecondary")) { + colorSecondary = (data["colorSecondary"] is List<Object?>) + ? colorSecondary = Color( + int.parse(data['colorSecondary'][0], radix: radix), + ) + : Color(int.parse(data['colorSecondary'], radix: radix)); + } + + List<SummonModel> summonList = []; + if (data.containsKey('summons')) { + final summons = data['summons'] as Map<dynamic, dynamic>; + for (String key in summons.keys) { + summonList.add(SummonModel.fromJson(summons[key], key)); + } + } + + List<PerkModel> perkList = []; + if (data.containsKey('perks')) { + final perks = (data['perks'] as List<Object?>) + .cast<Map<String, dynamic>>(); + for (final item in perks) { + perkList.add(PerkModel.fromJson(item)); + } + } + + List<PerkModel> perkFHList = []; + if (data.containsKey('perks_fh')) { + final perks = (data['perks_fh'] as List<Object?>) + .cast<Map<String, dynamic>>(); + for (final item in perks) { + perkFHList.add(PerkModel.fromJson(item)); + } + } + + return CharacterClass( + id, + name, + healthByLevel, + edition, + color, + colorSecondary, + hidden, + summonList, + perkList, + perkFHList, + ); + } +} + +class PerkModel { + final String text; + final List<String> remove; + final List<String> add; + const PerkModel(this.text, this.remove, this.add); + + factory PerkModel.fromJson(Map<String, dynamic> data) { + String text = ""; + if (data.containsKey('text')) { + text = data['text']; + } + + List<String> addList = []; + if (data.containsKey('adds')) { + final adds = (data['adds'] as List<Object?>).cast<String>(); + for (String item in adds) { + addList.add(item); + } + } + + List<String> removeList = []; + if (data.containsKey('removes')) { + final removes = (data['removes'] as List<Object?>).cast<String>(); + for (String item in removes) { + removeList.add(item); + } + } + + return PerkModel(text, removeList, addList); + } +} diff --git a/server/lib/core/models/color.dart b/server/lib/core/models/color.dart new file mode 100644 index 0000000..a4f99de --- /dev/null +++ b/server/lib/core/models/color.dart @@ -0,0 +1,16 @@ +/// Minimal stand-in for Flutter's `Color` used by data models. +/// +/// The reference app parses character colors from JSON as +/// `Color(int.parse(hex, radix: 16))` and only uses them for widget +/// styling. The engine never reads them; the web client renders them in +/// Phase 4. We keep the API shape (`Color(int)`) so the model files port +/// unchanged. +class Color { + const Color(this.value); + + /// ARGB value, as in Flutter's `Color.value`. + final int value; + + @override + String toString() => 'Color(0x${value.toRadixString(16).padLeft(8, '0')})'; +} diff --git a/server/lib/core/models/monster.dart b/server/lib/core/models/monster.dart new file mode 100644 index 0000000..77312cf --- /dev/null +++ b/server/lib/core/models/monster.dart @@ -0,0 +1,207 @@ +/// A monster stat that is either a plain integer or a formula string. +/// +/// Formulas may contain `C` (number of characters) and `L` (scenario level) +/// and are evaluated by [StatCalculator.calculateFormula]. Named formulas such +/// as `"Hollowpact"` or `"Incarnate"` have special handling in [MonsterInstance]. +/// +/// Use [StatValue.fromJson] when parsing JSON — it throws [FormatException] for +/// values that are neither int nor String, catching data errors at load time +/// instead of silently producing wrong results at runtime. +sealed class StatValue { + // ignore: prefer-match-file-name, file contains multiple monster model types + const StatValue(); + + static StatValue fromJson(Object? value) { + if (value is int) return IntStatValue(value); + if (value is String) return FormulaStatValue(value); + throw FormatException( + 'Stat value must be int or String, got ${value?.runtimeType}: $value', + ); + } + + /// Returns a constant representing zero. + static const StatValue zero = IntStatValue(0); +} + +/// A stat whose value is a fixed integer (e.g. `"health": 3`). +final class IntStatValue extends StatValue { + const IntStatValue(this.value); + final int value; + @override + String toString() => value.toString(); +} + +/// A stat whose value is a formula string (e.g. `"health": "C+1"`). +final class FormulaStatValue extends StatValue { + const FormulaStatValue(this.formula); + final String formula; + @override + String toString() => formula; +} + +class MonsterModel { + const MonsterModel( + this.name, + this.display, + this.gfx, + this.hidden, + this.flying, + this.deck, + this.count, + this.levels, + this.edition, + this.capture, + ); + final String name; //id + final String deck; + final String display; //same as name. most of the time + final String gfx; //same as name. most of the time + final bool hidden; + final bool flying; + final int count; + final bool capture; + final String edition; + final List<MonsterLevelModel> levels; + + factory MonsterModel.fromJson( + Map<String, dynamic> data, + String name, + String edition, + ) { + String anEdition = edition; + if (data.containsKey('edition')) { + anEdition = data['edition'] as String; + } + + String display = name; + if (data.containsKey('display')) { + display = data['display'] as String; + } + String gfx = display; + if (data.containsKey('gfx')) { + gfx = data['gfx'] as String; + } + bool hidden = false; + if (data.containsKey('hidden')) { + hidden = data['hidden'] as bool; + } + bool flying = false; + if (data.containsKey('flying')) { + flying = data['flying'] as bool; + } + bool capture = false; + if (data.containsKey('capture')) { + capture = data['capture'] as bool; + } + final deck = data['deck'] as String; + final count = data['count'] as int; + + final levels = (data['levels'] as List<Object?>) + .cast<Map<String, dynamic>>(); + List<MonsterLevelModel> monsterLevelDataList = []; + for (final item in levels) { + monsterLevelDataList.add(MonsterLevelModel.fromJson(item)); + } + return MonsterModel( + name, + display, + gfx, + hidden, + flying, + deck, + count, + monsterLevelDataList, + anEdition, + capture, + ); + } +} + +class MonsterLevelModel { + const MonsterLevelModel(this.level, this.normal, this.elite, this.boss); + final int level; + final MonsterStatsModel? normal; + final MonsterStatsModel? elite; + final MonsterStatsModel? boss; + + factory MonsterLevelModel.fromJson(Map<String, dynamic> data) { + final level = data['level'] as int; + MonsterStatsModel normal; + MonsterStatsModel elite; + if (data.containsKey('normal') && data.containsKey('elite')) { + normal = MonsterStatsModel.fromJson(data['normal']); + elite = MonsterStatsModel.fromJson(data['elite']); + return MonsterLevelModel(level, normal, elite, null); + } else { + return MonsterLevelModel( + level, + null, + null, + MonsterStatsModel.fromJson(data), + ); + } + } +} + +class MonsterStatsModel { + const MonsterStatsModel( + this.health, + this.move, + this.attack, + this.range, + this.attributes, + this.immunities, + this.special1, + this.special2, + ); + final StatValue health; + final StatValue move; + final StatValue attack; + final int range; + final List<String> attributes; + final List<String> immunities; + final List<String> special1; + final List<String> special2; + + factory MonsterStatsModel.fromJson(Map<String, dynamic> data) { + final health = StatValue.fromJson(data['health']); + StatValue move = StatValue.zero; + if (data.containsKey('move')) { + move = StatValue.fromJson(data['move']); + } + StatValue attack = StatValue.zero; + if (data.containsKey('attack')) { + attack = StatValue.fromJson(data['attack']); + } + int range = 0; + if (data.containsKey('range')) { + range = data['range'] as int; + } + List<String> attributes = []; + if (data.containsKey('attributes')) { + attributes = (data['attributes'] as List<Object?>).cast<String>(); + } + List<String> immunities = []; + if (data.containsKey('immunities')) { + immunities = (data['immunities'] as List<Object?>).cast<String>(); + } + List<String> special1 = []; + if (data.containsKey('special1')) { + special1 = (data['special1'] as List<Object?>).cast<String>(); + } + List<String> special2 = []; + if (data.containsKey('special2')) { + special2 = (data['special2'] as List<Object?>).cast<String>(); + } + return MonsterStatsModel( + health, + move, + attack, + range, + attributes, + immunities, + special1, + special2, + ); + } +} diff --git a/server/lib/core/models/monster_ability.dart b/server/lib/core/models/monster_ability.dart new file mode 100644 index 0000000..3a90a52 --- /dev/null +++ b/server/lib/core/models/monster_ability.dart @@ -0,0 +1,113 @@ +const int _kGraphicsIndex = 3; + +class MonsterAbilityDeckModel { + // ignore: prefer-match-file-name, file contains multiple ability model types + final String name; + final String edition; + final List<MonsterAbilityCardModel> cards; + const MonsterAbilityDeckModel(this.name, this.edition, this.cards); + + factory MonsterAbilityDeckModel.fromJson( + Map<String, dynamic> data, + String edition, + ) { + final name = data['name'] as String; + final List<dynamic> dynamicCards = + data['cards'] + as List< + dynamic + >; // ignore: avoid-dynamic, complex JSON cards array requires mutable dynamic list + List<MonsterAbilityCardModel> cards = []; + for (final card in dynamicCards) { + String title = name; + if (card[0] is String) { + title = card[0] as String; + card.removeAt(0); + } + int nr = card[0] as int; + + ///duplication check + if (cards.any((element) => element.nr == nr)) { + print("duplicate monster ability card nr: $nr for $name"); + } + bool shuffle = card[1] as bool; + int initiative = card[2] as int; + List<GraphicPositional> graphicPositionals = []; + if (card[3] is List) { + //handle the graphic extras + List<dynamic> list = + card[3]; // ignore: avoid-dynamic, graphic extras are a dynamic JSON array + + for (final item in list) { + double angle = 0; + double scale = 1; + if (item.containsKey('angle')) { + angle = item['angle']; + } + if (item.containsKey('scale')) { + scale = item['scale']; + } + GraphicPositional pos = GraphicPositional( + item["gfx"], + item["x"], + item["y"], + scale, + angle, + ); + graphicPositionals.add(pos); + } + + card.removeAt(_kGraphicsIndex); + } + List<String> lines = []; + for (int i = _kGraphicsIndex; i < card.length; i++) { + lines.add(card[i] as String); + } + cards.add( + MonsterAbilityCardModel( + title, + nr, + shuffle, + initiative, + lines, + name, + graphicPositionals, + ), + ); + } + return MonsterAbilityDeckModel(name, edition, cards); + } +} + +class GraphicPositional { + final String gfx; + final double x; + final double y; + final double scale; + final double angle; + const GraphicPositional(this.gfx, this.x, this.y, this.scale, this.angle); +} + +class MonsterAbilityCardModel { + final String deck; + final String title; + final int nr; + final bool shuffle; + final int initiative; + final List<String> lines; + final List<GraphicPositional> graphicPositional; + const MonsterAbilityCardModel( + this.title, + this.nr, + this.shuffle, + this.initiative, + this.lines, + this.deck, + this.graphicPositional, + ); + + Map<String, dynamic> toJson() => {'nr': nr, 'deck': deck}; + + @override + String toString() => '{"nr": $nr, "deck": "$deck" }'; +} diff --git a/server/lib/core/models/room.dart b/server/lib/core/models/room.dart new file mode 100644 index 0000000..903dc47 --- /dev/null +++ b/server/lib/core/models/room.dart @@ -0,0 +1,64 @@ +class RoomMonsterData { + // ignore: prefer-match-file-name, file contains multiple room model types + final String name; + final List<int> normal; + final List<int> elite; + const RoomMonsterData(this.name, this.normal, this.elite); + + factory RoomMonsterData.fromJson(Map<String, dynamic> data, String key) { + String name = key; + List<int> normal = data.containsKey('normal') + ? data['normal'].cast<int>() + : [0, 0, 0]; + List<int> elite = data.containsKey('elite') + ? data['elite'].cast<int>() + : [0, 0, 0]; + + return RoomMonsterData(name, normal, elite); + } +} + +class RoomModel { + final String name; + final List<RoomMonsterData> monsterData; + const RoomModel(this.name, this.monsterData); + + factory RoomModel.fromJson(Map<String, dynamic> data) { + String name = data.keys.first; + List<RoomMonsterData> monsterList = []; + data[name].forEach((key, value) { + monsterList.add(RoomMonsterData.fromJson(value, key)); + }); + + return RoomModel(name, monsterList); + } +} + +class RoomsModel { + final String scenarioName; + final List<RoomModel> roomData; + const RoomsModel(this.scenarioName, this.roomData); + + factory RoomsModel.fromJson(List<Object?> sectionData, String scenarioName) { + List<RoomModel> roomList = []; + for (final value in sectionData) { + roomList.add(RoomModel.fromJson(value as Map<String, dynamic>)); + } + + return RoomsModel(scenarioName, roomList); + } +} + +class EditionRoomsModel { + final List<RoomsModel> roomData; + const EditionRoomsModel(this.roomData); + + factory EditionRoomsModel.fromJson(Map<String, dynamic> data) { + List<RoomsModel> roomList = []; + for (final entry in data.entries) { + roomList.add(RoomsModel.fromJson(entry.value, entry.key)); + } + + return EditionRoomsModel(roomList); + } +} diff --git a/server/lib/core/models/scenario.dart b/server/lib/core/models/scenario.dart new file mode 100644 index 0000000..18cd709 --- /dev/null +++ b/server/lib/core/models/scenario.dart @@ -0,0 +1,314 @@ +import 'dart:convert'; + +import 'package:collection/collection.dart'; +import 'package:x_haven_server/core/game_methods.dart'; +import 'package:x_haven_server/core/models/room.dart'; + +class LootDeckModel { + // ignore: prefer-match-file-name, file contains multiple scenario model types + final int lumber; + final int hide; + final int metal; + final int coin; + final int arrowvine; + final int corpsecap; + final int snowthistle; + final int axenut; + final int flamefruit; + final int rockroot; + final int treasure; + const LootDeckModel( + this.lumber, + this.hide, + this.metal, + this.coin, + this.arrowvine, + this.corpsecap, + this.snowthistle, + this.axenut, + this.flamefruit, + this.rockroot, + this.treasure, + ); + + static int _getValueFromJson(Map<String, dynamic> data, String value) { + if (data.containsKey(value)) { + return data[value]; + } + return 0; + } + + factory LootDeckModel.fromJson(Map<String, dynamic> data) { + return LootDeckModel( + _getValueFromJson(data, "lumber"), + _getValueFromJson(data, "hide"), + _getValueFromJson(data, "metal"), + _getValueFromJson(data, "coin"), + _getValueFromJson(data, "arrowvine"), + _getValueFromJson(data, "corpsecap"), + _getValueFromJson(data, "snowthistle"), + _getValueFromJson(data, "axenut"), + _getValueFromJson(data, "flamefruit"), + _getValueFromJson(data, "rockroot"), + _getValueFromJson(data, "treasure"), + ); + } +} + +class SpecialRule { + final String type; + final String name; + final Object? health; + final int level; + final int init; + final String note; + final List<Object?> list; + final bool startOfRound; + final Object? condition; + + const SpecialRule( + this.type, + this.name, + this.health, + this.level, + this.init, + this.note, + this.list, + this.startOfRound, + this.condition, + ); + + factory SpecialRule.fromJson(Map<String, dynamic> data) { + final String type = data['type']; //required + String name = ""; + if (data.containsKey('name')) { + name = data['name']; + } + Object? health = ""; + if (data.containsKey('health')) { + health = data['health']; + } + Object? condition = ""; + if (data.containsKey('condition')) { + condition = data['condition']; + } + int level = 0; + if (data.containsKey('level')) { + level = data['level']; + } + int init = 99; + if (data.containsKey('init')) { + init = data['init']; + } + String note = ""; + if (data.containsKey('note')) { + note = data['note']; + } + bool startOfRound = true; + if (data.containsKey('startOfRound')) { + startOfRound = data['startOfRound']; + } + List<Object?> aList = []; + if (data.containsKey('list')) { + aList = data['list'] as List<Object?>; + } + return SpecialRule( + type, + name, + health, + level, + init, + note, + aList, + startOfRound, + condition, + ); + } + + Map<String, dynamic> toJson() => { + 'type': type, + 'note': note, + 'name': name, + 'health': health.toString(), + 'condition': condition.toString(), + 'init': init, + 'level': level, + 'startOfRound': startOfRound, + 'list': list, + }; + + @override + String toString() => json.encode(toJson()); +} + +//can't be immutable since merging other data late +class ScenarioModel { + ScenarioModel({ + required this.name, + required this.sections, + required this.monsters, + required this.specialRules, + required this.lootDeck, + required this.initMessage, + required this.monsterStandees, + }); + final String name; + List<String> monsters; + List<SpecialRule> specialRules; + final LootDeckModel? lootDeck; + String initMessage; + final List<RoomMonsterData>? monsterStandees; + final List<ScenarioModel> sections; + factory ScenarioModel.fromJson( + String name, + Map<String, dynamic> data, + RoomsModel? rooms, + ) { + List<String> monsterList = []; + if (data.containsKey('monsters')) { + final monsters = (data['monsters'] as List<Object?>).cast<String>(); + for (final monster in monsters) { + monsterList.add(monster); + } + } + List<SpecialRule> rulesList = []; + if (data.containsKey('special')) { + final specialRules = (data['special'] as List<Object?>) + .cast<Map<String, dynamic>>(); + for (final rule in specialRules) { + rulesList.add(SpecialRule.fromJson(rule)); + } + } + LootDeckModel? lootDeck; + if (data.containsKey('lootDeck')) { + lootDeck = LootDeckModel.fromJson(data['lootDeck']); + } + String initMessage = ""; + if (data.containsKey('initialMessage')) { + initMessage = data['initialMessage']; + } + + List<ScenarioModel> sectionList = []; + + if (rooms != null) { + List names = []; + for (final u in rooms.roomData) { + if (names.contains(u.name)) { + print("duplicate ${u.name} in ${rooms.scenarioName}"); + } else { + names.add(u.name); + } + } + } + + if (rooms != null) { + for (int i = 1; i < rooms.roomData.length; i++) { + //skip first as it is the scenario start room + sectionList.add(ScenarioModel.sectionFromRoomData(rooms.roomData[i])); + } + } + + if (data.containsKey("sections")) { + final sections = data['sections'] as Map<dynamic, dynamic>; + for (String key in sections.keys) { + if (rooms == null) { + sectionList.add( + ScenarioModel.sectionFromJson(key, sections[key], rooms), + ); + } else { + //it might be a bit silly that room data and section data is separate + //merge if has already + int splitIndex = 0; + //would be nice to know which is solo scenario + ScenarioModel? section = sectionList.firstWhereOrNull( + (element) => element.name == key.split(" ")[splitIndex], + ); + if (section != null) { + //merge + ScenarioModel model = ScenarioModel.sectionFromJson( + key, + sections[key], + rooms, + ); + section.monsters = model.monsters; + section.initMessage = model.initMessage; + section.specialRules = model.specialRules; + } else { + sectionList.add( + ScenarioModel.sectionFromJson(key, sections[key], rooms), + ); + } + } + } + } + if (rooms != null && rooms.roomData.isEmpty) { + rooms = null; + } + + return ScenarioModel( + name: name, + monsters: monsterList, + specialRules: rulesList, + lootDeck: lootDeck, + initMessage: initMessage, + sections: sectionList, + monsterStandees: rooms?.roomData.first.monsterData, + ); + } + + factory ScenarioModel.sectionFromJson( + String name, + Map<String, dynamic> data, + RoomsModel? rooms, + ) { + List<String> monsterList = []; + if (data.containsKey('monsters')) { + final monsters = (data['monsters'] as List<Object?>).cast<String>(); + for (final monster in monsters) { + monsterList.add(monster); + } + } + List<SpecialRule> rulesList = []; + if (data.containsKey('special')) { + final specialRules = (data['special'] as List<Object?>) + .cast<Map<String, dynamic>>(); + for (final rule in specialRules) { + rulesList.add(SpecialRule.fromJson(rule)); + } + } + String initMessage = ""; + if (data.containsKey('initialMessage')) { + initMessage = data['initialMessage']; + } + + //find right room + List<RoomMonsterData>? standees = rooms?.roomData + .firstWhereOrNull( + (element) => + element.name == + GameMethods.findNrFromScenarioName(name).toString(), + ) + ?.monsterData; + return ScenarioModel( + name: name, + monsters: monsterList, + specialRules: rulesList, + lootDeck: null, + initMessage: initMessage, + sections: [], + monsterStandees: standees, + ); + } + + factory ScenarioModel.sectionFromRoomData(RoomModel room) { + return ScenarioModel( + name: "#${room.name}", + monsters: [], + specialRules: [], + lootDeck: null, + initMessage: "", + sections: [], + monsterStandees: room.monsterData, + ); + } +} diff --git a/server/lib/core/models/summon_model.dart b/server/lib/core/models/summon_model.dart new file mode 100644 index 0000000..802f3f8 --- /dev/null +++ b/server/lib/core/models/summon_model.dart @@ -0,0 +1,57 @@ +import 'package:x_haven_server/core/models/monster.dart'; + +class SummonModel { + final String name; + final StatValue health; + final int move; + final int attack; + final int range; + final int level; + final String gfx; + final int standees; + const SummonModel( + this.name, + this.health, + this.move, + this.attack, + this.range, + this.level, + this.gfx, + this.standees, + ); + + factory SummonModel.fromJson(Map<String, dynamic> data, String key) { + String name = key; + StatValue health = const IntStatValue(2); //default to 2 not to die + if (data.containsKey('health')) { + health = StatValue.fromJson(data['health']); + } + int move = 0; + if (data.containsKey('move')) { + move = data['move']; + } + int attack = 0; + if (data.containsKey('attack')) { + attack = data['attack']; + } + int range = 0; + if (data.containsKey('range')) { + range = data['range']; + } + int level = 0; + if (data.containsKey('level')) { + level = data['level']; + } + String gfx = ""; + if (data.containsKey('gfx')) { + gfx = data['gfx']; + } + + int standees = 1; + if (data.containsKey('standees')) { + standees = data['standees']; + } + + return SummonModel(name, health, move, attack, range, level, gfx, standees); + } +} |
