change username

This commit is contained in:
Adam 2024-07-25 21:41:17 -04:00
parent 457e69bcbb
commit c1938347a2
3 changed files with 21 additions and 4 deletions

View file

@ -21,12 +21,18 @@ pub struct ServerStateSummary {
} }
/// User /// User
#[derive(Clone, Eq, PartialEq, Hash)] #[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct User { pub struct User {
pub name: String, pub name: String,
pub addr: SocketAddr, pub addr: SocketAddr,
} }
impl User {
pub fn change_name(&mut self, new_name: String) {
self.name = new_name;
}
}
/// New game request structure /// New game request structure
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct NewGameRequest { pub struct NewGameRequest {

View file

@ -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 // Add user to users
//if doesn't exist //if doesn't exist
let _true_if_not_exist = &state.users.lock().unwrap().insert(who.clone()); 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 // handle new incoming messages
let mut recv_task = tokio::spawn(async move { let mut recv_task = tokio::spawn(async move {
while let Some(Ok(message)) = receiver.next().await { while let Some(Ok(message)) = receiver.next().await {
message_handler(message, &state, &who).await message_handler(message, &state, &mut who).await
} }
}); });

View file

@ -4,7 +4,7 @@ use crate::api::*;
use crate::AppState; use crate::AppState;
use crate::Arc; 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; let tx = &state.tx;
match message { 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}; let msg = format! {"{0}: {1}", who.name, chat_message.text};
tracing::debug!("{msg}"); tracing::debug!("{msg}");
let _res = tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap()); 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 { } else {
tracing::debug!("Unhandled message: {}", &text); tracing::debug!("Unhandled message: {}", &text);
} }