[Fix] Anonymous users now have names
This commit is contained in:
parent
e4b687c884
commit
e8faca34e2
3 changed files with 47 additions and 4 deletions
|
|
@ -10,6 +10,7 @@ use tokio_util::compat::Compat;
|
||||||
use tungstenite::Utf8Bytes;
|
use tungstenite::Utf8Bytes;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::anonymous_clients::anonymous_manager::generate_username;
|
||||||
use crate::calls::call_manager;
|
use crate::calls::call_manager;
|
||||||
use crate::data::{
|
use crate::data::{
|
||||||
communication::{CommunicationType, CommunicationValue, DataTypes},
|
communication::{CommunicationType, CommunicationValue, DataTypes},
|
||||||
|
|
@ -38,6 +39,7 @@ impl AnonymousClientConnection {
|
||||||
sender: WebSocketSender<Compat<tokio::net::TcpStream>>,
|
sender: WebSocketSender<Compat<tokio::net::TcpStream>>,
|
||||||
receiver: WebSocketReceiver<Compat<tokio::net::TcpStream>>,
|
receiver: WebSocketReceiver<Compat<tokio::net::TcpStream>>,
|
||||||
) -> Arc<Self> {
|
) -> Arc<Self> {
|
||||||
|
let username: String = generate_username();
|
||||||
Arc::new(Self {
|
Arc::new(Self {
|
||||||
sender: Arc::new(RwLock::new(sender)),
|
sender: Arc::new(RwLock::new(sender)),
|
||||||
receiver: Arc::new(RwLock::new(receiver)),
|
receiver: Arc::new(RwLock::new(receiver)),
|
||||||
|
|
@ -50,8 +52,8 @@ impl AnonymousClientConnection {
|
||||||
ping: Arc::new(RwLock::new(-1)),
|
ping: Arc::new(RwLock::new(-1)),
|
||||||
interested_users: Arc::new(RwLock::new(Vec::new())),
|
interested_users: Arc::new(RwLock::new(Vec::new())),
|
||||||
is_open: Arc::new(RwLock::new(true)),
|
is_open: Arc::new(RwLock::new(true)),
|
||||||
user_name: Arc::new(RwLock::new(String::new())),
|
user_name: Arc::new(RwLock::new(username.to_lowercase())),
|
||||||
display_name: Arc::new(RwLock::new(String::new())),
|
display_name: Arc::new(RwLock::new(username)),
|
||||||
avatar: Arc::new(RwLock::new(String::new())),
|
avatar: Arc::new(RwLock::new(String::new())),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::anonymous_clients::anonymous_client_connection::AnonymousClientConnection;
|
use crate::anonymous_clients::anonymous_client_connection::AnonymousClientConnection;
|
||||||
|
|
||||||
|
|
@ -34,3 +35,17 @@ pub async fn get_anonymous_user_by_name(
|
||||||
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: implement check if taken
|
||||||
|
pub fn generate_username() -> String {
|
||||||
|
let adjectives = ["Swift", "Clever", "Brave", "Sneaky", "Fierce"];
|
||||||
|
let nouns = ["Tiger", "Eagle", "Shark", "Wolf", "Dragon"];
|
||||||
|
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let adj = adjectives.choose(&mut rng).unwrap();
|
||||||
|
let noun = nouns.choose(&mut rng).unwrap();
|
||||||
|
|
||||||
|
let number: u16 = rng.gen_range(0..10000);
|
||||||
|
|
||||||
|
format!("{}{}{}", adj, noun, number)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -401,11 +401,37 @@ impl OmegaConnection {
|
||||||
OMEGA_CONNECTION.send_message(&cv).await;
|
OMEGA_CONNECTION.send_message(&cv).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn await_connection(&self, timeout_duration: Option<Duration>) -> Result<(), String> {
|
||||||
|
if *self.is_connected.read().await {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let timeout = timeout_duration.unwrap_or(Duration::from_secs(10));
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
|
loop {
|
||||||
|
{
|
||||||
|
if *self.is_connected.read().await {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if start.elapsed() >= timeout {
|
||||||
|
return Err(format!(
|
||||||
|
"Connection not established within {} seconds",
|
||||||
|
timeout.as_secs()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep(Duration::from_millis(100)).await; // short interval polling
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn await_response(
|
pub async fn await_response(
|
||||||
&self,
|
&self,
|
||||||
cv: &CommunicationValue,
|
cv: &CommunicationValue,
|
||||||
timeout_duration: Option<Duration>,
|
timeout_duration: Option<Duration>,
|
||||||
) -> Result<CommunicationValue, String> {
|
) -> Result<CommunicationValue, String> {
|
||||||
|
let _ = self.await_connection(timeout_duration).await;
|
||||||
let (tx, mut rx) = mpsc::channel(1);
|
let (tx, mut rx) = mpsc::channel(1);
|
||||||
let msg_id = cv.get_id();
|
let msg_id = cv.get_id();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue