Got match restart/pause working gracefully

master
noah metz 2024-01-22 14:13:09 -07:00
parent 9c7a6aaa01
commit e707420e3c
1 changed files with 55 additions and 3 deletions

@ -1103,6 +1103,47 @@ fn on_field_assigned(notice: tm::Notice, event: &mut Event, connection: &mut TMC
return messages; return messages;
} }
fn on_timer_reset(notice: tm::Notice, event: &mut Event, connection: &mut TMConnection) -> Vec<MQTTMessage> {
log::info!("TIMER_RESTART: {:#?}", &notice);
let mut messages = Vec::new();
let Some(field_time) = &notice.field_time else { return Vec::new() };
let Some(field_info) = get_field_tuple(&field_time.field) else { return Vec::new() };
let Some(field) = get_field(&mut event.field_sets, field_info) else { return Vec::new() };
let Some(current_match) = field.last_known_match else { return Vec::new() };
let Some(m) = get_match(&mut event.divisions, current_match) else { return Vec::new() };
m.state = MatchState{
state: GameState::Scheduled,
start: get_float_time() - connection.time_offset,
};
match connection.state_cancels.remove(&field_info) {
None => {},
Some(cancel_tx) => {
match cancel_tx.send(()) {
Ok(_) => {},
Err(_) => {},
}
},
}
let match_state_topic = current_match.topic("/state");
let field_state_topic = field_info.topic("/state");
let serialized = serde_json::to_string_pretty(&m.state).unwrap();
messages.push(MQTTMessage{
topic: match_state_topic,
payload: serialized.clone(),
});
messages.push(MQTTMessage{
topic: field_state_topic,
payload: serialized,
});
return messages;
}
fn on_timer_stop(notice: tm::Notice, event: &mut Event, connection: &mut TMConnection) -> Vec<MQTTMessage> { fn on_timer_stop(notice: tm::Notice, event: &mut Event, connection: &mut TMConnection) -> Vec<MQTTMessage> {
let mut messages = Vec::new(); let mut messages = Vec::new();
let Some(field_time) = &notice.field_time else { return Vec::new() }; let Some(field_time) = &notice.field_time else { return Vec::new() };
@ -1116,6 +1157,16 @@ fn on_timer_stop(notice: tm::Notice, event: &mut Event, connection: &mut TMConne
start: get_float_time() - connection.time_offset, start: get_float_time() - connection.time_offset,
}; };
match connection.state_cancels.remove(&field_info) {
None => {},
Some(cancel_tx) => {
match cancel_tx.send(()) {
Ok(_) => {},
Err(_) => {},
}
},
}
let match_state_topic = current_match.topic("/state"); let match_state_topic = current_match.topic("/state");
let field_state_topic = field_info.topic("/state"); let field_state_topic = field_info.topic("/state");
let serialized = serde_json::to_string_pretty(&m.state).unwrap(); let serialized = serde_json::to_string_pretty(&m.state).unwrap();
@ -1133,6 +1184,7 @@ fn on_timer_stop(notice: tm::Notice, event: &mut Event, connection: &mut TMConne
} }
fn on_timer_start(notice: tm::Notice, event: &mut Event, connection: &mut TMConnection) -> Vec<MQTTMessage> { fn on_timer_start(notice: tm::Notice, event: &mut Event, connection: &mut TMConnection) -> Vec<MQTTMessage> {
log::info!("TIMER_START: {:#?}", &notice);
let Some(field_time) = &notice.field_time else { return Vec::new() }; let Some(field_time) = &notice.field_time else { return Vec::new() };
let Some(field_info) = get_field_tuple(&field_time.field) else { return Vec::new() }; let Some(field_info) = get_field_tuple(&field_time.field) else { return Vec::new() };
let Some(field) = get_field(&mut event.field_sets, field_info) else { return Vec::new() }; let Some(field) = get_field(&mut event.field_sets, field_info) else { return Vec::new() };
@ -1140,7 +1192,6 @@ fn on_timer_start(notice: tm::Notice, event: &mut Event, connection: &mut TMConn
let Some(m) = get_match(&mut event.divisions, tuple) else { return Vec::new() }; let Some(m) = get_match(&mut event.divisions, tuple) else { return Vec::new() };
let Some(block_list) = &field_time.block_list else { return Vec::new() }; let Some(block_list) = &field_time.block_list else { return Vec::new() };
let Some(current_block_idx) = &field_time.current_block else { return Vec::new() }; let Some(current_block_idx) = &field_time.current_block else { return Vec::new() };
let Some(current_block_start) = &field_time.current_block_start else { return Vec::new() };
let Some(current_block_end) = &field_time.current_block_end else { return Vec::new() }; let Some(current_block_end) = &field_time.current_block_end else { return Vec::new() };
let current_block = &block_list.entries[*current_block_idx as usize]; let current_block = &block_list.entries[*current_block_idx as usize];
@ -1150,7 +1201,7 @@ fn on_timer_start(notice: tm::Notice, event: &mut Event, connection: &mut TMConn
if current_block.r#type == Some(2) { //Auto if current_block.r#type == Some(2) { //Auto
m.state = MatchState{ m.state = MatchState{
state: GameState::Autonomous, state: GameState::Autonomous,
start: *current_block_start - connection.time_offset, start: *current_block_end - (current_block.seconds() as f64) - connection.time_offset,
}; };
let field_state_topic = field_info.topic("/state"); let field_state_topic = field_info.topic("/state");
let match_state_topic = tuple.topic("/state"); let match_state_topic = tuple.topic("/state");
@ -1180,7 +1231,7 @@ fn on_timer_start(notice: tm::Notice, event: &mut Event, connection: &mut TMConn
} else if current_block.r#type == Some(3) { //Driver } else if current_block.r#type == Some(3) { //Driver
m.state = MatchState{ m.state = MatchState{
state: GameState::Driver, state: GameState::Driver,
start: *current_block_start - connection.time_offset, start: *current_block_end - (current_block.seconds() as f64) - connection.time_offset,
}; };
let field_state_topic = field_info.topic("/state"); let field_state_topic = field_info.topic("/state");
let match_state_topic = tuple.topic("/state"); let match_state_topic = tuple.topic("/state");
@ -1227,6 +1278,7 @@ fn main() {
callbacks.insert(tm::NoticeId::NoticeFieldTimerStarted, on_timer_start); callbacks.insert(tm::NoticeId::NoticeFieldTimerStarted, on_timer_start);
callbacks.insert(tm::NoticeId::NoticeFieldMatchAssigned, on_field_assigned); callbacks.insert(tm::NoticeId::NoticeFieldMatchAssigned, on_field_assigned);
callbacks.insert(tm::NoticeId::NoticeFieldTimerStopped, on_timer_stop); callbacks.insert(tm::NoticeId::NoticeFieldTimerStopped, on_timer_stop);
callbacks.insert(tm::NoticeId::NoticeFieldResetTimer, on_timer_reset);
let mut mqttoptions = MqttOptions::new("vex-bridge", "localhost", 1883); let mut mqttoptions = MqttOptions::new("vex-bridge", "localhost", 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5)); mqttoptions.set_keep_alive(Duration::from_secs(5));