diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index c4fe8ce..72edafb 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -1,5 +1,6 @@ use crate::user_handler::*; use crate::AppState; +use crate::DmUserMethod::*; use crate::GameHandlerMessage::*; use crate::SendUserMessage::*; use crate::User; @@ -154,15 +155,13 @@ impl GameHandler { == this_user_id { // Tell player no + let message = ChatMessage { + text: "You can't submit cards to judge, you ARE the judge!".to_string(), + }; let _ = self .state .users_tx - .send(DmUserAddr { - addr, - message: SendChatMessage(ChatMessage { - text: "You can't submit cards to judge, you ARE the judge!".to_string(), - }), - }) + .send(DmUser(SendChatMessage(message), Addr(addr))) .await; } else { // Ignore extra cards @@ -214,14 +213,7 @@ impl GameHandler { tracing::debug!("send for judging"); let czar_id = this_game.read().unwrap().czar.read().unwrap().uuid.clone(); - let _ = self - .state - .users_tx - .send(DmUserId { - id: czar_id, - message, - }) - .await; + let _ = self.state.users_tx.send(DmUser(message, Id(czar_id))).await; } } } diff --git a/server/src/message_handler.rs b/server/src/message_handler.rs index c3e81b7..503d001 100644 --- a/server/src/message_handler.rs +++ b/server/src/message_handler.rs @@ -38,10 +38,7 @@ impl MessageHandler { { self.state .users_tx - .send(UserLogIn { - username: user_log_in.username, - addr, - }) + .send(UserLogIn(user_log_in, addr)) .await .unwrap(); } diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index 53f4ce9..17b773f 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -1,4 +1,5 @@ use crate::AppState; +use crate::DmUserMethod::*; use crate::SendUserMessage::*; use crate::User; use crate::UserHandlerMessage::*; @@ -13,24 +14,16 @@ pub struct UserHandler { state: Arc, } +pub enum DmUserMethod { + Addr(SocketAddr), + Id(String), +} + /// For interacting with the user handler pub enum UserHandlerMessage { - NewUser { - user: User, - addr: SocketAddr, - }, - UserLogIn { - username: String, - addr: SocketAddr, - }, - DmUserAddr { - addr: SocketAddr, - message: SendUserMessage, - }, - DmUserId { - id: String, - message: SendUserMessage, - }, + NewUser(User, SocketAddr), + UserLogIn(UserLogInRequest, SocketAddr), + DmUser(SendUserMessage, DmUserMethod), } /// Types of messages that can be sent to a user as a DM @@ -50,62 +43,50 @@ impl UserHandler { /// Handles incoming messages pub async fn handle(&self, message: UserHandlerMessage) { match message { - NewUser { user, addr } => { + NewUser(user, addr) => { // TODO: make this not async self.set_up_new_user(user, addr).await } - UserLogIn { username, addr } => self.login(username, addr).await, - DmUserAddr { addr, message } => self.send_message_addr(addr, message).await, - DmUserId { id, message } => self.send_message_id(id, message).await, + UserLogIn(request, addr) => self.login(request, addr).await, + DmUser(message, method) => self.dm_user(message, method).await, } } - /// Send message direct to a single user via user id - async fn send_message_id(&self, id: String, message: SendUserMessage) { - let tx = self - .state - .users_by_id - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .tx - .clone(); + /// Send message direct to a single user + async fn dm_user(&self, message: SendUserMessage, method: DmUserMethod) { + let tx; - // TODO: this feels messy - match message { - SendUserUpdate(message) => { - let msg = to_string::(&message).unwrap(); - tx.send(msg).await.unwrap() + // Determine lookup method + match method { + Id(id) => { + tx = self + .state + .users_by_id + .read() + .unwrap() + .get(&id) + .unwrap() + .read() + .unwrap() + .tx + .clone(); } - SendChatMessage(message) => { - let msg = to_string::(&message).unwrap(); - tx.send(msg).await.unwrap() - } - SendJudgeRound(message) => { - let msg = to_string::(&message).unwrap(); - tx.send(msg).await.unwrap() + Addr(addr) => { + tx = self + .state + .online_users + .read() + .unwrap() + .get(&addr) + .unwrap() + .read() + .unwrap() + .tx + .clone(); } } - } - // TODO: Combine ^v these - - /// Send message direct to a single user via addr - async fn send_message_addr(&self, addr: SocketAddr, message: SendUserMessage) { - let tx = self - .state - .online_users - .read() - .unwrap() - .get(&addr) - .unwrap() - .read() - .unwrap() - .tx - .clone(); + // Serialize and send message match message { SendUserUpdate(message) => { let msg = to_string::(&message).unwrap(); @@ -169,9 +150,9 @@ impl UserHandler { } /// Handle user login - async fn login(&self, username: String, addr: SocketAddr) { + async fn login(&self, request: UserLogInRequest, addr: SocketAddr) { let broadcast_tx = self.state.broadcast_tx.clone(); - let new_name = username.clone(); + let new_name = request.username.clone(); let old_name = self .state .online_users @@ -246,19 +227,19 @@ impl UserHandler { .unwrap() .contains(&new_name) { - self.send_message_addr( - addr, + self.dm_user( SendChatMessage(ChatMessage { text: "Name is taken".to_string(), }), + Addr(addr), ) .await; tracing::debug!("{}", old_name.clone()); - self.send_message_addr( - addr, + self.dm_user( SendUserUpdate(UserUpdate { username: old_name.clone(), }), + Addr(addr), ) .await; } else { @@ -278,7 +259,7 @@ impl UserHandler { .unwrap() .write() .unwrap() - .change_name(username); + .change_name(request.username); // Send chat updates let msg = format! { @@ -292,11 +273,11 @@ impl UserHandler { } // Send the user their new name - self.send_message_addr( - addr, + self.dm_user( SendUserUpdate(UserUpdate { username: new_name.clone(), }), + Addr(addr), ) .await; diff --git a/server/src/websocket.rs b/server/src/websocket.rs index 86838ed..0784ca2 100644 --- a/server/src/websocket.rs +++ b/server/src/websocket.rs @@ -39,8 +39,8 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc, ad state .users_tx - .send(UserHandlerMessage::NewUser { - user: User::new( + .send(UserHandlerMessage::NewUser( + User::new( format!( "{} {}", state.first_names.choose(&mut rand::thread_rng()).unwrap(), @@ -49,7 +49,7 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc, ad dm_tx.clone(), ), addr, - }) + )) .await .unwrap();