From 3b2a5b2c49476d903f0321fff23f5f4dfb717803 Mon Sep 17 00:00:00 2001 From: Adam Doyle Date: Wed, 9 Oct 2024 01:50:25 -0400 Subject: [PATCH] re-roll if name is taken --- server/src/websocket.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/server/src/websocket.rs b/server/src/websocket.rs index 7d78df1..a101862 100644 --- a/server/src/websocket.rs +++ b/server/src/websocket.rs @@ -37,20 +37,29 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc, ad let mut map = HashMap::new(); map.insert(addr, dm_tx.clone()); + // Roll for username and re-roll if taken + let mut username; + + loop { + username = format!( + "{} {}", + state.first_names.choose(&mut rand::thread_rng()).unwrap(), + state.last_names.choose(&mut rand::thread_rng()).unwrap(), + ); + + if !state.reserved_names.read().unwrap().contains(&username) { + break; + } + } + state .users_tx .send(UserHandlerMessage::NewUser( - User::new( - format!( - "{} {}", - state.first_names.choose(&mut rand::thread_rng()).unwrap(), - state.last_names.choose(&mut rand::thread_rng()).unwrap(), - ), - dm_tx.clone(), - ), + User::new(username, dm_tx.clone()), addr, )) - .await.expect("User handler is down"); + .await + .expect("User handler is down"); // Subscribe to receive from global broadcast channel let mut rx = state.broadcast_tx.subscribe();