cleanup
This commit is contained in:
parent
5845e4793b
commit
97e0a69a7b
4 changed files with 61 additions and 91 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<AppState>,
|
||||
}
|
||||
|
||||
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,19 +43,23 @@ 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
|
||||
/// Send message direct to a single user
|
||||
async fn dm_user(&self, message: SendUserMessage, method: DmUserMethod) {
|
||||
let tx;
|
||||
|
||||
// Determine lookup method
|
||||
match method {
|
||||
Id(id) => {
|
||||
tx = self
|
||||
.state
|
||||
.users_by_id
|
||||
.read()
|
||||
|
@ -73,29 +70,9 @@ impl UserHandler {
|
|||
.unwrap()
|
||||
.tx
|
||||
.clone();
|
||||
|
||||
// TODO: this feels messy
|
||||
match message {
|
||||
SendUserUpdate(message) => {
|
||||
let msg = to_string::<UserUpdate>(&message).unwrap();
|
||||
tx.send(msg).await.unwrap()
|
||||
}
|
||||
SendChatMessage(message) => {
|
||||
let msg = to_string::<ChatMessage>(&message).unwrap();
|
||||
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
|
||||
Addr(addr) => {
|
||||
tx = self
|
||||
.state
|
||||
.online_users
|
||||
.read()
|
||||
|
@ -106,6 +83,10 @@ impl UserHandler {
|
|||
.unwrap()
|
||||
.tx
|
||||
.clone();
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize and send message
|
||||
match message {
|
||||
SendUserUpdate(message) => {
|
||||
let msg = to_string::<UserUpdate>(&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;
|
||||
|
||||
|
|
|
@ -39,8 +39,8 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc<AppState>, 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<AppState>, ad
|
|||
dm_tx.clone(),
|
||||
),
|
||||
addr,
|
||||
})
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue