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
use std::cmp::Ordering;
use std::convert::From;
use std::fmt;
use regex::Regex;
use serde::{de, Deserialize, Deserializer};
use crate::dofapi::carac::CaracKind;
#[derive(Clone, Debug)]
pub enum ConditionAtom {
Other(String),
Stats(CaracKind, Ordering, i16),
RestrictSetBonuses,
}
impl ConditionAtom {
pub fn is_stronger_than(&self, other: &ConditionAtom) -> bool {
use ConditionAtom::*;
match (self, other) {
(Stats(lkind, lord, lval), Stats(rkind, rord, rval))
if lkind == rkind
&& (lord == rord || *lord == Ordering::Equal) =>
{
lval == rval
|| lval.cmp(rval) == *lord
|| lval.cmp(rval) == *rord
}
(RestrictSetBonuses, RestrictSetBonuses) => true,
(Other(s1), Other(s2)) => s1 == s2,
_ => false,
}
}
}
impl From<&str> for ConditionAtom {
fn from(from: &str) -> Self {
lazy_static! {
static ref RE_CMP: Regex =
Regex::new(r"(?P<carac>.+) (?P<sign>>|<) (?P<value>\d+)")
.unwrap();
}
if let Some(captures) = RE_CMP.captures(from) {
let kind = captures.name("carac").unwrap().as_str().into();
let ordering = match captures.name("sign").unwrap().as_str() {
"<" => Ordering::Less,
">" => Ordering::Greater,
_ => unreachable!(),
};
let value = captures.name("value").unwrap().as_str().parse();
if let Ok(value) = value {
return ConditionAtom::Stats(kind, ordering, value);
}
}
ConditionAtom::Other(String::from(from))
}
}
#[derive(Clone, Debug, Default)]
pub struct Condition(Vec<Vec<ConditionAtom>>);
impl Condition {
pub fn new() -> Self {
Condition(vec![])
}
pub fn clauses(&self) -> &Vec<Vec<ConditionAtom>> {
match self {
Condition(clauses) => clauses,
}
}
pub fn into_clauses(self) -> Vec<Vec<ConditionAtom>> {
match self {
Condition(clauses) => clauses,
}
}
pub fn and(cond1: Self, cond2: Self) -> Self {
let clause_stronger_than = |clause1: &Vec<ConditionAtom>,
clause2: &Vec<ConditionAtom>|
-> bool {
clause1.iter().all(|atom1| {
clause2.iter().any(|atom2| atom1.is_stronger_than(atom2))
})
};
let mut clauses = cond1.into_clauses();
for next_clause in cond2.into_clauses() {
let next_is_weaker = clauses.iter().any(|prev_clause| {
clause_stronger_than(prev_clause, &next_clause)
});
if next_is_weaker {
continue;
}
let mut i = 0;
while i < clauses.len() {
if clause_stronger_than(&next_clause, &clauses[i]) {
clauses.swap_remove(i);
} else {
i += 1;
}
}
clauses.push(next_clause);
}
Condition(clauses)
}
}
impl From<ConditionAtom> for Condition {
fn from(atom: ConditionAtom) -> Self {
Condition(vec![vec![atom]])
}
}
struct ConditionVisitor;
impl<'de> Deserialize<'de> for Condition {
fn deserialize<D>(deserializer: D) -> Result<Condition, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_seq(ConditionVisitor)
}
}
impl<'de> de::Visitor<'de> for ConditionVisitor {
type Value = Condition;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("A condition")
}
fn visit_seq<D>(self, mut access: D) -> Result<Self::Value, D::Error>
where
D: de::SeqAccess<'de>,
{
let mut condition = Vec::new();
while let Some(line) = access.next_element()? {
let line: String = line;
condition.push(
line.split("or").map(|atom| atom.trim().into()).collect(),
);
}
Ok(Condition(condition))
}
}