From 883977a42f698b4a19089eb6b33028d5cd8aed76 Mon Sep 17 00:00:00 2001 From: Rickey Chamblee Date: Mon, 12 Apr 2021 23:00:38 +0000 Subject: [PATCH] Denies any active trades on exit to char select. --- CHANGELOG.md | 1 + server/src/events/mod.rs | 7 +++++-- server/src/events/trade.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d449224398..807f0b36da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Social window no longer moves when group is open - Combat rating no longer takes buffs into account - Minimap icons are now displayed in both map modes +- Server now denies any running trades when a user exits to the character selection screen. ## [0.9.0] - 2021-03-20 diff --git a/server/src/events/mod.rs b/server/src/events/mod.rs index 59c5d0c26a..be3da41314 100644 --- a/server/src/events/mod.rs +++ b/server/src/events/mod.rs @@ -20,7 +20,7 @@ use inventory_manip::handle_inventory; use invite::{handle_invite, handle_invite_response}; use player::{handle_client_disconnect, handle_exit_ingame}; use specs::{Entity as EcsEntity, WorldExt}; -use trade::handle_process_trade_action; +use trade::{cancel_trade_for, handle_process_trade_action}; mod entity_creation; mod entity_manipulation; @@ -133,7 +133,10 @@ impl Server { ServerEvent::UpdateCharacterData { entity, components } => { handle_loaded_character_data(self, entity, components); }, - ServerEvent::ExitIngame { entity } => handle_exit_ingame(self, entity), + ServerEvent::ExitIngame { entity } => { + cancel_trade_for(self, entity); + handle_exit_ingame(self, entity); + }, ServerEvent::CreateNpc { pos, stats, diff --git a/server/src/events/trade.rs b/server/src/events/trade.rs index bda4a32a4c..f00d031700 100644 --- a/server/src/events/trade.rs +++ b/server/src/events/trade.rs @@ -156,6 +156,32 @@ pub fn handle_process_trade_action( } } +//Cancel all trades registered for a given UID. +pub fn cancel_trade_for(server: &mut Server, entity: EcsEntity) { + if let Some(uid) = server.state().ecs().uid_from_entity(entity) { + let mut trades = server.state.ecs().write_resource::(); + + let active_trade = match trades.entity_trades.get(&uid) { + Some(n) => *n, + None => { + return; + }, + }; + + let to_notify = trades.decline_trade(active_trade, uid); + to_notify + .and_then(|u| server.state.ecs().entity_from_uid(u.0)) + .map(|e| { + server.notify_client(e, ServerGeneral::FinishedTrade(TradeResult::Declined)); + notify_agent_simple( + server.state.ecs().write_storage::(), + e, + AgentEvent::FinishedTrade(TradeResult::Declined), + ); + }); + } +} + /// Commit a trade that both parties have agreed to, modifying their respective /// inventories fn commit_trade(ecs: &specs::World, trade: &PendingTrade) -> TradeResult {