toolheim/mobile-app/lib/data/warband_roaster.dart

239 lines
6.3 KiB
Dart
Raw Normal View History

2019-06-25 00:53:45 +02:00
import 'dart:collection';
2019-07-10 14:16:13 +02:00
import 'package:flutter/widgets.dart';
2019-06-25 00:53:45 +02:00
import 'package:json_annotation/json_annotation.dart';
2019-07-06 00:44:20 +02:00
part 'warband_roaster.g.dart';
2019-06-25 00:53:45 +02:00
// flutter packages pub run build_runner build --delete-conflicting-outputs
abstract class Unit {
static List<String> _splitListFromJson(String list) {
if (list == null) {
return new List();
}
return list.split(new RegExp(r" *, *"));
}
static Stats _statsFromJson(String stats) {
2019-07-06 00:44:20 +02:00
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*");
2019-06-25 00:53:45 +02:00
var matches = re.allMatches(stats).toList().first;
return Stats(
2019-07-06 00:44:20 +02:00
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);
2019-06-25 00:53:45 +02:00
}
}
@JsonSerializable(nullable: true, anyMap: true, createToJson: false)
class HenchmenGroup extends Unit {
@JsonKey(name: 'group', fromJson: _henchmenHeaderFromJson)
final HashMap<String, String> header;
@JsonKey(ignore: true)
String name;
@JsonKey(ignore: true)
String type;
@JsonKey(ignore: true)
int number;
@JsonKey(ignore: true)
int experience;
@JsonKey(fromJson: Unit._statsFromJson)
final Stats stats;
@JsonKey(fromJson: Unit._splitListFromJson)
final List<String> weapons;
@JsonKey(fromJson: Unit._splitListFromJson)
final List<String> amour;
HenchmenGroup(this.header, this.stats, this.weapons, this.amour) {
this.name = this.header['name'];
this.type = this.header['type'];
this.number = int.tryParse(this.header['number']) ?? 1;
this.experience = int.tryParse(this.header['experience']) ?? 0;
}
static HashMap<String, String> _henchmenHeaderFromJson(String header) {
HashMap<String, String> h = new HashMap();
2019-07-06 00:44:20 +02:00
RegExp re =
new RegExp(r"([^\(]+)\(([0-9]+)x?\s+([^\)]+)\)\s*\[([0-9]+)XP\]\s*");
2019-06-25 00:53:45 +02:00
var matches = re.allMatches(header).toList().first;
h['name'] = matches.group(1);
2019-06-27 00:16:58 +02:00
h['number'] = matches.group(2);
h['type'] = matches.group(3);
2019-06-25 00:53:45 +02:00
h['experience'] = matches.group(4);
return h;
}
2019-07-06 00:44:20 +02:00
factory HenchmenGroup.fromJson(yaml) => _$HenchmenGroupFromJson(yaml);
2019-06-25 00:53:45 +02:00
}
@JsonSerializable(nullable: true, anyMap: true, createToJson: false)
class Hero extends Unit {
@JsonKey(name: 'hero', fromJson: _heroHeaderFromJson)
final HashMap<String, String> header;
@JsonKey(ignore: true)
String name;
@JsonKey(ignore: true)
String type;
@JsonKey(ignore: true)
int experience;
@JsonKey(fromJson: Unit._statsFromJson)
final Stats stats;
@JsonKey(fromJson: Unit._splitListFromJson)
final List<String> skilllists;
@JsonKey(fromJson: Unit._splitListFromJson)
final List<String> weapons;
@JsonKey(fromJson: Unit._splitListFromJson)
final List<String> amour;
@JsonKey(fromJson: Unit._splitListFromJson)
final List<String> rules;
final int warbandaddition;
2019-07-06 00:44:20 +02:00
Hero(this.stats, this.skilllists, this.weapons, this.amour, this.rules,
this.warbandaddition, this.header) {
2019-06-25 00:53:45 +02:00
this.name = this.header['name'];
this.type = this.header['type'];
this.experience = int.tryParse(this.header['experience']) ?? 0;
}
2019-07-06 00:44:20 +02:00
factory Hero.fromJson(yaml) => _$HeroFromJson(yaml);
2019-06-25 00:53:45 +02:00
static HashMap<String, String> _heroHeaderFromJson(String header) {
HashMap<String, String> h = new HashMap();
2019-07-06 00:44:20 +02:00
RegExp re = new RegExp(r"([^\(]+)\(([^\)]+)\)\s*\[([0-9]+)XP\]\s*");
2019-06-25 00:53:45 +02:00
var matches = re.allMatches(header).toList().first;
h['name'] = matches.group(1);
h['type'] = matches.group(2);
h['experience'] = matches.group(3);
return h;
}
}
2019-07-10 14:16:13 +02:00
@immutable
2019-06-25 00:53:45 +02:00
class Stats {
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;
2019-07-06 00:44:20 +02:00
Stats(
this.movement,
this.weaponSkill,
this.ballisticSkill,
this.strength,
this.toughtness,
this.wounds,
this.initiative,
this.attacks,
this.leadership,
this.save);
2019-06-25 00:53:45 +02:00
}
2019-07-07 22:31:06 +02:00
class Version {
String gitHash;
String date;
String author;
String message;
Version(this.gitHash, this.date, this.author, this.message);
}
2019-06-25 00:53:45 +02:00
@JsonSerializable(nullable: true, anyMap: true, createToJson: false)
class WarbandRoaster {
2019-07-06 00:44:20 +02:00
/// Store the complete string of name and race. This will split up into the
/// fields name and race.
2019-06-25 00:53:45 +02:00
@JsonKey(name: 'warband', fromJson: _warbandNameAndRace)
final HashMap<String, String> nameAndRace;
@JsonKey(ignore: true)
String name;
@JsonKey(ignore: true)
String race;
bool active;
2019-06-25 00:53:45 +02:00
@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;
@JsonKey(name: 'henchmen')
final List<HenchmenGroup> henchmenGroups;
2019-07-06 00:44:20 +02:00
/// The players name is not defined in the yml file. This will be added later
/// from the GitHubAdapter.
@JsonKey(ignore: true)
2019-07-07 22:31:06 +02:00
String playerName = 'Lonely Recluse';
@JsonKey(ignore: true)
2019-07-07 22:31:06 +02:00
Version lastSyncVersion;
@JsonKey(ignore: true)
2019-07-07 22:31:06 +02:00
Version currentVersion;
2019-07-06 00:44:20 +02:00
WarbandRoaster(
this.nameAndRace,
this.campaignPoints,
this.objective,
this.alignment,
this.gc,
this.shards,
this.equipment,
this.achievments,
this.heros,
this.henchmenGroups) {
2019-06-25 00:53:45 +02:00
this.name = this.nameAndRace['name'];
this.race = this.nameAndRace['race'];
}
2019-07-10 14:16:13 +02:00
int experience() {
// TODO: Calculate
return 1337;
}
2019-06-25 00:53:45 +02:00
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;
}
2019-07-06 00:44:20 +02:00
factory WarbandRoaster.fromJson(yaml) => _$WarbandRoasterFromJson(yaml);
}