diff --git a/gameloop.py b/gameloop.py index 9c6d227..95e1c05 100644 --- a/gameloop.py +++ b/gameloop.py @@ -21,6 +21,8 @@ async def gameloop(socket): match message[0]: case "CONNECT": await socket.send("READY") + case "RECONNECT": + await socket.send("READY") case "GAME": if message[1] == "START": diff --git a/src/main.rs b/src/main.rs index a9062e2..07b960d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -308,7 +308,9 @@ async fn handle_connection( if let Some(username) = sd.usernames.read().await.get(&addr).cloned() { let tournament_guard = sd.tournament.read().await; let client = sd.clients.read().await.get(&username).cloned().unwrap(); - let client = client.write().await; + let mut client = client.write().await; + client.ready = false; + 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; diff --git a/src/server.rs b/src/server.rs index 14b4970..b1734ee 100644 --- a/src/server.rs +++ b/src/server.rs @@ -170,8 +170,6 @@ impl Server { client.addr = addr; client.connection = tx.clone(); - let _ = send(&tx, "RECONNECT:ACK"); - if let Some(current_match_id) = client.current_match { let matches_guard = self.matches.read().await; let the_match = matches_guard.get(¤t_match_id).unwrap().read().await; @@ -189,6 +187,15 @@ impl Server { // Clear they're state just in case, even if it's not terminated let _ = send(&tx, "GAME:TERMINATED"); } + + let tournament = self.tournament.read().await.as_ref().cloned(); + if let Some(tourney) = tournament { + let tourney = tourney.read().await; + if !tourney.contains_player(requested_username) { + let _ = send(&tx, "RECONNECT:ACK"); + } + } + } else { return Err(anyhow::anyhow!(format!( "ERROR:INVALID:RECONNECT:{}", @@ -269,7 +276,8 @@ impl Server { let username = username.unwrap(); if clients_guard.get(&username).unwrap().read().await.ready { - return Err(anyhow::anyhow!("ERROR:INVALID:READY")); + let _ = send(&tx, "READY:ACK"); + return Ok(()); } let mut client = clients_guard.get(&username).unwrap().write().await;