subtr_actor/stats/calculators/
pass.rs1use super::*;
2
3const PASS_MAX_DURATION_SECONDS: f32 = 3.0;
4const PASS_MIN_BALL_TRAVEL_DISTANCE: f32 = 500.0;
5
6#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
7#[ts(export)]
8pub struct PassEvent {
9 pub time: f32,
10 pub frame: usize,
11 #[ts(as = "crate::ts_bindings::RemoteIdTs")]
12 pub passer: PlayerId,
13 #[ts(as = "crate::ts_bindings::RemoteIdTs")]
14 pub receiver: PlayerId,
15 pub is_team_0: bool,
16 pub start_time: f32,
17 pub start_frame: usize,
18 pub duration: f32,
19 pub ball_travel_distance: f32,
20 pub ball_advance_distance: f32,
21}
22
23#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
24#[ts(export)]
25pub struct PassPlayerStats {
26 pub completed_pass_count: u32,
27 pub received_pass_count: u32,
28 pub total_pass_distance: f32,
29 pub total_pass_advance: f32,
30 pub longest_pass_distance: f32,
31 pub is_last_completed_pass: bool,
32 pub last_completed_pass_time: Option<f32>,
33 pub last_completed_pass_frame: Option<usize>,
34 pub time_since_last_completed_pass: Option<f32>,
35 pub frames_since_last_completed_pass: Option<usize>,
36}
37
38impl PassPlayerStats {
39 pub fn average_pass_distance(&self) -> f32 {
40 if self.completed_pass_count == 0 {
41 0.0
42 } else {
43 self.total_pass_distance / self.completed_pass_count as f32
44 }
45 }
46
47 pub fn average_pass_advance(&self) -> f32 {
48 if self.completed_pass_count == 0 {
49 0.0
50 } else {
51 self.total_pass_advance / self.completed_pass_count as f32
52 }
53 }
54}
55
56#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
57#[ts(export)]
58pub struct PassTeamStats {
59 pub completed_pass_count: u32,
60 pub total_pass_distance: f32,
61 pub total_pass_advance: f32,
62 pub longest_pass_distance: f32,
63}
64
65impl PassTeamStats {
66 pub fn average_pass_distance(&self) -> f32 {
67 if self.completed_pass_count == 0 {
68 0.0
69 } else {
70 self.total_pass_distance / self.completed_pass_count as f32
71 }
72 }
73
74 pub fn average_pass_advance(&self) -> f32 {
75 if self.completed_pass_count == 0 {
76 0.0
77 } else {
78 self.total_pass_advance / self.completed_pass_count as f32
79 }
80 }
81}
82
83#[derive(Debug, Clone)]
84struct PendingPassTouch {
85 player: PlayerId,
86 is_team_0: bool,
87 time: f32,
88 frame: usize,
89 ball_position: glam::Vec3,
90}
91
92#[derive(Debug, Clone, Default)]
93pub struct PassCalculator {
94 player_stats: HashMap<PlayerId, PassPlayerStats>,
95 team_zero_stats: PassTeamStats,
96 team_one_stats: PassTeamStats,
97 events: Vec<PassEvent>,
98 last_touch: Option<PendingPassTouch>,
99 current_last_completed_pass_player: Option<PlayerId>,
100}
101
102impl PassCalculator {
103 pub fn new() -> Self {
104 Self::default()
105 }
106
107 pub fn player_stats(&self) -> &HashMap<PlayerId, PassPlayerStats> {
108 &self.player_stats
109 }
110
111 pub fn team_zero_stats(&self) -> &PassTeamStats {
112 &self.team_zero_stats
113 }
114
115 pub fn team_one_stats(&self) -> &PassTeamStats {
116 &self.team_one_stats
117 }
118
119 pub fn events(&self) -> &[PassEvent] {
120 &self.events
121 }
122
123 fn begin_sample(&mut self, frame: &FrameInfo) {
124 for stats in self.player_stats.values_mut() {
125 stats.is_last_completed_pass = false;
126 stats.time_since_last_completed_pass = stats
127 .last_completed_pass_time
128 .map(|time| (frame.time - time).max(0.0));
129 stats.frames_since_last_completed_pass = stats
130 .last_completed_pass_frame
131 .map(|last_frame| frame.frame_number.saturating_sub(last_frame));
132 }
133 }
134
135 fn pass_event_for_touch(
136 &self,
137 touch: &TouchEvent,
138 receiver: &PlayerId,
139 ball_position: glam::Vec3,
140 ) -> Option<PassEvent> {
141 let previous = self.last_touch.as_ref()?;
142 if previous.player == *receiver || previous.is_team_0 != touch.team_is_team_0 {
143 return None;
144 }
145
146 let duration = touch.time - previous.time;
147 if !(0.0..=PASS_MAX_DURATION_SECONDS).contains(&duration) {
148 return None;
149 }
150
151 let ball_delta = ball_position - previous.ball_position;
152 let ball_travel_distance = ball_delta.length();
153 if ball_travel_distance < PASS_MIN_BALL_TRAVEL_DISTANCE {
154 return None;
155 }
156
157 let team_forward_sign = if touch.team_is_team_0 { 1.0 } else { -1.0 };
158 Some(PassEvent {
159 time: touch.time,
160 frame: touch.frame,
161 passer: previous.player.clone(),
162 receiver: receiver.clone(),
163 is_team_0: touch.team_is_team_0,
164 start_time: previous.time,
165 start_frame: previous.frame,
166 duration,
167 ball_travel_distance,
168 ball_advance_distance: ball_delta.y * team_forward_sign,
169 })
170 }
171
172 fn record_pass(&mut self, frame: &FrameInfo, event: PassEvent) {
173 let passer_stats = self.player_stats.entry(event.passer.clone()).or_default();
174 passer_stats.completed_pass_count += 1;
175 passer_stats.total_pass_distance += event.ball_travel_distance;
176 passer_stats.total_pass_advance += event.ball_advance_distance;
177 passer_stats.longest_pass_distance = passer_stats
178 .longest_pass_distance
179 .max(event.ball_travel_distance);
180 passer_stats.last_completed_pass_time = Some(event.time);
181 passer_stats.last_completed_pass_frame = Some(event.frame);
182 passer_stats.time_since_last_completed_pass = Some((frame.time - event.time).max(0.0));
183 passer_stats.frames_since_last_completed_pass =
184 Some(frame.frame_number.saturating_sub(event.frame));
185
186 self.player_stats
187 .entry(event.receiver.clone())
188 .or_default()
189 .received_pass_count += 1;
190
191 let team_stats = if event.is_team_0 {
192 &mut self.team_zero_stats
193 } else {
194 &mut self.team_one_stats
195 };
196 team_stats.completed_pass_count += 1;
197 team_stats.total_pass_distance += event.ball_travel_distance;
198 team_stats.total_pass_advance += event.ball_advance_distance;
199 team_stats.longest_pass_distance = team_stats
200 .longest_pass_distance
201 .max(event.ball_travel_distance);
202
203 self.current_last_completed_pass_player = Some(event.passer.clone());
204 self.events.push(event);
205 }
206
207 pub fn update(
208 &mut self,
209 frame: &FrameInfo,
210 ball: &BallFrameState,
211 events: &FrameEventsState,
212 live_play: bool,
213 ) -> SubtrActorResult<()> {
214 self.begin_sample(frame);
215 if !live_play {
216 self.last_touch = None;
217 self.current_last_completed_pass_player = None;
218 return Ok(());
219 }
220
221 let Some(ball_position) = ball.position() else {
222 return Ok(());
223 };
224
225 for touch in &events.touch_events {
226 let Some(player) = touch.player.clone() else {
227 self.last_touch = None;
228 continue;
229 };
230
231 if let Some(pass_event) = self.pass_event_for_touch(touch, &player, ball_position) {
232 self.record_pass(frame, pass_event);
233 }
234
235 self.last_touch = Some(PendingPassTouch {
236 player,
237 is_team_0: touch.team_is_team_0,
238 time: touch.time,
239 frame: touch.frame,
240 ball_position,
241 });
242 }
243
244 if let Some(player_id) = self.current_last_completed_pass_player.as_ref() {
245 if let Some(stats) = self.player_stats.get_mut(player_id) {
246 stats.is_last_completed_pass = true;
247 }
248 }
249
250 Ok(())
251 }
252}
253
254#[cfg(test)]
255#[path = "pass_tests.rs"]
256mod tests;