fbsim_core/game/
stat.rs

1#![doc = include_str!("../../docs/game/stat.md")]
2#[cfg(feature = "rocket_okapi")]
3use rocket_okapi::okapi::schemars;
4#[cfg(feature = "rocket_okapi")]
5use rocket_okapi::okapi::schemars::JsonSchema;
6use serde::{Serialize, Deserialize};
7
8/// # `RushingStats` struct
9///
10/// A `RushingStats` represents aggregated rushing statistics
11#[cfg_attr(feature = "rocket_okapi", derive(JsonSchema))]
12#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default, Serialize, Deserialize)]
13pub struct RushingStats {
14    rushes: u32,
15    fumbles: u32,
16    touchdowns: u32,
17    yards: i32
18}
19
20impl RushingStats {
21    /// Constructor for the RushingStats struct
22    ///
23    /// ### Example
24    /// ```
25    /// use fbsim_core::game::stat::RushingStats;
26    ///
27    /// let my_stats = RushingStats::new();
28    /// ```
29    pub fn new() -> RushingStats {
30        RushingStats::default()
31    }
32
33    /// Get the number of rushes from the RushingStats struct
34    ///
35    /// ### Example
36    /// ```
37    /// use fbsim_core::game::stat::RushingStats;
38    ///
39    /// let my_stats = RushingStats::new();
40    /// let rushes = my_stats.rushes();
41    /// assert!(rushes == 0);
42    /// ```
43    pub fn rushes(&self) -> u32 {
44        self.rushes
45    }
46
47    /// Increment the rushes in the RushingStats struct
48    ///
49    /// ### Example
50    /// ```
51    /// use fbsim_core::game::stat::RushingStats;
52    ///
53    /// let mut my_stats = RushingStats::new();
54    /// my_stats.increment_rushes();
55    /// assert!(my_stats.rushes() == 1);
56    /// ```
57    pub fn increment_rushes(&mut self) {
58        self.rushes += 1;
59    }
60
61    /// Get the number of fumbles from the RushingStats struct
62    ///
63    /// ### Example
64    /// ```
65    /// use fbsim_core::game::stat::RushingStats;
66    ///
67    /// let my_stats = RushingStats::new();
68    /// let fumbles = my_stats.fumbles();
69    /// assert!(fumbles == 0);
70    /// ```
71    pub fn fumbles(&self) -> u32 {
72        self.fumbles
73    }
74
75    /// Increment the fumbles in the RushingStats struct
76    ///
77    /// ### Example
78    /// ```
79    /// use fbsim_core::game::stat::RushingStats;
80    ///
81    /// let mut my_stats = RushingStats::new();
82    /// my_stats.increment_fumbles();
83    /// assert!(my_stats.fumbles() == 1);
84    /// ```
85    pub fn increment_fumbles(&mut self) {
86        self.fumbles += 1;
87    }
88
89    /// Get the number of touchdowns in the RushingStats struct
90    ///
91    /// ### Example
92    /// ```
93    /// use fbsim_core::game::stat::RushingStats;
94    ///
95    /// let my_stats = RushingStats::new();
96    /// let touchdowns = my_stats.touchdowns();
97    /// assert!(touchdowns == 0);
98    /// ```
99    pub fn touchdowns(&self) -> u32 {
100        self.touchdowns
101    }
102
103    /// Increment the touchdowns in the RushingStats struct
104    ///
105    /// ### Example
106    /// ```
107    /// use fbsim_core::game::stat::RushingStats;
108    ///
109    /// let mut my_stats = RushingStats::new();
110    /// my_stats.increment_touchdowns();
111    /// assert!(my_stats.touchdowns() == 1);
112    /// ```
113    pub fn increment_touchdowns(&mut self) {
114        self.touchdowns += 1;
115    }
116
117    /// Get the rushing yards in the RushingStats struct
118    ///
119    /// ### Example
120    /// ```
121    /// use fbsim_core::game::stat::RushingStats;
122    ///
123    /// let my_stats = RushingStats::new();
124    /// let yards = my_stats.yards();
125    /// assert!(yards == 0);
126    /// ```
127    pub fn yards(&self) -> i32 {
128        self.yards
129    }
130
131    /// Increment the rushing yards in the RushingStats struct
132    ///
133    /// ### Example
134    /// ```
135    /// use fbsim_core::game::stat::RushingStats;
136    ///
137    /// let mut my_stats = RushingStats::new();
138    /// my_stats.increment_yards(12);
139    /// assert!(my_stats.yards() == 12);
140    /// ```
141    pub fn increment_yards(&mut self, yards: i32) {
142        self.yards += yards;
143    }
144}
145
146impl std::fmt::Display for RushingStats {
147    /// Display rushing stats as a human readable string
148    ///
149    /// ### Example
150    /// ```
151    /// use fbsim_core::game::stat::RushingStats;
152    /// 
153    /// let my_stats = RushingStats::new();
154    /// println!("{}", my_stats);
155    /// ```
156    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
157        let mut rushing_str = format!(
158            "{} rush, {} yards",
159            self.rushes,
160            self.yards
161        );
162        if self.touchdowns > 0 {
163            rushing_str = format!(
164                "{}, {} TD",
165                rushing_str,
166                self.touchdowns
167            );
168        }
169        if self.fumbles > 0 {
170            rushing_str = format!(
171                "{}, {} FUM",
172                rushing_str,
173                self.fumbles
174            );
175        }
176        f.write_str(&rushing_str)
177    }
178}
179
180/// # `PassingStats` struct
181///
182/// A `PassingStats` represents aggregated passing statistics
183#[cfg_attr(feature = "rocket_okapi", derive(JsonSchema))]
184#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default, Serialize, Deserialize)]
185pub struct PassingStats {
186    attempts: u32,
187    completions: u32,
188    touchdowns: u32,
189    interceptions: u32,
190    yards: i32
191}
192
193impl PassingStats {
194    /// Constructor for the PassingStats struct
195    ///
196    /// ### Example
197    /// ```
198    /// use fbsim_core::game::stat::PassingStats;
199    ///
200    /// let my_stats = PassingStats::new();
201    /// ```
202    pub fn new() -> PassingStats {
203        PassingStats::default()
204    }
205
206    /// Get the pass attempts from the PassingStats
207    ///
208    /// ### Example
209    /// ```
210    /// use fbsim_core::game::stat::PassingStats;
211    ///
212    /// let my_stats = PassingStats::new();
213    /// let attempts = my_stats.attempts();
214    /// assert!(attempts == 0);
215    /// ```
216    pub fn attempts(&self) -> u32 {
217        self.attempts
218    }
219
220    /// Increment the pass attempts in the PassingStats
221    ///
222    /// ### Example
223    /// ```
224    /// use fbsim_core::game::stat::PassingStats;
225    ///
226    /// let mut my_stats = PassingStats::new();
227    /// my_stats.increment_attempts();
228    /// assert!(my_stats.attempts() == 1);
229    /// ```
230    pub fn increment_attempts(&mut self) {
231        self.attempts += 1;
232    }
233
234    /// Get the completions from the PassingStats
235    ///
236    /// ### Example
237    /// ```
238    /// use fbsim_core::game::stat::PassingStats;
239    ///
240    /// let my_stats = PassingStats::new();
241    /// let completions = my_stats.completions();
242    /// assert!(completions == 0);
243    /// ```
244    pub fn completions(&self) -> u32 {
245        self.completions
246    }
247
248    /// Increment the completions in the PassingStats
249    ///
250    /// ### Example
251    /// ```
252    /// use fbsim_core::game::stat::PassingStats;
253    ///
254    /// let mut my_stats = PassingStats::new();
255    /// my_stats.increment_completions();
256    /// assert!(my_stats.completions() == 1);
257    /// ```
258    pub fn increment_completions(&mut self) {
259        self.completions += 1;
260    }
261
262    /// Get the touchdowns from the PassingStats
263    ///
264    /// ### Example
265    /// ```
266    /// use fbsim_core::game::stat::PassingStats;
267    ///
268    /// let my_stats = PassingStats::new();
269    /// let touchdowns = my_stats.touchdowns();
270    /// assert!(touchdowns == 0);
271    /// ```
272    pub fn touchdowns(&self) -> u32 {
273        self.touchdowns
274    }
275
276    /// Increment the touchdowns in the PassingStats
277    ///
278    /// ### Example
279    /// ```
280    /// use fbsim_core::game::stat::PassingStats;
281    ///
282    /// let mut my_stats = PassingStats::new();
283    /// my_stats.increment_touchdowns();
284    /// assert!(my_stats.touchdowns() == 1);
285    /// ```
286    pub fn increment_touchdowns(&mut self) {
287        self.touchdowns += 1;
288    }
289
290    /// Get the interceptions from the PassingStats
291    ///
292    /// ### Example
293    /// ```
294    /// use fbsim_core::game::stat::PassingStats;
295    ///
296    /// let my_stats = PassingStats::new();
297    /// let interceptions = my_stats.interceptions();
298    /// assert!(interceptions == 0);
299    /// ```
300    pub fn interceptions(&self) -> u32 {
301        self.interceptions
302    }
303
304    /// Increment the interceptions in the PassingStats
305    ///
306    /// ### Example
307    /// ```
308    /// use fbsim_core::game::stat::PassingStats;
309    ///
310    /// let mut my_stats = PassingStats::new();
311    /// my_stats.increment_interceptions();
312    /// assert!(my_stats.interceptions() == 1);
313    /// ```
314    pub fn increment_interceptions(&mut self) {
315        self.interceptions += 1;
316    }
317
318    /// Get the yards from the PassingStats
319    ///
320    /// ### Example
321    /// ```
322    /// use fbsim_core::game::stat::PassingStats;
323    ///
324    /// let my_stats = PassingStats::new();
325    /// let yards = my_stats.yards();
326    /// assert!(yards == 0);
327    /// ```
328    pub fn yards(&self) -> i32 {
329        self.yards
330    }
331
332    /// Increment the yards in the PassingStats
333    ///
334    /// ### Example
335    /// ```
336    /// use fbsim_core::game::stat::PassingStats;
337    ///
338    /// let mut my_stats = PassingStats::new();
339    /// my_stats.increment_yards(25);
340    /// assert!(my_stats.yards() == 25);
341    /// ```
342    pub fn increment_yards(&mut self, yards: i32) {
343        self.yards += yards;
344    }
345}
346
347impl std::fmt::Display for PassingStats {
348    /// Display passing stats as a human readable string
349    ///
350    /// ### Example
351    /// ```
352    /// use fbsim_core::game::stat::PassingStats;
353    /// 
354    /// let my_stats = PassingStats::new();
355    /// println!("{}", my_stats);
356    /// ```
357    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
358        let mut passing_str = format!(
359            "{}/{}, {} yards",
360            self.completions,
361            self.attempts,
362            self.yards
363        );
364        if self.touchdowns > 0 {
365            passing_str = format!(
366                "{}, {} TD",
367                passing_str,
368                self.touchdowns
369            );
370        }
371        if self.interceptions > 0 {
372            passing_str = format!(
373                "{}, {} INT",
374                passing_str,
375                self.interceptions
376            );
377        }
378        f.write_str(&passing_str)
379    }
380}
381
382/// # `ReceivingStats` struct
383///
384/// A `ReceivingStats` represents aggregated receiving statistics
385#[cfg_attr(feature = "rocket_okapi", derive(JsonSchema))]
386#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default, Serialize, Deserialize)]
387pub struct ReceivingStats {
388    targets: u32,
389    receptions: u32,
390    touchdowns: u32,
391    fumbles: u32,
392    yards: i32
393}
394
395impl ReceivingStats {
396    /// Constructor for the ReceivingStats struct
397    ///
398    /// ### Example
399    /// ```
400    /// use fbsim_core::game::stat::ReceivingStats;
401    ///
402    /// let my_stats = ReceivingStats::new();
403    /// ```
404    pub fn new() -> ReceivingStats {
405        ReceivingStats::default()
406    }
407
408    /// Get the receiving targets from the ReceivingStats
409    ///
410    /// ### Example
411    /// ```
412    /// use fbsim_core::game::stat::ReceivingStats;
413    ///
414    /// let my_stats = ReceivingStats::new();
415    /// let targets = my_stats.targets();
416    /// assert!(targets == 0);
417    /// ```
418    pub fn targets(&self) -> u32 {
419        self.targets
420    }
421
422    /// Increment the receiving targets in the ReceivingStats
423    ///
424    /// ### Example
425    /// ```
426    /// use fbsim_core::game::stat::ReceivingStats;
427    ///
428    /// let mut my_stats = ReceivingStats::new();
429    /// my_stats.increment_targets(1);
430    /// assert!(my_stats.targets() == 1);
431    /// ```
432    pub fn increment_targets(&mut self, targets: u32) {
433        self.targets += targets;
434    }
435
436    /// Get the receptions from the ReceivingStats
437    ///
438    /// ### Example
439    /// ```
440    /// use fbsim_core::game::stat::ReceivingStats;
441    ///
442    /// let my_stats = ReceivingStats::new();
443    /// let receptions = my_stats.receptions();
444    /// assert!(receptions == 0);
445    /// ```
446    pub fn receptions(&self) -> u32 {
447        self.receptions
448    }
449
450    /// Increment the receptions in the ReceivingStats
451    ///
452    /// ### Example
453    /// ```
454    /// use fbsim_core::game::stat::ReceivingStats;
455    ///
456    /// let mut my_stats = ReceivingStats::new();
457    /// my_stats.increment_receptions(1);
458    /// assert!(my_stats.receptions() == 1);
459    /// ```
460    pub fn increment_receptions(&mut self, receptions: u32) {
461        self.receptions += receptions;
462    }
463
464    /// Get the touchdowns from the ReceivingStats
465    ///
466    /// ### Example
467    /// ```
468    /// use fbsim_core::game::stat::ReceivingStats;
469    ///
470    /// let my_stats = ReceivingStats::new();
471    /// let touchdowns = my_stats.touchdowns();
472    /// assert!(touchdowns == 0);
473    /// ```
474    pub fn touchdowns(&self) -> u32 {
475        self.touchdowns
476    }
477
478    /// Increment the touchdowns in the ReceivingStats
479    ///
480    /// ### Example
481    /// ```
482    /// use fbsim_core::game::stat::ReceivingStats;
483    ///
484    /// let mut my_stats = ReceivingStats::new();
485    /// my_stats.increment_touchdowns(1);
486    /// assert!(my_stats.touchdowns() == 1);
487    /// ```
488    pub fn increment_touchdowns(&mut self, touchdowns: u32) {
489        self.touchdowns += touchdowns;
490    }
491
492    /// Get the fumbles from the ReceivingStats
493    ///
494    /// ### Example
495    /// ```
496    /// use fbsim_core::game::stat::ReceivingStats;
497    ///
498    /// let my_stats = ReceivingStats::new();
499    /// let fumbles = my_stats.fumbles();
500    /// assert!(fumbles == 0);
501    /// ```
502    pub fn fumbles(&self) -> u32 {
503        self.fumbles
504    }
505
506    /// Increment the fumbles in the ReceivingStats
507    ///
508    /// ### Example
509    /// ```
510    /// use fbsim_core::game::stat::ReceivingStats;
511    ///
512    /// let mut my_stats = ReceivingStats::new();
513    /// my_stats.increment_fumbles(1);
514    /// assert!(my_stats.fumbles() == 1);
515    /// ```
516    pub fn increment_fumbles(&mut self, fumbles: u32) {
517        self.fumbles += fumbles;
518    }
519
520    /// Get the yards from the ReceivingStats
521    ///
522    /// ### Example
523    /// ```
524    /// use fbsim_core::game::stat::ReceivingStats;
525    ///
526    /// let my_stats = ReceivingStats::new();
527    /// let yards = my_stats.yards();
528    /// assert!(yards == 0);
529    /// ```
530    pub fn yards(&self) -> i32 {
531        self.yards
532    }
533
534    /// Increment the yards in the ReceivingStats
535    ///
536    /// ### Example
537    /// ```
538    /// use fbsim_core::game::stat::ReceivingStats;
539    ///
540    /// let mut my_stats = ReceivingStats::new();
541    /// my_stats.increment_yards(11);
542    /// assert!(my_stats.yards() == 11);
543    /// ```
544    pub fn increment_yards(&mut self, yards: i32) {
545        self.yards += yards;
546    }
547}
548
549impl std::fmt::Display for ReceivingStats {
550    /// Display receiving stats as a human readable string
551    ///
552    /// ### Example
553    /// ```
554    /// use fbsim_core::game::stat::ReceivingStats;
555    /// 
556    /// let my_stats = ReceivingStats::new();
557    /// println!("{}", my_stats);
558    /// ```
559    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
560        let mut receiving_str = format!(
561            "{} rec ({} tar), {} yards",
562            self.receptions,
563            self.targets,
564            self.yards
565        );
566        if self.touchdowns > 0 {
567            receiving_str = format!(
568                "{}, {} TD",
569                receiving_str,
570                self.touchdowns
571            );
572        }
573        if self.fumbles > 0 {
574            receiving_str = format!(
575                "{}, {} FUM",
576                receiving_str,
577                self.fumbles
578            );
579        }
580        f.write_str(&receiving_str)
581    }
582}