re-roll if name is taken
This commit is contained in:
parent
04b350d598
commit
3b2a5b2c49
1 changed files with 18 additions and 9 deletions
|
@ -37,20 +37,29 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc<AppState>, ad
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert(addr, dm_tx.clone());
|
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
|
state
|
||||||
.users_tx
|
.users_tx
|
||||||
.send(UserHandlerMessage::NewUser(
|
.send(UserHandlerMessage::NewUser(
|
||||||
User::new(
|
User::new(username, dm_tx.clone()),
|
||||||
format!(
|
|
||||||
"{} {}",
|
|
||||||
state.first_names.choose(&mut rand::thread_rng()).unwrap(),
|
|
||||||
state.last_names.choose(&mut rand::thread_rng()).unwrap(),
|
|
||||||
),
|
|
||||||
dm_tx.clone(),
|
|
||||||
),
|
|
||||||
addr,
|
addr,
|
||||||
))
|
))
|
||||||
.await.expect("User handler is down");
|
.await
|
||||||
|
.expect("User handler is down");
|
||||||
|
|
||||||
// Subscribe to receive from global broadcast channel
|
// Subscribe to receive from global broadcast channel
|
||||||
let mut rx = state.broadcast_tx.subscribe();
|
let mut rx = state.broadcast_tx.subscribe();
|
||||||
|
|
Loading…
Add table
Reference in a new issue