clean up new game

This commit is contained in:
Adam 2024-08-15 00:14:47 -04:00
parent afaf695f2d
commit 87f814b7d8

View file

@ -107,93 +107,86 @@ impl GameHandler {
return; return;
} }
// Get host pointer
let host = self
.state
.online_users
.read()
.unwrap()
.get(&addr)
.unwrap()
.clone();
// Create manifest
let manifest = NewGameManifest { let manifest = NewGameManifest {
name: new_game.name, name: new_game.name,
host: self host: host.clone(),
.state
.online_users
.read()
.unwrap()
.get(&addr)
.unwrap()
.clone(),
packs: new_game.packs, packs: new_game.packs,
}; };
// Create game // Create game using manifest
if let Ok(new_game_object) = Game::new(self.state.clone(), manifest) { let new_game_object = Game::new(self.state.clone(), manifest);
let tx = self
.state // Create update for user's game view
.online_users let meta = GameStateMeta {
.read() uuid: new_game_object.uuid.to_string(),
name: new_game_object.name.clone(),
host: new_game_object.host.read().unwrap().name.clone(),
players: new_game_object
.players
.iter()
.map(|player| {
self.state
.user_uuid
.read()
.unwrap()
.get(player.0)
.unwrap()
.read()
.unwrap()
.name
.clone()
})
.collect(),
czar: new_game_object.host.read().unwrap().name.clone(),
black: (
new_game_object.current_black.text.clone(),
new_game_object.current_black.pick,
),
white: new_game_object
.players
.get(&new_game_object.host.read().unwrap().uuid)
.unwrap() .unwrap()
.get(&addr) .white
.unwrap() .iter()
.read() .map(|card| card.text.clone())
.unwrap() .collect(),
.tx packs: new_game_object.packs.clone(),
.clone(); };
// Create update for user's game view // Send user's update
let meta = GameStateMeta { let tx = host.read().unwrap().tx.clone();
uuid: new_game_object.uuid.to_string(), tx.send(serde_json::to_string(&meta).unwrap())
name: new_game_object.name.clone(), .await
host: new_game_object.host.read().unwrap().name.clone(), .unwrap();
players: new_game_object
.players
.iter()
.map(|player| {
self.state
.user_uuid
.read()
.unwrap()
.get(player.0)
.unwrap()
.read()
.unwrap()
.name
.clone()
})
.collect(),
czar: new_game_object.host.read().unwrap().name.clone(),
black: (
new_game_object.current_black.text.clone(),
new_game_object.current_black.pick,
),
white: new_game_object
.players
.get(&new_game_object.host.read().unwrap().uuid)
.unwrap()
.white
.iter()
.map(|card| card.text.clone())
.collect(),
packs: new_game_object.packs.clone(),
};
// Send user's update // Add game to active list
tx.send(serde_json::to_string(&meta).unwrap()) self.state.games.write().unwrap().insert(
.await new_game_object.uuid.to_string(),
.unwrap(); Arc::new(RwLock::new(new_game_object)),
);
// Add game to active list // Broadcast game browser update
self.state.games.write().unwrap().insert( self.state
new_game_object.uuid.to_string(), .broadcast_tx
Arc::new(RwLock::new(new_game_object)), .send(meta_games_browser_update(&self.state))
); .unwrap();
// Broadcast game browser update // Broadcast server meta update
self.state self.state
.broadcast_tx .broadcast_tx
.send(meta_games_browser_update(&self.state)) .send(meta_server_summary_update(&self.state))
.unwrap(); .unwrap();
// Broadcast server meta update
self.state
.broadcast_tx
.send(meta_server_summary_update(&self.state))
.unwrap();
}
} }
} }
@ -288,7 +281,7 @@ pub struct Game {
} }
impl Game { impl Game {
fn new(state: Arc<AppState>, request: NewGameManifest) -> Result<Self> { fn new(state: Arc<AppState>, request: NewGameManifest) -> Self {
tracing::debug!( tracing::debug!(
"Creating game {} with {} as host", "Creating game {} with {} as host",
&request.name, &request.name,
@ -320,7 +313,7 @@ impl Game {
white.shrink_to_fit(); white.shrink_to_fit();
black.shrink_to_fit(); black.shrink_to_fit();
Ok(Game { Game {
uuid: Uuid::now_v7(), uuid: Uuid::now_v7(),
name: request.name, name: request.name,
host: request.host.clone(), host: request.host.clone(),
@ -329,7 +322,7 @@ impl Game {
players: HashMap::new(), players: HashMap::new(),
current_black, current_black,
packs: request.packs.clone(), packs: request.packs.clone(),
}) }
} }
/// Draw one white card at random from play deck. /// Draw one white card at random from play deck.