fix: trying to remove clients that are only observers
This commit is contained in:
47
src/main.rs
47
src/main.rs
@@ -240,6 +240,8 @@ async fn handle_connection(
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
// TODO: add to ledger
|
||||||
|
|
||||||
// Check game end conditions
|
// Check game end conditions
|
||||||
let (winner, filled) = {
|
let (winner, filled) = {
|
||||||
let mut result = (Color::None, false);
|
let mut result = (Color::None, false);
|
||||||
@@ -394,6 +396,7 @@ async fn handle_connection(
|
|||||||
Ok(match_id) => {
|
Ok(match_id) => {
|
||||||
let result = watch(&matches, match_id, addr).await;
|
let result = watch(&matches, match_id, addr).await;
|
||||||
if result.is_err() { let _ = send(&tx, "ERROR:INVALID:WATCH"); }
|
if result.is_err() { let _ = send(&tx, "ERROR:INVALID:WATCH"); }
|
||||||
|
// TODO: send ledger
|
||||||
}
|
}
|
||||||
Err(_) => { let _ = send(&tx, "ERROR:INVALID:WATCH"); }
|
Err(_) => { let _ = send(&tx, "ERROR:INVALID:WATCH"); }
|
||||||
}
|
}
|
||||||
@@ -505,32 +508,34 @@ async fn handle_connection(
|
|||||||
|
|
||||||
// Remove and terminate any matches
|
// Remove and terminate any matches
|
||||||
// TODO: Support reconnecting behaviors
|
// TODO: Support reconnecting behaviors
|
||||||
if let Some(match_id) = clients.read().await.get(&addr).unwrap().read().await.current_match {
|
if clients.read().await.get(&addr).is_some() { // We may not be a client disconnecting
|
||||||
let matches_guard = matches.read().await;
|
if let Some(match_id) = clients.read().await.get(&addr).unwrap().read().await.current_match {
|
||||||
let clients_guard = clients.read().await;
|
let matches_guard = matches.read().await;
|
||||||
let the_match = matches_guard.get(&match_id).unwrap().read().await;
|
let clients_guard = clients.read().await;
|
||||||
|
let the_match = matches_guard.get(&match_id).unwrap().read().await;
|
||||||
|
|
||||||
let player1 = clients_guard.get(&the_match.player1).unwrap().read().await;
|
let player1 = clients_guard.get(&the_match.player1).unwrap().read().await;
|
||||||
let player2 = clients_guard.get(&the_match.player2).unwrap().read().await;
|
let player2 = clients_guard.get(&the_match.player2).unwrap().read().await;
|
||||||
|
|
||||||
let _ = send(&player1.connection, "GAME:TERMINATED");
|
let _ = send(&player1.connection, "GAME:TERMINATED");
|
||||||
let _ = send(&player2.connection, "GAME:TERMINATED");
|
let _ = send(&player2.connection, "GAME:TERMINATED");
|
||||||
|
|
||||||
broadcast_message(&the_match.viewers, &observers, "GAME:TERMINATED").await;
|
broadcast_message(&the_match.viewers, &observers, "GAME:TERMINATED").await;
|
||||||
|
|
||||||
drop(player1);
|
drop(player1);
|
||||||
drop(player2);
|
drop(player2);
|
||||||
drop(the_match);
|
drop(the_match);
|
||||||
drop(matches_guard);
|
drop(matches_guard);
|
||||||
drop(clients_guard);
|
drop(clients_guard);
|
||||||
|
|
||||||
matches.write().await.remove(&match_id);
|
matches.write().await.remove(&match_id);
|
||||||
}
|
}
|
||||||
|
let client = clients.write().await.remove(&addr).unwrap();
|
||||||
let client = clients.write().await.remove(&addr).unwrap();
|
let username = client.read().await.username.clone();
|
||||||
let username = client.read().await.username.clone();
|
usernames.write().await.remove(&username);
|
||||||
observers.write().await.remove(&addr);
|
} else {
|
||||||
usernames.write().await.remove(&username);
|
observers.write().await.remove(&addr);
|
||||||
|
}
|
||||||
|
|
||||||
let mut admin_guard = admin.write().await;
|
let mut admin_guard = admin.write().await;
|
||||||
if let Some(admin_addr) = *admin_guard {
|
if let Some(admin_addr) = *admin_guard {
|
||||||
|
|||||||
Reference in New Issue
Block a user