get rid of that mess

This commit is contained in:
Adam 2024-08-14 18:01:43 -04:00
parent ef2250b88d
commit 8ccb5c840a
2 changed files with 60 additions and 189 deletions

View file

@ -162,6 +162,8 @@ struct NewGameManifest {
/// A struct that represents a player /// A struct that represents a player
#[derive(Debug)] #[derive(Debug)]
pub struct Player { pub struct Player {
/// Pointer to user
user: Arc<RwLock<User>>,
/// The player's hand /// The player's hand
white: Vec<CardWhite>, white: Vec<CardWhite>,
/// The player's wins /// The player's wins
@ -282,6 +284,7 @@ impl Game {
/// Create a new player and add them to the game. /// Create a new player and add them to the game.
pub fn create_player(&mut self, user: Arc<RwLock<User>>) -> Result<()> { pub fn create_player(&mut self, user: Arc<RwLock<User>>) -> Result<()> {
let mut new_player = Player { let mut new_player = Player {
user: user.clone(),
white: vec![], white: vec![],
black: vec![], black: vec![],
}; };
@ -334,202 +337,49 @@ impl GameHandler {
} }
async fn join_game(&self, addr: SocketAddr, id: String) { async fn join_game(&self, addr: SocketAddr, id: String) {
// Create player // Get pointers
self.state let this_game = self.state.games.read().unwrap().get(&id).unwrap().clone();
.games let this_user = self
.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
.state .state
.games .online_users
.read() .read()
.unwrap() .unwrap()
.get(&id) .get(&addr)
.unwrap() .unwrap()
.read() .clone();
.unwrap()
.current_black // Create player
{ this_game.write().unwrap().create_player(this_user).unwrap();
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(),
};
// Send updates for all players // Send updates for all players
for player in self for player in this_game.read().unwrap().players.values() {
.state // Create update for user's game view
.games let mut black_card = ("Error".to_string(), 0u8);
.read() if let Some(ref current_black) = this_game.read().unwrap().current_black {
.unwrap() black_card = (current_black.text.to_owned(), current_black.pick)
.get(&id) }
.unwrap() let meta = GameStateMeta {
.read() uuid: id.clone(),
.unwrap() name: this_game.read().unwrap().name.clone(),
.players host: this_game.read().unwrap().host.read().unwrap().name.clone(),
.iter() players: this_game
{ .read()
// // Send user's update .unwrap()
// self.state .players
// .user_uuid .values()
// .read() .map(|player| player.user.read().unwrap().name.clone())
// .unwrap() .collect(),
// .get(&player.0) czar: this_game.read().unwrap().host.read().unwrap().name.clone(),
// .unwrap() black: black_card,
// .read() white: player.white.iter().map(|card| card.text.clone()).collect(),
// .unwrap() packs: this_game.read().unwrap().packs.clone(),
// .tx };
// .send(serde_json::to_string(&meta).unwrap())
// .await // Send user's update
// .unwrap(); let msg = serde_json::to_string(&meta).unwrap();
tracing::debug! {"y"}; 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 // Broadcast game browser update
self.state self.state

View file

@ -8,6 +8,7 @@ use std::sync::{Arc, RwLock};
pub enum UserHandlerMessage { pub enum UserHandlerMessage {
NewUser { user: User, addr: SocketAddr }, NewUser { user: User, addr: SocketAddr },
UserLogIn { username: String, addr: SocketAddr }, UserLogIn { username: String, addr: SocketAddr },
DmUserAddr { addr: SocketAddr, message: String },
} }
pub struct UserHandler { pub struct UserHandler {
@ -26,9 +27,29 @@ impl UserHandler {
self.set_up_new_user(user, addr).await self.set_up_new_user(user, addr).await
} }
UserHandlerMessage::UserLogIn { username, addr } => self.login(username, 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>(&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) { async fn set_up_new_user(&self, user: User, addr: SocketAddr) {
// //
// Create, Register, and Hydrate new user // Create, Register, and Hydrate new user