|
|
@ -1,10 +1,14 @@ |
|
|
|
// TODO: replace various u32s and usizes denoting item amounts for ItemAmount(u32) for consistency
|
|
|
|
use crate::ship::items::ClientItemId;
|
|
|
|
use crate::entity::item::{Meseta, ItemNote};
|
|
|
|
use async_std::sync::Arc;
|
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::iter::IntoIterator;
|
|
|
|
|
|
|
|
use libpso::packet::{ship::Message, messages::GameMessage};
|
|
|
|
use crate::ship::map::MapArea;
|
|
|
|
use crate::ship::ship::SendShipPacket;
|
|
|
|
use crate::entity::character::{CharacterEntity, CharacterEntityId};
|
|
|
|
use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction};
|
|
|
|
use crate::ship::items::state::{ItemStateProxy, ItemStateError, AddItemResult, StackedItemDetail, IndividualItemDetail};
|
|
|
@ -17,6 +21,8 @@ use crate::entity::item::tool::Tool; |
|
|
|
use crate::entity::item::ItemModifier;
|
|
|
|
use crate::ship::shops::ShopItem;
|
|
|
|
use crate::ship::drops::{ItemDrop, ItemDropType};
|
|
|
|
use crate::ship::packet::builder;
|
|
|
|
use crate::ship::location::AreaClient;
|
|
|
|
|
|
|
|
type BoxFuture<T> = Pin<Box<dyn Future<Output=T> + Send>>;
|
|
|
|
|
|
|
@ -462,13 +468,14 @@ where |
|
|
|
|
|
|
|
|
|
|
|
pub(super) fn use_consumed_item<EG, TR>(
|
|
|
|
character: CharacterEntity,
|
|
|
|
character: &CharacterEntity,
|
|
|
|
) -> impl Fn((ItemStateProxy, TR), InventoryItem)
|
|
|
|
-> BoxFuture<Result<((ItemStateProxy, TR), Vec<ApplyItemAction>), ItemStateError>>
|
|
|
|
where
|
|
|
|
EG: EntityGateway + Clone + 'static,
|
|
|
|
TR: EntityGatewayTransaction<ParentGateway = EG> + 'static,
|
|
|
|
{
|
|
|
|
let character = character.clone();
|
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
|
|
|
let mut character = character.clone();
|
|
|
|
Box::pin(async move {
|
|
|
@ -690,22 +697,22 @@ where |
|
|
|
|
|
|
|
|
|
|
|
#[async_recursion::async_recursion]
|
|
|
|
async fn foreach_inner<'a, EG, TR, O, T, F>(
|
|
|
|
async fn foreach_inner<'a, EG, TR, O, T, F, I>(
|
|
|
|
state: (ItemStateProxy, TR),
|
|
|
|
mut input: Vec<T>,
|
|
|
|
func: F,
|
|
|
|
mut input: I,
|
|
|
|
func: Arc<F>,
|
|
|
|
) -> Result<((ItemStateProxy, TR), Vec<O>), ItemStateError>
|
|
|
|
where
|
|
|
|
'a: 'async_recursion,
|
|
|
|
EG: EntityGateway,
|
|
|
|
TR: EntityGatewayTransaction<ParentGateway = EG> + 'static,
|
|
|
|
O: Send,
|
|
|
|
T: Clone + Send,
|
|
|
|
T: Send,
|
|
|
|
F: Fn((ItemStateProxy, TR), T)
|
|
|
|
-> BoxFuture<Result<((ItemStateProxy, TR), O), ItemStateError>> + Send + Sync,
|
|
|
|
F: Clone,
|
|
|
|
I: Iterator<Item = T> + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
let item = match input.pop() {
|
|
|
|
let item = match input.next() {
|
|
|
|
Some(item) => item,
|
|
|
|
None => return Ok((state, Vec::new()))
|
|
|
|
};
|
|
|
@ -718,9 +725,9 @@ where |
|
|
|
Ok((state, output))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn foreach<EG, TR, O, T, F>(
|
|
|
|
pub(super) fn foreach<EG, TR, O, T, F, I>(
|
|
|
|
func: F
|
|
|
|
) -> impl Fn((ItemStateProxy, TR), Vec<T>)
|
|
|
|
) -> impl Fn((ItemStateProxy, TR), I)
|
|
|
|
-> BoxFuture<Result<((ItemStateProxy, TR), Vec<O>), ItemStateError>>
|
|
|
|
where
|
|
|
|
EG: EntityGateway,
|
|
|
@ -729,11 +736,14 @@ where |
|
|
|
T: Send + Clone + 'static + std::fmt::Debug,
|
|
|
|
F: Fn((ItemStateProxy, TR), T)
|
|
|
|
-> BoxFuture<Result<((ItemStateProxy, TR), O), ItemStateError>> + Send + Sync + 'static,
|
|
|
|
F: Clone,
|
|
|
|
T: Clone + Send + Sync,
|
|
|
|
T: Send + Sync,
|
|
|
|
I: IntoIterator<Item = T> + Send + Sync + 'static,
|
|
|
|
I::IntoIter: Send + Sync,
|
|
|
|
{
|
|
|
|
let func = Arc::new(func);
|
|
|
|
move |(item_state, transaction), items| {
|
|
|
|
let func = func.clone();
|
|
|
|
let items = items.into_iter();
|
|
|
|
Box::pin(async move {
|
|
|
|
let (state, result) = foreach_inner((item_state, transaction), items, func).await?;
|
|
|
|
Ok((state, result))
|
|
|
@ -758,6 +768,35 @@ where |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn fork<EG, TR, F1, F2, T, O1, O2>(
|
|
|
|
func1: F1,
|
|
|
|
func2: F2,
|
|
|
|
) -> impl Fn((ItemStateProxy, TR), T)
|
|
|
|
-> BoxFuture<Result<((ItemStateProxy, TR), (O1, O2)), ItemStateError>>
|
|
|
|
where
|
|
|
|
EG: EntityGateway,
|
|
|
|
TR: EntityGatewayTransaction<ParentGateway = EG> + 'static,
|
|
|
|
F1: Fn((ItemStateProxy, TR), T) -> BoxFuture<Result<((ItemStateProxy, TR), O1), ItemStateError>> + Send + Sync + 'static,
|
|
|
|
F2: Fn((ItemStateProxy, TR), T) -> BoxFuture<Result<((ItemStateProxy, TR), O2), ItemStateError>> + Send + Sync + 'static,
|
|
|
|
T: Send + Sync + Clone + 'static,
|
|
|
|
O1: Send,
|
|
|
|
O2: Send,
|
|
|
|
{
|
|
|
|
let func1 = Arc::new(func1);
|
|
|
|
let func2 = Arc::new(func2);
|
|
|
|
move |(item_state, transaction), input| {
|
|
|
|
let input = input.clone();
|
|
|
|
let func1 = func1.clone();
|
|
|
|
let func2 = func2.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
let ((item_state, transaction), result1) = func1((item_state, transaction), input.clone()).await?;
|
|
|
|
let ((item_state, transaction), result2) = func2((item_state, transaction), input).await?;
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), (result1, result2)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn add_item_to_inventory<EG, TR>(
|
|
|
|
character: CharacterEntity,
|
|
|
|
) -> impl Fn((ItemStateProxy, TR), InventoryItem)
|
|
|
@ -994,3 +1033,86 @@ where |
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub(super) fn apply_item_action_packets<EG, TR>(
|
|
|
|
character_id: CharacterEntityId,
|
|
|
|
area_client: AreaClient,
|
|
|
|
) -> impl Fn((ItemStateProxy, TR), ApplyItemAction)
|
|
|
|
-> BoxFuture<Result<((ItemStateProxy, TR), Vec<SendShipPacket>), ItemStateError>>
|
|
|
|
where
|
|
|
|
EG: EntityGateway,
|
|
|
|
TR: EntityGatewayTransaction<ParentGateway = EG> + 'static,
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), apply_item_action| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let pkts = if let ApplyItemAction::CreateItem(item_detail) = apply_item_action {
|
|
|
|
let new_item = transaction.gateway().create_item(NewItemEntity {
|
|
|
|
item: item_detail.clone(),
|
|
|
|
}).await?;
|
|
|
|
|
|
|
|
let item_id = item_state.new_item_id().await?;
|
|
|
|
|
|
|
|
let (inventory_item_detail, create_item) = if item_detail.is_stackable() {
|
|
|
|
let tool = item_detail.as_tool().ok_or_else(|| ItemStateError::NotATool(ClientItemId(0xFFFFFFFF)))?;
|
|
|
|
|
|
|
|
let create_item = builder::message::create_stacked_item(area_client, item_id, &tool, 1).map_err(|_err| ItemStateError::Dummy)?;
|
|
|
|
let item_detail = StackedItemDetail {
|
|
|
|
entity_ids: vec![new_item.id],
|
|
|
|
tool
|
|
|
|
};
|
|
|
|
(InventoryItemDetail::Stacked(item_detail), create_item)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let item_detail = IndividualItemDetail {
|
|
|
|
entity_id: new_item.id,
|
|
|
|
item: item_detail,
|
|
|
|
};
|
|
|
|
let create_item = builder::message::create_individual_item(area_client, item_id, &item_detail).map_err(|_err| ItemStateError::Dummy)?;
|
|
|
|
(InventoryItemDetail::Individual(item_detail), create_item)
|
|
|
|
};
|
|
|
|
|
|
|
|
let inventory_item = InventoryItem {
|
|
|
|
item_id,
|
|
|
|
item: inventory_item_detail,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut inventory = item_state.inventory(&character_id).await?;
|
|
|
|
inventory.add_item(inventory_item)?;
|
|
|
|
transaction.gateway().set_character_inventory(&character_id, &inventory.as_inventory_entity(&character_id)).await?;
|
|
|
|
item_state.set_inventory(inventory).await;
|
|
|
|
|
|
|
|
vec![SendShipPacket::Message(Message::new(GameMessage::CreateItem(create_item)))]
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), pkts))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn apply_item_action_character<EG, TR>(
|
|
|
|
character: &CharacterEntity
|
|
|
|
) -> impl Fn((ItemStateProxy, TR), Vec<ApplyItemAction>)
|
|
|
|
-> BoxFuture<Result<((ItemStateProxy, TR), CharacterEntity), ItemStateError>>
|
|
|
|
where
|
|
|
|
EG: EntityGateway,
|
|
|
|
TR: EntityGatewayTransaction<ParentGateway = EG> + 'static,
|
|
|
|
{
|
|
|
|
let character = character.clone();
|
|
|
|
move |(item_state, transaction), apply_item_actions| {
|
|
|
|
let mut character = character.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
for action in apply_item_actions {
|
|
|
|
match action {
|
|
|
|
ApplyItemAction::UpdateCharacter(new_character) => character = *new_character,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), character))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|