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
//! `StructureLink` data description.
use super::ActionLogTarget;
use crate::data::RoomName;

with_structure_fields_and_update_struct! {
    /// A link structure - a structure that can be filled with energy, then instantly send energy to other links
    /// in the same room.
    #[derive(Clone, Debug, PartialEq)]
    #[serde(rename_all = "camelCase")]
    pub struct StructureLink {
        /// The user ID of the owner of this structure.
        pub user: String,
        /// Whether or not this structure is non-functional due to a degraded controller.
        #[serde(default, rename = "off")]
        pub disabled: bool,
        /// The current amount of energy held in this structure.
        pub energy: i32,
        /// The maximum amount of energy that can be held in this structure.
        pub energy_capacity: i32,
        /// The number of ticks till this link can be used to send energy again.
        pub cooldown: i32,
        /// A record of all actions this structure performed last tick.
        pub action_log: StructureLinkActions,
        /// Whether or not an attack on this structure will send an email to the owner automatically.
        pub notify_when_attacked: bool,
    }

    /// The update structure for a `StructureLink`.
    #[derive(Clone, Debug)]
    #[serde(rename_all = "camelCase")]
    pub struct StructureLinkUpdate {
        - user: String,
        #[serde(rename = "off")]
        - disabled: bool,
        - energy: i32,
        - energy_capacity: i32,
        - cooldown: i32,
        - action_log: StructureLinkActions,
        - notify_when_attacked: bool,
    }
}

with_update_struct! {
    /// A struct describing a link's actions.
    #[derive(serde_derive::Deserialize, Clone, Debug, PartialEq)]
    #[serde(rename_all = "camelCase")]
    pub struct StructureLinkActions {
        /// The x,y position the link last transfered energy to.
        pub transfer_energy: Option<ActionLogTarget>,
    }

    /// The update structure for StructureLinkActions.
    #[derive(serde_derive::Deserialize, Clone, Debug)]
    #[serde(rename_all = "camelCase")]
    pub struct StructureLinkActionsUpdate { ... }
}

#[cfg(test)]
mod test {
    use serde::Deserialize;
    use serde_json;

    use crate::data::RoomName;

    use super::{ActionLogTarget, StructureLink, StructureLinkActions};

    #[test]
    fn parse_link_and_updates() {
        let json = json!({
            "_id": "57fdb3ea3dad49a17265ecea",
            "actionLog": {
                "transferEnergy": null
            },
            "cooldown": 3,
            "energy": 100,
            "energyCapacity": 800,
            "hits": 1000,
            "hitsMax": 1000,
            "notifyWhenAttacked": true,
            "room": "E17N55",
            "type": "link",
            "user": "57874d42d0ae911e3bd15bbc",
            "x": 9,
            "y": 6
        });

        let mut obj = StructureLink::deserialize(json).unwrap();

        assert_eq!(
            obj,
            StructureLink {
                room: RoomName::new("E17N55").unwrap(),
                x: 9,
                y: 6,
                id: "57fdb3ea3dad49a17265ecea".to_owned(),
                energy: 100,
                energy_capacity: 800,
                hits: 1000,
                hits_max: 1000,
                notify_when_attacked: true,
                disabled: false,
                cooldown: 3,
                action_log: StructureLinkActions {
                    transfer_energy: None,
                },
                user: "57874d42d0ae911e3bd15bbc".to_owned(),
            }
        );

        obj.update(
            serde_json::from_value(json!({
                "cooldown": 2
            }))
            .unwrap(),
        );

        obj.update(
            serde_json::from_value(json!({
                "cooldown": 1
            }))
            .unwrap(),
        );

        obj.update(
            serde_json::from_value(json!({
                "cooldown": 0
            }))
            .unwrap(),
        );

        assert_eq!(obj.cooldown, 0);

        obj.update(
            serde_json::from_value(json!({
                "actionLog": {
                    "transferEnergy": {
                        "x": 9,
                        "y": 18
                    }
                },
                "cooldown": 11,
                "energy": 0
            }))
            .unwrap(),
        );

        assert_eq!(
            obj.action_log,
            StructureLinkActions {
                transfer_energy: Some(ActionLogTarget { x: 9, y: 18 }),
            }
        );

        obj.update(
            serde_json::from_value(json!({
                "actionLog": {
                    "transferEnergy": null
                },
                "cooldown": 10,
                "energy": 50
            }))
            .unwrap(),
        );

        assert_eq!(
            obj,
            StructureLink {
                room: RoomName::new("E17N55").unwrap(),
                x: 9,
                y: 6,
                id: "57fdb3ea3dad49a17265ecea".to_owned(),
                energy: 50,
                energy_capacity: 800,
                hits: 1000,
                hits_max: 1000,
                notify_when_attacked: true,
                disabled: false,
                cooldown: 10,
                action_log: StructureLinkActions {
                    transfer_energy: None,
                },
                user: "57874d42d0ae911e3bd15bbc".to_owned(),
            }
        );
    }
}