diff --git a/src/main.rs b/src/main.rs index 8e573e8..208f729 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,14 +159,6 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { token: format!("{:x}", random::random::()), }; - for usr in &mut channel.lock().unwrap().online_users { - if usr.name == user.name { - usr.status = user.status.clone(); - usr.token = user.token.clone(); - break; - } - } - stream .write_fmt(format_args!( "\x1b[0m\x1b[32;1mYour token to rejoin after quitting is {}.\x1b[0m\n", @@ -180,7 +172,19 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { token: "".to_owned(), }; - channel.lock().unwrap().online_users.push(user.clone()); + let mut broke = false; + for usr in &mut channel.lock().unwrap().online_users { + if usr.name == user.name { + usr.status = user.status.clone(); + usr.token = user.token.clone(); + broke = true; + break; + } + } + + if !broke { + channel.lock().unwrap().online_users.push(user.clone()); + } let (recieve_terminate_send, recieve_terminate_recv) = std::sync::mpsc::channel::<()>(); let recieve_thread: JoinHandle<_>;