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
use indexmap::IndexMap;
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::hash::Hash;
pub trait Id {
#[cfg(not(feature = "serialization"))]
type Id: Hash + Eq + Clone + Debug + Send;
#[cfg(feature = "serialization")]
type Id: Hash + Eq + Clone + Debug + Send + Serialize + for<'a> Deserialize<'a>;
fn id(&self) -> &Self::Id;
}
pub(crate) fn collect_from_iter<I>(
it: I,
) -> IndexMap<<<I as Iterator>::Item as Id>::Id, <I as Iterator>::Item>
where
I: Iterator,
<I as Iterator>::Item: Id,
{
let mut map = IndexMap::new();
for e in it {
if !map.contains_key(e.id()) {
map.insert(e.id().clone(), e);
}
}
map
}
#[cfg(test)]
pub(crate) mod tests {
use crate::battle::{Battle, BattleRules};
use crate::creature::{CreateCreature, CreatureId};
use crate::event::{DefaultOutput, DummyEvent, EventProcessor, EventTrigger};
use crate::object::{CreateObject, ObjectId};
use crate::server::Server;
use crate::space::Position;
use crate::team::{CreateTeam, TeamId};
pub(crate) fn server<R: BattleRules + 'static>(rules: R) -> Server<R> {
let battle = Battle::builder(rules).build();
Server::builder(battle).build()
}
pub(crate) fn team<'a, R: BattleRules + 'static>(server: &'a mut Server<R>, id: TeamId<R>) {
assert_eq!(CreateTeam::trigger(server, id).fire().err(), None);
}
pub(crate) fn creature<'a, R: BattleRules + 'static>(
server: &'a mut Server<R>,
creature_id: CreatureId<R>,
team_id: TeamId<R>,
position: Position<R>,
) {
assert_eq!(
CreateCreature::trigger(server, creature_id, team_id, position)
.fire()
.err(),
None
);
}
pub(crate) fn object<'a, R: BattleRules + 'static>(
server: &'a mut Server<R>,
object_id: ObjectId<R>,
position: Position<R>,
) {
assert_eq!(
CreateObject::trigger(server, object_id, position)
.fire()
.err(),
None
);
}
pub(crate) fn dummy<R, P>(processor: &mut P)
where
R: BattleRules + 'static,
P: EventProcessor<R>,
{
assert_eq!(DummyEvent::trigger(processor).fire().err(), None);
}
}