Browse Source

check inventory has enough space to accept traded items

pull/80/head
jake 3 years ago
parent
commit
6bd341cd01
  1. 12
      src/ship/items/inventory.rs
  2. 48
      src/ship/items/manager.rs

12
src/ship/items/inventory.rs

@ -93,10 +93,16 @@ pub enum YesThereIsSpace {
ExistingStack, ExistingStack,
} }
#[derive(Debug, Clone)]
pub enum NoThereIsNotSpace {
FullStack,
FullInventory,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SpaceForStack { pub enum SpaceForStack {
Yes(YesThereIsSpace), Yes(YesThereIsSpace),
No,
No(NoThereIsNotSpace),
} }
impl InventoryItem { impl InventoryItem {
@ -470,7 +476,7 @@ impl CharacterInventory {
SpaceForStack::Yes(YesThereIsSpace::ExistingStack) SpaceForStack::Yes(YesThereIsSpace::ExistingStack)
} }
else { else {
SpaceForStack::No
SpaceForStack::No(NoThereIsNotSpace::FullStack)
} }
} }
None => { None => {
@ -478,7 +484,7 @@ impl CharacterInventory {
SpaceForStack::Yes(YesThereIsSpace::NewStack) SpaceForStack::Yes(YesThereIsSpace::NewStack)
} }
else { else {
SpaceForStack::No
SpaceForStack::No(NoThereIsNotSpace::FullInventory)
} }
} }
} }

48
src/ship/items/manager.rs

@ -15,6 +15,7 @@ use crate::ship::trade::TradeItem;
use crate::ship::drops::{ItemDrop, ItemDropType}; use crate::ship::drops::{ItemDrop, ItemDropType};
use crate::ship::location::{AreaClient, RoomId}; use crate::ship::location::{AreaClient, RoomId};
use crate::ship::shops::ShopItem; use crate::ship::shops::ShopItem;
use crate::ship::packet::handler::trade::TradeError;
use crate::ship::items::bank::*; use crate::ship::items::bank::*;
use crate::ship::items::floor::*; use crate::ship::items::floor::*;
@ -329,7 +330,7 @@ impl ItemManager {
})); }));
TriggerCreateItem::No TriggerCreateItem::No
}, },
SpaceForStack::No => {
SpaceForStack::No(_) => {
return Err(ItemManagerError::CouldNotAddToInventory(*item_id).into()); return Err(ItemManagerError::CouldNotAddToInventory(*item_id).into());
}, },
} }
@ -945,9 +946,48 @@ impl ItemManager {
let p1_inventory = it.manager.get_character_inventory(p1.1)?; let p1_inventory = it.manager.get_character_inventory(p1.1)?;
let p2_inventory = it.manager.get_character_inventory(p2.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::<Result<Vec<_>, _>>()?;
let trade_items = [(p1, p2, p1_inventory, p2_inventory), (p2, p1, p2_inventory, p1_inventory)] let trade_items = [(p1, p2, p1_inventory, p2_inventory), (p2, p1, p2_inventory, p1_inventory)]
.map(|(src_client, dest_client, src_inventory, dest_inventory)| { .map(|(src_client, dest_client, src_inventory, dest_inventory)| {

Loading…
Cancel
Save