1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use crate::error::*;
use error_chain::bail;
use std::collections::HashMap;
#[derive(Debug,Clone,Default)]
pub struct Hero
{
pub name: String,
pub health: isize,
pub stamina: isize,
pub astral: isize,
pub qualities: HashMap<Quality,isize>,
pub skills: HashMap<String,(isize,[Quality;3])>,
}
impl std::str::FromStr for Hero
{
type Err = Error;
fn from_str(input: &str) -> Result<Self>
{
let document = roxmltree::Document::parse(&input).chain_err(|| "xml document could not be parsed")?;
let root = document.root_element();
if ! root.has_tag_name("helden")
{
bail!("unknown root element");
}
let held = root.children()
.find(|child| child.has_tag_name("held"))
.ok_or("root element does not contain held element")?;
let qualities = held.children()
.find(|elem| elem.has_tag_name("eigenschaften"))
.ok_or("hero does not have base values")?;
let mut qualities: HashMap<_,isize> = qualities.children()
.map(|elem|
{
let name = elem.attribute("name").unwrap_or("").to_lowercase();
let base = elem.attribute("value").and_then(|i| i.parse().ok()).unwrap_or(0);
let modi = elem.attribute("mod").and_then(|i| i.parse().ok()).unwrap_or(0);
(name,base + modi)
})
.collect();
let health_base = qualities.remove("lebensenergie").unwrap_or(0);
let stamina_base = qualities.remove("ausdauer").unwrap_or(0);
let astral_base = qualities.remove("astralenergie").unwrap_or(0);
let qualities: HashMap<_,_> = qualities.into_iter()
.filter_map(|(k,v)|
{
k.parse::<Quality>()
.map(|k| (k,v))
.ok()
})
.collect();
use Quality::*;
let skills: HashMap<_,_> = held.children()
.filter(|elem| elem.has_tag_name("talentliste") || elem.has_tag_name("zauberliste"))
.map(|elem| elem.children())
.flatten()
.map(|elem| -> Result<(String,(isize,[Quality;3]))>
{
let name = elem.attribute("name").unwrap_or("").to_lowercase();
let value = elem.attribute("value").and_then(|i| i.parse().ok()).unwrap_or(0);
let probe = elem.attribute("probe").ok_or("probe not parsable")?;
let probe = probe.trim().trim_start_matches('(').trim_end_matches(')');
let probe = probe.split('/').map(|quality| quality.parse::<Quality>()).collect::<Result<Vec<_>>>()?;
if probe.len() != 3
{
bail!("skill does not have three 'probe'");
}
Ok((name,(value,[probe[0],probe[1],probe[2]])))
})
.filter_map(Result::ok)
.collect();
let hero = Hero
{
name: held.attribute("name").ok_or("hero does not have a name")?.into(),
health:
{
let ko = *qualities.get(&Constitution).unwrap_or(&0);
let kk = *qualities.get(&Strength).unwrap_or(&0);
((ko as f32 * 2.0 + kk as f32) / 2.0).round() as isize + health_base
},
stamina:
{
let mu = *qualities.get(&Courage).unwrap_or(&0);
let ko = *qualities.get(&Constitution).unwrap_or(&0);
let ge = *qualities.get(&Agility).unwrap_or(&0);
((mu as f32 + ko as f32 + ge as f32) / 2.0).round() as isize + stamina_base
},
astral:
{
let mu = *qualities.get(&Courage).unwrap_or(&0);
let int = *qualities.get(&Intuition).unwrap_or(&0);
let ch = *qualities.get(&Charisma).unwrap_or(&0);
((mu as f32 + int as f32 + ch as f32) / 2.0).round() as isize + astral_base
},
qualities,
skills,
};
Ok(hero)
}
}
#[derive(Debug,PartialEq,Eq,PartialOrd,Ord,Hash,Clone,Copy)]
pub enum Quality
{
Courage,
Wisdom,
Intuition,
Charisma,
Dexterity,
Agility,
Constitution,
Strength,
SocialStatus,
MagicResistance,
Initiative,
CloseCombat,
Parry,
RangedCombat,
}
impl std::str::FromStr for Quality
{
type Err = Error;
fn from_str(input: &str) -> Result<Self>
{
use Quality::*;
match input.to_lowercase().as_str()
{
"mu" | "mut" | "courage" => Ok(Courage),
"kl" | "klugheit" | "wisdom" => Ok(Wisdom),
"in" | "intuition" => Ok(Intuition),
"ch" | "charisma" => Ok(Charisma),
"ff" | "fingerfertigkeit" | "dexterity" => Ok(Dexterity),
"ge" | "gewandtheit" | "agility" => Ok(Agility),
"ko" | "konstitution" | "constitution" => Ok(Constitution),
"kk" | "körperkraft" | "strength" => Ok(Strength),
"gs" | "sozialstatus" | "socialstatus" => Ok(SocialStatus),
"mr" | "magieresistenz" | "magicrestistance" => Ok(MagicResistance),
"ini"| "initiative" => Ok(Initiative),
"at" | "attacke" | "nahkampf" | "combat" | "closecombat" => Ok(CloseCombat),
"pa" | "parrieren" | "parry" => Ok(Parry),
"fk" | "fernkampf" | "ranged" | "rangedcombat" => Ok(RangedCombat),
_ => bail!("unknown quality '{}'", input),
}
}
}