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
use crate::data::RoomName;
use super::ActionLogTarget;
with_structure_fields_and_update_struct! {
#[derive(Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct StructureTower {
pub user: String,
#[serde(default, rename = "off")]
pub disabled: bool,
pub energy: i32,
pub energy_capacity: i32,
pub notify_when_attacked: bool,
pub action_log: StructureTowerActions,
}
#[derive(Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct StructureTowerUpdate {
- user: String,
#[serde(rename = "off")]
- disabled: bool,
- energy: i32,
- energy_capacity: i32,
- notify_when_attacked: bool,
- action_log: StructureTowerActions,
}
}
with_update_struct! {
#[derive(serde_derive::Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct StructureTowerActions {
pub attack: Option<ActionLogTarget>,
pub heal: Option<ActionLogTarget>,
pub repair: Option<ActionLogTarget>,
}
#[derive(serde_derive::Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct StructureTowerActionsUpdate { ... }
}
#[cfg(test)]
mod test {
use serde::Deserialize;
use serde_json;
use crate::data::RoomName;
use super::{ActionLogTarget, StructureTower, StructureTowerActions};
#[test]
fn parse_tower_and_update() {
let json = json!({
"_id": "57f1cc9d27e2c0520e93ba95",
"actionLog": {
"attack": null,
"heal": null,
"repair": null
},
"energy": 920,
"energyCapacity": 1000,
"hits": 3000,
"hitsMax": 3000,
"notifyWhenAttacked": true,
"room": "E17N55",
"type": "tower",
"user": "57874d42d0ae911e3bd15bbc",
"x": 9,
"y": 19
});
let mut obj = StructureTower::deserialize(json).unwrap();
assert_eq!(
obj,
StructureTower {
room: RoomName::new("E17N55").unwrap(),
x: 9,
y: 19,
id: "57f1cc9d27e2c0520e93ba95".to_owned(),
energy: 920,
energy_capacity: 1000,
hits: 3000,
hits_max: 3000,
notify_when_attacked: true,
disabled: false,
action_log: StructureTowerActions {
attack: None,
heal: None,
repair: None,
},
user: "57874d42d0ae911e3bd15bbc".to_owned(),
}
);
obj.update(
serde_json::from_value(json!({
"actionLog": {
"attack": {
"x": 10,
"y": 10
}
},
"energy": 820
}))
.unwrap(),
);
assert_eq!(
obj.action_log,
StructureTowerActions {
attack: Some(ActionLogTarget { x: 10, y: 10 }),
heal: None,
repair: None,
}
);
}
}