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,11 +487,15 @@ async fn handle_connection(
continue;
}
if !demo_mode {
let connection = opponent.connection.clone();
let column = column_parse.clone()?;
let connection_to_send = if !demo_mode { opponent.connection.clone() } else { tx.clone() };
let column = if !demo_mode { column_parse.clone()? } else { random_move(&current_match.board) };
if demo_mode {
current_match.place_token(Color::Blue, column);
}
let waiting = *waiting_timeout.read().await as i64 * 1000 + (rand::rng().random_range(0..=500) - 250);
let matches_move = matches.clone();
let observers_move = observers.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;
@@ -502,24 +506,19 @@ async fn handle_connection(
current_match.ledger.push(move_to_dispatch);
current_match.move_to_dispatch = (Color::None, 0);
if demo_mode {
broadcast_message(
&current_match.viewers,
&observers_move,
&format!("GAME:MOVE:{}:{}", "demo", column),
).await;
}
drop(current_match);
drop(matches_guard);
let _ = send(
&connection,
&format!("OPPONENT:{}", column),
);
let _ = send(&connection_to_send, &format!("OPPONENT:{}", column));
}));
} else {
let random_move = random_move(&current_match.board);
current_match.place_token(Color::Blue, random_move);
let _ = send(&tx, &format!("OPPONENT:{}", random_move));
broadcast_message(
&current_match.viewers,
&observers,
&format!("GAME:MOVE:{}:{}", "demo", random_move),
).await;
}
}
else if text == "PLAYER:LIST" {
@@ -573,15 +572,19 @@ async fn handle_connection(
drop(the_match);
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 {
if a_move.0 == Color::Red {
let _ = send(&tx, &format!("GAME:MOVE:{}:{}", player1, a_move.1));
message += &format!("{},{}|", player1, a_move.1);
} 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; }
}