blastmud/blastmud_game/src/message_handler/user_commands/status.rs

66 lines
1.7 KiB
Rust

use super::{get_player_item_or_fail, user_error, UResult, UserVerb, UserVerbRef, VerbContext};
use crate::services::combat::max_health;
use ansi::ansi;
use async_trait::async_trait;
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!("<bold>Health [<green>{}<reset><bold>] [ {}/{} ]<reset>\n"),
bar_n_of_m(player_item.health, maxh),
player_item.health,
maxh
));
msg.push_str(&format!(
ansi!("<bold>Credits <green>${}<reset>\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), " ");
}
}