Skip to main content

ed_journals/modules/status/models/
status.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::modules::status::models::destination_status::DestinationStatus;
5use crate::modules::status::models::flags2::Flags2;
6use crate::modules::status::models::fuel_status::FuelStatus;
7use crate::modules::status::models::gui_focus::GuiFocus;
8use crate::modules::status::models::legal_status::LegalStatus;
9use crate::modules::status::models::planet_status::PlanetStatus;
10use crate::status::Flags;
11
12/// Struct representing the live status of the player. Sometimes the file does exist, but might
13/// not contain any data (for example when just starting the game.)
14#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
15#[serde(rename_all = "PascalCase")]
16pub struct Status {
17    /// The timestamp when the status was updated.
18    #[serde(rename = "timestamp")]
19    pub timestamp: DateTime<Utc>,
20
21    /// The event of the status. This is always set to 'Status'.
22    #[serde(rename = "event")]
23    pub event: String,
24
25    /// In some cases the status file might not contain any data.
26    #[serde(flatten)]
27    pub contents: Option<StatusContents>,
28}
29
30/// The actual contents of the status file, containing flags for the different states part of the
31/// ship can be in and might also contain information about the planet the player is currently close
32/// to.
33#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
34#[serde(rename_all = "PascalCase")]
35pub struct StatusContents {
36    /// The current flags for the player. These flags are mostly for things that are related to
37    /// the player's ship.
38    pub flags: Flags,
39
40    /// A second flags field which includes flags for the on-foot status of the player.
41    pub flags2: Flags2,
42
43    /// The current legal state of the player, indicating whether they are currently at risk of
44    /// committing a crime.
45    pub legal_state: LegalStatus,
46
47    /// The current credit balance of the player.
48    pub balance: u64,
49
50    /// Information about the planet the player is currently close to.
51    #[serde(flatten)]
52    pub planet_status: Option<PlanetStatus>,
53
54    /// Depending on the current state of the game, the status file includes information about
55    /// either the ship of the player or information about the player when they're on-foot.
56    #[serde(flatten)]
57    pub kind: StatusKind,
58}
59
60/// The different sets of additional fields which are dependent on the current state of the player.
61#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
62#[serde(untagged)]
63pub enum StatusKind {
64    /// Variant containing the fields for when the player is piloting a ship.
65    Ship(ShipStatus),
66
67    /// Variant containing the fields for when the player is on-foot.
68    OnFoot(OnFootStatus),
69}
70
71impl StatusKind {
72    /// Whether the current status kind is a ship status.
73    pub fn is_ship_status(&self) -> bool {
74        matches!(self, StatusKind::Ship(_))
75    }
76
77    /// Whether the current status kind is an on-foot status.
78    pub fn is_on_foot_status(&self) -> bool {
79        matches!(self, StatusKind::OnFoot(_))
80    }
81
82    /// Returns the ship status if it is set and returns None otherwise.
83    pub fn ship_status(&self) -> Option<&ShipStatus> {
84        match self {
85            StatusKind::Ship(ship_status) => Some(ship_status),
86            _ => None,
87        }
88    }
89
90    /// Returns the on-foot status if it is set and returns None otherwise.
91    pub fn on_foot_status(&self) -> Option<&OnFootStatus> {
92        match self {
93            StatusKind::OnFoot(on_foot_status) => Some(on_foot_status),
94            _ => None,
95        }
96    }
97}
98
99/// This model contains the fields which are included when the player is piloting a ship.
100#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
101#[serde(rename_all = "PascalCase")]
102pub struct ShipStatus {
103    /// The current status of the pips of the ship.
104    pub pips: [u8; 3],
105
106    /// The number of the fire-group which is currently selected.
107    pub fire_group: u8,
108
109    /// Which GUI currently has focus in the ship.
110    pub gui_focus: GuiFocus,
111
112    /// Information about the fuel of the current ship.
113    pub fuel: FuelStatus,
114
115    /// The number of tonnes of cargo the ship has on board.
116    pub cargo: f32,
117
118    /// Information about the currently targeted destination.
119    pub destination: Option<DestinationStatus>,
120}
121
122impl ShipStatus {
123    /// Returns the current number of pips that are set for the system category.
124    pub fn system_pips(&self) -> u8 {
125        self.pips[0]
126    }
127
128    /// Returns the current number of pips that are set for the engine category.
129    pub fn engine_pips(&self) -> u8 {
130        self.pips[1]
131    }
132
133    /// Returns the current number of pips that are set for the weapon category.
134    pub fn weapon_pips(&self) -> u8 {
135        self.pips[2]
136    }
137}
138
139/// This model contains the fields which are included when the player is on-foot.
140#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
141#[serde(rename_all = "PascalCase")]
142pub struct OnFootStatus {
143    /// The percentage of oxygen the player currently has left.
144    pub oxygen: f32,
145
146    /// The percentage of health the player currently has left.
147    pub health: f32,
148
149    /// The current temperature of the player.
150    pub temperature: f32,
151
152    /// The name of the weapon the player currently has selected.
153    // TODO replace with enum
154    pub selected_weapon: String,
155    pub selected_weapon_localized: Option<String>,
156}