From caa3d840817886bf7d9be7c623eff2c7609a8daa Mon Sep 17 00:00:00 2001 From: Condorra Date: Sun, 26 Feb 2023 22:34:26 +1100 Subject: [PATCH] Allow checking score and status. --- .../src/message_handler/user_commands.rs | 9 +++ .../message_handler/user_commands/score.rs | 58 +++++++++++++++++++ .../message_handler/user_commands/status.rs | 58 +++++++++++++++++++ blastmud_game/src/models/item.rs | 11 ++++ 4 files changed, 136 insertions(+) create mode 100644 blastmud_game/src/message_handler/user_commands/score.rs create mode 100644 blastmud_game/src/message_handler/user_commands/status.rs diff --git a/blastmud_game/src/message_handler/user_commands.rs b/blastmud_game/src/message_handler/user_commands.rs index e13bec7..11ed724 100644 --- a/blastmud_game/src/message_handler/user_commands.rs +++ b/blastmud_game/src/message_handler/user_commands.rs @@ -31,6 +31,8 @@ pub mod parsing; mod quit; mod register; pub mod say; +mod score; +mod status; pub mod use_cmd; mod whisper; mod who; @@ -136,6 +138,13 @@ static REGISTERED_COMMANDS: UserVerbRegistry = phf_map! { "\'" => say::VERB, "say" => say::VERB, + "sc" => score::VERB, + "score" => score::VERB, + + "st" => status::VERB, + "stat" => status::VERB, + "status" => status::VERB, + "use" => use_cmd::VERB, "-" => whisper::VERB, diff --git a/blastmud_game/src/message_handler/user_commands/score.rs b/blastmud_game/src/message_handler/user_commands/score.rs new file mode 100644 index 0000000..49770af --- /dev/null +++ b/blastmud_game/src/message_handler/user_commands/score.rs @@ -0,0 +1,58 @@ +use super::{VerbContext, UserVerb, UserVerbRef, UResult, + user_error, get_player_item_or_fail}; +use crate::{ + models::item::{StatType, SkillType} +}; +use async_trait::async_trait; +use ansi::ansi; + +pub struct Verb; +#[async_trait] +impl UserVerb for Verb { + async fn handle(self: &Self, ctx: &mut VerbContext, _verb: &str, _remaining: &str) -> UResult<()> { + let player_item = get_player_item_or_fail(ctx).await?; + + let user = match ctx.user_dat { + None => user_error("Log in first".to_owned())?, + Some(user) => user + }; + + let mut msg = String::new(); + msg.push_str(&format!(ansi!("| {:11} | {:5} | {:5} |\n"), + "Stat", "Raw", "Total" + )); + for st in StatType::values().iter() { + msg.push_str(&format!(ansi!("| {:11} | {:5.2} | {:5.2} |\n"), + st.display(), + user.raw_stats.get(st).unwrap_or(&0.0), + player_item.total_stats.get(st).unwrap_or(&0.0) + )); + } + msg.push_str("\n"); + + msg.push_str(&format!(ansi!("| {:11} | {:5} | {:5} |\n"), + "Skill", "Raw", "Total" + )); + for st in SkillType::values().iter() { + msg.push_str(&format!(ansi!("| {:11} | {:5.2} | {:5.2} |\n"), + st.display(), + user.raw_skills.get(st).unwrap_or(&0.0), + player_item.total_skills.get(st).unwrap_or(&0.0) + )); + } + msg.push_str("\n"); + msg.push_str(&format!(ansi!("Experience: {} total, \ + change of {} this re-roll, \ + {} spent since re-roll.\n"), + player_item.total_xp, + user.experience.xp_change_for_this_reroll, + user.experience.spent_xp + )); + + ctx.trans.queue_for_session(ctx.session, Some(&msg)).await?; + + Ok(()) + } +} +static VERB_INT: Verb = Verb; +pub static VERB: UserVerbRef = &VERB_INT as UserVerbRef; diff --git a/blastmud_game/src/message_handler/user_commands/status.rs b/blastmud_game/src/message_handler/user_commands/status.rs new file mode 100644 index 0000000..c9dd817 --- /dev/null +++ b/blastmud_game/src/message_handler/user_commands/status.rs @@ -0,0 +1,58 @@ +use super::{VerbContext, UserVerb, UserVerbRef, UResult, + user_error, get_player_item_or_fail}; +use crate::{ + services::combat::max_health, +}; +use async_trait::async_trait; +use ansi::ansi; + +fn bar_n_of_m(mut actual: u64, max: u64) -> String { + if actual > max { + actual = max; + } + let mut r = String::new(); + for _i in 0..actual { + r += "|"; + } + for _i in actual..max { + r += " "; + } + r +} + +pub struct Verb; +#[async_trait] +impl UserVerb for Verb { + async fn handle(self: &Self, ctx: &mut VerbContext, _verb: &str, _remaining: &str) -> UResult<()> { + let player_item = get_player_item_or_fail(ctx).await?; + + let user = match ctx.user_dat { + None => user_error("Log in first".to_owned())?, + Some(user) => user + }; + + let mut msg = String::new(); + let maxh = max_health(&player_item); + msg.push_str(&format!(ansi!("Health [{}] [ {}/{} ]\n"), + bar_n_of_m(player_item.health, maxh), + player_item.health, maxh + )); + msg.push_str(&format!(ansi!("Credits ${}\n"), user.credits)); + ctx.trans.queue_for_session(ctx.session, Some(&msg)).await?; + + Ok(()) + } +} +static VERB_INT: Verb = Verb; +pub static VERB: UserVerbRef = &VERB_INT as UserVerbRef; + +#[cfg(test)] +mod test { + #[test] + fn bar_n_of_m_works() { + assert_eq!(super::bar_n_of_m(4,7), "|||| "); + assert_eq!(super::bar_n_of_m(8,7), "|||||||"); + assert_eq!(super::bar_n_of_m(8,8), "||||||||"); + assert_eq!(super::bar_n_of_m(0,5), " "); + } +} diff --git a/blastmud_game/src/models/item.rs b/blastmud_game/src/models/item.rs index 9339d66..df18a78 100644 --- a/blastmud_game/src/models/item.rs +++ b/blastmud_game/src/models/item.rs @@ -165,6 +165,17 @@ impl StatType { Cool ) } + pub fn display(&self) -> &'static str { + use StatType::*; + match self { + Brains => "brains", + Senses => "senses", + Brawn => "brawn", + Reflexes => "reflexes", + Endurance => "endurance", + Cool => "cool" + } + } } #[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]