diff --git a/src/main.rs b/src/main.rs index e7a8dbe..2c90388 100644 --- a/src/main.rs +++ b/src/main.rs @@ -525,9 +525,7 @@ impl TMClient { // it only processes a single requests/response at a time. This is fine since there's // only a single callback thread though. for request in self.requests.try_iter() { - let time = SystemTime::now(); - let millis = time.duration_since(UNIX_EPOCH).unwrap(); - let packet = BackendPacket::new(TM_HEADER, (millis.as_millis() as f64)/1000.0, 2, self.last_seq_num + 1, request.as_bytes()); + let packet = BackendPacket::new(TM_HEADER, get_float_time(), 2, self.last_seq_num + 1, request.as_bytes()); match self.stream.write(&packet.as_bytes()) { Ok(_) => { log::debug!("Sent: {:?}", packet); @@ -845,33 +843,51 @@ fn on_match_list_update(_notice: tm::Notice, event: &mut Event, connection: &mut return messages; } +fn get_float_time() -> f64 { + let time = SystemTime::now(); + let millis = time.duration_since(UNIX_EPOCH).unwrap(); + return (millis.as_millis() as f64)/1000.0; +} + fn on_field_assigned(notice: tm::Notice, event: &mut Event, connection: &mut TMConnection) -> Vec { let Some(field_info) = ¬ice.field else { return Vec::new() }; let Some(tuple) = get_affected_match(¬ice) else { return Vec::new() }; let Some(field_set) = &mut event.field_sets.get_mut(&field_info.field_set_id()) else { return Vec::new() }; let Some(field) = &mut field_set.fields.get_mut(&field_info.id()) else { return Vec::new() }; - let Some(division) = &event.divisions.get(&tuple.division) else { return Vec::new() }; - let Some(m) = &division.matches.iter().find(|a| a.tuple == tuple) else { return Vec::new() }; + let Some(division) = &mut event.divisions.get_mut(&tuple.division) else { return Vec::new() }; + let Some(m) = &mut division.matches.iter_mut().find(|a| a.tuple == tuple) else { return Vec::new() }; + m.state = Some(MatchState{ + state: GameState::Scheduled, + start: get_float_time(), + }); field.last_known_match = Some(tuple); let mut messages = Vec::new(); - if let Some(score) = &m.score { - let serialized = serde_json::to_string_pretty(&score).unwrap(); - let topic = format!("field/{}/{}/score", &field_info.field_set_id(), &field_info.id()); + + if let Some(state) = &m.state { + let serialized = serde_json::to_string_pretty(&state).unwrap(); + let field_topic = format!("field/{}/{}/state", &field_info.field_set_id(), &field_info.id()); + let match_topic = format!("division/{}/{:?}/{}/state", &m.tuple.division, &m.tuple.round, &m.tuple.match_num); messages.push(MQTTMessage{ - topic, + topic: field_topic, + payload: serialized.clone(), + }); + + messages.push(MQTTMessage{ + topic: match_topic, payload: serialized, }); } - if let Some(state) = &m.state { - let serialized = serde_json::to_string_pretty(&state).unwrap(); - let topic = format!("field/{}/{}/state", &field_info.field_set_id(), &field_info.id()); + if let Some(score) = &m.score { + let serialized = serde_json::to_string_pretty(&score).unwrap(); + let topic = format!("field/{}/{}/score", &field_info.field_set_id(), &field_info.id()); messages.push(MQTTMessage{ topic, payload: serialized, }); } + return messages; }