blastmud/blastmud_game/src/message_handler/user_commands/status.rs
Condorra 0c280711e8 Add a mini-game around sharing knowledge
It still needs to apply buffs at the end and a few other details!
2023-12-27 00:34:47 +11:00

82 lines
2.4 KiB
Rust

use super::{get_player_item_or_fail, user_error, UResult, UserVerb, UserVerbRef, VerbContext};
use crate::{
models::item::Urges,
services::{combat::max_health, display::bar_n_of_m},
};
use ansi::ansi;
use async_trait::async_trait;
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
));
let (hunger, thirst, stress) = match player_item.urges.as_ref() {
None => (0, 0, 0),
Some(Urges {
hunger,
thirst,
stress,
}) => (hunger.value, thirst.value, stress.value),
};
msg.push_str(&format!(
ansi!("<bold>Hunger [<green>{}<reset><bold>] [ {}/{} ]<reset>\n"),
bar_n_of_m((hunger / 200) as u64, 50),
hunger / 100,
100
));
msg.push_str(&format!(
ansi!("<bold>Thirst [<green>{}<reset><bold>] [ {}/{} ]<reset>\n"),
bar_n_of_m((thirst / 200) as u64, 50),
thirst / 100,
100
));
msg.push_str(&format!(
ansi!("<bold>Stress [<green>{}<reset><bold>] [ {}/{} ]<reset>\n"),
bar_n_of_m((stress / 200) as u64, 50),
stress / 100,
100
));
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), " ");
}
}