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