diff --git a/server/src/lib.rs b/server/src/lib.rs index 804dcad..3a76a12 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -46,7 +46,6 @@ impl User { /// Shared state pub struct AppState { - pub offline_users: RwLock>>>, pub online_users: RwLock>>>, pub tx_broadcast: tokio::sync::broadcast::Sender, pub tx_game_handler: mpsc::Sender, diff --git a/server/src/main.rs b/server/src/main.rs index dd889a9..67bfc3f 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -82,11 +82,9 @@ async fn main() -> Result<()> { let (tx_incoming_message_handler, mut rx_incoming_message_handler) = mpsc::channel(32); let (tx_outgoing_message_handler, mut rx_outgoing_message_handler) = mpsc::channel(32); let (tx_user_handler, mut rx_user_handler) = mpsc::channel(32); - let offline_users = RwLock::new(HashMap::>>::new()); let online_users = RwLock::new(HashMap::>>::new()); let app_state = Arc::new(AppState { - offline_users, online_users, tx_broadcast: tx_broadcast.clone(), tx_game_handler, diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index 6ebf772..b19c0a0 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -24,6 +24,7 @@ pub struct UserHandler { users_by_id: HashMap>>, name_generator: NameGenerator, reserved_names: HashSet, + offline_users: HashMap>>, } pub enum DmUserMethod { @@ -50,7 +51,9 @@ pub enum SendUserMessage { impl UserHandler { /// Returns new UserHandler pub fn new(state: Arc) -> Self { + let offline_users = HashMap::>>::new(); UserHandler { + offline_users, state, users_by_id: HashMap::>>::new(), reserved_names: HashSet::::new(), @@ -225,25 +228,20 @@ impl UserHandler { if let Some(user) = self.state.online_users.read().unwrap().get(&addr) { old_name = user.read().unwrap().name.clone(); + + // User abandons current name by requesting a new one + self.reserved_names.remove(&old_name); } else { tracing::error!("Nonexistent user attempting login"); return; } // Resume user's old session if they exist as offline - if self - .state - .offline_users - .read() - .unwrap() - .contains_key(&new_name) - { + if self.offline_users.contains_key(&new_name) { let buf; // Copy over new tx if let Some(online_user) = self.state.online_users.write().unwrap().remove(&addr) { - if let Some(offline_user) = - self.state.offline_users.write().unwrap().remove(&new_name) - { + if let Some(offline_user) = self.offline_users.remove(&new_name) { offline_user.write().unwrap().tx = online_user.write().unwrap().tx.clone(); buf = offline_user; } else { @@ -292,6 +290,7 @@ impl UserHandler { Addr(addr), ) .await; + return; } else { // Reserve name self.reserved_names.insert(new_name.clone()); @@ -362,7 +361,7 @@ impl UserHandler { } /// Clean up after a user when they disconnect - async fn user_cleanup(&self, addr: SocketAddr) { + async fn user_cleanup(&mut self, addr: SocketAddr) { let user_name = self .state .online_users @@ -403,8 +402,8 @@ impl UserHandler { } /// Set user status to offline - fn set_user_offline(&self, user_name: String, addr: &SocketAddr) { - self.state.offline_users.write().unwrap().insert( + fn set_user_offline(&mut self, user_name: String, addr: &SocketAddr) { + self.offline_users.insert( user_name, self.state .online_users