From 7d5486e5144305656353cbfcdfd8a177c99cb88b Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 25 May 2023 10:58:58 +0300 Subject: [PATCH] feat(client): advanced roll action, log configs --- client/assets/card_manifest.yaml | 26 ++++++++++++++----- client/src/api/game/mod.rs | 14 ++++++++--- client/src/game_status/parser.rs | 43 ++++++++++++++++++++++++-------- client/src/util/action_to_log.rs | 29 +++++++++++++++++---- 4 files changed, 86 insertions(+), 26 deletions(-) diff --git a/client/assets/card_manifest.yaml b/client/assets/card_manifest.yaml index 583c8e8..71019c5 100644 --- a/client/assets/card_manifest.yaml +++ b/client/assets/card_manifest.yaml @@ -83,22 +83,36 @@ cards: - name: Test Card 6 short_details: - - 1d20 currency, <15 = 0, 15-19 = 2, 20 = 6 + - 1d20 for currency, <4 = -2VP, 4-15 = 0, 15-19 = 2, 20 = 6 long_details: - - Roll 1d20 for currency + - Roll 1d20 for currency, with a possibility to lose VP cost: 4 actions: - command: - !RollForCurrencyAdvanced + !RollForAdvanced amount: 1 sides: 20 pairs: + - - 4 + - command: + !RemoveVP + amount: 2 + target_self: true - - 15 - - 0 + - command: + !GiveCurrency + amount: 0 + target_self: true - - 19 - - 2 + - command: + !GiveCurrency + amount: 2 + target_self: true - - 20 - - 6 + - command: + !GiveCurrency + amount: 6 + target_self: true target_self: true to_be_trashed: false diff --git a/client/src/api/game/mod.rs b/client/src/api/game/mod.rs index 09dd17c..9cad4f9 100644 --- a/client/src/api/game/mod.rs +++ b/client/src/api/game/mod.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; use serde_repr::Deserialize_repr; use crate::{ - game_status::{Card, PlayerState}, + game_status::{Card, PlayerState, CardAction}, seed_gen, }; @@ -69,14 +69,17 @@ pub enum Command { ChangePlayerState { state: PlayerState, }, - RollForCurrency { + RollForAdvanced { amount: usize, sides: usize, + pairs: Vec<(usize, CardAction)>, + }, + GiveCurrency { + amount: usize, }, - RollForCurrencyAdvanced { + RollForCurrency { amount: usize, sides: usize, - pairs: Vec<(usize, usize)>, }, GivePlays { amount: usize, @@ -91,6 +94,9 @@ pub enum Command { GiveVP { amount: usize, }, + RemoveVP { + amount: usize, + }, /// mark a card in the targets deck to be trashed on play. /// if index in None, a random card will be chosen MarkCardInDeckToBeTrashed { diff --git a/client/src/game_status/parser.rs b/client/src/game_status/parser.rs index 5c886e5..f326014 100644 --- a/client/src/game_status/parser.rs +++ b/client/src/game_status/parser.rs @@ -248,15 +248,36 @@ fn parse_action(action: &Action, game: &Game, game_status: &mut GameStatus) { Command::ChangePlayerState { state } => { target.state = *state; } - Command::RollForCurrency { amount, sides } => { + Command::RollForAdvanced { amount, sides, pairs } => { let mut results: Vec = Vec::with_capacity(*amount); for _ in 0..*amount { let result = Rng::with_seed(action.seed.parse::().unwrap()).usize(1..=*sides); - target.currency += result; + + let mut last = 0; + for (pos, res) in pairs { + if (last..=*pos).contains(&result) { + // TODO: check res.target_self, and select another + // player as target if false. + let action = Action::new( + &game.id, + &action.target, + &action.target, + &res.command, + current_seed!(action), + ); + game_status + .actions + .insert(action_pos + 1, action.clone()); + + parse_action(&action, game, game_status); + } + last = *pos; + } results.push(result); } + /* if *amount == 1 { game_status .log @@ -276,19 +297,16 @@ fn parse_action(action: &Action, game: &Game, game_status: &mut GameStatus) { results.iter().sum::(), ))])); } + */ + } + Command::GiveCurrency { amount } => { + target.currency += amount; } - Command::RollForCurrencyAdvanced { amount, sides, pairs } => { + Command::RollForCurrency { amount, sides } => { let mut results: Vec = Vec::with_capacity(*amount); for _ in 0..*amount { let result = Rng::with_seed(action.seed.parse::().unwrap()).usize(1..=*sides); - - let mut last = 0; - for (pos, res) in pairs { - if (last..=*pos).contains(&result) { - target.currency += res; - } - last = *pos; - } + target.currency += result; results.push(result); } @@ -351,6 +369,9 @@ fn parse_action(action: &Action, game: &Game, game_status: &mut GameStatus) { Command::GiveVP { amount } => { target.vp += amount; } + Command::RemoveVP { amount } => { + target.vp -= amount; + } Command::MarkCardInDeckToBeTrashed { index } => { if target.deck.is_empty() { return; diff --git a/client/src/util/action_to_log.rs b/client/src/util/action_to_log.rs index 89211d7..be07dd5 100644 --- a/client/src/util/action_to_log.rs +++ b/client/src/util/action_to_log.rs @@ -88,17 +88,21 @@ pub fn action_to_log(action: &Action, game_status: &GameStatus) -> LogEntry { LogSection::normal(" changed state to "), LogSection::bold(&format!("{:?}", state)), ]), + Command::GiveCurrency { amount } => LogEntry::from_sections(vec![ + LogSection::bold(target_name), + LogSection::bold(&format!("+{}", amount)), + LogSection::normal(" currency"), + ]), Command::RollForCurrency { amount, sides } => LogEntry::from_sections(vec![ LogSection::bold(invoker_name), LogSection::normal(" rolled "), LogSection::bold(&format!("{}d{}", amount, sides)), LogSection::normal(" for currency:"), ]), - Command::RollForCurrencyAdvanced { amount, sides, pairs: _ } => LogEntry::from_sections(vec![ - LogSection::bold(invoker_name), - LogSection::normal(" rolled "), - LogSection::bold(&format!("{}d{}", amount, sides)), - LogSection::normal(" for currency:"), + Command::GivePlays { amount } => LogEntry::from_sections(vec![ + LogSection::bold(target_name), + LogSection::bold(&format!("+{}", amount)), + LogSection::normal(" plays"), ]), Command::RollForPlays { amount, sides } => LogEntry::from_sections(vec![ LogSection::bold(invoker_name), @@ -106,6 +110,21 @@ pub fn action_to_log(action: &Action, game_status: &GameStatus) -> LogEntry { LogSection::bold(&format!("{}d{}", amount, sides)), LogSection::normal(" for plays:"), ]), + Command::GiveBuys { amount } => LogEntry::from_sections(vec![ + LogSection::bold(target_name), + LogSection::bold(&format!(" +{}", amount)), + LogSection::normal(" buys"), + ]), + Command::GiveVP { amount } => LogEntry::from_sections(vec![ + LogSection::bold(target_name), + LogSection::bold(&format!(" +{}", amount)), + LogSection::normal(" VP"), + ]), + Command::RemoveVP { amount } => LogEntry::from_sections(vec![ + LogSection::bold(target_name), + LogSection::bold(&format!(" -{}", amount)), + LogSection::normal(" VP"), + ]), #[allow(unreachable_patterns)] _ => LogEntry { sections: vec![LogSection::normal("No log configuration for this command")], -- 2.44.1