From 72d7dc95db4e1405c751d3cf5d1a6ae80b8db09a Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Mon, 24 Jun 2019 01:25:47 +0200 Subject: [PATCH] Add hero stats parser --- mobile-app/lib/Warband.dart | 47 ++++++++++++++++++++++++++++++----- mobile-app/lib/Warband.g.dart | 24 +----------------- mobile-app/lib/main.dart | 2 +- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/mobile-app/lib/Warband.dart b/mobile-app/lib/Warband.dart index 82316a5..616835d 100644 --- a/mobile-app/lib/Warband.dart +++ b/mobile-app/lib/Warband.dart @@ -7,11 +7,14 @@ part 'Warband.g.dart'; // flutter packages pub run build_runner build --delete-conflicting-outputs -@JsonSerializable(nullable: true, anyMap: true) +@JsonSerializable(nullable: true, anyMap: true, createToJson: false) class Hero { @JsonKey(name: 'hero') final String name; - final String stats; + + @JsonKey(fromJson: _statsFromJson) + final HeroStats stats; + final String skilllists; final String weapons; final String amour; @@ -22,12 +25,44 @@ class Hero { factory Hero.fromJson(yaml) => _$HeroFromJson(yaml); + + static HeroStats _statsFromJson(String stats) { + RegExp re = new RegExp(r"\s*M([0-9]+[dD]*[6]*)\s*,\s*WS([0-9]+)\s*,\s*BS([0-9]+)\s*,\s*S([0-9]+)\s*,\s*T([0-9]+)\s*,\s*W([0-9]+)\s*,\s*I([0-9]+)\s*,\s*A([0-9]+)\s*,\s*Ld([0-9]+)\s*,\s*Sv([0-9\-]+)\s*"); + var matches = re.allMatches(stats).toList().first; + return HeroStats( + int.tryParse(matches.group(1)) ?? 0, + int.tryParse(matches.group(2)) ?? 0, + int.tryParse(matches.group(3)) ?? 0, + int.tryParse(matches.group(4)) ?? 0, + int.tryParse(matches.group(5)) ?? 0, + int.tryParse(matches.group(6)) ?? 0, + int.tryParse(matches.group(7)) ?? 0, + int.tryParse(matches.group(8)) ?? 0, + int.tryParse(matches.group(9)) ?? 0, + int.tryParse(matches.group(10)) ?? 0 + ); + } } -@JsonSerializable(nullable: true, anyMap: true) +class HeroStats { + final int movement; + final int weaponSkill; + final int ballisticSkill; + final int strength; + final int toughtness; + final int wounds; + final int initiative; + final int attacks; + final int leadership; + final int save; + + HeroStats(this.movement, this.weaponSkill, this.ballisticSkill, this.strength, this.toughtness, this.wounds, this.initiative, this.attacks, this.leadership, this.save); +} + +@JsonSerializable(nullable: true, anyMap: true, createToJson: false) class Warband { @JsonKey(name: 'warband', fromJson: _warbandNameAndRace) - HashMap nameAndRace; + final HashMap nameAndRace; @JsonKey(ignore: true) String name; @JsonKey(ignore: true) @@ -50,7 +85,7 @@ class Warband { this.name = this.nameAndRace['race']; } - static HashMap_warbandNameAndRace(String nameAndRace) { + static HashMap _warbandNameAndRace(String nameAndRace) { HashMap nr = new HashMap(); RegExp re = new RegExp(r"(.*) \((.*)\)"); @@ -61,6 +96,6 @@ class Warband { return nr; } - factory Warband.fromJson(YamlMap yaml) => + factory Warband.fromJson(yaml) => _$WarbandFromJson(yaml); } \ No newline at end of file diff --git a/mobile-app/lib/Warband.g.dart b/mobile-app/lib/Warband.g.dart index 08d0ba5..dce69b7 100644 --- a/mobile-app/lib/Warband.g.dart +++ b/mobile-app/lib/Warband.g.dart @@ -8,7 +8,7 @@ part of 'Warband.dart'; Hero _$HeroFromJson(Map json) { return Hero( - json['stats'] as String, + Hero._statsFromJson(json['stats'] as String), json['skilllists'] as String, json['weapons'] as String, json['amour'] as String, @@ -17,16 +17,6 @@ Hero _$HeroFromJson(Map json) { json['hero'] as String); } -Map _$HeroToJson(Hero instance) => { - 'hero': instance.name, - 'stats': instance.stats, - 'skilllists': instance.skilllists, - 'weapons': instance.weapons, - 'amour': instance.amour, - 'rules': instance.rules, - 'warbandaddition': instance.warbandaddition - }; - Warband _$WarbandFromJson(Map json) { return Warband( Warband._warbandNameAndRace(json['warband'] as String), @@ -41,15 +31,3 @@ Warband _$WarbandFromJson(Map json) { ?.map((e) => e == null ? null : Hero.fromJson(e)) ?.toList()); } - -Map _$WarbandToJson(Warband instance) => { - 'warband': instance.nameAndRace, - 'campaign': instance.campaignPoints, - 'objective': instance.objective, - 'alignment': instance.alignment, - 'achievments': instance.achievments, - 'gc': instance.gc, - 'shards': instance.shards, - 'equipment': instance.equipment, - 'heros': instance.heros - }; diff --git a/mobile-app/lib/main.dart b/mobile-app/lib/main.dart index bbba8d2..d59040d 100644 --- a/mobile-app/lib/main.dart +++ b/mobile-app/lib/main.dart @@ -51,7 +51,7 @@ class _RoasterWidgetState extends State { appBar: AppBar( title: Text(warband.name) ), - body: Text(warband.race, textScaleFactor: 1.3) + body: Text(warband.heros.first.stats.weaponSkill.toString(), textScaleFactor: 1.3) ); } }