toolheim/mobile-app/lib/Warband.dart

101 lines
3 KiB
Dart
Raw Normal View History

2019-06-24 00:53:43 +02:00
import 'dart:collection';
import 'package:json_annotation/json_annotation.dart';
import 'package:yaml/yaml.dart';
part 'Warband.g.dart';
// flutter packages pub run build_runner build --delete-conflicting-outputs
2019-06-24 01:25:47 +02:00
@JsonSerializable(nullable: true, anyMap: true, createToJson: false)
2019-06-24 00:53:43 +02:00
class Hero {
@JsonKey(name: 'hero')
final String name;
2019-06-24 01:25:47 +02:00
@JsonKey(fromJson: _statsFromJson)
final HeroStats stats;
2019-06-24 00:53:43 +02:00
final String skilllists;
final String weapons;
final String amour;
final String rules;
final int warbandaddition;
Hero(this.stats, this.skilllists, this.weapons, this.amour, this.rules, this.warbandaddition, this.name);
factory Hero.fromJson(yaml) =>
_$HeroFromJson(yaml);
2019-06-24 01:25:47 +02:00
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
);
}
}
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);
2019-06-24 00:53:43 +02:00
}
2019-06-24 01:25:47 +02:00
@JsonSerializable(nullable: true, anyMap: true, createToJson: false)
2019-06-24 00:53:43 +02:00
class Warband {
@JsonKey(name: 'warband', fromJson: _warbandNameAndRace)
2019-06-24 01:25:47 +02:00
final HashMap<String, String> nameAndRace;
2019-06-24 00:53:43 +02:00
@JsonKey(ignore: true)
String name;
@JsonKey(ignore: true)
String race;
@JsonKey(name: 'campaign')
final int campaignPoints;
final String objective;
final String alignment;
final String achievments;
final int gc;
final int shards;
final String equipment;
final List<Hero> heros;
Warband(this.nameAndRace, this.campaignPoints, this.objective, this.alignment, this.gc, this.shards, this.equipment, this.achievments, this.heros) {
this.race = this.nameAndRace['name'];
this.name = this.nameAndRace['race'];
}
2019-06-24 01:25:47 +02:00
static HashMap<String, String> _warbandNameAndRace(String nameAndRace) {
2019-06-24 00:53:43 +02:00
HashMap<String, String> nr = new HashMap();
RegExp re = new RegExp(r"(.*) \((.*)\)");
var matches = re.allMatches(nameAndRace);
nr['name'] = matches.toList().first.group(1).toString();
nr['race'] = matches.toList().first.group(2).toString();
return nr;
}
2019-06-24 01:25:47 +02:00
factory Warband.fromJson(yaml) =>
2019-06-24 00:53:43 +02:00
_$WarbandFromJson(yaml);
}