From d0a902d9188b72a9f7600de0eb9d169bbfd156f1 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 16 Apr 2023 15:07:34 -0400 Subject: [PATCH] Being attacked now cancels all active trades. --- server/src/events/entity_manipulation.rs | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/server/src/events/entity_manipulation.rs b/server/src/events/entity_manipulation.rs index 554221f3e3..e91b92d584 100644 --- a/server/src/events/entity_manipulation.rs +++ b/server/src/events/entity_manipulation.rs @@ -28,6 +28,7 @@ use common::{ resources::{Secs, Time}, states::utils::StageSection, terrain::{Block, BlockKind, TerrainGrid}, + trade::{TradeResult, Trades}, uid::{Uid, UidAllocator}, util::Dir, vol::ReadVol, @@ -1409,6 +1410,35 @@ pub fn handle_entity_attacked_hook(server: &Server, entity: EcsEntity) { entity, buff_change: buff::BuffChange::RemoveByKind(BuffKind::Saturation), }); + + // If entity was in an active trade, cancel it + let mut trades = ecs.write_resource::(); + let uids = ecs.read_storage::(); + let clients = ecs.read_storage::(); + let mut agents = ecs.write_storage::(); + let mut notify_trade_party = |entity| { + if let Some(client) = clients.get(entity) { + client.send_fallible(ServerGeneral::FinishedTrade(TradeResult::Declined)); + } + if let Some(agent) = agents.get_mut(entity) { + agent + .inbox + .push_back(AgentEvent::FinishedTrade(TradeResult::Declined)); + } + }; + if let Some(uid) = uids.get(entity) { + // Notify attacked entity + notify_trade_party(entity); + if let Some(trade) = trades.entity_trades.get(uid).copied() { + trades + .decline_trade(trade, *uid) + .and_then(|uid| ecs.entity_from_uid(uid.0)) + .map(|entity| { + // Notify person trading with attacked person + notify_trade_party(entity) + }); + } + } } pub fn handle_change_ability(