Browse Source

fix bug where picking up a stack caused 2x that item to be picked up

pbs
jake 5 years ago
parent
commit
ddee920080
  1. 24
      src/ship/items.rs
  2. 9
      src/ship/packet/handler/direct_message.rs

24
src/ship/items.rs

@ -28,7 +28,6 @@ pub enum ActiveItemEntityId {
Meseta(Meseta), Meseta(Meseta),
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum HeldItemType { pub enum HeldItemType {
Individual(ItemDetail), Individual(ItemDetail),
@ -142,6 +141,12 @@ impl<'a> CharacterInventory<'a> {
} }
} }
pub enum TriggerCreateItem {
Yes,
No
}
#[derive(Error, Debug)] #[derive(Error, Debug)]
#[error("")] #[error("")]
pub enum ItemManagerError { pub enum ItemManagerError {
@ -284,7 +289,7 @@ impl ItemManager {
.map(Clone::clone) .map(Clone::clone)
} }
pub fn character_picks_up_item<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, floor_item: FloorItem) -> Result<(), ItemManagerError> {
pub fn character_picks_up_item<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, floor_item: FloorItem) -> Result<TriggerCreateItem, ItemManagerError> {
let local_floor = self.character_floor.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let local_floor = self.character_floor.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
@ -333,7 +338,7 @@ impl ItemManager {
return Err(ItemManagerError::NoSuchItemId(floor_item.item_id)) return Err(ItemManagerError::NoSuchItemId(floor_item.item_id))
} }
match floor_item.item {
let trigger_create = match floor_item.item {
FloorItemType::Individual(item) => { FloorItemType::Individual(item) => {
let inventory_item = InventoryItem { let inventory_item = InventoryItem {
entity_id: floor_item.entity_id, entity_id: floor_item.entity_id,
@ -354,9 +359,10 @@ impl ItemManager {
}); // TODO: error check }); // TODO: error check
inventory.push(inventory_item); inventory.push(inventory_item);
} // else something went very wrong TODO: log it } // else something went very wrong TODO: log it
TriggerCreateItem::Yes
}, },
FloorItemType::Stacked(tool, amount) => { FloorItemType::Stacked(tool, amount) => {
let inventory_item = inventory.iter_mut()
let (trigger_create, inventory_item) = inventory.iter_mut()
.filter(|i| { .filter(|i| {
if let HeldItemType::Stacked(tooltype, _amount) = i.item { if let HeldItemType::Stacked(tooltype, _amount) = i.item {
tooltype == tool tooltype == tool
@ -382,7 +388,7 @@ impl ItemManager {
*inv_amount += floor_amount *inv_amount += floor_amount
} }
existing_inv_item.clone()
(TriggerCreateItem::No, existing_inv_item.clone())
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| {
let picked_up_item = InventoryItem { let picked_up_item = InventoryItem {
@ -392,7 +398,7 @@ impl ItemManager {
equipped: false, equipped: false,
}; };
inventory.push(picked_up_item.clone()); inventory.push(picked_up_item.clone());
picked_up_item
(TriggerCreateItem::Yes, picked_up_item)
}); });
if let ActiveItemEntityId::Stacked(item_ids) = &inventory_item.entity_id { if let ActiveItemEntityId::Stacked(item_ids) = &inventory_item.entity_id {
@ -408,14 +414,16 @@ impl ItemManager {
}); // TODO: error check }); // TODO: error check
}; };
} // else something went very wrong TODO: log it } // else something went very wrong TODO: log it
trigger_create
}, },
FloorItemType::Meseta(meseta) => { FloorItemType::Meseta(meseta) => {
character.meseta = std::cmp::min(character.meseta + meseta.0, 999999); character.meseta = std::cmp::min(character.meseta + meseta.0, 999999);
entity_gateway.save_character(&character); entity_gateway.save_character(&character);
TriggerCreateItem::No
} }
}
};
Ok(())
Ok(trigger_create)
} }
pub fn enemy_drop_item_on_local_floor<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity, item_drop: ItemDrop) -> Result<&FloorItem, ItemManagerError> { pub fn enemy_drop_item_on_local_floor<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity, item_drop: ItemDrop) -> Result<&FloorItem, ItemManagerError> {

9
src/ship/packet/handler/direct_message.rs

@ -5,7 +5,7 @@ use crate::common::serverstate::ClientId;
use crate::ship::ship::{SendShipPacket, ShipError, Clients, Rooms}; use crate::ship::ship::{SendShipPacket, ShipError, Clients, Rooms};
use crate::ship::location::{ClientLocation, ClientLocationError}; use crate::ship::location::{ClientLocation, ClientLocationError};
use crate::ship::drops::ItemDrop; use crate::ship::drops::ItemDrop;
use crate::ship::items::{ItemManager, FloorItemType, ClientItemId};
use crate::ship::items::{ItemManager, FloorItemType, ClientItemId, TriggerCreateItem};
use crate::entity::gateway::EntityGateway; use crate::entity::gateway::EntityGateway;
use libpso::utf8_to_utf16_array; use libpso::utf8_to_utf16_array;
use crate::ship::packet::builder; use crate::ship::packet::builder;
@ -119,7 +119,7 @@ where
}; };
match item_manager.character_picks_up_item(entity_gateway, &mut client.character, item) { match item_manager.character_picks_up_item(entity_gateway, &mut client.character, item) {
Ok(_) => {
Ok(trigger_create_item) => {
Ok(Box::new(Vec::new().into_iter() Ok(Box::new(Vec::new().into_iter()
.chain(clients_in_area.clone().into_iter() .chain(clients_in_area.clone().into_iter()
.map(move |c| { .map(move |c| {
@ -127,7 +127,10 @@ where
})) }))
.chain(clients_in_area.into_iter(). .chain(clients_in_area.into_iter().
filter_map(move |c| { filter_map(move |c| {
create_item.clone().map(|ci| (c.client, SendShipPacket::Message(Message::new(GameMessage::CreateItem(ci)))))
match trigger_create_item {
TriggerCreateItem::Yes => create_item.clone().map(|ci| (c.client, SendShipPacket::Message(Message::new(GameMessage::CreateItem(ci))))),
_ => None
}
} }
))) )))
) )

Loading…
Cancel
Save