Skip to main content

subtr_actor/stats/analysis_graph/
mod.rs

1#![allow(dead_code)]
2
3use std::collections::HashSet;
4
5use crate::Collector;
6use crate::{SubtrActorError, SubtrActorErrorVariant, SubtrActorResult};
7
8pub mod graph;
9pub use graph::{
10    AnalysisDependency, AnalysisGraph, AnalysisNode, AnalysisNodeDyn, AnalysisStateContext,
11    AnalysisStateRef,
12};
13
14#[macro_use]
15mod node_macros;
16
17mod collector;
18mod nodes;
19
20use crate::stats::calculators::FrameInput;
21
22#[allow(unused_imports)]
23pub use collector::AnalysisNodeCollector;
24#[allow(unused_imports)]
25pub use nodes::*;
26
27pub(crate) fn boxed_analysis_node_by_name(name: &str) -> Option<Box<dyn AnalysisNodeDyn>> {
28    match name {
29        "core" => Some(nodes::match_stats::boxed_default()),
30        "backboard" => Some(nodes::backboard::boxed_default()),
31        "ceiling_shot" => Some(nodes::ceiling_shot::boxed_default()),
32        "double_tap" => Some(nodes::double_tap::boxed_default()),
33        "fifty_fifty" => Some(nodes::fifty_fifty::boxed_default()),
34        "possession" => Some(nodes::possession::boxed_default()),
35        "pressure" => Some(nodes::pressure::boxed_default()),
36        "rotation" => Some(nodes::rotation::boxed_default()),
37        "rush" => Some(nodes::rush::boxed_default()),
38        "touch" => Some(nodes::touch::boxed_default()),
39        "whiff" => Some(nodes::whiff::boxed_default()),
40        "wavedash" => Some(nodes::wavedash::boxed_default()),
41        "speed_flip" => Some(nodes::speed_flip::boxed_default()),
42        "half_flip" => Some(nodes::half_flip::boxed_default()),
43        "half_volley" => Some(nodes::half_volley::boxed_default()),
44        "flick" => Some(nodes::flick::boxed_default()),
45        "aerial_goal" => Some(nodes::goal_tags::boxed_aerial_goal()),
46        "high_aerial_goal" => Some(nodes::goal_tags::boxed_high_aerial_goal()),
47        "long_distance_goal" => Some(nodes::goal_tags::boxed_long_distance_goal()),
48        "own_half_goal" => Some(nodes::goal_tags::boxed_own_half_goal()),
49        "empty_net_goal" => Some(nodes::goal_tags::boxed_empty_net_goal()),
50        "flick_goal" => Some(nodes::goal_tags::boxed_flick_goal()),
51        "one_timer_goal" => Some(nodes::goal_tags::boxed_one_timer_goal()),
52        "air_dribble_goal" => Some(nodes::goal_tags::boxed_air_dribble_goal()),
53        "flip_reset_goal" => Some(nodes::goal_tags::boxed_flip_reset_goal()),
54        "half_volley_goal" => Some(nodes::goal_tags::boxed_half_volley_goal()),
55        "musty_flick" => Some(nodes::musty_flick::boxed_default()),
56        "one_timer" => Some(nodes::one_timer::boxed_default()),
57        "pass" => Some(nodes::pass::boxed_default()),
58        "dodge_reset" => Some(nodes::dodge_reset::boxed_default()),
59        "ball_carry" => Some(nodes::ball_carry::boxed_default()),
60        "boost" => Some(nodes::boost::boxed_default()),
61        "bump" => Some(nodes::bump::boxed_default()),
62        "movement" => Some(nodes::movement::boxed_default()),
63        "positioning" => Some(nodes::positioning::boxed_default()),
64        "powerslide" => Some(nodes::powerslide::boxed_default()),
65        "demo" => Some(nodes::demo::boxed_default()),
66        _ => None,
67    }
68}
69
70pub fn graph_with_builtin_analysis_nodes<I, S>(names: I) -> SubtrActorResult<AnalysisGraph>
71where
72    I: IntoIterator<Item = S>,
73    S: AsRef<str>,
74{
75    let mut graph = AnalysisGraph::new().with_input_state_type::<FrameInput>();
76    graph.push_boxed_node(nodes::live_play::boxed_default());
77    let mut seen = HashSet::new();
78    for name in names {
79        let name = name.as_ref();
80        if !seen.insert(name.to_owned()) {
81            continue;
82        }
83        graph.push_boxed_node(boxed_analysis_node_by_name(name).ok_or_else(|| {
84            SubtrActorError::new(SubtrActorErrorVariant::UnknownStatsModuleName(
85                name.to_owned(),
86            ))
87        })?);
88    }
89    Ok(graph)
90}
91
92pub fn collect_analysis_graph_for_replay(
93    replay: &boxcars::Replay,
94    graph: AnalysisGraph,
95) -> SubtrActorResult<AnalysisGraph> {
96    let collector = collector::AnalysisNodeCollector::new(graph).process_replay(replay)?;
97    Ok(collector.into_graph())
98}
99
100pub fn collect_builtin_analysis_graph_for_replay<I, S>(
101    replay: &boxcars::Replay,
102    names: I,
103) -> SubtrActorResult<AnalysisGraph>
104where
105    I: IntoIterator<Item = S>,
106    S: AsRef<str>,
107{
108    collect_analysis_graph_for_replay(replay, graph_with_builtin_analysis_nodes(names)?)
109}
110
111pub fn all_analysis_nodes() -> Vec<Box<dyn AnalysisNodeDyn>> {
112    vec![
113        nodes::backboard::boxed_default(),
114        nodes::ball_carry::boxed_default(),
115        nodes::boost::boxed_default(),
116        nodes::bump::boxed_default(),
117        nodes::ceiling_shot::boxed_default(),
118        nodes::demo::boxed_default(),
119        nodes::dodge_reset::boxed_default(),
120        nodes::double_tap::boxed_default(),
121        nodes::fifty_fifty::boxed_default(),
122        nodes::match_stats::boxed_default(),
123        nodes::movement::boxed_default(),
124        nodes::flick::boxed_default(),
125        nodes::goal_tags::boxed_aerial_goal(),
126        nodes::goal_tags::boxed_high_aerial_goal(),
127        nodes::goal_tags::boxed_long_distance_goal(),
128        nodes::goal_tags::boxed_own_half_goal(),
129        nodes::goal_tags::boxed_empty_net_goal(),
130        nodes::goal_tags::boxed_flick_goal(),
131        nodes::goal_tags::boxed_one_timer_goal(),
132        nodes::goal_tags::boxed_air_dribble_goal(),
133        nodes::goal_tags::boxed_flip_reset_goal(),
134        nodes::goal_tags::boxed_half_volley_goal(),
135        nodes::musty_flick::boxed_default(),
136        nodes::one_timer::boxed_default(),
137        nodes::pass::boxed_default(),
138        nodes::positioning::boxed_default(),
139        nodes::possession::boxed_default(),
140        nodes::powerslide::boxed_default(),
141        nodes::pressure::boxed_default(),
142        nodes::rotation::boxed_default(),
143        nodes::rush::boxed_default(),
144        nodes::settings::boxed_default(),
145        nodes::speed_flip::boxed_default(),
146        nodes::half_flip::boxed_default(),
147        nodes::half_volley::boxed_default(),
148        nodes::wavedash::boxed_default(),
149        nodes::touch::boxed_default(),
150        nodes::whiff::boxed_default(),
151    ]
152}
153
154pub fn graph_with_all_analysis_nodes() -> AnalysisGraph {
155    let mut graph = AnalysisGraph::new().with_input_state_type::<FrameInput>();
156    graph.push_boxed_node(nodes::live_play::boxed_default());
157    for node in all_analysis_nodes() {
158        graph.push_boxed_node(node);
159    }
160    graph
161}
162
163#[cfg(test)]
164#[path = "module_tests.rs"]
165mod tests;