fix judging logic

This commit is contained in:
Adam 2024-08-26 20:25:46 -04:00
parent 25dcf01062
commit a24b4bb54b
2 changed files with 122 additions and 59 deletions

View file

@ -25,50 +25,44 @@ pub fn Game() -> impl IntoView {
let game_meta = expect_context::<ReadSignal<Option<GameStateMeta>>>(); let game_meta = expect_context::<ReadSignal<Option<GameStateMeta>>>();
let judge_round = expect_context::<ReadSignal<Option<JudgeRound>>>(); let judge_round = expect_context::<ReadSignal<Option<JudgeRound>>>();
// Internal // Signals //
let (selected_cards, set_selected_cards) = create_signal::<Vec<WhiteCardMeta>>(vec![]); let (selected_cards, set_selected_cards) = create_signal::<Vec<WhiteCardMeta>>(vec![]);
let (card_clicked, set_card_clicked) = create_signal::<String>(String::new()); let (card_clicked, set_card_clicked) = create_signal::<String>(String::new());
let (white_map, set_white_map) = let (player_hand, set_player_hand) =
create_signal::<HashMap<String, WhiteCardMeta>>(HashMap::new()); create_signal::<HashMap<String, WhiteCardMeta>>(HashMap::new());
let (judging, set_judging) = create_signal(false);
// For subcomponents // Outoging
provide_context::<WriteSignal<String>>(set_card_clicked); provide_context::<WriteSignal<String>>(set_card_clicked);
// On Incoming Meta //
// Put cards in a map for easier lookup // Put cards in a map for easier lookup
// The server sends a vec to preserve ordering // The server sends a vec to preserve ordering
create_effect(move |_| { create_effect(move |_| {
if game_meta().is_some() { if game_meta().is_some() {
for card in game_meta().unwrap().white { for card in game_meta().unwrap().white {
set_white_map.update(|map| { set_player_hand.update(|map| {
map.insert(card.uuid.clone(), card.clone()); 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 |_| { create_effect(move |_| {
if card_clicked() != "".to_string() { // Clear selected cards
if !selected_cards().contains(white_map().get(&card_clicked()).unwrap()) { if judge_round().is_some() {
set_selected_cards set_selected_cards.update(|list| {
.update(|cards| cards.push(white_map().get(&card_clicked()).unwrap().clone())) list.clear();
} else if selected_cards().contains(white_map().get(&card_clicked()).unwrap()) { });
set_selected_cards.update(|cards| { set_judging(true);
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());
} }
}); });
// Runs when submit button is clicked // Player Submit Handler //
let submit_move = move |_| { let submit_move = move |_| {
let msg = to_string(&PlayerMoveRequest { let msg = to_string(&PlayerMoveRequest {
game_id: game_meta().unwrap().uuid.clone(), game_id: game_meta().unwrap().uuid.clone(),
@ -80,19 +74,12 @@ pub fn Game() -> impl IntoView {
.unwrap(); .unwrap();
set_websocket_send(msg); set_websocket_send(msg);
};
// On incoming judge
create_effect(move |_| {
// Clear selected cards
if judge_round().is_some() {
set_selected_cards.update(|list| { set_selected_cards.update(|list| {
list.clear(); list.clear();
}); });
} };
});
// Runs when judging submit button is clicked // Judging Submit Handler //
let submit_judge = move |_| { let submit_judge = move |_| {
let msg = to_string(&JudgeDecisionRequest { let msg = to_string(&JudgeDecisionRequest {
game_id: game_meta().unwrap().uuid.clone(), game_id: game_meta().unwrap().uuid.clone(),
@ -104,8 +91,76 @@ pub fn Game() -> impl IntoView {
.unwrap(); .unwrap();
set_websocket_send(msg); 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::<Vec<WhiteCardMeta>>();
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! { view! {
<div class="p-1"> <div class="p-1">
<h2 class="text-2xl">Game</h2> <h2 class="text-2xl">Game</h2>
@ -116,11 +171,9 @@ pub fn Game() -> impl IntoView {
<Header game_meta=game_meta().unwrap() /> <Header game_meta=game_meta().unwrap() />
</Show> </Show>
// //////////////////// // Judging view //
// Player is judging //
// ////////////////////
<Show when=move || { judge_round.get().is_some() && connected() }> <Show when=move || { judging() && connected() }>
<div class="w-full ms-16 inline-flex flex-wrap"> <div class="w-full ms-16 inline-flex flex-wrap">
<BlackCard card_data=game_meta().unwrap().black /> <BlackCard card_data=game_meta().unwrap().black />
@ -134,32 +187,43 @@ pub fn Game() -> impl IntoView {
/> />
</div> </div>
{judge_round()
.unwrap()
.cards_to_judge
.iter()
.map(|group| view! { {format!("{:#?}", group)} })
.collect_view()}
<For
each=move || judge_round().unwrap().cards_to_judge
key=move |_| 69
children=move |group| { { format!("{:#?}", group) } }
/>
// Submit button // Submit button
<div class="w-full inline-flex flex-wrap justify-center"> <div class="w-full inline-flex flex-wrap justify-center">
<button type="button" on:click=submit_judge> <button type="button" on:click=submit_judge>
Submit Submit
</button> </button>
</div> </div>
<For
each=move || judge_round().unwrap().cards_to_judge
key=move |_| 69
children=move |group| {
view! {
<div class="m-2 inline-flex flex-wrap justify-center">
<For
each=move || group.clone()
key=move |card| card.uuid.clone()
children=move |card| {
view! {
// Hide cards from hand view when they exist as selected
<Show when={
let waste_of_memory = card.clone();
move || { !selected_cards().contains(&waste_of_memory) }
}>
<WhiteCard card_data=card.clone() />
</Show>
}
}
/>
</div>
}
}
/>
</Show> </Show>
// //////////////////// // Playing view //
// Player is playing //
// ////////////////////
<Show when=move || game_meta.get().is_some() && connected() && !judge_round().is_some()> <Show when=move || game_meta.get().is_some() && connected() && !judging()>
// Play cards // Play cards
<div class="w-full ms-16 inline-flex flex-wrap"> <div class="w-full ms-16 inline-flex flex-wrap">
@ -193,7 +257,7 @@ pub fn Game() -> impl IntoView {
<Show when={ <Show when={
let id = card.uuid.clone(); let id = card.uuid.clone();
move || { move || {
if let Some(card) = white_map().get(&id) { if let Some(card) = player_hand().get(&id) {
!selected_cards().contains(card) !selected_cards().contains(card)
} else { } else {
true true

View file

@ -161,7 +161,7 @@ impl Game {
player_user_id: String, player_user_id: String,
) -> Result<Option<(JudgeRound, String)>, String> { ) -> Result<Option<(JudgeRound, String)>, String> {
if self.czar.read().unwrap().uuid == player_user_id { 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()) Err("You can't submit cards to judge, you ARE the judge!".to_string())
} else { } else {
// Error if not enough cards // Error if not enough cards
@ -171,7 +171,6 @@ impl Game {
// Ignore extra cards // Ignore extra cards
let trimmed = request.card_ids[..self.current_black.pick.into()].to_vec(); 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 // Move card from player's hand to judge pile
for id in &trimmed { for id in &trimmed {
@ -326,7 +325,7 @@ impl Game {
// Add player to rotation // Add player to rotation
self.rotation.push(user.read().unwrap().uuid.to_string()); self.rotation.push(user.read().unwrap().uuid.to_string());
} else { } else {
tracing::debug!("User already has a player in this game!"); tracing::error!("User already has a player in this game!");
} }
} }
} }