ed_journals/modules/status/models/
status.rs

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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::modules::status::models::destination_status::DestinationStatus;
use crate::modules::status::models::flags2::Flags2;
use crate::modules::status::models::fuel_status::FuelStatus;
use crate::modules::status::models::gui_focus::GuiFocus;
use crate::modules::status::models::legal_status::LegalStatus;
use crate::modules::status::models::planet_status::PlanetStatus;
use crate::status::Flags;

/// Struct representing the live status of the player. Sometimes the file does exist, but might
/// not contain any data (for example when just starting the game.)
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Status {
    /// The timestamp when the status was updated.
    #[serde(rename = "timestamp")]
    pub timestamp: DateTime<Utc>,

    /// The event of the status. This is always set to 'Status'.
    #[serde(rename = "event")]
    pub event: String,

    /// In some cases the status file might not contain any data.
    #[serde(flatten)]
    pub contents: Option<StatusContents>,
}

/// The actual contents of the status file, containing flags for the different states part of the
/// ship can be in and might also contain information about the planet the player is currently close
/// to.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct StatusContents {
    /// The current flags for the player. These flags are mostly for things that are related to
    /// the player's ship.
    pub flags: Flags,

    /// A second flags field which includes flags for the on-foot status of the player.
    pub flags2: Flags2,

    /// The current legal state of the player, indicating whether they are currently at risk of
    /// committing a crime.
    pub legal_state: LegalStatus,

    /// The current credit balance of the player.
    pub balance: u64,

    /// Information about the planet the player is currently close to.
    #[serde(flatten)]
    pub planet_status: Option<PlanetStatus>,

    /// Depending on the current state of the game, the status file includes information about
    /// either the ship of the player or information about the player when they're on-foot.
    #[serde(flatten)]
    pub kind: StatusKind,
}

/// The different sets of additional fields which are dependent on the current state of the player.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(untagged)]
pub enum StatusKind {
    /// Variant containing the fields for when the player is piloting a ship.
    Ship(ShipStatus),

    /// Variant containing the fields for when the player is on-foot.
    OnFoot(OnFootStatus),
}

impl StatusKind {
    /// Whether the current status kind is a ship status.
    pub fn is_ship_status(&self) -> bool {
        matches!(self, StatusKind::Ship(_))
    }

    /// Whether the current status kind is an on-foot status.
    pub fn is_on_foot_status(&self) -> bool {
        matches!(self, StatusKind::OnFoot(_))
    }

    /// Returns the ship status if it is set and returns None otherwise.
    pub fn ship_status(&self) -> Option<&ShipStatus> {
        match self {
            StatusKind::Ship(ship_status) => Some(ship_status),
            _ => None,
        }
    }

    /// Returns the on-foot status if it is set and returns None otherwise.
    pub fn on_foot_status(&self) -> Option<&OnFootStatus> {
        match self {
            StatusKind::OnFoot(on_foot_status) => Some(on_foot_status),
            _ => None,
        }
    }
}

/// This model contains the fields which are included when the player is piloting a ship.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct ShipStatus {
    /// The current status of the pips of the ship.
    pub pips: [u8; 3],

    /// The number of the fire-group which is currently selected.
    pub fire_group: u8,

    /// Which GUI currently has focus in the ship.
    pub gui_focus: GuiFocus,

    /// Information about the fuel of the current ship.
    pub fuel: FuelStatus,

    /// The number of tonnes of cargo the ship has on board.
    pub cargo: f32,

    /// Information about the currently targeted destination.
    pub destination: Option<DestinationStatus>,
}

impl ShipStatus {
    /// Returns the current number of pips that are set for the system category.
    pub fn system_pips(&self) -> u8 {
        self.pips[0]
    }

    /// Returns the current number of pips that are set for the engine category.
    pub fn engine_pips(&self) -> u8 {
        self.pips[1]
    }

    /// Returns the current number of pips that are set for the weapon category.
    pub fn weapon_pips(&self) -> u8 {
        self.pips[2]
    }
}

/// This model contains the fields which are included when the player is on-foot.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct OnFootStatus {
    /// The percentage of oxygen the player currently has left.
    pub oxygen: f32,

    /// The percentage of health the player currently has left.
    pub health: f32,

    /// The current temperature of the player.
    pub temperature: f32,

    /// The name of the weapon the player currently has selected.
    // TODO replace with enum
    pub selected_weapon: String,
    pub selected_weapon_localized: Option<String>,
}