2023-01-12 23:12:50 +11:00
|
|
|
use crate::{
|
2023-01-20 23:08:40 +11:00
|
|
|
DResult,
|
2023-01-22 22:43:44 +11:00
|
|
|
models::item::Item,
|
2023-03-13 15:23:07 +11:00
|
|
|
models::consent::{Consent, ConsentType, ConsentStatus},
|
|
|
|
static_content::npc::npc_by_code,
|
2023-04-24 00:56:42 +10:00
|
|
|
message_handler::user_commands::drop::consider_expire_job_for_item,
|
2023-01-12 23:12:50 +11:00
|
|
|
};
|
2023-02-19 14:03:15 +11:00
|
|
|
use mockall_double::double;
|
|
|
|
#[double] use crate::db::DBTrans;
|
2023-01-20 23:08:40 +11:00
|
|
|
|
2023-03-25 00:58:19 +11:00
|
|
|
pub mod comms;
|
2023-01-22 22:43:44 +11:00
|
|
|
pub mod combat;
|
2023-03-25 00:58:19 +11:00
|
|
|
pub mod skills;
|
2023-02-03 23:26:24 +11:00
|
|
|
pub mod capacity;
|
2023-02-25 23:49:46 +11:00
|
|
|
pub mod effect;
|
|
|
|
|
2023-03-13 15:23:07 +11:00
|
|
|
fn check_one_consent(consent: &Consent, action: &str, target: &Item) -> bool {
|
|
|
|
if let Some((loctype, loccode)) = target.location.split_once("/") {
|
|
|
|
if !consent.only_in.is_empty() {
|
|
|
|
if loctype != "room" || !consent.only_in.iter().any(|v| v == loccode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !consent.allow_private && loctype != "room" {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !consent.only_in.is_empty() || !consent.allow_private {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(fight_consent) = consent.fight_consent.as_ref() {
|
|
|
|
if fight_consent.status == ConsentStatus::PendingAdd {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if !fight_consent.allow_pick && action == "pick" {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn check_consent(trans: &DBTrans, action: &str,
|
|
|
|
consent_type: &ConsentType,
|
|
|
|
by: &Item,
|
|
|
|
target: &Item) -> DResult<bool> {
|
|
|
|
// Consent is only a factor on actions by players towards other players or npcs.
|
|
|
|
if by.item_type != "player" || (target.item_type != "player" && target.item_type != "npc") {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
if target.item_type == "npc" {
|
|
|
|
return Ok(match npc_by_code().get(target.item_code.as_str()) {
|
|
|
|
None => false,
|
|
|
|
Some(npc) => npc.player_consents.contains(consent_type)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:28:03 +11:00
|
|
|
if target.item_code == by.item_code {
|
|
|
|
return Ok(true)
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:23:07 +11:00
|
|
|
trans.delete_expired_user_consent().await?;
|
|
|
|
if let Some(consent) = trans.find_user_consent_by_parties_type(
|
|
|
|
&target.item_code, &by.item_code, consent_type).await? {
|
|
|
|
if check_one_consent(&consent, action, &target) {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 22:31:59 +11:00
|
|
|
|
|
|
|
trans.delete_expired_corp_consent().await?;
|
|
|
|
if let Some(consent) = trans.find_corp_consent_by_user_parties_type(
|
|
|
|
&target.item_code, &by.item_code, consent_type).await? {
|
|
|
|
if check_one_consent(&consent, action, &target) {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 15:23:07 +11:00
|
|
|
|
|
|
|
Ok(false)
|
|
|
|
}
|
2023-04-24 00:56:42 +10:00
|
|
|
|
|
|
|
pub async fn destroy_container(trans: &DBTrans, container: &Item) -> DResult<()> {
|
|
|
|
trans.delete_item(&container.item_type, &container.item_code).await?;
|
|
|
|
|
|
|
|
for item in trans.find_items_by_location(
|
|
|
|
&container.refstr()
|
|
|
|
).await?.into_iter() {
|
|
|
|
let mut item_mut = (*item).clone();
|
|
|
|
// We only update this to support consider_expire_job - it gets updated in bulk
|
|
|
|
// by transfer_all_possession below.
|
|
|
|
item_mut.location = container.location.clone();
|
|
|
|
match capacity::check_item_capacity(trans, &container.location, item_mut.weight).await? {
|
|
|
|
capacity::CapacityLevel::OverBurdened | capacity::CapacityLevel::AboveItemLimit =>
|
|
|
|
trans.delete_item(&item_mut.item_type, &item_mut.item_code).await?,
|
|
|
|
_ => consider_expire_job_for_item(trans, &item_mut).await?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trans.transfer_all_possessions_code(
|
|
|
|
&container.refstr(),
|
|
|
|
&container.location).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|