From 808568949014469badeb3d13af3e414c6dc7473b Mon Sep 17 00:00:00 2001 From: Condorra Date: Sun, 26 Feb 2023 00:56:22 +1100 Subject: [PATCH] Fix edge cases + use up charges when using "use" --- blastmud_game/src/db.rs | 10 +++++ .../src/message_handler/user_commands/look.rs | 10 ++++- .../message_handler/user_commands/use_cmd.rs | 40 +++++++++++++++++-- blastmud_game/src/services/effect.rs | 6 ++- .../src/static_content/possession_type.rs | 12 +++++- 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/blastmud_game/src/db.rs b/blastmud_game/src/db.rs index 5313b433..32ffcf7a 100644 --- a/blastmud_game/src/db.rs +++ b/blastmud_game/src/db.rs @@ -659,6 +659,16 @@ impl DBTrans { Ok(()) } + pub async fn check_task_by_type_code<'a>(&'a self, task_type: &'a str, task_code: &'a str) + -> DResult { + let n : i64 = self.pg_trans()?.query_one( + "SELECT COUNT(*) FROM tasks WHERE \ + details->>'task_type' = $1 AND \ + details->>'task_code' = $2", &[&task_type, &task_code] + ).await?.get(0); + Ok(n > 0) + } + pub async fn alloc_item_code(&self) -> DResult { Ok(self.pg_trans()?.query_one("SELECT NEXTVAL('item_seq')", &[]).await?.get(0)) } diff --git a/blastmud_game/src/message_handler/user_commands/look.rs b/blastmud_game/src/message_handler/user_commands/look.rs index 1a6c99df..34c5ca10 100644 --- a/blastmud_game/src/message_handler/user_commands/look.rs +++ b/blastmud_game/src/message_handler/user_commands/look.rs @@ -98,7 +98,15 @@ pub async fn describe_normal_item(ctx: &VerbContext<'_>, item: &Item) -> UResult &language::caps_first( &item.pronouns.subject), &item.pronouns.possessive)); - } + } + + if ctx.trans.check_task_by_type_code("DelayedHealth", + &format!("{}/{}/bandage", &item.item_type, &item.item_code) + ).await? { + contents_desc.push_str(&format!("{} is wrapped up in bandages.\n", + &language::caps_first(&item.pronouns.subject)) + ); + } } else if item.item_type == "possession" { if health_ratio == 1.0 { contents_desc.push_str(&format!("{}'s in perfect condition.\n", &language::caps_first(&item.pronouns.subject))); diff --git a/blastmud_game/src/message_handler/user_commands/use_cmd.rs b/blastmud_game/src/message_handler/user_commands/use_cmd.rs index 24c3e822..dc2b6531 100644 --- a/blastmud_game/src/message_handler/user_commands/use_cmd.rs +++ b/blastmud_game/src/message_handler/user_commands/use_cmd.rs @@ -26,6 +26,7 @@ use crate::{ skills::skill_check_and_grind, effect::run_effects, }, + language, }; use async_trait::async_trait; use std::time; @@ -156,17 +157,41 @@ impl QueueCommandHandler for QueueHandler { 1, false), target_name, target_name ))? - } - let use_data = match item.possession_type.as_ref() + } + let use_data = match item.possession_type.as_ref() .and_then(|poss_type| possession_data().get(&poss_type)) .and_then(|poss_data| poss_data.use_data.as_ref()) { None => user_error("You can't use that!".to_owned())?, Some(d) => d }; + if let Some(charge_data) = item.possession_type.as_ref() + .and_then(|poss_type| possession_data().get(&poss_type)) + .and_then(|poss_data| poss_data.charge_data.as_ref()) { + if item.charges < 1 { + user_error( + format!("{} has no {} {} left", + item.display_for_sentence(!ctx.session_dat.less_explicit_mode, + 1, true), + &language::pluralise(charge_data.charge_name_prefix), + charge_data.charge_name_suffix + ))?; + } + } if let Some(err) = (use_data.errorf)(&item, &target) { user_error(err)?; } - + if ctx.trans.check_task_by_type_code( + "DelayedHealth", + &format!("{}/{}/{}", &target.item_type, &target.item_code, + use_data.task_ref) + ).await? { + user_error(format!("You see no reason to use {} on {}", + item.display_for_sentence(!ctx.session_dat.less_explicit_mode, + 1, false), + target.display_for_sentence(!ctx.session_dat.less_explicit_mode, + 1, false) + ))?; + } let is_self_use = target_type == &"player" && target_code == &player_item.item_code; let mut player_mut = (*player_item).clone(); let skillcheck = skill_check_and_grind(&ctx.trans, &mut player_mut, @@ -187,7 +212,14 @@ impl QueueCommandHandler for QueueHandler { ctx.trans.save_item_model(&target_mut_save).await?; } ctx.trans.save_item_model(&player_mut).await?; - + + if item.possession_type.as_ref() + .and_then(|poss_type| possession_data().get(&poss_type)) + .and_then(|poss_data| poss_data.charge_data.as_ref()).is_some() { + let mut item_mut = (*item).clone(); + item_mut.charges -= 1; + ctx.trans.save_item_model(&item_mut).await?; + } Ok(()) } } diff --git a/blastmud_game/src/services/effect.rs b/blastmud_game/src/services/effect.rs index b3179f0f..09c33579 100644 --- a/blastmud_game/src/services/effect.rs +++ b/blastmud_game/src/services/effect.rs @@ -1,5 +1,4 @@ use crate::{ - db::DBTrans, models::{ item::Item, task::{ @@ -28,6 +27,8 @@ use serde::{Serialize, Deserialize}; use std::collections::{BTreeMap, VecDeque}; use chrono::Utc; use log::info; +use mockall_double::double; +#[double] use crate::db::DBTrans; #[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub struct DelayedHealthEffect { @@ -58,6 +59,9 @@ impl TaskHandler for DelayedHealthTaskHandler { } Some(it) => it }; + if item.is_dead { + return Ok(None); + } match item_effect_series.1.pop_front() { None => Ok(None), Some(DelayedHealthEffect { magnitude, message, message_nonexp, .. }) => { diff --git a/blastmud_game/src/static_content/possession_type.rs b/blastmud_game/src/static_content/possession_type.rs index 7b40560b..ae934c9c 100644 --- a/blastmud_game/src/static_content/possession_type.rs +++ b/blastmud_game/src/static_content/possession_type.rs @@ -64,7 +64,7 @@ impl Default for ChargeData { Self { max_charges: 1, charge_name_prefix: "charge", - charge_name_suffix: "" + charge_name_suffix: "", } } } @@ -484,6 +484,16 @@ pub fn possession_data() -> &'static BTreeMap { ) }, ), + task_ref: "bandage", + errorf: Box::new( + |_item, target| + if target.is_dead { + Some(format!("It is too late, {}'s dead", target.pronouns.subject)) + } else if target.item_type != "player" && target.item_type != "npc" { + Some("It only works on animals.".to_owned()) + } else { + None + }), ..Default::default() }), ..Default::default()