toolheim/mobile-app/lib/Warband.dart

101 lines
3 KiB
Dart

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
@JsonSerializable(nullable: true, anyMap: true, createToJson: false)
class Hero {
@JsonKey(name: 'hero')
final String name;
@JsonKey(fromJson: _statsFromJson)
final HeroStats stats;
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);
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);
}
@JsonSerializable(nullable: true, anyMap: true, createToJson: false)
class Warband {
@JsonKey(name: 'warband', fromJson: _warbandNameAndRace)
final HashMap<String, String> nameAndRace;
@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'];
}
static HashMap<String, String> _warbandNameAndRace(String nameAndRace) {
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;
}
factory Warband.fromJson(yaml) =>
_$WarbandFromJson(yaml);
}