fix: improve disconnect behaviors
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -31,3 +31,7 @@ target
|
|||||||
/node_modules
|
/node_modules
|
||||||
|
|
||||||
bracket_pairings.txt
|
bracket_pairings.txt
|
||||||
|
|
||||||
|
.cursor/
|
||||||
|
.claude/
|
||||||
|
.codex/
|
||||||
36
src/main.rs
36
src/main.rs
@@ -305,33 +305,39 @@ async fn handle_connection(
|
|||||||
|
|
||||||
// Remove and terminate any matches
|
// Remove and terminate any matches
|
||||||
// We may not be a client disconnecting, do this check
|
// We may not be a client disconnecting, do this check
|
||||||
let clients_guard = sd.clients.read().await;
|
if let Some(username) = sd.usernames.read().await.get(&addr).cloned() {
|
||||||
for client in clients_guard.values() {
|
|
||||||
let client = client.read().await;
|
|
||||||
if client.addr == addr {
|
|
||||||
let username = client.username.clone();
|
|
||||||
let tournament_guard = sd.tournament.read().await;
|
let tournament_guard = sd.tournament.read().await;
|
||||||
|
let client = sd.clients.read().await.get(&username).cloned().unwrap();
|
||||||
|
let client = client.write().await;
|
||||||
if client.current_match.is_some() {
|
if client.current_match.is_some() {
|
||||||
|
let current_match = sd.matches.read().await.get(&client.current_match.unwrap()).cloned().unwrap();
|
||||||
|
let current_match = current_match.read().await;
|
||||||
|
if current_match.timeout_thread.is_some() {
|
||||||
|
current_match.timeout_thread.as_ref().unwrap().abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
if current_match.demo_mode {
|
||||||
|
sd.matches.write().await.remove(¤t_match.id);
|
||||||
|
sd.broadcast(&format!("GAME:{}:TERMINATED", current_match.id)).await;
|
||||||
|
sd.clients.write().await.remove(&username);
|
||||||
|
} else {
|
||||||
sd.disconnected_clients.write().await.push(username.clone());
|
sd.disconnected_clients.write().await.push(username.clone());
|
||||||
|
}
|
||||||
} else if tournament_guard.is_some() {
|
} else if tournament_guard.is_some() {
|
||||||
let tourney = tournament_guard.clone().unwrap();
|
let tourney = tournament_guard.clone().unwrap();
|
||||||
if tourney.read().await.contains_player(username.clone()) {
|
if tourney.read().await.contains_player(username.clone()) {
|
||||||
sd.disconnected_clients.write().await.push(username.clone());
|
sd.disconnected_clients.write().await.push(username.clone());
|
||||||
}
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
drop(client);
|
|
||||||
drop(clients_guard);
|
|
||||||
|
|
||||||
sd.clients.write().await.remove(&username);
|
sd.clients.write().await.remove(&username);
|
||||||
sd.usernames.write().await.remove(&addr);
|
}
|
||||||
|
} else {
|
||||||
|
sd.clients.write().await.remove(&username);
|
||||||
|
}
|
||||||
|
|
||||||
sd.broadcast(&format!("DISCONNECT:{}", username)).await;
|
sd.broadcast(&format!("DISCONNECT:{}", username)).await;
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sd.usernames.write().await.remove(&addr);
|
||||||
sd.observers.write().await.remove(&addr);
|
sd.observers.write().await.remove(&addr);
|
||||||
|
|
||||||
let mut admin_guard = sd.admin.write().await;
|
let mut admin_guard = sd.admin.write().await;
|
||||||
|
|||||||
@@ -63,11 +63,7 @@ impl Server {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut reconnecting = false;
|
let reconnecting = self.disconnected_clients.read().await.contains(&requested_username);
|
||||||
let disconnected_guard = self.disconnected_clients.read().await;
|
|
||||||
if disconnected_guard.contains(&requested_username) {
|
|
||||||
reconnecting = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
let clients_guard = self.clients.read().await;
|
let clients_guard = self.clients.read().await;
|
||||||
let existing_client = clients_guard.get(&requested_username).cloned();
|
let existing_client = clients_guard.get(&requested_username).cloned();
|
||||||
@@ -176,8 +172,9 @@ impl Server {
|
|||||||
|
|
||||||
let _ = send(&tx, "RECONNECT:ACK");
|
let _ = send(&tx, "RECONNECT:ACK");
|
||||||
|
|
||||||
|
if let Some(current_match_id) = client.current_match {
|
||||||
let matches_guard = self.matches.read().await;
|
let matches_guard = self.matches.read().await;
|
||||||
let the_match = matches_guard.get(&client.current_match.unwrap()).unwrap().read().await;
|
let the_match = matches_guard.get(¤t_match_id).unwrap().read().await;
|
||||||
|
|
||||||
let last = the_match.ledger.last();
|
let last = the_match.ledger.last();
|
||||||
if last.is_some() && last.unwrap().0 != client.color {
|
if last.is_some() && last.unwrap().0 != client.color {
|
||||||
@@ -185,6 +182,12 @@ impl Server {
|
|||||||
&tx,
|
&tx,
|
||||||
&format!("OPPONENT:{}", the_match.ledger.last().unwrap().1),
|
&format!("OPPONENT:{}", the_match.ledger.last().unwrap().1),
|
||||||
);
|
);
|
||||||
|
} else if last.is_none() && client.color == Color::Red {
|
||||||
|
let _ = send(&tx, "GAME:START:1");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Clear they're state just in case, even if it's not terminated
|
||||||
|
let _ = send(&tx, "GAME:TERMINATED");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow::anyhow!(format!(
|
return Err(anyhow::anyhow!(format!(
|
||||||
@@ -281,6 +284,7 @@ impl Server {
|
|||||||
let mut client = clients_guard.get(&username).unwrap().write().await;
|
let mut client = clients_guard.get(&username).unwrap().write().await;
|
||||||
let mut opponent = clients_guard.get(&opponent_username).unwrap().write().await;
|
let mut opponent = clients_guard.get(&opponent_username).unwrap().write().await;
|
||||||
|
|
||||||
|
if opponent.ready {
|
||||||
let match_id: u32 = gen_match_id(&self.matches).await;
|
let match_id: u32 = gen_match_id(&self.matches).await;
|
||||||
let new_match = Arc::new(RwLock::new(Match::new(
|
let new_match = Arc::new(RwLock::new(Match::new(
|
||||||
match_id,
|
match_id,
|
||||||
@@ -329,6 +333,7 @@ impl Server {
|
|||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let clients_guard = self.clients.read().await;
|
let clients_guard = self.clients.read().await;
|
||||||
let mut client = clients_guard.get(&username).unwrap().write().await;
|
let mut client = clients_guard.get(&username).unwrap().write().await;
|
||||||
|
|||||||
Reference in New Issue
Block a user