|
@ -1,12 +1,12 @@ |
|
|
use crate::ship::items::ClientItemId;
|
|
|
use crate::ship::items::ClientItemId;
|
|
|
use crate::entity::item::ItemNote;
|
|
|
|
|
|
|
|
|
use crate::entity::item::{Meseta, ItemNote};
|
|
|
use std::future::Future;
|
|
|
use std::future::Future;
|
|
|
use std::pin::Pin;
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
|
use crate::ship::map::MapArea;
|
|
|
use crate::ship::map::MapArea;
|
|
|
use crate::entity::character::{CharacterEntity, CharacterEntityId};
|
|
|
use crate::entity::character::{CharacterEntity, CharacterEntityId};
|
|
|
use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction};
|
|
|
use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction};
|
|
|
use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateAction, ItemAction, ItemStateError, FloorItem, InventoryItem, AddItemResult};
|
|
|
|
|
|
|
|
|
use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateAction, ItemAction, ItemStateError, FloorItem, InventoryItem, AddItemResult, FloorItemDetail, StackedItemDetail};
|
|
|
|
|
|
|
|
|
pub enum TriggerCreateItem {ItemAction,
|
|
|
pub enum TriggerCreateItem {ItemAction,
|
|
|
Yes,
|
|
|
Yes,
|
|
@ -112,7 +112,7 @@ fn take_item_from_inventory(character_id: CharacterEntityId, item_id: ClientItem |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn add_inventory_item_to_shared_floor(character_id: CharacterEntityId, item_id: ClientItemId, map_area: MapArea, drop_position: (f32, f32, f32))
|
|
|
|
|
|
|
|
|
fn add_inventory_item_to_shared_floor(character_id: CharacterEntityId, map_area: MapArea, drop_position: (f32, f32, f32))
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
{
|
|
|
{
|
|
@ -134,6 +134,7 @@ fn add_inventory_item_to_shared_floor(character_id: CharacterEntityId, item_id: |
|
|
|
|
|
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
floor.add_inventory_item(inventory_item, map_area, drop_position);
|
|
|
floor.add_inventory_item(inventory_item, map_area, drop_position);
|
|
|
|
|
|
item_state.set_floor(floor);
|
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
})
|
|
|
})
|
|
@ -155,7 +156,151 @@ where |
|
|
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.id, *item_id, map_area, drop_position))
|
|
|
|
|
|
|
|
|
.act(add_inventory_item_to_shared_floor(character.id, map_area, drop_position))
|
|
|
|
|
|
.commit((item_state_proxy, transaction))
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
item_state_proxy.commit();
|
|
|
|
|
|
Ok((transaction, result))
|
|
|
|
|
|
}).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn take_partial_item_from_inventory(character_id: CharacterEntityId, item_id: ClientItemId, amount: u32)
|
|
|
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), StackedItemDetail), 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_partial_item(&item_id, amount).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_partial_inventory_item_to_shared_floor(character_id: CharacterEntityId, map_area: MapArea, drop_position: (f32, f32))
|
|
|
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), StackedItemDetail)
|
|
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem), ItemStateError>> + Send + 'a>>
|
|
|
|
|
|
{
|
|
|
|
|
|
move |(mut item_state, transaction), stacked_item| {
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
|
let floor_item = FloorItem {
|
|
|
|
|
|
item_id: item_state.new_item_id()?,
|
|
|
|
|
|
item: FloorItemDetail::Stacked(stacked_item),
|
|
|
|
|
|
map_area,
|
|
|
|
|
|
x: drop_position.0,
|
|
|
|
|
|
y: 0.0,
|
|
|
|
|
|
z: drop_position.1,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let transaction = floor_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: 0.0,
|
|
|
|
|
|
z: drop_position.1,
|
|
|
|
|
|
}).await?;
|
|
|
|
|
|
}
|
|
|
|
|
|
transaction
|
|
|
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
|
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
|
|
|
let floor_item = floor.add_item(floor_item).clone();
|
|
|
|
|
|
item_state.set_floor(floor);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), floor_item))
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn drop_partial_item<'a, EG>(
|
|
|
|
|
|
item_state: &'a mut ItemState,
|
|
|
|
|
|
entity_gateway: &mut EG,
|
|
|
|
|
|
character: &CharacterEntity,
|
|
|
|
|
|
item_id: &ClientItemId,
|
|
|
|
|
|
map_area: MapArea,
|
|
|
|
|
|
drop_position: (f32, f32),
|
|
|
|
|
|
amount: u32)
|
|
|
|
|
|
-> Result<FloorItem, ItemStateError>
|
|
|
|
|
|
where
|
|
|
|
|
|
EG: EntityGateway,
|
|
|
|
|
|
{
|
|
|
|
|
|
entity_gateway.with_transaction(|transaction| async move {
|
|
|
|
|
|
let item_state_proxy = ItemStateProxy::new(item_state);
|
|
|
|
|
|
let ((item_state_proxy, transaction), result) = ItemStateAction::default()
|
|
|
|
|
|
.act(take_partial_item_from_inventory(character.id, *item_id, amount))
|
|
|
|
|
|
.act(add_partial_inventory_item_to_shared_floor(character.id, map_area, drop_position))
|
|
|
|
|
|
.commit((item_state_proxy, transaction))
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
item_state_proxy.commit();
|
|
|
|
|
|
Ok((transaction, result))
|
|
|
|
|
|
}).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn take_meseta_from_inventory(character_id: CharacterEntityId, amount: u32)
|
|
|
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), u32), ItemStateError>> + Send + 'a>>
|
|
|
|
|
|
{
|
|
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
|
|
|
|
|
inventory.remove_meseta(amount)?;
|
|
|
|
|
|
transaction.gateway().set_character_meseta(&character_id, inventory.meseta).await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), amount))
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn add_meseta_to_shared_floor(character_id: CharacterEntityId, map_area: MapArea, drop_position: (f32, f32))
|
|
|
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), u32)
|
|
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem), ItemStateError>> + Send + 'a>>
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
move |(mut item_state, transaction), amount| {
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
|
let floor_item = FloorItem {
|
|
|
|
|
|
item_id: item_state.new_item_id()?,
|
|
|
|
|
|
item: FloorItemDetail::Meseta(Meseta(amount)),
|
|
|
|
|
|
map_area: map_area,
|
|
|
|
|
|
x: drop_position.0,
|
|
|
|
|
|
y: 0.0,
|
|
|
|
|
|
z: drop_position.1,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
|
|
|
let floor_item = floor.add_item(floor_item).clone();
|
|
|
|
|
|
item_state.set_floor(floor);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), floor_item))
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn drop_meseta<'a, EG>(
|
|
|
|
|
|
item_state: &'a mut ItemState,
|
|
|
|
|
|
entity_gateway: &mut EG,
|
|
|
|
|
|
character: &CharacterEntity,
|
|
|
|
|
|
map_area: MapArea,
|
|
|
|
|
|
drop_position: (f32, f32),
|
|
|
|
|
|
amount: u32)
|
|
|
|
|
|
-> Result<FloorItem, ItemStateError>
|
|
|
|
|
|
where
|
|
|
|
|
|
EG: EntityGateway,
|
|
|
|
|
|
{
|
|
|
|
|
|
entity_gateway.with_transaction(|transaction| async move {
|
|
|
|
|
|
let item_state_proxy = ItemStateProxy::new(item_state);
|
|
|
|
|
|
let ((item_state_proxy, transaction), result) = ItemStateAction::default()
|
|
|
|
|
|
.act(take_meseta_from_inventory(character.id, amount))
|
|
|
|
|
|
.act(add_meseta_to_shared_floor(character.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();
|
|
|