diff --git a/client/src/components/game.rs b/client/src/components/game.rs index 6d5acf9..3f58a58 100644 --- a/client/src/components/game.rs +++ b/client/src/components/game.rs @@ -25,50 +25,44 @@ pub fn Game() -> impl IntoView { let game_meta = expect_context::>>(); let judge_round = expect_context::>>(); - // Internal + // Signals // let (selected_cards, set_selected_cards) = create_signal::>(vec![]); let (card_clicked, set_card_clicked) = create_signal::(String::new()); - let (white_map, set_white_map) = + let (player_hand, set_player_hand) = create_signal::>(HashMap::new()); + let (judging, set_judging) = create_signal(false); - // For subcomponents + // Outoging provide_context::>(set_card_clicked); + // On Incoming Meta // // Put cards in a map for easier lookup // The server sends a vec to preserve ordering create_effect(move |_| { if game_meta().is_some() { for card in game_meta().unwrap().white { - set_white_map.update(|map| { + set_player_hand.update(|map| { map.insert(card.uuid.clone(), card.clone()); }); } } + set_selected_cards.update(|list| { + list.clear(); + }); }); - // Toggle selected status of cards + // On Incoming Judge // create_effect(move |_| { - if card_clicked() != "".to_string() { - if !selected_cards().contains(white_map().get(&card_clicked()).unwrap()) { - set_selected_cards - .update(|cards| cards.push(white_map().get(&card_clicked()).unwrap().clone())) - } else if selected_cards().contains(white_map().get(&card_clicked()).unwrap()) { - set_selected_cards.update(|cards| { - cards.remove( - cards - .iter() - .position(|card| card == white_map().get(&card_clicked()).unwrap()) - .unwrap(), - ); - }) - } - - // You can't clear selected_cards without this line so don't fuck with it - set_card_clicked("".to_string()); + // Clear selected cards + if judge_round().is_some() { + set_selected_cards.update(|list| { + list.clear(); + }); + set_judging(true); } }); - // Runs when submit button is clicked + // Player Submit Handler // let submit_move = move |_| { let msg = to_string(&PlayerMoveRequest { game_id: game_meta().unwrap().uuid.clone(), @@ -80,19 +74,12 @@ pub fn Game() -> impl IntoView { .unwrap(); set_websocket_send(msg); + set_selected_cards.update(|list| { + list.clear(); + }); }; - // On incoming judge - create_effect(move |_| { - // Clear selected cards - if judge_round().is_some() { - set_selected_cards.update(|list| { - list.clear(); - }); - } - }); - - // Runs when judging submit button is clicked + // Judging Submit Handler // let submit_judge = move |_| { let msg = to_string(&JudgeDecisionRequest { game_id: game_meta().unwrap().uuid.clone(), @@ -104,8 +91,76 @@ pub fn Game() -> impl IntoView { .unwrap(); set_websocket_send(msg); + set_judging(false); + set_selected_cards.update(|list| { + list.clear(); + }); }; + // Card selection // + // Toggle selected status of cards + create_effect(move |_| { + if card_clicked() != "".to_string() { + if judging.get_untracked() { + let identical_cards = selected_cards + .get_untracked() + .into_iter() + .filter(|card| card.uuid == card_clicked.get_untracked()) + .collect::>(); + + if identical_cards.len() > 0 { + set_selected_cards.update(|list| { + list.clear(); + }); + } else { + // Clear selected cards + set_selected_cards.update(|list| { + list.clear(); + }); + + // Select card group + for group in judge_round.get_untracked().unwrap().cards_to_judge { + for card in &group { + if card.uuid == card_clicked() { + set_selected_cards.update(|cards| cards.extend(group)); + break; + } + } + } + } + // Clear the signal otherwise it selects the last selected card again + set_card_clicked.update_untracked(|value| value.clear()); + } else { + if !selected_cards.get_untracked().contains( + player_hand + .get_untracked() + .get(&card_clicked.get_untracked()) + .unwrap(), + ) { + set_selected_cards.update(|cards| { + cards.push(player_hand().get(&card_clicked()).unwrap().clone()) + }) + } else if selected_cards + .get_untracked() + .contains(player_hand.get_untracked().get(&card_clicked()).unwrap()) + { + set_selected_cards.update(|cards| { + cards.remove( + cards + .iter() + .position(|card| { + card == player_hand().get(&card_clicked()).unwrap() + }) + .unwrap(), + ); + }) + } + // Clear the signal otherwise it selects the last selected card again + set_card_clicked.update_untracked(|value| value.clear()); + } + } + }); + view! {

Game

@@ -116,11 +171,9 @@ pub fn Game() -> impl IntoView {
- // //////////////////// - // Player is judging // - // //////////////////// + // Judging view // - +
@@ -134,32 +187,43 @@ pub fn Game() -> impl IntoView { />
- {judge_round() - .unwrap() - .cards_to_judge - .iter() - .map(|group| view! { {format!("{:#?}", group)} }) - .collect_view()} - - - // Submit button
+ + + + +
+ } + } + /> +
+ } + } + /> - // //////////////////// - // Player is playing // - // //////////////////// + // Playing view // - + // Play cards
@@ -193,7 +257,7 @@ pub fn Game() -> impl IntoView { Result, String> { if self.czar.read().unwrap().uuid == player_user_id { - tracing::debug!("Player is czar"); + tracing::error!("Player submitting move is czar!"); Err("You can't submit cards to judge, you ARE the judge!".to_string()) } else { // Error if not enough cards @@ -171,7 +171,6 @@ impl Game { // Ignore extra cards let trimmed = request.card_ids[..self.current_black.pick.into()].to_vec(); - tracing::debug!("Trimmed: {:#?}", trimmed); // Move card from player's hand to judge pile for id in &trimmed { @@ -326,7 +325,7 @@ impl Game { // Add player to rotation self.rotation.push(user.read().unwrap().uuid.to_string()); } else { - tracing::debug!("User already has a player in this game!"); + tracing::error!("User already has a player in this game!"); } } }