From 6bd341cd010b250b22b7dbe91d88b922c313d504 Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 12 Dec 2021 15:55:24 -0700 Subject: [PATCH] check inventory has enough space to accept traded items --- src/ship/items/inventory.rs | 12 +++++++--- src/ship/items/manager.rs | 48 +++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/ship/items/inventory.rs b/src/ship/items/inventory.rs index a44c97d..5bb5c19 100644 --- a/src/ship/items/inventory.rs +++ b/src/ship/items/inventory.rs @@ -93,10 +93,16 @@ pub enum YesThereIsSpace { ExistingStack, } +#[derive(Debug, Clone)] +pub enum NoThereIsNotSpace { + FullStack, + FullInventory, +} + #[derive(Debug, Clone)] pub enum SpaceForStack { Yes(YesThereIsSpace), - No, + No(NoThereIsNotSpace), } impl InventoryItem { @@ -470,7 +476,7 @@ impl CharacterInventory { SpaceForStack::Yes(YesThereIsSpace::ExistingStack) } else { - SpaceForStack::No + SpaceForStack::No(NoThereIsNotSpace::FullStack) } } None => { @@ -478,7 +484,7 @@ impl CharacterInventory { SpaceForStack::Yes(YesThereIsSpace::NewStack) } else { - SpaceForStack::No + SpaceForStack::No(NoThereIsNotSpace::FullInventory) } } } diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index 143fd26..3050432 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -15,6 +15,7 @@ use crate::ship::trade::TradeItem; use crate::ship::drops::{ItemDrop, ItemDropType}; use crate::ship::location::{AreaClient, RoomId}; use crate::ship::shops::ShopItem; +use crate::ship::packet::handler::trade::TradeError; use crate::ship::items::bank::*; use crate::ship::items::floor::*; @@ -329,7 +330,7 @@ impl ItemManager { })); TriggerCreateItem::No }, - SpaceForStack::No => { + SpaceForStack::No(_) => { return Err(ItemManagerError::CouldNotAddToInventory(*item_id).into()); }, } @@ -945,9 +946,48 @@ impl ItemManager { let p1_inventory = it.manager.get_character_inventory(p1.1)?; let p2_inventory = it.manager.get_character_inventory(p2.1)?; - //TODO: inv-selftrade+othertrade <= 30 - //if p1_inventory - + [(p2_inventory, p1_inventory, p2.2), (p1_inventory, p2_inventory, p1.2)].iter() + .map(|(src_inventory, dest_inventory, to_trade)| { + to_trade + .iter() + .try_fold(dest_inventory.count(), |acc, item| { + match item { + TradeItem::Individual(..) => { + if acc >= 30 { + Err(TradeError::NoInventorySpace) + } + else { + Ok(acc + 1) + } + }, + TradeItem::Stacked(item_id, amount) => { + let stacked_inventory_item = src_inventory + .get_item_by_id(*item_id) + .ok_or_else(|| TradeError::InvalidItemId(*item_id))? + .stacked() + .ok_or_else(|| TradeError::InvalidItemId(*item_id))?; + match dest_inventory.space_for_stacked_item(&stacked_inventory_item.tool, *amount) { + SpaceForStack::Yes(YesThereIsSpace::ExistingStack) => { + Ok(acc) + }, + SpaceForStack::Yes(YesThereIsSpace::NewStack) => { + Ok(acc + 1) + }, + SpaceForStack::No(NoThereIsNotSpace::FullStack) => { + Err(TradeError::NoStackSpace) + }, + SpaceForStack::No(NoThereIsNotSpace::FullInventory) => { + Err(TradeError::NoInventorySpace) + }, + } + }, + TradeItem::Meseta(..) => { + Ok(acc) + } + } + }) + }) + .collect::, _>>()?; let trade_items = [(p1, p2, p1_inventory, p2_inventory), (p2, p1, p2_inventory, p1_inventory)] .map(|(src_client, dest_client, src_inventory, dest_inventory)| {