From 1cf13413e8b0d5ce3b63299fdef6a6e2ae3f6a14 Mon Sep 17 00:00:00 2001 From: Condorra Date: Tue, 28 Mar 2023 22:31:59 +1100 Subject: [PATCH] Allow corp leaders to authorise corp v corp warfare. --- blastmud_game/src/db.rs | 15 +++++++++++++++ .../message_handler/user_commands/allow.rs | 19 ++++++++++++++++++- blastmud_game/src/services.rs | 8 ++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/blastmud_game/src/db.rs b/blastmud_game/src/db.rs index eaa74fc7..2772080c 100644 --- a/blastmud_game/src/db.rs +++ b/blastmud_game/src/db.rs @@ -767,6 +767,21 @@ impl DBTrans { Some(row) => Ok(Some(serde_json::from_value(row.get(0))?)) } } + + pub async fn find_corp_consent_by_user_parties_type(&self, usr_consenting: &str, usr_consented: &str, + consent_type: &ConsentType) -> DResult> { + match self.pg_trans()?.query_opt( + "SELECT cc.details FROM corp_consent cc \ + JOIN corp_membership cm_consenting ON cc.consenting_corp = cm_consenting.corp_id \ + JOIN corp_membership cm_consented ON cc.consented_corp = cm_consented.corp_id \ + WHERE cm_consenting.member_username = $1 AND \ + cm_consented.member_username = $2 AND cc.consent_type = $3", + &[&usr_consenting, &usr_consented, &ConsentType::to_str(consent_type)] + ).await? { + None => Ok(None), + Some(row) => Ok(Some(serde_json::from_value(row.get(0))?)) + } + } pub async fn revoke_until_death_consent(&self, party: &str) -> DResult<()> { self.pg_trans()?.execute( diff --git a/blastmud_game/src/message_handler/user_commands/allow.rs b/blastmud_game/src/message_handler/user_commands/allow.rs index 596874df..9f45cf09 100644 --- a/blastmud_game/src/message_handler/user_commands/allow.rs +++ b/blastmud_game/src/message_handler/user_commands/allow.rs @@ -21,6 +21,7 @@ use crate::{ static_content::room::room_map_by_code, }; use ansi::ansi; +use itertools::Itertools; #[derive(Debug, PartialEq)] pub enum ConsentTarget<'t> { @@ -693,6 +694,22 @@ async fn handle_corp_consent(ctx: &mut VerbContext<'_>, source_player: &Item, user_error("You don't have permission to declare war on behalf of that corp.".to_owned())?; } + if is_allow { + let mut not_allowing_users = vec!(); + for (name, mem) in ctx.trans.list_corp_members(&from_corp_id).await? { + if name != source_player.item_code && !mem.allow_combat { + not_allowing_users.push(name); + } + } + if !not_allowing_users.is_empty() { + user_error(format!(ansi!("War can only be declared when all corp mates allow combat for that corp. Ask them to run corp allow combat from {}, or have them fired! corp config {} allow combat required will stop anyone joining without it / turning it off. Corp mates with it off: {}"), + &from_corp.name, + &from_corp.name, + ¬_allowing_users.iter().join(", ") + ))?; + } + } + ctx.trans.delete_expired_corp_consent().await?; let current_consent = ctx.trans.find_corp_consent_by_parties_type( @@ -711,7 +728,7 @@ async fn handle_corp_consent(ctx: &mut VerbContext<'_>, source_player: &Item, }; let update = compute_new_consent_state( - &to_corp.name, + &from_corp.name, "their", &to_corp.name, "their", diff --git a/blastmud_game/src/services.rs b/blastmud_game/src/services.rs index 7ef1d606..cf22d864 100644 --- a/blastmud_game/src/services.rs +++ b/blastmud_game/src/services.rs @@ -67,6 +67,14 @@ pub async fn check_consent(trans: &DBTrans, action: &str, return Ok(true); } } + + 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); + } + } Ok(false) }