fix: send all previous moves with ack

This commit is contained in:
2025-12-04 18:33:02 -05:00
Unverified
parent ef622748f9
commit 55f3d3fc4b

View File

@@ -487,39 +487,38 @@ async fn handle_connection(
continue; continue;
} }
if !demo_mode { let connection_to_send = if !demo_mode { opponent.connection.clone() } else { tx.clone() };
let connection = opponent.connection.clone(); let column = if !demo_mode { column_parse.clone()? } else { random_move(&current_match.board) };
let column = column_parse.clone()?; if demo_mode {
let waiting = *waiting_timeout.read().await as i64 * 1000 + (rand::rng().random_range(0..=500) - 250); current_match.place_token(Color::Blue, column);
let matches_move = matches.clone(); }
let match_id_move = current_match.id;
current_match.wait_thread = Some(tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_millis(waiting as u64)).await;
let mut matches_guard = matches_move.write().await; let waiting = *waiting_timeout.read().await as i64 * 1000 + (rand::rng().random_range(0..=500) - 250);
let mut current_match = matches_guard.get_mut(&match_id_move).unwrap().write().await; let matches_move = matches.clone();
let move_to_dispatch = current_match.move_to_dispatch.clone(); let observers_move = observers.clone();
current_match.ledger.push(move_to_dispatch); let match_id_move = current_match.id;
current_match.move_to_dispatch = (Color::None, 0); current_match.wait_thread = Some(tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_millis(waiting as u64)).await;
drop(current_match); let mut matches_guard = matches_move.write().await;
drop(matches_guard); let mut current_match = matches_guard.get_mut(&match_id_move).unwrap().write().await;
let move_to_dispatch = current_match.move_to_dispatch.clone();
current_match.ledger.push(move_to_dispatch);
current_match.move_to_dispatch = (Color::None, 0);
let _ = send( if demo_mode {
&connection, broadcast_message(
&format!("OPPONENT:{}", column), &current_match.viewers,
); &observers_move,
})); &format!("GAME:MOVE:{}:{}", "demo", column),
} else { ).await;
let random_move = random_move(&current_match.board); }
current_match.place_token(Color::Blue, random_move);
let _ = send(&tx, &format!("OPPONENT:{}", random_move)); drop(current_match);
broadcast_message( drop(matches_guard);
&current_match.viewers,
&observers, let _ = send(&connection_to_send, &format!("OPPONENT:{}", column));
&format!("GAME:MOVE:{}:{}", "demo", random_move), }));
).await;
}
} }
else if text == "PLAYER:LIST" { else if text == "PLAYER:LIST" {
@@ -573,15 +572,19 @@ async fn handle_connection(
drop(the_match); drop(the_match);
drop(matches_guard); drop(matches_guard);
let _ = send(&tx, &format!("GAME:WATCH:ACK:{},{},{}", match_id, player1, player2)); let mut message = format!("GAME:WATCH:ACK:{},{},{}|", match_id, player1, player2);
for a_move in ledger { for a_move in ledger {
if a_move.0 == Color::Red { if a_move.0 == Color::Red {
let _ = send(&tx, &format!("GAME:MOVE:{}:{}", player1, a_move.1)); message += &format!("{},{}|", player1, a_move.1);
} else { } else {
let _ = send(&tx, &format!("GAME:MOVE:{}:{}", player2, a_move.1)); message += &format!("{},{}|", player2, a_move.1);
} }
} }
message.pop();
let _ = send(&tx, &message);
} }
Err(_) => { let _ = send(&tx, "ERROR:INVALID:WATCH"); continue; } Err(_) => { let _ = send(&tx, "ERROR:INVALID:WATCH"); continue; }
} }