From 66fe22c89e03dfe8124c9703f1f3187b2d867f50 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 16 May 2023 22:16:05 +0300 Subject: [PATCH] feat(client): play cards --- client/src/api/game/mod.rs | 4 +++ client/src/game_status/parser.rs | 24 +++++++++++++ client/src/plugins/game/hand/mod.rs | 52 +++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/client/src/api/game/mod.rs b/client/src/api/game/mod.rs index 7593f0f..073283d 100644 --- a/client/src/api/game/mod.rs +++ b/client/src/api/game/mod.rs @@ -42,6 +42,10 @@ pub enum Command { index: usize, for_cost: usize, }, + /// play card from hand index N + PlayCard { + index: usize, + }, /// draw N amount of cards from deck Draw { amount: usize, diff --git a/client/src/game_status/parser.rs b/client/src/game_status/parser.rs index e54cb19..9029cf4 100644 --- a/client/src/game_status/parser.rs +++ b/client/src/game_status/parser.rs @@ -135,6 +135,30 @@ fn parse_action(action: &Action, game: &Game, game_status: &mut GameStatus) { target.discard.push(pile.card.clone()); } + Command::PlayCard { index } => { + // index should be within range + assert!(*index <= target.hand.len()); + + // player should have plays + assert!(target.plays > 0); + target.plays -= 1; + + let card = target.hand.remove(*index); + target.discard.push(card.clone()); + + for card_action in &card.actions { + let action = &Action::new( + &game.id, + &action.invoker, + &action.target, + &card_action.command, + current_seed!(action), + ); + game_status.actions.insert(action_pos + 1, action.clone()); + + parse_action(action, game, game_status); + } + } Command::Draw { amount } => { for _ in 0..*amount { if target.deck.is_empty() { diff --git a/client/src/plugins/game/hand/mod.rs b/client/src/plugins/game/hand/mod.rs index 13125af..41be511 100644 --- a/client/src/plugins/game/hand/mod.rs +++ b/client/src/plugins/game/hand/mod.rs @@ -9,10 +9,15 @@ use bevy::prelude::*; use bevy_rapier3d::prelude::*; -use crate::Global; +use crate::{ + api::game::{Action, Command}, + game_status::PlayerState, + plugins::GameActionCreateCallEvent, + seed_gen, Global, +}; use super::{ - card::{visual_card_kind, VisualCard, VisualCardBundle}, + card::{visual_card_kind, ClickedCard, VisualCard, VisualCardBundle}, GameData, }; @@ -23,7 +28,8 @@ impl Plugin for HandPlugin { app.add_event::() .add_event::() .add_system(spawn_hand.run_if(on_event::())) - .add_system(position_hand.run_if(on_event::())); //.add_system(handle_clicked_hand_card); + .add_system(position_hand.run_if(on_event::())) + .add_system(handle_clicked_hand_card); } } @@ -86,31 +92,51 @@ fn position_hand( } } -/* fn handle_clicked_hand_card( mut commands: Commands, - mut card_query: Query<(Entity, &VisualCard, &visual_card_kind::Supply), (With, With)>, + card_query: Query< + (Entity, &visual_card_kind::Hand), + (With, With), + >, mut gac_ev_w: EventWriter, - game_data: Res, + mut game_data: ResMut, global: Res, ) { - let Ok((entity, card, card_kind)) = card_query.get_single() else { + if game_data.locked { + return; + } + + let Ok((entity, card_kind)) = card_query.get_single() else { return; }; commands.entity(entity).remove::(); + let player = game_data + .game_status + .as_ref() + .unwrap() + .players + .get(&global.user.as_ref().unwrap().id) + .unwrap(); + + #[allow(clippy::if_same_then_else)] + if player.state != PlayerState::PlayPhase { + // we ain't playing rn + return; + } else if player.plays == 0 { + // not enough plays + return; + } + + game_data.locked = true; gac_ev_w.send(GameActionCreateCallEvent { action: Action::new( &game_data.game.as_ref().unwrap().id, &global.user.as_ref().unwrap().id, &global.user.as_ref().unwrap().id, - &Command::TakeFromPile { - index: card_kind.0, - for_cost: card.card.cost, - }, - fastrand::u64(u64::MIN..=u64::MAX), + &Command::PlayCard { index: card_kind.0 }, + seed_gen!(), ), }); } -*/ -- 2.44.1