esf-dogma-engine 10.4.0

Calculates the dogma attributes of EVE Online ship fits
Documentation
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! The fit to calculate.

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Deserializer, Serialize};

/// A ship, what is fitted to it, and the character flying it.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Fit {
    /// Only for display; the calculation does not use it.
    pub name: Option<String>,
    /// The ship.
    pub ship: Ship,
    /// Modules, drones, fighters, implants, boosters and cargo.
    pub items: Vec<FitItem>,
    /// The character flying the ship.
    #[serde(default)]
    pub character: Character,
    /// Where the ship is.
    #[serde(default)]
    pub environment: Environment,
}

/// The ship of a fit.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Ship {
    /// The type id of the ship.
    pub type_id: i32,
    /// The type id of the active mode, for ships that have modes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mode: Option<i32>,
}

/// A module, drone, fighter squadron, implant, booster or item in cargo.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FitItem {
    /// The type id of the item.
    pub type_id: i32,
    /// Where the item is.
    pub slot: Slot,
    /// 1 for modules. Stack size for drones, fighters and cargo; for fighters
    /// in a tube, the size of the squadron.
    #[serde(default = "one")]
    pub quantity: u32,
    /// The state asked for; the calculation lowers it when the item cannot
    /// reach it.
    pub state: State,
    /// The charge loaded in the module, if any.
    pub charge: Option<Charge>,
    /// Only for mutated modules and drones.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mutation: Option<Mutation>,
    /// Only for fighters: the abilities used, by effect id. `None` uses the
    /// abilities the fighter uses by default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fighter_abilities: Option<BTreeSet<i32>>,
    /// Only for boosters: the side effects rolled, by effect id. Empty means
    /// none.
    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
    pub booster_side_effects: BTreeSet<i32>,
    /// How far a module whose bonus grows every cycle has spooled.
    /// Only per-second stats use it; volley is always unspooled.
    /// `None` is fully spooled.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub spool: Option<Spool>,
}

/// Where an item is. The number is the position in its rack, starting at 0;
/// for implants and boosters, the slot as EVE numbers it, starting at 1.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[serde(tag = "type", content = "index", rename_all = "snake_case")]
pub enum Slot {
    /// A high slot.
    High(u8),
    /// A medium slot.
    Medium(u8),
    /// A low slot.
    Low(u8),
    /// A rig slot.
    Rig(u8),
    /// A subsystem slot.
    Subsystem(u8),
    /// A service slot, on a structure.
    Service(u8),
    /// A fighter tube; a squadron that is not offline is in space.
    FighterTube(u8),
    /// The fighter bay; nothing in it is in space.
    FighterBay,
    /// An implant; the number is its `implantness`.
    Implant(u8),
    /// A booster; the number is its `boosterness`.
    Booster(u16),
    /// The drone bay; a drone that is not offline is in space.
    DroneBay,
    /// The cargo hold; nothing in it is calculated.
    Cargo,
}

/// The state of an item, lowest first.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum State {
    /// Only passive effects apply.
    Offline,
    /// Online effects apply too.
    Online,
    /// Active effects apply too.
    Active,
    /// Overload effects apply too.
    Overload,
}

/// A charge loaded in a module.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Charge {
    /// The type id of the charge.
    pub type_id: i32,
}

/// How a module or drone was mutated.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mutation {
    /// The type id of the item before it was mutated.
    pub base: i32,
    /// The rolled value of each mutated attribute, by attribute id.
    #[serde(default, deserialize_with = "id_map")]
    pub attributes: BTreeMap<i32, f64>,
}

/// How far a module has spooled.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Spool {
    /// The bonus reached so far: 0.0 is unspooled, 2.125 is +212.5%.
    MultiplierBonus(f64),
}

/// The character flying the ship.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Character {
    /// The level of each skill, by type id. A skill that is not listed is not
    /// trained, and gives no bonus.
    #[serde(default, deserialize_with = "id_map")]
    pub skills: BTreeMap<i32, u8>,
    /// -10.0 to 5.0. Only a few ships have a bonus that scales with it.
    #[serde(default)]
    pub security_status: f64,
}

/// Where the ship is.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Environment {
    /// The damage the ship is assumed to take, for effective hitpoints.
    #[serde(default)]
    pub damage_profile: DamageProfile,
    /// The security of the solar system.
    #[serde(default)]
    pub security: Security,
}

/// How incoming damage is split over the four damage types. Only the ratio
/// matters; the calculation scales the four to add up to one.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
pub struct DamageProfile {
    /// EM damage.
    #[serde(default)]
    pub em: f64,
    /// Explosive damage.
    #[serde(default)]
    pub explosive: f64,
    /// Kinetic damage.
    #[serde(default)]
    pub kinetic: f64,
    /// Thermal damage.
    #[serde(default)]
    pub thermal: f64,
}

impl Default for DamageProfile {
    fn default() -> Self {
        Self {
            em: 0.25,
            explosive: 0.25,
            kinetic: 0.25,
            thermal: 0.25,
        }
    }
}

/// The security of a solar system.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Security {
    /// High-sec.
    #[default]
    HighSec,
    /// Low-sec.
    LowSec,
    /// Null-sec.
    NullSec,
    /// Wormhole space.
    Wormhole,
}

fn one() -> u32 {
    1
}

/* JSON and JavaScript objects only have string keys. */
fn id_map<'de, D, V>(deserializer: D) -> Result<BTreeMap<i32, V>, D::Error>
where
    D: Deserializer<'de>,
    V: Deserialize<'de>,
{
    BTreeMap::<String, V>::deserialize(deserializer)?
        .into_iter()
        .map(|(key, value)| {
            let key = key.parse().map_err(|_| {
                serde::de::Error::custom(format!("expected an identifier, found {key:?}"))
            })?;
            Ok((key, value))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn minimal_fit_fills_defaults() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 587},
                "items": [
                    {"type_id": 2873, "slot": {"type": "high", "index": 0}, "state": "active"},
                    {"type_id": 2488, "slot": {"type": "drone_bay"}, "quantity": 5, "state": "active"}
                ],
                "character": {"skills": {"3300": 5}}
            }"#,
        )
        .unwrap();

        assert_eq!(fit.ship.mode, None);
        assert_eq!(fit.items[0].slot, Slot::High(0));
        assert_eq!(fit.items[0].quantity, 1);
        assert_eq!(fit.items[0].fighter_abilities, None);
        assert_eq!(fit.items[0].spool, None);
        assert_eq!(fit.items[1].slot, Slot::DroneBay);
        assert_eq!(fit.items[1].quantity, 5);
        assert_eq!(fit.character.skills[&3300], 5);
        assert_eq!(fit.character.security_status, 0.0);
        assert_eq!(fit.environment.security, Security::HighSec);
        assert_eq!(fit.environment.damage_profile, DamageProfile::default());
    }

    #[test]
    fn reads_damage_profile() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 587},
                "items": [],
                "environment": {"damage_profile": {"kinetic": 3, "thermal": 1}}
            }"#,
        )
        .unwrap();

        assert_eq!(
            fit.environment.damage_profile,
            DamageProfile {
                em: 0.0,
                explosive: 0.0,
                kinetic: 3.0,
                thermal: 1.0,
            }
        );
        assert_eq!(fit.environment.security, Security::HighSec);
    }

    #[test]
    fn reads_security() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 35832},
                "items": [],
                "environment": {"security": "null_sec"}
            }"#,
        )
        .unwrap();

        assert_eq!(fit.environment.security, Security::NullSec);
    }

    #[test]
    fn reads_fighters() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 23911},
                "items": [
                    {"type_id": 40556, "slot": {"type": "fighter_tube", "index": 1}, "quantity": 6, "state": "active", "fighter_abilities": [6465, 6431]},
                    {"type_id": 40556, "slot": {"type": "fighter_bay"}, "quantity": 3, "state": "offline", "fighter_abilities": []}
                ]
            }"#,
        )
        .unwrap();

        assert_eq!(fit.items[0].slot, Slot::FighterTube(1));
        assert_eq!(
            fit.items[0].fighter_abilities,
            Some(BTreeSet::from([6431, 6465]))
        );
        assert_eq!(fit.items[1].slot, Slot::FighterBay);
        assert_eq!(fit.items[1].fighter_abilities, Some(BTreeSet::new()));
    }

    #[test]
    fn reads_implants_and_boosters() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 587},
                "items": [
                    {"type_id": 20499, "slot": {"type": "implant", "index": 1}, "state": "online"},
                    {"type_id": 9950, "slot": {"type": "booster", "index": 1}, "state": "online", "booster_side_effects": [2745, 2737]},
                    {"type_id": 57285, "slot": {"type": "booster", "index": 504}, "state": "online"}
                ]
            }"#,
        )
        .unwrap();

        assert_eq!(fit.items[0].slot, Slot::Implant(1));
        assert_eq!(fit.items[1].slot, Slot::Booster(1));
        assert_eq!(
            fit.items[1].booster_side_effects,
            BTreeSet::from([2737, 2745])
        );
        assert_eq!(fit.items[2].slot, Slot::Booster(504));
        assert!(fit.items[2].booster_side_effects.is_empty());
    }

    #[test]
    fn reads_mutation() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 587},
                "items": [
                    {"type_id": 47732, "slot": {"type": "medium", "index": 0}, "state": "active", "mutation": {"base": 448, "attributes": {"6": 7.5, "54": 10500}}}
                ]
            }"#,
        )
        .unwrap();

        let mutation = fit.items[0].mutation.as_ref().unwrap();
        assert_eq!(mutation.base, 448);
        assert_eq!(
            mutation.attributes,
            BTreeMap::from([(6, 7.5), (54, 10500.0)])
        );
    }

    #[test]
    fn reads_spool() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 52250},
                "items": [
                    {"type_id": 47914, "slot": {"type": "high", "index": 0}, "state": "active", "spool": {"multiplier_bonus": 0.7}}
                ]
            }"#,
        )
        .unwrap();

        assert_eq!(fit.items[0].spool, Some(Spool::MultiplierBonus(0.7)));
    }

    #[test]
    fn reads_security_status() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 44995},
                "items": [],
                "character": {"security_status": -2.5}
            }"#,
        )
        .unwrap();

        assert_eq!(fit.character.security_status, -2.5);
        assert!(fit.character.skills.is_empty());
    }

    #[test]
    fn reads_mode() {
        let fit: Fit = serde_json::from_str(
            r#"{
                "ship": {"type_id": 34317, "mode": 34319},
                "items": []
            }"#,
        )
        .unwrap();

        assert_eq!(fit.ship.type_id, 34317);
        assert_eq!(fit.ship.mode, Some(34319));
    }

    #[test]
    fn round_trips_through_json() {
        let fit = Fit {
            name: Some("Rifter".to_string()),
            ship: Ship {
                type_id: 587,
                mode: None,
            },
            items: vec![FitItem {
                type_id: 47408,
                slot: Slot::Medium(2),
                quantity: 1,
                state: State::Overload,
                charge: None,
                mutation: None,
                fighter_abilities: None,
                booster_side_effects: BTreeSet::new(),
                spool: None,
            }],
            character: Character::default(),
            environment: Environment::default(),
        };

        let json = serde_json::to_string(&fit).unwrap();
        let parsed: Fit = serde_json::from_str(&json).unwrap();

        assert!(!json.contains("fighter_abilities"));
        assert!(!json.contains("booster_side_effects"));
        assert!(!json.contains("spool"));
        assert!(!json.contains("mutation"));
        assert!(!json.contains("mode"));
        assert_eq!(parsed.items[0].slot, Slot::Medium(2));
        assert_eq!(parsed.items[0].state, State::Overload);
    }
}