subtr-actor 1.1.0

Rocket League replay transformer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use super::*;

const TOUCH_KIND_LABEL_VALUES: [&str; 3] = ["control", "medium_hit", "hard_hit"];
const TOUCH_SURFACE_LABEL_VALUES: [&str; 3] = ["ground", "air", "wall"];
const TOUCH_DODGE_STATE_LABEL_VALUES: [&str; 2] = ["no_dodge", "dodge"];
// Action and possession are independent tag axes, counted separately rather
// than collapsed into one slot. A touch contributes to an axis only when it
// carries that tag: no action tag (a loose poke) → no action count; no
// possession outcome → no possession count. There is no "neutral" catch-all,
// and contested is its own tag, not folded in here.
const TOUCH_ACTION_LABEL_VALUES: [&str; 5] = ["shot", "save", "clear", "boom", "pass"];
const TOUCH_POSSESSION_LABEL_VALUES: [&str; 2] = ["control", "advance"];
const TOUCH_RECEPTION_LABEL_VALUES: [&str; 2] = ["first_touch", "continuation"];

fn touch_kind_label(value: &str) -> StatLabel {
    match value {
        "medium_hit" => StatLabel::new("kind", "medium_hit"),
        "hard_hit" => StatLabel::new("kind", "hard_hit"),
        _ => StatLabel::new("kind", "control"),
    }
}

fn touch_height_band_label(value: &str) -> StatLabel {
    match value {
        "low_air" => StatLabel::new("height_band", "low_air"),
        "high_air" => StatLabel::new("height_band", "high_air"),
        _ => StatLabel::new("height_band", "ground"),
    }
}

fn touch_surface_label(value: &str) -> StatLabel {
    match value {
        "air" => StatLabel::new("surface", "air"),
        "wall" => StatLabel::new("surface", "wall"),
        _ => StatLabel::new("surface", "ground"),
    }
}

fn touch_dodge_state_label(value: &str) -> StatLabel {
    match value {
        "dodge" => StatLabel::new("dodge_state", "dodge"),
        _ => StatLabel::new("dodge_state", "no_dodge"),
    }
}

fn touch_action_label(value: &str) -> Option<StatLabel> {
    let label = match value {
        "shot" => StatLabel::new("action", "shot"),
        "save" => StatLabel::new("action", "save"),
        "clear" => StatLabel::new("action", "clear"),
        "boom" => StatLabel::new("action", "boom"),
        "pass" => StatLabel::new("action", "pass"),
        _ => return None,
    };
    Some(label)
}

fn touch_possession_label(value: &str) -> Option<StatLabel> {
    let label = match value {
        "control" => StatLabel::new("possession", "control"),
        "advance" => StatLabel::new("possession", "advance"),
        _ => return None,
    };
    Some(label)
}

fn touch_reception_label(first_touch: bool) -> StatLabel {
    StatLabel::new(
        "reception",
        if first_touch {
            "first_touch"
        } else {
            "continuation"
        },
    )
}

/// Accumulated touch stats: counts by control, hardness, and aerial context.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
#[ts(export)]
pub struct TouchStats {
    pub touch_count: u32,
    pub control_touch_count: u32,
    pub medium_hit_count: u32,
    pub hard_hit_count: u32,
    pub aerial_touch_count: u32,
    pub high_aerial_touch_count: u32,
    #[serde(default)]
    pub wall_touch_count: u32,
    #[serde(default)]
    pub first_touch_count: u32,
    pub is_last_touch: bool,
    pub last_touch_time: Option<f32>,
    pub last_touch_frame: Option<usize>,
    pub time_since_last_touch: Option<f32>,
    pub frames_since_last_touch: Option<usize>,
    pub last_ball_speed_change: Option<f32>,
    pub max_ball_speed_change: f32,
    pub cumulative_ball_speed_change: f32,
    #[serde(default)]
    pub total_ball_travel_distance: f32,
    #[serde(default)]
    pub total_ball_advance_distance: f32,
    #[serde(default)]
    pub total_ball_retreat_distance: f32,
    #[serde(default, skip_serializing_if = "LabeledCounts::is_empty")]
    pub labeled_touch_counts: LabeledCounts,
    #[serde(default, skip_serializing_if = "LabeledCounts::is_empty")]
    pub labeled_action_counts: LabeledCounts,
    #[serde(default, skip_serializing_if = "LabeledCounts::is_empty")]
    pub labeled_possession_counts: LabeledCounts,
    #[serde(default, skip_serializing_if = "LabeledCounts::is_empty")]
    pub touch_counts_by_role: LabeledCounts,
    #[serde(default, skip_serializing_if = "LabeledCounts::is_empty")]
    pub touch_counts_by_play_depth: LabeledCounts,
}

impl TouchStats {
    pub fn average_ball_speed_change(&self) -> f32 {
        if self.touch_count == 0 {
            0.0
        } else {
            self.cumulative_ball_speed_change / self.touch_count as f32
        }
    }

    pub fn touch_count_with_labels(&self, labels: &[StatLabel]) -> u32 {
        self.labeled_touch_counts.count_matching(labels)
    }

    pub fn dodge_touch_count(&self) -> u32 {
        self.touch_count_with_labels(&[StatLabel::new("dodge_state", "dodge")])
    }

    pub fn dodge_hit_count(&self) -> u32 {
        self.touch_count_with_labels(&[
            StatLabel::new("dodge_state", "dodge"),
            StatLabel::new("kind", "medium_hit"),
        ]) + self.touch_count_with_labels(&[
            StatLabel::new("dodge_state", "dodge"),
            StatLabel::new("kind", "hard_hit"),
        ])
    }

    pub fn action_count(&self, action: &str) -> u32 {
        let Some(label) = touch_action_label(action) else {
            return 0;
        };
        self.labeled_action_counts.count_matching(&[label])
    }

    pub fn first_touch_action_count(&self, action: &str) -> u32 {
        let Some(label) = touch_action_label(action) else {
            return 0;
        };
        self.labeled_action_counts
            .count_matching(&[label, touch_reception_label(true)])
    }

    pub fn possession_count(&self, possession: &str) -> u32 {
        let Some(label) = touch_possession_label(possession) else {
            return 0;
        };
        self.labeled_possession_counts.count_matching(&[label])
    }

    pub fn first_touch_possession_count(&self, possession: &str) -> u32 {
        let Some(label) = touch_possession_label(possession) else {
            return 0;
        };
        self.labeled_possession_counts
            .count_matching(&[label, touch_reception_label(true)])
    }

    fn complete_labeled_counts(
        source: &LabeledCounts,
        key: &'static str,
        values: &[&'static str],
    ) -> LabeledCounts {
        let mut entries: Vec<_> = values
            .iter()
            .flat_map(|&value| {
                TOUCH_RECEPTION_LABEL_VALUES
                    .into_iter()
                    .map(move |reception| {
                        let mut labels = vec![
                            StatLabel::new(key, value),
                            StatLabel::new("reception", reception),
                        ];
                        labels.sort();
                        LabeledCountEntry {
                            count: source.count_exact(&labels),
                            labels,
                        }
                    })
            })
            .collect();

        entries.sort_by(|left, right| left.labels.cmp(&right.labels));

        LabeledCounts { entries }
    }

    pub fn complete_labeled_action_counts(&self) -> LabeledCounts {
        Self::complete_labeled_counts(
            &self.labeled_action_counts,
            "action",
            &TOUCH_ACTION_LABEL_VALUES,
        )
    }

    pub fn complete_labeled_possession_counts(&self) -> LabeledCounts {
        Self::complete_labeled_counts(
            &self.labeled_possession_counts,
            "possession",
            &TOUCH_POSSESSION_LABEL_VALUES,
        )
    }

    pub fn touch_count_with_role(&self, role: RoleState) -> u32 {
        self.touch_counts_by_role.count_exact(&[role.as_label()])
    }

    pub fn touch_count_with_play_depth(&self, play_depth: PlayDepthState) -> u32 {
        self.touch_counts_by_play_depth
            .count_exact(&[play_depth.as_label()])
    }

    pub fn touches_as_first_man(&self) -> u32 {
        self.touch_count_with_role(RoleState::FirstMan)
    }

    pub fn touches_as_second_man(&self) -> u32 {
        self.touch_count_with_role(RoleState::SecondMan)
    }

    pub fn touches_as_third_man(&self) -> u32 {
        self.touch_count_with_role(RoleState::ThirdMan)
    }

    pub fn touches_behind_play(&self) -> u32 {
        self.touch_count_with_play_depth(PlayDepthState::BehindPlay)
    }

    pub fn touches_ahead_of_play(&self) -> u32 {
        self.touch_count_with_play_depth(PlayDepthState::AheadOfPlay)
    }

    pub fn complete_touch_counts_by_role(&self) -> LabeledCounts {
        let mut entries: Vec<_> = ALL_ROLE_STATES
            .into_iter()
            .map(|role| {
                let labels = vec![role.as_label()];
                LabeledCountEntry {
                    count: self.touch_counts_by_role.count_exact(&labels),
                    labels,
                }
            })
            .collect();
        entries.sort_by(|left, right| left.labels.cmp(&right.labels));
        LabeledCounts { entries }
    }

    pub fn complete_touch_counts_by_play_depth(&self) -> LabeledCounts {
        let mut entries: Vec<_> = ALL_PLAY_DEPTH_STATES
            .into_iter()
            .map(|play_depth| {
                let labels = vec![play_depth.as_label()];
                LabeledCountEntry {
                    count: self.touch_counts_by_play_depth.count_exact(&labels),
                    labels,
                }
            })
            .collect();
        entries.sort_by(|left, right| left.labels.cmp(&right.labels));
        LabeledCounts { entries }
    }

    pub fn complete_labeled_touch_counts(&self) -> LabeledCounts {
        let mut entries: Vec<_> = ALL_PLAYER_VERTICAL_BANDS
            .into_iter()
            .flat_map(|height_band| {
                TOUCH_SURFACE_LABEL_VALUES
                    .into_iter()
                    .flat_map(move |surface| {
                        TOUCH_DODGE_STATE_LABEL_VALUES
                            .into_iter()
                            .flat_map(move |dodge_state| {
                                TOUCH_KIND_LABEL_VALUES.into_iter().map(move |kind| {
                                    let mut labels = vec![
                                        StatLabel::new("kind", kind),
                                        height_band.as_label(),
                                        StatLabel::new("surface", surface),
                                        StatLabel::new("dodge_state", dodge_state),
                                    ];
                                    labels.sort();
                                    LabeledCountEntry {
                                        count: self.labeled_touch_counts.count_exact(&labels),
                                        labels,
                                    }
                                })
                            })
                    })
            })
            .collect();

        entries.sort_by(|left, right| left.labels.cmp(&right.labels));

        LabeledCounts { entries }
    }

    pub fn with_complete_labeled_touch_counts(mut self) -> Self {
        self.labeled_touch_counts = self.complete_labeled_touch_counts();
        self.labeled_action_counts = self.complete_labeled_action_counts();
        self.labeled_possession_counts = self.complete_labeled_possession_counts();
        self
    }
}

/// Accumulates touch stats over the replay from touch events.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TouchStatsAccumulator {
    player_stats: HashMap<PlayerId, TouchStats>,
    current_last_touch_player: Option<PlayerId>,
}

impl TouchStatsAccumulator {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn player_stats(&self) -> &HashMap<PlayerId, TouchStats> {
        &self.player_stats
    }

    pub fn begin_sample(&mut self, frame: &FrameInfo) {
        for stats in self.player_stats.values_mut() {
            stats.is_last_touch = false;
            stats.time_since_last_touch = stats
                .last_touch_time
                .map(|time| (frame.time - time).max(0.0));
            stats.frames_since_last_touch = stats
                .last_touch_frame
                .map(|last_frame| frame.frame_number.saturating_sub(last_frame));
        }
    }

    pub fn apply_touch_event(&mut self, event: &TouchClassificationEvent, frame: &FrameInfo) {
        // The event now carries classification as a tag set; read the
        // single-valued groups back out, defaulting to the same fallbacks the
        // old flat fields used. The `intention` stat label preserves the
        // each single-valued group back out, defaulting to the same fallbacks
        // the old flat fields used. Action and possession are counted on their
        // own axes — a touch contributes to each only when it carries that tag.
        let kind = event.tag("kind").unwrap_or("control");
        let height_band = event.tag("height_band").unwrap_or("ground");
        let surface = event.tag("surface").unwrap_or("ground");
        let dodge_state = event.tag("dodge_state").unwrap_or("no_dodge");
        let action = event.tag("action");
        let possession = event.tag("possession");
        let first_touch = event.tag("reception") == Some("first_touch");

        let stats = self.player_stats.entry(event.player.clone()).or_default();
        stats.touch_count += 1;
        match height_band {
            "low_air" => stats.aerial_touch_count += 1,
            "high_air" => {
                stats.aerial_touch_count += 1;
                stats.high_aerial_touch_count += 1;
            }
            _ => {}
        }
        match kind {
            "control" => stats.control_touch_count += 1,
            "medium_hit" => stats.medium_hit_count += 1,
            "hard_hit" => stats.hard_hit_count += 1,
            _ => {}
        }
        if surface == "wall" {
            stats.wall_touch_count += 1;
        }
        stats.labeled_touch_counts.increment([
            touch_kind_label(kind),
            touch_height_band_label(height_band),
            touch_surface_label(surface),
            touch_dodge_state_label(dodge_state),
        ]);
        if first_touch {
            stats.first_touch_count += 1;
        }
        if let Some(label) = action.and_then(touch_action_label) {
            stats
                .labeled_action_counts
                .increment([label, touch_reception_label(first_touch)]);
        }
        if let Some(label) = possession.and_then(touch_possession_label) {
            stats
                .labeled_possession_counts
                .increment([label, touch_reception_label(first_touch)]);
        }
        stats
            .touch_counts_by_role
            .increment([event.role.as_label()]);
        stats
            .touch_counts_by_play_depth
            .increment([event.play_depth.as_label()]);
        stats.last_touch_time = Some(event.time);
        stats.last_touch_frame = Some(event.frame);
        stats.time_since_last_touch = Some((frame.time - event.time).max(0.0));
        stats.frames_since_last_touch = Some(frame.frame_number.saturating_sub(event.frame));
        stats.last_ball_speed_change = Some(event.ball_speed_change);
        stats.max_ball_speed_change = stats.max_ball_speed_change.max(event.ball_speed_change);
        stats.cumulative_ball_speed_change += event.ball_speed_change;
        if let Some(movement) = event.ball_movement.as_ref() {
            stats.total_ball_travel_distance += movement.travel_distance;
            stats.total_ball_advance_distance += movement.advance_distance;
            stats.total_ball_retreat_distance += movement.retreat_distance;
        }
        self.current_last_touch_player = Some(event.player.clone());
    }

    pub fn set_current_last_touch_player(&mut self, player: Option<PlayerId>) {
        self.current_last_touch_player = player;
    }

    pub fn restore_current_last_touch_marker(&mut self) {
        if let Some(player_id) = self.current_last_touch_player.as_ref() {
            if let Some(stats) = self.player_stats.get_mut(player_id) {
                stats.is_last_touch = true;
            }
        }
    }
}