diff --git a/client/src/components/game.rs b/client/src/components/game.rs index 75b7f11..e8b1782 100644 --- a/client/src/components/game.rs +++ b/client/src/components/game.rs @@ -17,11 +17,10 @@ pub fn Game() -> impl IntoView { 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, set_selected_cards) = + let (player_white, set_player_white) = create_signal::>(HashMap::new()); let (selected_cards_ordered, set_selected_cards_ordered) = create_signal::>(vec![]); - let (player_hand, set_player_hand) = - create_signal::>(HashMap::new()); + let (player_hand, set_player_hand) = create_signal::>(vec![]); let (card_clicked, set_card_clicked) = create_signal::(String::new()); provide_context::>(set_card_clicked); @@ -36,55 +35,46 @@ pub fn Game() -> impl IntoView { set_game_white(game.white.clone()); // Primitive reset when joining new game - set_player_hand(HashMap::new()); + set_player_hand(vec![]); + set_selected_cards_ordered(vec![]); // Load hand for card in game.white { - set_player_hand.update(|hand| { + set_player_white.update(|hand| { hand.insert(card.uuid.clone(), card.clone()); - }) + }); + + set_player_hand.update(|hand| { + hand.push(card.uuid.clone()); + }); } } }); // Move cards back and forth from hand to selected when clicked create_effect(move |_| { - // Move the card - if player_hand().contains_key(&card_clicked()) { - set_selected_cards.update(|cards| { - cards.insert( - card_clicked(), - set_player_hand - .try_update(|cards| cards.remove(&card_clicked())) - .unwrap() - .unwrap(), - ); + if let Some(card_index) = player_hand() + .iter() + .position(|card| *card == card_clicked()) + { + set_player_hand.update(|list| { + list.remove(card_index); }); - // Track ordering set_selected_cards_ordered.update(|list| { list.push(card_clicked()); - }); - } else if selected_cards().contains_key(&card_clicked()) { - // Update tracking before moving the card or else the view tries to display a card that - // doesn't exist + }) + } else if let Some(card_index) = selected_cards_ordered() + .iter() + .position(|card| *card == card_clicked()) + { set_selected_cards_ordered.update(|list| { - list.remove( - list.iter() - .position(|card| *card == card_clicked()) - .unwrap(), - ); + list.remove(card_index); }); - set_player_hand.update(|cards| { - cards.insert( - card_clicked(), - set_selected_cards - .try_update(|cards| cards.remove(&card_clicked())) - .unwrap() - .unwrap(), - ); - }); + set_player_hand.update(|list| { + list.push(card_clicked()); + }) } }); @@ -127,20 +117,18 @@ pub fn Game() -> impl IntoView { // Card selection - } + view! { } } />
} + view! { } } /> diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index ea5e2ae..11c00cf 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -110,28 +110,16 @@ impl GameHandler { .unwrap() .clone(); - if this_game + if !this_game .read() .unwrap() .players .contains_key(&this_user.read().unwrap().uuid) { - self.state - .users_tx - .send(DmUserAddr { - addr, - message: SendChatMessage(ChatMessage { - text: "You're already in this game!".to_owned(), - }), - }) - .await - .unwrap(); - return; + // Create player + this_game.write().unwrap().create_player(this_user); } - // Create player - this_game.write().unwrap().create_player(this_user); - // Send updates for all players for player in this_game.read().unwrap().players.values() { // Create update for user's game view