Browse Source

move floor code out of state

kill_counters
jake 2 years ago
committed by andy
parent
commit
317f236f7c
  1. 4
      src/ship/items/actions.rs
  2. 139
      src/ship/items/floor.rs
  3. 2
      src/ship/items/inventory.rs
  4. 1
      src/ship/items/mod.rs
  5. 130
      src/ship/items/state.rs
  6. 2
      src/ship/packet/builder/message.rs
  7. 3
      src/ship/packet/handler/direct_message.rs

4
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;

139
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<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where
F: FnMut(T, ItemEntityId) -> Fut,
Fut: Future<Output=Result<T, ItemStateError>>,
{
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<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where
F: FnMut(T, ItemEntityId, Mag) -> Fut,
Fut: Future<Output=Result<T, ItemStateError>>,
{
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<FloorItem>);
#[derive(Debug, Clone, Default)]
pub struct SharedFloor(pub Vec<FloorItem>);
#[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<FloorItem> {
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]
}
}

2
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 {

1
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);

130
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<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where
F: FnMut(T, ItemEntityId) -> Fut,
Fut: Future<Output=Result<T, ItemStateError>>,
{
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<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where
F: FnMut(T, ItemEntityId, Mag) -> Fut,
Fut: Future<Output=Result<T, ItemStateError>>,
{
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<FloorItem>);
#[derive(Debug, Clone, Default)]
pub struct SharedFloor(Vec<FloorItem>);
#[derive(Clone, Debug)]
pub struct Bank(Vec<BankItem>);
@ -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<FloorItem> {
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<CharacterEntityId, InventoryState>,

2
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;

3
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;

Loading…
Cancel
Save