change username
This commit is contained in:
parent
457e69bcbb
commit
c1938347a2
3 changed files with 21 additions and 4 deletions
|
@ -21,12 +21,18 @@ pub struct ServerStateSummary {
|
|||
}
|
||||
|
||||
/// User
|
||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub struct User {
|
||||
pub name: String,
|
||||
pub addr: SocketAddr,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn change_name(&mut self, new_name: String) {
|
||||
self.name = new_name;
|
||||
}
|
||||
}
|
||||
|
||||
/// New game request structure
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct NewGameRequest {
|
||||
|
|
|
@ -27,7 +27,7 @@ fn server_sum_update(state: &Arc<AppState>) -> ServerStateSummary {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn on_websocket_connection(stream: WebSocket, state: Arc<AppState>, who: User) {
|
||||
pub async fn on_websocket_connection(stream: WebSocket, state: Arc<AppState>, mut who: User) {
|
||||
// Add user to users
|
||||
//if doesn't exist
|
||||
let _true_if_not_exist = &state.users.lock().unwrap().insert(who.clone());
|
||||
|
@ -73,7 +73,7 @@ pub async fn on_websocket_connection(stream: WebSocket, state: Arc<AppState>, wh
|
|||
// handle new incoming messages
|
||||
let mut recv_task = tokio::spawn(async move {
|
||||
while let Some(Ok(message)) = receiver.next().await {
|
||||
message_handler(message, &state, &who).await
|
||||
message_handler(message, &state, &mut who).await
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::api::*;
|
|||
use crate::AppState;
|
||||
use crate::Arc;
|
||||
|
||||
pub async fn message_handler(message: Message, state: &Arc<AppState>, who: &User) {
|
||||
pub async fn message_handler(message: Message, state: &Arc<AppState>, who: &mut User) {
|
||||
let tx = &state.tx;
|
||||
|
||||
match message {
|
||||
|
@ -30,6 +30,17 @@ pub async fn message_handler(message: Message, state: &Arc<AppState>, who: &User
|
|||
let msg = format! {"{0}: {1}", who.name, chat_message.text};
|
||||
tracing::debug!("{msg}");
|
||||
let _res = tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap());
|
||||
} else if let Ok(user_log_in) = serde_json::from_str::<UserLogIn>(&text) {
|
||||
let old_name = who.name.clone();
|
||||
let new_name = user_log_in.username.clone();
|
||||
who.change_name(user_log_in.username);
|
||||
let msg = format!{
|
||||
"{0} changed name to {1}.",
|
||||
old_name,
|
||||
new_name
|
||||
};
|
||||
tracing::debug!("{msg}");
|
||||
let _res = tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap());
|
||||
} else {
|
||||
tracing::debug!("Unhandled message: {}", &text);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue