Skip to main content

subtr_actor/stats/accumulators/
ceiling_shot.rs

1use super::*;
2
3const CEILING_SHOT_HIGH_CONFIDENCE: f32 = 0.78;
4
5/// Per-player accumulated ceiling-shot stats with confidence.
6#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
7#[ts(export)]
8pub struct CeilingShotStats {
9    pub count: u32,
10    pub high_confidence_count: u32,
11    pub is_last_ceiling_shot: bool,
12    pub last_ceiling_shot_time: Option<f32>,
13    pub last_ceiling_shot_frame: Option<usize>,
14    pub time_since_last_ceiling_shot: Option<f32>,
15    pub frames_since_last_ceiling_shot: Option<usize>,
16    pub last_confidence: Option<f32>,
17    pub best_confidence: f32,
18    pub cumulative_confidence: f32,
19    #[serde(default, skip_serializing_if = "LabeledCounts::is_empty")]
20    pub labeled_event_counts: LabeledCounts,
21}
22
23impl CeilingShotStats {
24    pub fn average_confidence(&self) -> f32 {
25        if self.count == 0 {
26            0.0
27        } else {
28            self.cumulative_confidence / self.count as f32
29        }
30    }
31
32    fn record_event(&mut self, event: &CeilingShotEvent) {
33        self.labeled_event_counts.increment([confidence_band_label(
34            event.confidence >= CEILING_SHOT_HIGH_CONFIDENCE,
35        )]);
36        self.sync_legacy_counts();
37        self.last_ceiling_shot_time = Some(event.time);
38        self.last_ceiling_shot_frame = Some(event.frame);
39        self.last_confidence = Some(event.confidence);
40        self.best_confidence = self.best_confidence.max(event.confidence);
41        self.cumulative_confidence += event.confidence;
42    }
43
44    pub fn event_count_with_labels(&self, labels: &[StatLabel]) -> u32 {
45        self.labeled_event_counts.count_matching(labels)
46    }
47
48    pub fn complete_labeled_event_counts(&self) -> LabeledCounts {
49        LabeledCounts::complete_from_label_sets(
50            &[&CONFIDENCE_BAND_LABELS],
51            &self.labeled_event_counts,
52        )
53    }
54
55    fn sync_legacy_counts(&mut self) {
56        self.count = self.labeled_event_counts.total();
57        self.high_confidence_count = self.event_count_with_labels(&[confidence_band_label(true)]);
58    }
59}
60
61/// Accumulates ceiling-shot stats over the replay.
62#[derive(Debug, Clone, Default, PartialEq)]
63pub struct CeilingShotStatsAccumulator {
64    player_stats: HashMap<PlayerId, CeilingShotStats>,
65    current_last_ceiling_shot_player: Option<PlayerId>,
66}
67
68impl CeilingShotStatsAccumulator {
69    pub fn new() -> Self {
70        Self::default()
71    }
72
73    pub fn player_stats(&self) -> &HashMap<PlayerId, CeilingShotStats> {
74        &self.player_stats
75    }
76
77    pub fn begin_sample(&mut self, frame: &FrameInfo) {
78        for stats in self.player_stats.values_mut() {
79            stats.is_last_ceiling_shot = false;
80            stats.time_since_last_ceiling_shot = stats
81                .last_ceiling_shot_time
82                .map(|time| (frame.time - time).max(0.0));
83            stats.frames_since_last_ceiling_shot = stats
84                .last_ceiling_shot_frame
85                .map(|last_frame| frame.frame_number.saturating_sub(last_frame));
86        }
87
88        if let Some(player_id) = self.current_last_ceiling_shot_player.as_ref() {
89            if let Some(stats) = self.player_stats.get_mut(player_id) {
90                stats.is_last_ceiling_shot = true;
91            }
92        }
93    }
94
95    pub fn apply_event(&mut self, event: &CeilingShotEvent, frame: &FrameInfo) {
96        let stats = self.player_stats.entry(event.player.clone()).or_default();
97        stats.record_event(event);
98        stats.is_last_ceiling_shot = true;
99        stats.time_since_last_ceiling_shot = Some((frame.time - event.time).max(0.0));
100        stats.frames_since_last_ceiling_shot = Some(frame.frame_number.saturating_sub(event.frame));
101
102        self.current_last_ceiling_shot_player = Some(event.player.clone());
103
104        if let Some(player_id) = self.current_last_ceiling_shot_player.as_ref() {
105            if let Some(stats) = self.player_stats.get_mut(player_id) {
106                stats.is_last_ceiling_shot = true;
107            }
108        }
109    }
110
111    pub fn reset_current_last_event_marker(&mut self) {
112        self.current_last_ceiling_shot_player = None;
113    }
114}