From acff43c6f620067485402a03c6ace420a0d1e354 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sun, 21 Jan 2024 01:04:58 -0700 Subject: [PATCH] Added function to parse match list into map of match tuples by division --- src/main.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4717ed3..be49e84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,7 @@ struct ArenaStateInfo { start_ns: usize, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] enum Round { None = 0, Practice = 1, @@ -115,13 +115,32 @@ enum Round { Eliminations = 21, } -#[derive(Serialize, Deserialize, Debug)] +fn int_to_round(round: i32) -> Round { + match round { + 1 => Round::Practice, + 2 => Round::Qualification, + 3 => Round::QuarterFinals, + 4 => Round::SemiFinals, + 5 => Round::Finals, + 6 => Round::RoundOf16, + 7 => Round::RoundOf32, + 8 => Round::RoundOf64, + 9 => Round::RoundOf128, + 15 => Round::TopN, + 16 => Round::RoundRobin, + 20 => Round::PreEliminations, + 21 => Round::Eliminations, + _ => Round::None, + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] struct MatchTuple { - division: String, + division: i32, round: Round, - instance: usize, - match_num: usize, - session: usize, + instance: i32, + match_num: i32, + session: i32, } #[derive(Serialize, Deserialize, Debug)] @@ -136,41 +155,77 @@ struct MQTTMessage { payload: String, } +#[derive(Debug)] struct Event { name: String, - divisions: Vec, + divisions: HashMap, } impl Event { fn new(name: String) -> Event { Event{ name, - divisions: Vec::new(), + divisions: HashMap::new(), } } - fn parse_match_list(self: &Event, match_list: BackendMessage) { - let divisions: Vec = Vec::new(); + fn parse_match_list(self: &mut Event, msg: BackendMessage) { + match msg.data.match_list { + Some(matches) => { + for m in matches.matches.iter() { + let match_tuple = MatchTuple{ + division: m.division.unwrap(), + round: int_to_round(m.round.unwrap()), + instance: m.instance.unwrap(), + match_num: m.r#match.unwrap(), + session: m.session.unwrap(), + }; + match self.divisions.get_mut(&match_tuple.division) { + Some(division) => { + division.matches.push(Match{ + match_tuple: match_tuple.clone(), + }) + }, + None => { + let mut new_division = Division{ + name: String::from(""), + matches: Vec::new(), + field_set: None, + }; + new_division.matches.push(Match{ + match_tuple: match_tuple.clone(), + }); + self.divisions.insert(match_tuple.division, new_division); + }, + } + } + }, + None => log::warn!("Parsed match list without match_list"), + } } } +#[derive(Debug)] struct Division { name: String, matches: Vec, - field_set: FieldSet, + field_set: Option
, } +#[derive(Debug)] struct FieldSet { fields: Vec, } +#[derive(Debug)] struct Field { name: String, current_match: u32, } +#[derive(Debug)] struct Match { - name: String, + match_tuple: MatchTuple, } #[derive(Debug)] @@ -691,6 +746,7 @@ fn main() { let get_match_list_resp = tm_connection.responses.recv().unwrap(); event.parse_match_list(*get_match_list_resp); + println!("Event after parse: {:?}", event); while running { thread::sleep(Duration::from_millis(1000));