Added function to parse match list into map of match tuples by division

master
noah metz 2024-01-21 01:04:58 -07:00
parent cd36ee4957
commit acff43c6f6
1 changed files with 68 additions and 12 deletions

@ -97,7 +97,7 @@ struct ArenaStateInfo {
start_ns: usize, start_ns: usize,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
enum Round { enum Round {
None = 0, None = 0,
Practice = 1, Practice = 1,
@ -115,13 +115,32 @@ enum Round {
Eliminations = 21, 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 { struct MatchTuple {
division: String, division: i32,
round: Round, round: Round,
instance: usize, instance: i32,
match_num: usize, match_num: i32,
session: usize, session: i32,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -136,41 +155,77 @@ struct MQTTMessage {
payload: String, payload: String,
} }
#[derive(Debug)]
struct Event { struct Event {
name: String, name: String,
divisions: Vec<Division>, divisions: HashMap<i32, Division>,
} }
impl Event { impl Event {
fn new(name: String) -> Event { fn new(name: String) -> Event {
Event{ Event{
name, name,
divisions: Vec::new(), divisions: HashMap::new(),
} }
} }
fn parse_match_list(self: &Event, match_list: BackendMessage) { fn parse_match_list(self: &mut Event, msg: BackendMessage) {
let divisions: Vec<Division> = Vec::new(); 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 { struct Division {
name: String, name: String,
matches: Vec<Match>, matches: Vec<Match>,
field_set: FieldSet, field_set: Option<FieldSet>,
} }
#[derive(Debug)]
struct FieldSet { struct FieldSet {
fields: Vec<Field>, fields: Vec<Field>,
} }
#[derive(Debug)]
struct Field { struct Field {
name: String, name: String,
current_match: u32, current_match: u32,
} }
#[derive(Debug)]
struct Match { struct Match {
name: String, match_tuple: MatchTuple,
} }
#[derive(Debug)] #[derive(Debug)]
@ -691,6 +746,7 @@ fn main() {
let get_match_list_resp = tm_connection.responses.recv().unwrap(); let get_match_list_resp = tm_connection.responses.recv().unwrap();
event.parse_match_list(*get_match_list_resp); event.parse_match_list(*get_match_list_resp);
println!("Event after parse: {:?}", event);
while running { while running {
thread::sleep(Duration::from_millis(1000)); thread::sleep(Duration::from_millis(1000));