subtr_actor/stats/accumulators/
pass.rs1use super::*;
2
3#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
5#[ts(export)]
6pub struct PassPlayerStats {
7 pub completed_pass_count: u32,
8 pub received_pass_count: u32,
9 pub total_pass_distance: f32,
10 pub total_pass_advance: f32,
11 pub longest_pass_distance: f32,
12 pub is_last_completed_pass: bool,
13 pub last_completed_pass_time: Option<f32>,
14 pub last_completed_pass_frame: Option<usize>,
15 pub time_since_last_completed_pass: Option<f32>,
16 pub frames_since_last_completed_pass: Option<usize>,
17}
18
19impl PassPlayerStats {
20 pub fn average_pass_distance(&self) -> f32 {
21 if self.completed_pass_count == 0 {
22 0.0
23 } else {
24 self.total_pass_distance / self.completed_pass_count as f32
25 }
26 }
27
28 pub fn average_pass_advance(&self) -> f32 {
29 if self.completed_pass_count == 0 {
30 0.0
31 } else {
32 self.total_pass_advance / self.completed_pass_count as f32
33 }
34 }
35}
36
37#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
39#[ts(export)]
40pub struct PassTeamStats {
41 pub completed_pass_count: u32,
42 pub total_pass_distance: f32,
43 pub total_pass_advance: f32,
44 pub longest_pass_distance: f32,
45}
46
47impl PassTeamStats {
48 pub fn average_pass_distance(&self) -> f32 {
49 if self.completed_pass_count == 0 {
50 0.0
51 } else {
52 self.total_pass_distance / self.completed_pass_count as f32
53 }
54 }
55
56 pub fn average_pass_advance(&self) -> f32 {
57 if self.completed_pass_count == 0 {
58 0.0
59 } else {
60 self.total_pass_advance / self.completed_pass_count as f32
61 }
62 }
63}
64
65#[derive(Debug, Clone, Default, PartialEq)]
67pub struct PassStatsAccumulator {
68 player_stats: HashMap<PlayerId, PassPlayerStats>,
69 team_zero_stats: PassTeamStats,
70 team_one_stats: PassTeamStats,
71 current_last_completed_pass_player: Option<PlayerId>,
72}
73
74impl PassStatsAccumulator {
75 pub fn new() -> Self {
76 Self::default()
77 }
78
79 pub fn player_stats(&self) -> &HashMap<PlayerId, PassPlayerStats> {
80 &self.player_stats
81 }
82
83 pub fn team_zero_stats(&self) -> &PassTeamStats {
84 &self.team_zero_stats
85 }
86
87 pub fn team_one_stats(&self) -> &PassTeamStats {
88 &self.team_one_stats
89 }
90
91 pub fn current_last_completed_pass_player(&self) -> Option<&PlayerId> {
92 self.current_last_completed_pass_player.as_ref()
93 }
94
95 pub fn begin_sample(&mut self, frame: &FrameInfo) {
96 for stats in self.player_stats.values_mut() {
97 stats.is_last_completed_pass = false;
98 stats.time_since_last_completed_pass = stats
99 .last_completed_pass_time
100 .map(|time| (frame.time - time).max(0.0));
101 stats.frames_since_last_completed_pass = stats
102 .last_completed_pass_frame
103 .map(|last_frame| frame.frame_number.saturating_sub(last_frame));
104 }
105 }
106
107 pub fn clear_current_last(&mut self) {
108 self.current_last_completed_pass_player = None;
109 }
110
111 pub fn set_current_last_completed_pass_player(&mut self, player: Option<PlayerId>) {
112 self.current_last_completed_pass_player = player;
113 }
114
115 pub fn apply_event(&mut self, frame: &FrameInfo, event: &PassEvent) {
116 let passer_stats = self.player_stats.entry(event.passer.clone()).or_default();
117 passer_stats.completed_pass_count += 1;
118 passer_stats.total_pass_distance += event.ball_travel_distance;
119 passer_stats.total_pass_advance += event.ball_advance_distance;
120 passer_stats.longest_pass_distance = passer_stats
121 .longest_pass_distance
122 .max(event.ball_travel_distance);
123 passer_stats.last_completed_pass_time = Some(event.time);
124 passer_stats.last_completed_pass_frame = Some(event.frame);
125 passer_stats.time_since_last_completed_pass = Some((frame.time - event.time).max(0.0));
126 passer_stats.frames_since_last_completed_pass =
127 Some(frame.frame_number.saturating_sub(event.frame));
128
129 self.player_stats
130 .entry(event.receiver.clone())
131 .or_default()
132 .received_pass_count += 1;
133
134 let team_stats = if event.is_team_0 {
135 &mut self.team_zero_stats
136 } else {
137 &mut self.team_one_stats
138 };
139 team_stats.completed_pass_count += 1;
140 team_stats.total_pass_distance += event.ball_travel_distance;
141 team_stats.total_pass_advance += event.ball_advance_distance;
142 team_stats.longest_pass_distance = team_stats
143 .longest_pass_distance
144 .max(event.ball_travel_distance);
145
146 self.current_last_completed_pass_player = Some(event.passer.clone());
147 }
148
149 pub fn finish_sample(&mut self) {
150 if let Some(player_id) = self.current_last_completed_pass_player.as_ref() {
151 if let Some(stats) = self.player_stats.get_mut(player_id) {
152 stats.is_last_completed_pass = true;
153 }
154 }
155 }
156}