Add hero stats parser

This commit is contained in:
Aaron Fischer 2019-06-24 01:25:47 +02:00
parent ff790c8e4a
commit 72d7dc95db
3 changed files with 43 additions and 30 deletions

View file

@ -7,11 +7,14 @@ part 'Warband.g.dart';
// flutter packages pub run build_runner build --delete-conflicting-outputs // flutter packages pub run build_runner build --delete-conflicting-outputs
@JsonSerializable(nullable: true, anyMap: true) @JsonSerializable(nullable: true, anyMap: true, createToJson: false)
class Hero { class Hero {
@JsonKey(name: 'hero') @JsonKey(name: 'hero')
final String name; final String name;
final String stats;
@JsonKey(fromJson: _statsFromJson)
final HeroStats stats;
final String skilllists; final String skilllists;
final String weapons; final String weapons;
final String amour; final String amour;
@ -22,12 +25,44 @@ class Hero {
factory Hero.fromJson(yaml) => factory Hero.fromJson(yaml) =>
_$HeroFromJson(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 { class Warband {
@JsonKey(name: 'warband', fromJson: _warbandNameAndRace) @JsonKey(name: 'warband', fromJson: _warbandNameAndRace)
HashMap<String, String> nameAndRace; final HashMap<String, String> nameAndRace;
@JsonKey(ignore: true) @JsonKey(ignore: true)
String name; String name;
@JsonKey(ignore: true) @JsonKey(ignore: true)
@ -50,7 +85,7 @@ class Warband {
this.name = this.nameAndRace['race']; this.name = this.nameAndRace['race'];
} }
static HashMap<String, String>_warbandNameAndRace(String nameAndRace) { static HashMap<String, String> _warbandNameAndRace(String nameAndRace) {
HashMap<String, String> nr = new HashMap(); HashMap<String, String> nr = new HashMap();
RegExp re = new RegExp(r"(.*) \((.*)\)"); RegExp re = new RegExp(r"(.*) \((.*)\)");
@ -61,6 +96,6 @@ class Warband {
return nr; return nr;
} }
factory Warband.fromJson(YamlMap yaml) => factory Warband.fromJson(yaml) =>
_$WarbandFromJson(yaml); _$WarbandFromJson(yaml);
} }

View file

@ -8,7 +8,7 @@ part of 'Warband.dart';
Hero _$HeroFromJson(Map json) { Hero _$HeroFromJson(Map json) {
return Hero( return Hero(
json['stats'] as String, Hero._statsFromJson(json['stats'] as String),
json['skilllists'] as String, json['skilllists'] as String,
json['weapons'] as String, json['weapons'] as String,
json['amour'] as String, json['amour'] as String,
@ -17,16 +17,6 @@ Hero _$HeroFromJson(Map json) {
json['hero'] as String); json['hero'] as String);
} }
Map<String, dynamic> _$HeroToJson(Hero instance) => <String, dynamic>{
'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) { Warband _$WarbandFromJson(Map json) {
return Warband( return Warband(
Warband._warbandNameAndRace(json['warband'] as String), Warband._warbandNameAndRace(json['warband'] as String),
@ -41,15 +31,3 @@ Warband _$WarbandFromJson(Map json) {
?.map((e) => e == null ? null : Hero.fromJson(e)) ?.map((e) => e == null ? null : Hero.fromJson(e))
?.toList()); ?.toList());
} }
Map<String, dynamic> _$WarbandToJson(Warband instance) => <String, dynamic>{
'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
};

View file

@ -51,7 +51,7 @@ class _RoasterWidgetState extends State<RoasterWidget> {
appBar: AppBar( appBar: AppBar(
title: Text(warband.name) title: Text(warband.name)
), ),
body: Text(warband.race, textScaleFactor: 1.3) body: Text(warband.heros.first.stats.weaponSkill.toString(), textScaleFactor: 1.3)
); );
} }
} }