fix: send all previous moves with ack
This commit is contained in:
43
src/main.rs
43
src/main.rs
@@ -487,11 +487,15 @@ 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(¤t_match.board) };
|
||||||
let column = column_parse.clone()?;
|
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 waiting = *waiting_timeout.read().await as i64 * 1000 + (rand::rng().random_range(0..=500) - 250);
|
||||||
let matches_move = matches.clone();
|
let matches_move = matches.clone();
|
||||||
|
let observers_move = observers.clone();
|
||||||
let match_id_move = current_match.id;
|
let match_id_move = current_match.id;
|
||||||
current_match.wait_thread = Some(tokio::spawn(async move {
|
current_match.wait_thread = Some(tokio::spawn(async move {
|
||||||
tokio::time::sleep(tokio::time::Duration::from_millis(waiting as u64)).await;
|
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.ledger.push(move_to_dispatch);
|
||||||
current_match.move_to_dispatch = (Color::None, 0);
|
current_match.move_to_dispatch = (Color::None, 0);
|
||||||
|
|
||||||
|
if demo_mode {
|
||||||
|
broadcast_message(
|
||||||
|
¤t_match.viewers,
|
||||||
|
&observers_move,
|
||||||
|
&format!("GAME:MOVE:{}:{}", "demo", column),
|
||||||
|
).await;
|
||||||
|
}
|
||||||
|
|
||||||
drop(current_match);
|
drop(current_match);
|
||||||
drop(matches_guard);
|
drop(matches_guard);
|
||||||
|
|
||||||
let _ = send(
|
let _ = send(&connection_to_send, &format!("OPPONENT:{}", column));
|
||||||
&connection,
|
|
||||||
&format!("OPPONENT:{}", column),
|
|
||||||
);
|
|
||||||
}));
|
}));
|
||||||
} else {
|
|
||||||
let random_move = random_move(¤t_match.board);
|
|
||||||
current_match.place_token(Color::Blue, random_move);
|
|
||||||
let _ = send(&tx, &format!("OPPONENT:{}", random_move));
|
|
||||||
broadcast_message(
|
|
||||||
¤t_match.viewers,
|
|
||||||
&observers,
|
|
||||||
&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; }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user