This commit is contained in:
Adam 2024-08-21 22:05:13 -04:00
parent 5d1ca4a96d
commit 69bc1714f4
2 changed files with 31 additions and 60 deletions

View file

@ -17,7 +17,6 @@ pub fn Game() -> impl IntoView {
let (game_players, set_game_players) = create_signal(vec![]);
let (game_czar, set_game_czar) = create_signal("".to_string());
let (game_black, set_game_black) = create_signal(("".to_string(), 0u8));
let (_game_white, set_game_white) = create_signal(vec![]);
let (selected_cards_ordered, set_selected_cards_ordered) = create_signal::<Vec<String>>(vec![]);
let (player_hand, set_player_hand) = create_signal::<Vec<String>>(vec![]);
let (player_white, set_player_white) =
@ -81,7 +80,6 @@ pub fn Game() -> impl IntoView {
set_game_players(game.players.clone());
set_game_czar(game.czar.clone());
set_game_black(game.black.clone());
set_game_white(game.white.clone());
// Load hand
for card in game.white {

View file

@ -225,6 +225,7 @@ impl GameHandler {
.unwrap()
.clone();
// Check if player already exists
if !this_game
.read()
.unwrap()
@ -236,10 +237,36 @@ impl GameHandler {
}
// Send updates for all players
self.send_game_state_update(id).await;
// Broadcast game browser update
self.state
.broadcast_tx
.send(meta_games_browser_update(&self.state))
.unwrap();
// Broadcast server meta update
self.state
.broadcast_tx
.send(meta_server_summary_update(&self.state))
.unwrap();
}
/// Send game state update for all players of a game
async fn send_game_state_update(&self, game_id: String) {
let this_game = self
.state
.games
.read()
.unwrap()
.get(&game_id)
.unwrap()
.clone();
for player in this_game.read().unwrap().players.values() {
// Create update for user's game view
let meta = GameStateMeta {
uuid: id.clone(),
uuid: game_id.clone(),
name: this_game.read().unwrap().name.clone(),
host: this_game.read().unwrap().host.read().unwrap().name.clone(),
players: this_game
@ -270,18 +297,6 @@ impl GameHandler {
let user_tx = player.user.read().unwrap().tx.clone();
tokio::spawn(async move { user_tx.send(msg).await });
}
// Broadcast game browser update
self.state
.broadcast_tx
.send(meta_games_browser_update(&self.state))
.unwrap();
// Broadcast server meta update
self.state
.broadcast_tx
.send(meta_server_summary_update(&self.state))
.unwrap();
}
/// Creates a new game
@ -317,51 +332,7 @@ impl GameHandler {
// Don't forget to create the host player!!!
new_game_object.create_player(host.clone());
// Create update for user's game view
let meta = GameStateMeta {
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
.users_by_id
.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| WhiteCardMeta {
uuid: card.uuid.to_string(),
text: card.text.clone(),
})
.collect(),
packs: new_game_object.packs.clone(),
};
// Send user's update
let tx = host.read().unwrap().tx.clone();
tx.send(serde_json::to_string(&meta).unwrap())
.await
.unwrap();
let game_id = new_game_object.uuid.to_string();
// Add game to active list
self.state.games.write().unwrap().insert(
@ -369,6 +340,8 @@ impl GameHandler {
Arc::new(RwLock::new(new_game_object)),
);
self.send_game_state_update(game_id).await;
// Broadcast game browser update
self.state
.broadcast_tx