f1_api/packet/lap.rs
1//! Data about all the cars in the session and their lap times
2//!
3//! The F1 games publish data about each car in a session and their lap times. The frequency with
4//! which the packets are sent can be configured in the game. F1 2018 and F1 2019 share the same
5//! packet format.
6
7use std::time::Duration;
8
9use derive_new::new;
10use getset::{CopyGetters, Getters};
11
12use crate::packet::header::Header;
13
14/// Statuses a driver can have during a lap
15#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
16pub enum DriverStatus {
17 /// The driver is still in the garage, and has not left it yet.
18 InGarage,
19
20 /// The driver is on a flying lap, and cars that are on an in- or out-lap have to give room.
21 FlyingLap,
22
23 /// The driver is on an in-lap, i.e. on the way to the pits. The in-lap is often used to cool
24 /// the car down, so the driver might be going slower than normal.
25 InLap,
26
27 /// The driver is on an out-lap. The out-lap is used to get heat into the tires and breaks to
28 /// optimize them for the following flying lap.
29 OutLap,
30
31 /// The driver is on track, but not on a special lap. This is the case during a race or practice
32 /// session, where drivers do many laps in a row.
33 OnTrack,
34}
35
36impl Default for DriverStatus {
37 fn default() -> Self {
38 DriverStatus::InGarage
39 }
40}
41
42/// Statuses used to signal the progression of a pit stop
43#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
44pub enum PitStatus {
45 /// No pit stop is being performed, and the car is most likely on track or in the garage.
46 None,
47
48 /// The car is pitting, which means it is on the pit lane but not stationary in the pit box.
49 Pitting,
50
51 /// The car is stationary in the pit box, and the pit stop is being performed.
52 InPits,
53}
54
55impl Default for PitStatus {
56 fn default() -> Self {
57 PitStatus::None
58 }
59}
60
61/// Statuses that classify the result
62#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
63pub enum ResultStatus {
64 /// The results are invalid.
65 Invalid,
66
67 /// The results are not being collected yet.
68 Inactive,
69
70 /// The results are being actively collected.
71 Active,
72
73 /// The session has finished and the results are final.
74 Finished,
75
76 /// The car has been disqualified from the session.
77 Disqualified,
78
79 /// The car failed to classify in the session, for example because it did not achieve the
80 /// required number of laps.
81 NotClassified,
82
83 /// The car has been retired.
84 Retired,
85}
86
87impl Default for ResultStatus {
88 fn default() -> Self {
89 ResultStatus::Invalid
90 }
91}
92
93/// The three sectors of a race track in F1
94#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
95pub enum Sector {
96 /// The first sector
97 First,
98
99 /// The second sector
100 Second,
101
102 /// The third sector
103 Third,
104}
105
106impl Default for Sector {
107 fn default() -> Self {
108 Sector::First
109 }
110}
111
112/// Data about a car and its lap times
113///
114/// For each car in the session, a set of lap data is published. It contains data on the current
115/// lap, e.g. the current lap time and the sector the car is currently in, but also the time of the
116/// last and best lap.
117#[derive(new, Debug, Getters, CopyGetters, PartialEq, Copy, Clone, PartialOrd, Default)]
118#[allow(clippy::too_many_arguments)]
119pub struct Lap {
120 /// Returns the time of the last lap.
121 #[getset(get = "pub")]
122 last_lap_time: Duration,
123
124 /// Returns the time of the current lap.
125 #[getset(get = "pub")]
126 current_lap_time: Duration,
127
128 /// Returns the time of the best lap.
129 #[getset(get = "pub")]
130 best_lap_time: Duration,
131
132 /// Returns the time spent in sector 1 during the current lap.
133 #[getset(get = "pub")]
134 sector1_time: Duration,
135
136 /// Returns the time spent in sector 2 during the current lap.
137 #[getset(get = "pub")]
138 sector2_time: Duration,
139
140 /// Returns the distance the car has travelled in the current lap in meters.
141 #[getset(get_copy = "pub")]
142 lap_distance: f32,
143
144 /// Returns the total distance the car has travelled in the session in meters.
145 #[getset(get_copy = "pub")]
146 total_distance: f32,
147
148 /// Returns the delta during a safety car in seconds.
149 #[getset(get = "pub")]
150 safety_car_delta: Duration,
151
152 /// Returns a car's position in the race.
153 #[getset(get_copy = "pub")]
154 position: u8,
155
156 /// Returns the number of the current lap.
157 #[getset(get_copy = "pub")]
158 current_lap_number: u8,
159
160 /// Returns a car's pit stop status.
161 #[getset(get_copy = "pub")]
162 pit_status: PitStatus,
163
164 /// Returns the sector the car is currently in.
165 #[getset(get_copy = "pub")]
166 sector: Sector,
167
168 /// Returns whether the current lap is valid.
169 ///
170 /// The F1 games apply different rules to determine if a lap is valid. Cutting the track, losing
171 /// control, or hitting objects or opponents can all invalidate a lap. This is crucial for
172 /// qualifying, where invalid laps might not count for the results.
173 #[getset(get_copy = "pub")]
174 is_valid_lap: bool,
175
176 /// Returns the accumulated penalties for a car in seconds.
177 #[getset(get_copy = "pub")]
178 penalties: u8,
179
180 /// Returns the grid position the car started the race in.
181 #[getset(get_copy = "pub")]
182 grid_position: u8,
183
184 /// Returns the status of the driver.
185 #[getset(get_copy = "pub")]
186 driver_status: DriverStatus,
187
188 /// Returns the status of the race results.
189 #[getset(get_copy = "pub")]
190 result_status: ResultStatus,
191}
192
193/// Packet containing lap data for all 20 cars in a session
194///
195/// The F1 games publish a lap packet that contains data on all 20 cars in a session. The packet is
196/// sent at a fixed interval that can be configured in the game.
197#[derive(new, Debug, Getters, PartialEq, Clone, PartialOrd)]
198pub struct LapPacket {
199 /// Returns the packet header prefixing the lap data packet.
200 #[getset(get = "pub")]
201 header: Header,
202
203 /// Returns the laps for all 20 cars in a session.
204 #[getset(get = "pub")]
205 laps: Vec<Lap>,
206}