subtr_actor/stats/accumulators/
rotation.rs1use super::*;
2
3#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
5#[ts(export)]
6pub struct RotationPlayerStats {
7 pub active_game_time: f32,
8 pub time_first_man: f32,
9 pub time_second_man: f32,
10 pub time_third_man: f32,
11 pub time_ambiguous_role: f32,
12 pub longest_first_man_stint_time: f32,
13 pub first_man_stint_count: u32,
14 pub became_first_man_count: u32,
15 pub lost_first_man_count: u32,
16 pub current_role_state: RoleState,
17}
18
19impl RotationPlayerStats {
20 fn role_pct(&self, value: f32) -> f32 {
21 if self.active_game_time == 0.0 {
22 0.0
23 } else {
24 value * 100.0 / self.active_game_time
25 }
26 }
27
28 pub fn first_man_pct(&self) -> f32 {
29 self.role_pct(self.time_first_man)
30 }
31
32 pub fn second_man_pct(&self) -> f32 {
33 self.role_pct(self.time_second_man)
34 }
35
36 pub fn third_man_pct(&self) -> f32 {
37 self.role_pct(self.time_third_man)
38 }
39
40 pub fn ambiguous_role_pct(&self) -> f32 {
41 self.role_pct(self.time_ambiguous_role)
42 }
43
44 pub fn average_first_man_stint_time(&self) -> f32 {
45 if self.first_man_stint_count == 0 {
46 0.0
47 } else {
48 self.time_first_man / self.first_man_stint_count as f32
49 }
50 }
51}
52
53#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
55#[ts(export)]
56pub struct RotationTeamStats {
57 pub first_man_changes_for_team: u32,
58 pub rotation_count: u32,
59}
60
61#[derive(Debug, Clone, PartialEq)]
66pub struct RotationStatsAccumulator {
67 player_stats: HashMap<PlayerId, RotationPlayerStats>,
68 team_zero_stats: RotationTeamStats,
69 team_one_stats: RotationTeamStats,
70 first_man_stints: HashMap<PlayerId, FirstManStintAccumulatorState>,
71 first_man_stint_end_grace_seconds: f32,
72}
73
74#[derive(Debug, Clone, Copy, Default, PartialEq)]
75struct FirstManStintAccumulatorState {
76 current_stint_time: f32,
77 last_first_man_end_time: Option<f32>,
78}
79
80impl Default for RotationStatsAccumulator {
81 fn default() -> Self {
82 Self::with_first_man_stint_end_grace_seconds(
83 RotationCalculatorConfig::default().first_man_stint_end_grace_seconds,
84 )
85 }
86}
87
88impl RotationStatsAccumulator {
89 pub fn new() -> Self {
90 Self::default()
91 }
92
93 pub fn with_first_man_stint_end_grace_seconds(first_man_stint_end_grace_seconds: f32) -> Self {
94 Self {
95 player_stats: HashMap::default(),
96 team_zero_stats: RotationTeamStats::default(),
97 team_one_stats: RotationTeamStats::default(),
98 first_man_stints: HashMap::default(),
99 first_man_stint_end_grace_seconds,
100 }
101 }
102
103 pub fn player_stats(&self) -> &HashMap<PlayerId, RotationPlayerStats> {
104 &self.player_stats
105 }
106
107 pub fn team_zero_stats(&self) -> &RotationTeamStats {
108 &self.team_zero_stats
109 }
110
111 pub fn team_one_stats(&self) -> &RotationTeamStats {
112 &self.team_one_stats
113 }
114
115 pub fn apply_role_event(&mut self, event: &RotationRoleEvent) {
118 let stats = self.player_stats.entry(event.player.clone()).or_default();
119 stats.active_game_time += event.duration;
120 match event.state {
121 RoleState::FirstMan => stats.time_first_man += event.duration,
122 RoleState::SecondMan => stats.time_second_man += event.duration,
123 RoleState::ThirdMan => stats.time_third_man += event.duration,
124 RoleState::Ambiguous => stats.time_ambiguous_role += event.duration,
125 RoleState::Unknown => {}
126 }
127 stats.current_role_state = event.state;
128
129 if event.state == RoleState::FirstMan {
130 let stint = self
131 .first_man_stints
132 .entry(event.player.clone())
133 .or_default();
134 let continues_stint = stint
135 .last_first_man_end_time
136 .is_some_and(|end| event.time - end <= self.first_man_stint_end_grace_seconds);
137 if continues_stint {
138 stint.current_stint_time += event.duration;
139 } else {
140 stint.current_stint_time = event.duration;
141 stats.first_man_stint_count += 1;
142 }
143 stint.last_first_man_end_time = Some(event.end_time);
144 stats.longest_first_man_stint_time = stats
145 .longest_first_man_stint_time
146 .max(stint.current_stint_time);
147 }
148 }
149
150 pub fn apply_first_man_change_event(&mut self, event: &FirstManChangeEvent) {
151 let stats = if event.is_team_0 {
152 &mut self.team_zero_stats
153 } else {
154 &mut self.team_one_stats
155 };
156 stats.first_man_changes_for_team += 1;
157 stats.rotation_count += 1;
158 self.player_stats
159 .entry(event.previous_first_man.clone())
160 .or_default()
161 .lost_first_man_count += 1;
162 self.player_stats
163 .entry(event.next_first_man.clone())
164 .or_default()
165 .became_first_man_count += 1;
166 }
167}