Skip to main content

subtr_actor/stats/accumulators/
pass.rs

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