From 317f236f7c5e008eb65b0fdd7e4b69b67b8af0c3 Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 19 Jul 2022 20:06:43 -0600 Subject: [PATCH] move floor code out of state --- src/ship/items/actions.rs | 4 +- src/ship/items/floor.rs | 139 ++++++++++++++++++++++ src/ship/items/inventory.rs | 2 +- src/ship/items/mod.rs | 1 + src/ship/items/state.rs | 130 +------------------- src/ship/packet/builder/message.rs | 2 +- src/ship/packet/handler/direct_message.rs | 3 +- 7 files changed, 148 insertions(+), 133 deletions(-) create mode 100644 src/ship/items/floor.rs diff --git a/src/ship/items/actions.rs b/src/ship/items/actions.rs index 321db6a..9753043 100644 --- a/src/ship/items/actions.rs +++ b/src/ship/items/actions.rs @@ -8,10 +8,10 @@ use std::pin::Pin; use crate::ship::map::MapArea; use crate::entity::character::{CharacterEntity, CharacterEntityId}; use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction}; -use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateError, FloorItem, AddItemResult, FloorItemDetail, - StackedItemDetail, BankItem, BankItemDetail, IndividualItemDetail}; +use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateError, AddItemResult, StackedItemDetail, BankItem, BankItemDetail, IndividualItemDetail}; use crate::ship::items::itemstateaction::{ItemStateAction, ItemAction}; use crate::ship::items::inventory::{InventoryItem, InventoryItemDetail}; +use crate::ship::items::floor::{FloorItem, FloorItemDetail}; use crate::ship::items::apply_item::apply_item; use crate::entity::item::{ItemDetail, NewItemEntity, TradeId}; use crate::entity::item::tool::Tool; diff --git a/src/ship/items/floor.rs b/src/ship/items/floor.rs new file mode 100644 index 0000000..2aab742 --- /dev/null +++ b/src/ship/items/floor.rs @@ -0,0 +1,139 @@ +use crate::ship::items::ClientItemId; +use crate::entity::item::{Meseta, ItemEntityId, ItemDetail}; +use std::future::Future; + +use crate::ship::map::MapArea; +use crate::entity::character::CharacterEntityId; +use crate::entity::item::mag::Mag; + +use crate::ship::items::state::ItemStateError; +use crate::ship::items::state::{IndividualItemDetail, StackedItemDetail}; +use crate::ship::items::inventory::{InventoryItem, InventoryItemDetail}; + +pub enum FloorType { + Local, + Shared, +} + +#[derive(Debug, Clone)] +pub enum FloorItemDetail { + Individual(IndividualItemDetail), + Stacked(StackedItemDetail), + Meseta(Meseta), +} + +#[derive(Debug, Clone)] +pub struct FloorItem { + pub item_id: ClientItemId, + pub item: FloorItemDetail, + pub map_area: MapArea, + pub x: f32, + pub y: f32, + pub z: f32, +} + +impl FloorItem { + pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result + where + F: FnMut(T, ItemEntityId) -> Fut, + Fut: Future>, + { + match &self.item { + FloorItemDetail::Individual(individual_item) => { + param = func(param, individual_item.entity_id).await?; + }, + FloorItemDetail::Stacked(stacked_item) => { + for entity_id in &stacked_item.entity_ids { + param = func(param, *entity_id).await?; + } + }, + FloorItemDetail::Meseta(_meseta) => {}, + } + + Ok(param) + } + + pub async fn with_mag(&self, mut param: T, mut func: F) -> Result + where + F: FnMut(T, ItemEntityId, Mag) -> Fut, + Fut: Future>, + { + if let FloorItemDetail::Individual(individual_item) = &self.item { + if let ItemDetail::Mag(mag) = &individual_item.item { + param = func(param, individual_item.entity_id, mag.clone()).await?; + } + } + Ok(param) + } + + pub fn as_client_bytes(&self) -> [u8; 16] { + match &self.item { + FloorItemDetail::Individual(individual_floor_item) => { + individual_floor_item.item.as_client_bytes() + }, + FloorItemDetail::Stacked(stacked_floor_item) => { + stacked_floor_item.tool.as_stacked_bytes(stacked_floor_item.entity_ids.len()) + }, + FloorItemDetail::Meseta(meseta_floor_item) => { + meseta_floor_item.as_bytes() + } + } + } +} + +#[derive(Debug, Clone, Default)] +pub struct LocalFloor(pub Vec); +#[derive(Debug, Clone, Default)] +pub struct SharedFloor(pub Vec); + +#[derive(Debug)] +pub struct FloorState { + pub character_id: CharacterEntityId, + pub local: LocalFloor, + pub shared: SharedFloor, +} + +impl FloorState { + pub fn take_item(&mut self, item_id: &ClientItemId) -> Option { + let item = self.local.0 + .drain_filter(|item| { + item.item_id == *item_id + }) + .next(); + item.or_else(|| { + self.shared.0 + .drain_filter(|item| { + item.item_id == *item_id + }) + .next() + }) + } + + pub fn add_inventory_item(&mut self, inventory_item: InventoryItem, map_area: MapArea, position: (f32, f32, f32)) -> &FloorItem { + let floor_item = FloorItem { + item_id: inventory_item.item_id, + item: match inventory_item.item { + InventoryItemDetail::Individual(individual_item) => FloorItemDetail::Individual(individual_item), + InventoryItemDetail::Stacked(stacked_item) => FloorItemDetail::Stacked(stacked_item), + }, + map_area, + x: position.0, + y: position.1, + z: position.2, + }; + + self.shared.0.push(floor_item); + &self.shared.0[self.shared.0.len()-1] + } + + pub fn add_shared_item(&mut self, floor_item: FloorItem) -> &FloorItem { + self.shared.0.push(floor_item); + &self.shared.0[self.shared.0.len()-1] + } + + pub fn add_local_item(&mut self, floor_item: FloorItem) -> &FloorItem { + self.local.0.push(floor_item); + &self.local.0[self.local.0.len()-1] + } +} + diff --git a/src/ship/items/inventory.rs b/src/ship/items/inventory.rs index 602b515..4024fc8 100644 --- a/src/ship/items/inventory.rs +++ b/src/ship/items/inventory.rs @@ -10,7 +10,7 @@ use crate::entity::item::mag::Mag; use crate::ship::shops::{ShopItem, ArmorShopItem, ToolShopItem, WeaponShopItem}; use crate::ship::items::state::ItemStateError; use crate::ship::items::state::{IndividualItemDetail, StackedItemDetail, AddItemResult}; -use crate::ship::items::state::{FloorItem, FloorItemDetail}; +use crate::ship::items::floor::{FloorItem, FloorItemDetail}; #[derive(Clone, Debug)] pub enum InventoryItemDetail { diff --git a/src/ship/items/mod.rs b/src/ship/items/mod.rs index 3e161f4..e318df8 100644 --- a/src/ship/items/mod.rs +++ b/src/ship/items/mod.rs @@ -3,6 +3,7 @@ pub mod actions; pub mod apply_item; pub mod itemstateaction; pub mod inventory; +pub mod floor; #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, serde::Serialize, serde::Deserialize, derive_more::Display)] pub struct ClientItemId(pub u32); diff --git a/src/ship/items/state.rs b/src/ship/items/state.rs index 724282e..84bd883 100644 --- a/src/ship/items/state.rs +++ b/src/ship/items/state.rs @@ -5,7 +5,6 @@ use crate::ship::items::ClientItemId; use crate::entity::item::{Meseta, ItemEntityId, ItemDetail, ItemEntity, InventoryItemEntity, BankEntity, BankItemEntity, BankName}; use std::future::Future; -use crate::ship::map::MapArea; use crate::ship::location::{AreaClient, RoomId}; use crate::entity::character::{CharacterEntity, CharacterEntityId}; use crate::entity::gateway::{EntityGateway, GatewayError}; @@ -16,6 +15,8 @@ use crate::ship::drops::ItemDrop; use crate::ship::items::inventory::{Inventory, InventoryItem, InventoryItemDetail, InventoryError, InventoryState}; +use crate::ship::items::floor::{FloorState, FloorItem, LocalFloor, SharedFloor, FloorType}; + // TODO: Commit trait that ItemStateProxy and EntityTransaction implement that .commit requires and acts on upon everything succeeding (like 3 less lines of code!) #[derive(thiserror::Error, Debug)] @@ -79,11 +80,6 @@ pub enum ItemStateError { WrongItemType(ClientItemId), } -pub enum FloorType { - Local, - Shared, -} - #[derive(Clone, Debug)] pub struct IndividualItemDetail { @@ -201,71 +197,6 @@ impl BankItem { } } -#[derive(Debug, Clone)] -pub enum FloorItemDetail { - Individual(IndividualItemDetail), - Stacked(StackedItemDetail), - Meseta(Meseta), -} - -#[derive(Debug, Clone)] -pub struct FloorItem { - pub item_id: ClientItemId, - pub item: FloorItemDetail, - pub map_area: MapArea, - pub x: f32, - pub y: f32, - pub z: f32, -} - -impl FloorItem { - pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result - where - F: FnMut(T, ItemEntityId) -> Fut, - Fut: Future>, - { - match &self.item { - FloorItemDetail::Individual(individual_item) => { - param = func(param, individual_item.entity_id).await?; - }, - FloorItemDetail::Stacked(stacked_item) => { - for entity_id in &stacked_item.entity_ids { - param = func(param, *entity_id).await?; - } - }, - FloorItemDetail::Meseta(_meseta) => {}, - } - - Ok(param) - } - - pub async fn with_mag(&self, mut param: T, mut func: F) -> Result - where - F: FnMut(T, ItemEntityId, Mag) -> Fut, - Fut: Future>, - { - if let FloorItemDetail::Individual(individual_item) = &self.item { - if let ItemDetail::Mag(mag) = &individual_item.item { - param = func(param, individual_item.entity_id, mag.clone()).await?; - } - } - Ok(param) - } - - pub fn as_client_bytes(&self) -> [u8; 16] { - match &self.item { - FloorItemDetail::Individual(individual_floor_item) => { - individual_floor_item.item.as_client_bytes() - }, - FloorItemDetail::Stacked(stacked_floor_item) => { - stacked_floor_item.tool.as_stacked_bytes(stacked_floor_item.entity_ids.len()) - }, - FloorItemDetail::Meseta(meseta_floor_item) => { - meseta_floor_item.as_bytes() - } - } - } -} #[derive(thiserror::Error, Debug)] @@ -285,12 +216,6 @@ pub enum AddItemResult { Meseta, } -#[derive(Debug, Clone, Default)] -pub struct LocalFloor(Vec); -#[derive(Debug, Clone, Default)] -pub struct SharedFloor(Vec); - - #[derive(Clone, Debug)] pub struct Bank(Vec); @@ -539,57 +464,6 @@ impl std::cmp::Ord for BankItem { } } -#[derive(Debug)] -pub struct FloorState { - character_id: CharacterEntityId, - local: LocalFloor, - shared: SharedFloor, -} - -impl FloorState { - pub fn take_item(&mut self, item_id: &ClientItemId) -> Option { - let item = self.local.0 - .drain_filter(|item| { - item.item_id == *item_id - }) - .next(); - item.or_else(|| { - self.shared.0 - .drain_filter(|item| { - item.item_id == *item_id - }) - .next() - }) - } - - pub fn add_inventory_item(&mut self, inventory_item: InventoryItem, map_area: MapArea, position: (f32, f32, f32)) -> &FloorItem { - let floor_item = FloorItem { - item_id: inventory_item.item_id, - item: match inventory_item.item { - InventoryItemDetail::Individual(individual_item) => FloorItemDetail::Individual(individual_item), - InventoryItemDetail::Stacked(stacked_item) => FloorItemDetail::Stacked(stacked_item), - }, - map_area, - x: position.0, - y: position.1, - z: position.2, - }; - - self.shared.0.push(floor_item); - &self.shared.0[self.shared.0.len()-1] - } - - pub fn add_shared_item(&mut self, floor_item: FloorItem) -> &FloorItem { - self.shared.0.push(floor_item); - &self.shared.0[self.shared.0.len()-1] - } - - pub fn add_local_item(&mut self, floor_item: FloorItem) -> &FloorItem { - self.local.0.push(floor_item); - &self.local.0[self.local.0.len()-1] - } -} - pub struct ItemState { character_inventory: HashMap, diff --git a/src/ship/packet/builder/message.rs b/src/ship/packet/builder/message.rs index 644d2a1..fa9368c 100644 --- a/src/ship/packet/builder/message.rs +++ b/src/ship/packet/builder/message.rs @@ -7,7 +7,7 @@ use crate::ship::items::ClientItemId; use crate::ship::items::inventory::InventoryItem; use crate::ship::items::state::IndividualItemDetail; use crate::ship::items::state::BankState; -use crate::ship::items::state::FloorItem; +use crate::ship::items::floor::FloorItem; use crate::ship::location::AreaClient; use std::convert::TryInto; use crate::ship::shops::ShopItem; diff --git a/src/ship/packet/handler/direct_message.rs b/src/ship/packet/handler/direct_message.rs index 643136c..9e05ce3 100644 --- a/src/ship/packet/handler/direct_message.rs +++ b/src/ship/packet/handler/direct_message.rs @@ -14,7 +14,8 @@ use crate::entity::item; use libpso::utf8_to_utf16_array; use crate::ship::packet::builder; use crate::ship::shops::{ShopItem, ToolShopItem, ArmorShopItem}; -use crate::ship::items::state::{ItemState, ItemStateError, FloorType, FloorItemDetail}; +use crate::ship::items::state::{ItemState, ItemStateError}; +use crate::ship::items::floor::{FloorType, FloorItemDetail}; use crate::ship::items::actions::{pick_up_item, withdraw_meseta, deposit_meseta, withdraw_item, deposit_item, buy_shop_item, enemy_drops_item, take_meseta, apply_modifier, TriggerCreateItem}; const BANK_ACTION_DEPOSIT: u8 = 0;