This commit is contained in:
Adam 2024-08-22 17:05:26 -04:00
parent 5845e4793b
commit 97e0a69a7b
4 changed files with 61 additions and 91 deletions

View file

@ -1,5 +1,6 @@
use crate::user_handler::*; use crate::user_handler::*;
use crate::AppState; use crate::AppState;
use crate::DmUserMethod::*;
use crate::GameHandlerMessage::*; use crate::GameHandlerMessage::*;
use crate::SendUserMessage::*; use crate::SendUserMessage::*;
use crate::User; use crate::User;
@ -154,15 +155,13 @@ impl GameHandler {
== this_user_id == this_user_id
{ {
// Tell player no // Tell player no
let message = ChatMessage {
text: "You can't submit cards to judge, you ARE the judge!".to_string(),
};
let _ = self let _ = self
.state .state
.users_tx .users_tx
.send(DmUserAddr { .send(DmUser(SendChatMessage(message), Addr(addr)))
addr,
message: SendChatMessage(ChatMessage {
text: "You can't submit cards to judge, you ARE the judge!".to_string(),
}),
})
.await; .await;
} else { } else {
// Ignore extra cards // Ignore extra cards
@ -214,14 +213,7 @@ impl GameHandler {
tracing::debug!("send for judging"); tracing::debug!("send for judging");
let czar_id = this_game.read().unwrap().czar.read().unwrap().uuid.clone(); let czar_id = this_game.read().unwrap().czar.read().unwrap().uuid.clone();
let _ = self let _ = self.state.users_tx.send(DmUser(message, Id(czar_id))).await;
.state
.users_tx
.send(DmUserId {
id: czar_id,
message,
})
.await;
} }
} }
} }

View file

@ -38,10 +38,7 @@ impl MessageHandler {
{ {
self.state self.state
.users_tx .users_tx
.send(UserLogIn { .send(UserLogIn(user_log_in, addr))
username: user_log_in.username,
addr,
})
.await .await
.unwrap(); .unwrap();
} }

View file

@ -1,4 +1,5 @@
use crate::AppState; use crate::AppState;
use crate::DmUserMethod::*;
use crate::SendUserMessage::*; use crate::SendUserMessage::*;
use crate::User; use crate::User;
use crate::UserHandlerMessage::*; use crate::UserHandlerMessage::*;
@ -13,24 +14,16 @@ pub struct UserHandler {
state: Arc<AppState>, state: Arc<AppState>,
} }
pub enum DmUserMethod {
Addr(SocketAddr),
Id(String),
}
/// For interacting with the user handler /// For interacting with the user handler
pub enum UserHandlerMessage { pub enum UserHandlerMessage {
NewUser { NewUser(User, SocketAddr),
user: User, UserLogIn(UserLogInRequest, SocketAddr),
addr: SocketAddr, DmUser(SendUserMessage, DmUserMethod),
},
UserLogIn {
username: String,
addr: SocketAddr,
},
DmUserAddr {
addr: SocketAddr,
message: SendUserMessage,
},
DmUserId {
id: String,
message: SendUserMessage,
},
} }
/// Types of messages that can be sent to a user as a DM /// Types of messages that can be sent to a user as a DM
@ -50,19 +43,23 @@ impl UserHandler {
/// Handles incoming messages /// Handles incoming messages
pub async fn handle(&self, message: UserHandlerMessage) { pub async fn handle(&self, message: UserHandlerMessage) {
match message { match message {
NewUser { user, addr } => { NewUser(user, addr) => {
// TODO: make this not async // TODO: make this not async
self.set_up_new_user(user, addr).await self.set_up_new_user(user, addr).await
} }
UserLogIn { username, addr } => self.login(username, addr).await, UserLogIn(request, addr) => self.login(request, addr).await,
DmUserAddr { addr, message } => self.send_message_addr(addr, message).await, DmUser(message, method) => self.dm_user(message, method).await,
DmUserId { id, message } => self.send_message_id(id, message).await,
} }
} }
/// Send message direct to a single user via user id /// Send message direct to a single user
async fn send_message_id(&self, id: String, message: SendUserMessage) { async fn dm_user(&self, message: SendUserMessage, method: DmUserMethod) {
let tx = self let tx;
// Determine lookup method
match method {
Id(id) => {
tx = self
.state .state
.users_by_id .users_by_id
.read() .read()
@ -73,29 +70,9 @@ impl UserHandler {
.unwrap() .unwrap()
.tx .tx
.clone(); .clone();
// TODO: this feels messy
match message {
SendUserUpdate(message) => {
let msg = to_string::<UserUpdate>(&message).unwrap();
tx.send(msg).await.unwrap()
} }
SendChatMessage(message) => { Addr(addr) => {
let msg = to_string::<ChatMessage>(&message).unwrap(); tx = self
tx.send(msg).await.unwrap()
}
SendJudgeRound(message) => {
let msg = to_string::<JudgeRound>(&message).unwrap();
tx.send(msg).await.unwrap()
}
}
}
// 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 .state
.online_users .online_users
.read() .read()
@ -106,6 +83,10 @@ impl UserHandler {
.unwrap() .unwrap()
.tx .tx
.clone(); .clone();
}
}
// Serialize and send message
match message { match message {
SendUserUpdate(message) => { SendUserUpdate(message) => {
let msg = to_string::<UserUpdate>(&message).unwrap(); let msg = to_string::<UserUpdate>(&message).unwrap();
@ -169,9 +150,9 @@ impl UserHandler {
} }
/// Handle user login /// 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 broadcast_tx = self.state.broadcast_tx.clone();
let new_name = username.clone(); let new_name = request.username.clone();
let old_name = self let old_name = self
.state .state
.online_users .online_users
@ -246,19 +227,19 @@ impl UserHandler {
.unwrap() .unwrap()
.contains(&new_name) .contains(&new_name)
{ {
self.send_message_addr( self.dm_user(
addr,
SendChatMessage(ChatMessage { SendChatMessage(ChatMessage {
text: "Name is taken".to_string(), text: "Name is taken".to_string(),
}), }),
Addr(addr),
) )
.await; .await;
tracing::debug!("{}", old_name.clone()); tracing::debug!("{}", old_name.clone());
self.send_message_addr( self.dm_user(
addr,
SendUserUpdate(UserUpdate { SendUserUpdate(UserUpdate {
username: old_name.clone(), username: old_name.clone(),
}), }),
Addr(addr),
) )
.await; .await;
} else { } else {
@ -278,7 +259,7 @@ impl UserHandler {
.unwrap() .unwrap()
.write() .write()
.unwrap() .unwrap()
.change_name(username); .change_name(request.username);
// Send chat updates // Send chat updates
let msg = format! { let msg = format! {
@ -292,11 +273,11 @@ impl UserHandler {
} }
// Send the user their new name // Send the user their new name
self.send_message_addr( self.dm_user(
addr,
SendUserUpdate(UserUpdate { SendUserUpdate(UserUpdate {
username: new_name.clone(), username: new_name.clone(),
}), }),
Addr(addr),
) )
.await; .await;

View file

@ -39,8 +39,8 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc<AppState>, ad
state state
.users_tx .users_tx
.send(UserHandlerMessage::NewUser { .send(UserHandlerMessage::NewUser(
user: User::new( User::new(
format!( format!(
"{} {}", "{} {}",
state.first_names.choose(&mut rand::thread_rng()).unwrap(), state.first_names.choose(&mut rand::thread_rng()).unwrap(),
@ -49,7 +49,7 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc<AppState>, ad
dm_tx.clone(), dm_tx.clone(),
), ),
addr, addr,
}) ))
.await .await
.unwrap(); .unwrap();