Compare commits

...

2 commits

Author SHA1 Message Date
Adam
1da5606dd6 error on not enough cards 2024-08-25 15:58:28 -04:00
Adam
db180a66ed separate submit/judge 2024-08-25 15:57:41 -04:00
3 changed files with 35 additions and 33 deletions
client/src/components
lib/src
server/src

View file

@ -9,6 +9,11 @@ use std::collections::HashMap;
#[component]
pub fn Game() -> impl IntoView {
let websocket = expect_context::<WebSocketContext>();
let tx = websocket.clone();
let (websocket_send, set_websocket_send) = create_signal("".to_string());
create_effect(move |_| {
tx.send(&websocket_send());
});
let connected = move || websocket.ready_state.get() == ConnectionReadyState::Open;
let game_meta = expect_context::<ReadSignal<Option<GameStateMeta>>>();
let judge_round = expect_context::<ReadSignal<Option<JudgeRound>>>();
@ -61,9 +66,6 @@ pub fn Game() -> impl IntoView {
set_card_clicked.update(|list| {
list.clear();
});
set_selected_cards_ordered.update(|list| {
list.clear();
});
// Load hand
if let Some(judge) = judge_round {
@ -148,33 +150,24 @@ pub fn Game() -> impl IntoView {
}
});
let submit_cards = move |_| {
let tx = websocket.clone();
judge_round.with(move |judge| {
let mut _message: Option<String> = None;
if judge.is_some() {
_message = Some(
to_string(&JudgeDecisionRequest {
game_id: game_id(),
winning_cards: selected_cards_ordered(),
})
.unwrap(),
);
} else {
_message = Some(
to_string(&PlayerMoveRequest {
game_id: game_id(),
card_ids: selected_cards_ordered(),
})
.unwrap(),
);
}
if let Some(msg) = _message {
tx.send(&msg);
}
let submit_move = move |_| {
let msg = to_string(&PlayerMoveRequest {
game_id: game_id(),
card_ids: selected_cards_ordered(),
})
.unwrap();
set_websocket_send(msg);
};
let submit_judge = move |_| {
let msg = to_string(&JudgeDecisionRequest {
game_id: game_id(),
winning_cards: selected_cards_ordered(),
})
.unwrap();
set_websocket_send(msg);
};
view! {
@ -232,10 +225,15 @@ pub fn Game() -> impl IntoView {
/>
</div>
<div class="w-full inline-flex flex-wrap justify-center">
<button type="button" on:click=submit_cards>
<button type="button" on:click=submit_move>
Submit
</button>
</div>
<div class="w-full inline-flex flex-wrap justify-center">
<button type="button" on:click=submit_judge>
Judge
</button>
</div>
// Player cards
<div class="inline-flex flex-wrap justify-center">

View file

@ -8,7 +8,7 @@ pub struct JudgeDecisionRequest {
}
/// Judge round
#[derive(Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JudgeRound {
pub cards_to_judge: Vec<Vec<WhiteCardMeta>>,
}

View file

@ -148,9 +148,13 @@ impl Game {
tracing::debug!("Player is czar");
Err("You can't submit cards to judge, you ARE the judge!".to_string())
} else {
// Error if not enough cards
if request.card_ids.len() < self.current_black.pick.into() {
return Err("You didn't pick enough cards!".to_string());
}
// Ignore extra cards
let trimmed = request.card_ids[..self.current_black.pick.into()].to_vec();
// TODO: handle not enough cards
tracing::debug!("Trimmed: {:#?}", trimmed);
// Move card from player's hand to judge pile
@ -243,7 +247,7 @@ impl Game {
tracing::debug!("{:#?}", self.rotation);
tracing::debug!("{:#?}", self.rotation_index);
// Choose new czar
if self.rotation_index == self.rotation.len() {
if self.rotation_index == self.rotation.len() - 1 {
self.rotation_index = 0;
} else {
self.rotation_index += 1;