From 8ccb5c840a71ee621b220fe4598fa82d8bcbb1d7 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Wed, 14 Aug 2024 18:01:43 -0400 Subject: [PATCH] get rid of that mess --- server/src/game_handler.rs | 228 +++++++------------------------------ server/src/user_handler.rs | 21 ++++ 2 files changed, 60 insertions(+), 189 deletions(-) diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index aee6a69..b0519a9 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -162,6 +162,8 @@ struct NewGameManifest { /// A struct that represents a player #[derive(Debug)] pub struct Player { + /// Pointer to user + user: Arc>, /// The player's hand white: Vec, /// The player's wins @@ -282,6 +284,7 @@ impl Game { /// Create a new player and add them to the game. pub fn create_player(&mut self, user: Arc>) -> Result<()> { let mut new_player = Player { + user: user.clone(), white: vec![], black: vec![], }; @@ -334,202 +337,49 @@ impl GameHandler { } async fn join_game(&self, addr: SocketAddr, id: String) { - // Create player - self.state - .games - .read() - .unwrap() - .get(&id) - .unwrap() - .write() - .unwrap() - .create_player( - self.state - .online_users - .read() - .unwrap() - .get(&addr) - .unwrap() - .clone(), - ) - .unwrap(); - - // Create update for user's game view - let mut black_card = ("Error".to_string(), 0u8); - if let Some(ref current_black) = self + // Get pointers + let this_game = self.state.games.read().unwrap().get(&id).unwrap().clone(); + let this_user = self .state - .games + .online_users .read() .unwrap() - .get(&id) + .get(&addr) .unwrap() - .read() - .unwrap() - .current_black - { - black_card = (current_black.text.to_owned(), current_black.pick) - } - let meta = GameStateMeta { - uuid: id.clone(), - name: self - .state - .games - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .name - .clone(), - host: self - .state - .games - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .host - .read() - .unwrap() - .name - .clone(), - players: self - .state - .games - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .players - .iter() - .map(|player| { - self.state - .user_uuid - .read() - .unwrap() - .get(player.0) - .unwrap() - .read() - .unwrap() - .name - .clone() - }) - .collect(), - czar: self - .state - .games - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .host - .read() - .unwrap() - .name - .clone(), - black: black_card, - white: self - .state - .games - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .players - .get( - &self - .state - .online_users - .read() - .unwrap() - .get(&addr) - .unwrap() - .read() - .unwrap() - .uuid, - ) - .unwrap() - .white - .iter() - .map(|card| card.text.clone()) - .collect(), - packs: self - .state - .games - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .packs - .clone(), - }; + .clone(); + + // Create player + this_game.write().unwrap().create_player(this_user).unwrap(); // Send updates for all players - for player in self - .state - .games - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .players - .iter() - { - // // Send user's update - // self.state - // .user_uuid - // .read() - // .unwrap() - // .get(&player.0) - // .unwrap() - // .read() - // .unwrap() - // .tx - // .send(serde_json::to_string(&meta).unwrap()) - // .await - // .unwrap(); - tracing::debug! {"y"}; + for player in this_game.read().unwrap().players.values() { + // Create update for user's game view + let mut black_card = ("Error".to_string(), 0u8); + if let Some(ref current_black) = this_game.read().unwrap().current_black { + black_card = (current_black.text.to_owned(), current_black.pick) + } + let meta = GameStateMeta { + uuid: id.clone(), + name: this_game.read().unwrap().name.clone(), + host: this_game.read().unwrap().host.read().unwrap().name.clone(), + players: this_game + .read() + .unwrap() + .players + .values() + .map(|player| player.user.read().unwrap().name.clone()) + .collect(), + czar: this_game.read().unwrap().host.read().unwrap().name.clone(), + black: black_card, + white: player.white.iter().map(|card| card.text.clone()).collect(), + packs: this_game.read().unwrap().packs.clone(), + }; + + // Send user's update + let msg = serde_json::to_string(&meta).unwrap(); + let user_tx = player.user.read().unwrap().tx.clone(); + tokio::spawn(async move { user_tx.send(msg).await }); } - // let _ = self - // .state - // .games - // .read() - // .unwrap() - // .get(&id) - // .unwrap() - // .read() - // .unwrap() - // .players - // .iter() - // .map(async |player: (&Uuid, &Player)| { - // // Send user's update - // self.state - // .user_uuid - // .read() - // .unwrap() - // .get(player.0) - // .unwrap() - // .read() - // .unwrap() - // .tx - // .send(serde_json::to_string(&meta).unwrap()) - // .await - // .unwrap(); - // }) - // .count(); // Broadcast game browser update self.state diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index ceaa3b0..37356c2 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -8,6 +8,7 @@ use std::sync::{Arc, RwLock}; pub enum UserHandlerMessage { NewUser { user: User, addr: SocketAddr }, UserLogIn { username: String, addr: SocketAddr }, + DmUserAddr { addr: SocketAddr, message: String }, } pub struct UserHandler { @@ -26,9 +27,29 @@ impl UserHandler { self.set_up_new_user(user, addr).await } UserHandlerMessage::UserLogIn { username, addr } => self.login(username, addr).await, + UserHandlerMessage::DmUserAddr { addr, message } => { + self.send_message_addr(addr, message).await + } } } + async fn send_message_addr(&self, addr: SocketAddr, message: String) { + let msg = to_string::(&ChatMessage { text: message }).unwrap(); + let tx = self + .state + .online_users + .read() + .unwrap() + .get(&addr) + .unwrap() + .read() + .unwrap() + .tx + .clone(); + + tx.send(msg).await.unwrap() + } + async fn set_up_new_user(&self, user: User, addr: SocketAddr) { // // Create, Register, and Hydrate new user