|
@ -3,9 +3,10 @@ use crate::entity::item::ItemNote; |
|
|
use std::future::Future;
|
|
|
use std::future::Future;
|
|
|
use std::pin::Pin;
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::ship::map::MapArea;
|
|
|
use crate::entity::character::{CharacterEntity, CharacterEntityId};
|
|
|
use crate::entity::character::{CharacterEntity, CharacterEntityId};
|
|
|
use crate::entity::gateway::EntityGateway;
|
|
|
|
|
|
use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateAction, ItemAction, ItemStateError, FloorItem, AddItemResult};
|
|
|
|
|
|
|
|
|
use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction};
|
|
|
|
|
|
use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateAction, ItemAction, ItemStateError, FloorItem, InventoryItem, AddItemResult};
|
|
|
|
|
|
|
|
|
pub enum TriggerCreateItem {ItemAction,
|
|
|
pub enum TriggerCreateItem {ItemAction,
|
|
|
Yes,
|
|
|
Yes,
|
|
@ -21,7 +22,7 @@ fn take_item_from_floor(character_id: CharacterEntityId, item_id: ClientItemId) |
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
let item = floor.take_item(&item_id).ok_or(ItemStateError::NoFloorItem(item_id))?;
|
|
|
let item = floor.take_item(&item_id).ok_or(ItemStateError::NoFloorItem(item_id))?;
|
|
|
item_state.set_floor(floor);
|
|
|
item_state.set_floor(floor);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), item))
|
|
|
Ok(((item_state, transaction), item))
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
}
|
|
@ -71,6 +72,54 @@ fn add_floor_item_to_inventory(character: &CharacterEntity) |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn take_item_from_inventory(character_id: CharacterEntityId, item_id: ClientItemId)
|
|
|
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem), ItemStateError>> + Send + 'a>>
|
|
|
|
|
|
{
|
|
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
|
|
|
|
|
let item = inventory.take_item(&item_id).ok_or_else (|| ItemStateError::NoFloorItem(item_id))?;
|
|
|
|
|
|
|
|
|
|
|
|
transaction.gateway().set_character_inventory(&character_id, &inventory.as_inventory_entity(&character_id)).await?;
|
|
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), item))
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn add_inventory_item_to_shared_floor(character_id: CharacterEntityId, item_id: ClientItemId, map_area: MapArea, drop_position: (f32, f32, f32))
|
|
|
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
|
|
|
{
|
|
|
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
|
let transaction = inventory_item.with_entity_id(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id| {
|
|
|
|
|
|
async move {
|
|
|
|
|
|
if let Ok(transaction) = &mut transaction {
|
|
|
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::PlayerDrop {
|
|
|
|
|
|
character_id,
|
|
|
|
|
|
map_area,
|
|
|
|
|
|
x: drop_position.0,
|
|
|
|
|
|
y: drop_position.1,
|
|
|
|
|
|
z: drop_position.2,
|
|
|
|
|
|
}).await?;
|
|
|
|
|
|
}
|
|
|
|
|
|
transaction
|
|
|
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
|
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
|
|
|
floor.add_inventory_item(inventory_item, map_area, drop_position);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn pick_up_item<EG>(
|
|
|
pub async fn pick_up_item<EG>(
|
|
|
item_state: &mut ItemState,
|
|
|
item_state: &mut ItemState,
|
|
|
entity_gateway: &mut EG,
|
|
|
entity_gateway: &mut EG,
|
|
@ -96,23 +145,21 @@ pub async fn drop_item<EG>( |
|
|
item_state: &mut ItemState,
|
|
|
item_state: &mut ItemState,
|
|
|
entity_gateway: &mut EG,
|
|
|
entity_gateway: &mut EG,
|
|
|
character: &CharacterEntity,
|
|
|
character: &CharacterEntity,
|
|
|
item_id: &ClientItemId)
|
|
|
|
|
|
|
|
|
item_id: &ClientItemId,
|
|
|
|
|
|
map_area: MapArea,
|
|
|
|
|
|
drop_position: (f32, f32, f32))
|
|
|
-> Result<(), ItemStateError>
|
|
|
-> Result<(), ItemStateError>
|
|
|
where
|
|
|
where
|
|
|
EG: EntityGateway,
|
|
|
EG: EntityGateway,
|
|
|
{
|
|
|
{
|
|
|
/*
|
|
|
|
|
|
let result: Result<TriggerCreateItem, ItemStateError> = entity_gateway.with_transaction(|transaction| async move {
|
|
|
|
|
|
|
|
|
entity_gateway.with_transaction(|transaction| async move {
|
|
|
let item_state_proxy = ItemStateProxy::new(item_state);
|
|
|
let item_state_proxy = ItemStateProxy::new(item_state);
|
|
|
let ((item_state_proxy, transaction), result) = ItemStateAction::default()
|
|
|
let ((item_state_proxy, transaction), result) = ItemStateAction::default()
|
|
|
.act(take_item_from_inventory(character.id, *item_id))
|
|
|
.act(take_item_from_inventory(character.id, *item_id))
|
|
|
.act(add_inventory_item_to_shared_floor(&character))
|
|
|
|
|
|
|
|
|
.act(add_inventory_item_to_shared_floor(character.id, *item_id, map_area, drop_position))
|
|
|
.commit((item_state_proxy, transaction))
|
|
|
.commit((item_state_proxy, transaction))
|
|
|
.await?;
|
|
|
.await?;
|
|
|
item_state_proxy.commit();
|
|
|
item_state_proxy.commit();
|
|
|
Ok((transaction, result))
|
|
|
Ok((transaction, result))
|
|
|
}).await;
|
|
|
|
|
|
Ok(result?)
|
|
|
|
|
|
*/
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
|
}).await
|
|
|
}
|
|
|
}
|