|
|
@ -1,3 +1,5 @@ |
|
|
|
// 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 std::future::Future;
|
|
|
@ -14,7 +16,7 @@ use crate::entity::item::tool::Tool; |
|
|
|
use crate::entity::item::ItemModifier;
|
|
|
|
use crate::ship::shops::ShopItem;
|
|
|
|
use crate::ship::trade::TradeItem;
|
|
|
|
use crate::ship::location::{AreaClient, RoomId};
|
|
|
|
use crate::ship::location::AreaClient;
|
|
|
|
use crate::ship::drops::{ItemDrop, ItemDropType};
|
|
|
|
|
|
|
|
pub enum TriggerCreateItem {
|
|
|
@ -107,7 +109,7 @@ fn take_item_from_inventory(character_id: CharacterEntityId, item_id: ClientItem |
|
|
|
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, amount).ok_or_else (|| ItemStateError::NoFloorItem(item_id))?;
|
|
|
|
let item = inventory.take_item(&item_id, amount).ok_or (ItemStateError::NoFloorItem(item_id))?;
|
|
|
|
|
|
|
|
transaction.gateway().set_character_inventory(&character_id, &inventory.as_inventory_entity(&character_id)).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
@ -235,7 +237,7 @@ fn add_meseta_to_shared_floor(character_id: CharacterEntityId, amount: u32, map_ |
|
|
|
let floor_item = FloorItem {
|
|
|
|
item_id: item_state.new_item_id()?,
|
|
|
|
item: FloorItemDetail::Meseta(Meseta(amount)),
|
|
|
|
map_area: map_area,
|
|
|
|
map_area,
|
|
|
|
x: drop_position.0,
|
|
|
|
y: 0.0,
|
|
|
|
z: drop_position.1,
|
|
|
@ -352,7 +354,7 @@ where |
|
|
|
{
|
|
|
|
entity_gateway.with_transaction(|transaction| async move {
|
|
|
|
let item_state_proxy = ItemStateProxy::new(item_state);
|
|
|
|
let ((item_state_proxy, transaction), result) = ItemStateAction::default()
|
|
|
|
let ((item_state_proxy, transaction), _) = ItemStateAction::default()
|
|
|
|
.act(take_meseta_from_inventory(character.id, amount))
|
|
|
|
.act(add_meseta_to_bank(character.id, amount))
|
|
|
|
.commit((item_state_proxy, transaction))
|
|
|
@ -369,7 +371,7 @@ fn take_item_from_bank(character_id: CharacterEntityId, item_id: ClientItemId, a |
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut bank = item_state.bank(&character_id)?;
|
|
|
|
let item = bank.take_item(&item_id, amount).ok_or_else(|| ItemStateError::NoBankItem(item_id))?;
|
|
|
|
let item = bank.take_item(&item_id, amount).ok_or(ItemStateError::NoBankItem(item_id))?;
|
|
|
|
transaction.gateway().set_character_bank(&character_id, &bank.as_bank_entity(), &bank.name).await?;
|
|
|
|
item_state.set_bank(bank);
|
|
|
|
|
|
|
@ -440,7 +442,7 @@ where |
|
|
|
.act(take_item_from_bank(character.id, *item_id, amount))
|
|
|
|
//.act(bank_item_to_inventory_item)
|
|
|
|
//.act(add_item_to_inventory)
|
|
|
|
.act(add_bank_item_to_inventory(&character))
|
|
|
|
.act(add_bank_item_to_inventory(character))
|
|
|
|
.commit((item_state_proxy, transaction))
|
|
|
|
.await?;
|
|
|
|
item_state_proxy.commit();
|
|
|
@ -664,10 +666,10 @@ fn feed_mag_item(character: CharacterEntity, mag_item_id: ClientItemId) |
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character.id)?;
|
|
|
|
let mag_entity = inventory.get_by_client_id_mut(&mag_item_id)
|
|
|
|
.ok_or_else(|| ItemStateError::InvalidItemId(mag_item_id))?
|
|
|
|
.ok_or(ItemStateError::InvalidItemId(mag_item_id))?
|
|
|
|
.item
|
|
|
|
.as_individual_mut()
|
|
|
|
.ok_or_else(|| ItemStateError::NotAMag(mag_item_id))?;
|
|
|
|
.ok_or(ItemStateError::NotAMag(mag_item_id))?;
|
|
|
|
let mag_entity_id = mag_entity.entity_id;
|
|
|
|
|
|
|
|
let mut transaction = tool.with_entity_id(transaction, |mut transaction, entity_id| {
|
|
|
@ -683,13 +685,13 @@ fn feed_mag_item(character: CharacterEntity, mag_item_id: ClientItemId) |
|
|
|
let food_tool = tool
|
|
|
|
.item
|
|
|
|
.stacked()
|
|
|
|
.ok_or_else(|| ItemStateError::NotMagFood(tool.item_id))?
|
|
|
|
.ok_or(ItemStateError::NotMagFood(tool.item_id))?
|
|
|
|
.tool
|
|
|
|
.tool;
|
|
|
|
|
|
|
|
let mag_entity = mag_entity
|
|
|
|
.as_mag_mut()
|
|
|
|
.ok_or_else(|| ItemStateError::NotAMag(mag_item_id))?;
|
|
|
|
.ok_or(ItemStateError::NotAMag(mag_item_id))?;
|
|
|
|
|
|
|
|
mag_entity.feed(food_tool);
|
|
|
|
|
|
|
@ -745,7 +747,7 @@ fn add_bought_item_to_inventory<'a>(character_id: CharacterEntityId, |
|
|
|
item: ItemDetail::Tool(tool),
|
|
|
|
}).await?;
|
|
|
|
transaction.gateway().add_item_note(&item_entity.id, ItemNote::BoughtAtShop {
|
|
|
|
character_id: character_id,
|
|
|
|
character_id,
|
|
|
|
}).await?;
|
|
|
|
item_entities.push(item_entity);
|
|
|
|
}
|
|
|
@ -754,7 +756,7 @@ fn add_bought_item_to_inventory<'a>(character_id: CharacterEntityId, |
|
|
|
item_id,
|
|
|
|
item: InventoryItemDetail::Stacked(StackedItemDetail {
|
|
|
|
entity_ids: item_entities.into_iter().map(|i| i.id).collect(),
|
|
|
|
tool: tool,
|
|
|
|
tool,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
inventory.add_item(inventory_item)?.1
|
|
|
@ -764,7 +766,7 @@ fn add_bought_item_to_inventory<'a>(character_id: CharacterEntityId, |
|
|
|
item: item_detail.clone(),
|
|
|
|
}).await?;
|
|
|
|
transaction.gateway().add_item_note(&item_entity.id, ItemNote::BoughtAtShop {
|
|
|
|
character_id: character_id,
|
|
|
|
character_id,
|
|
|
|
}).await?;
|
|
|
|
|
|
|
|
let inventory_item = InventoryItem {
|
|
|
@ -816,7 +818,7 @@ fn sell_inventory_item<'a>(character_id: CharacterEntityId) |
|
|
|
-> impl Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), inventory_item| {
|
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
|
|
|
let price = inventory_item.item.sell_price()?;
|
|
|
@ -899,7 +901,7 @@ where |
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), O), ItemStateError>> + Send + 'a>> + Send + Sync,
|
|
|
|
T: Clone + Send + Sync,
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), arg| {
|
|
|
|
move |(item_state, transaction), arg| {
|
|
|
|
let input = input.clone();
|
|
|
|
let func = func.clone();
|
|
|
|
println!("i {:?} {:?}", input, arg);
|
|
|
@ -958,18 +960,6 @@ where |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn clear<'a, T: Send + Clone + 'a>()
|
|
|
|
-> impl Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), T)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |state, _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
Ok((state, ()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert<'a, T: Send + Clone + 'a>(element: T)
|
|
|
|
-> impl Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), T), ItemStateError>> + Send + 'a>>
|
|
|
@ -986,26 +976,10 @@ fn add_item_to_inventory(character: CharacterEntity) |
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem), ItemStateError>> + Send + 'a>> + Clone
|
|
|
|
{
|
|
|
|
let character = character.clone();
|
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
|
|
|
let character = character.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
//let bank_name = item_state.bank(&character.id)?.name;
|
|
|
|
let mut inventory = item_state.inventory(&character.id)?;
|
|
|
|
|
|
|
|
let character_id = character.id;
|
|
|
|
/*
|
|
|
|
let transaction = bank_item.with_entity_id(transaction, |mut transaction, entity_id| {
|
|
|
|
let bank_name = bank_name.clone();
|
|
|
|
async move {
|
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::Withdraw {
|
|
|
|
character_id,
|
|
|
|
bank: bank_name,
|
|
|
|
}).await?;
|
|
|
|
Ok(transaction)
|
|
|
|
}}).await?;
|
|
|
|
*/
|
|
|
|
|
|
|
|
let mut transaction = inventory_item.with_mag(transaction, |mut transaction, entity_id, _mag| {
|
|
|
|
let character = character.clone();
|
|
|
|
async move {
|
|
|
@ -1060,7 +1034,6 @@ fn assign_new_item_id() |
|
|
|
pub async fn trade_items<'a, EG> (
|
|
|
|
item_state: &'a mut ItemState,
|
|
|
|
entity_gateway: &mut EG,
|
|
|
|
room_id: RoomId,
|
|
|
|
p1: (&AreaClient, &CharacterEntity, &Vec<TradeItem>, Meseta),
|
|
|
|
p2: (&AreaClient, &CharacterEntity, &Vec<TradeItem>, Meseta))
|
|
|
|
-> Result<(Vec<InventoryItem>, Vec<InventoryItem>), ItemStateError>
|
|
|
@ -1137,9 +1110,9 @@ pub async fn take_meseta<'a, EG> ( |
|
|
|
where
|
|
|
|
EG: EntityGateway,
|
|
|
|
{
|
|
|
|
entity_gateway.with_transaction(|mut transaction| async move {
|
|
|
|
entity_gateway.with_transaction(|transaction| async move {
|
|
|
|
let item_state_proxy = ItemStateProxy::new(item_state);
|
|
|
|
let ((item_state_proxy, transaction), p1_removed_items) = ItemStateAction::default()
|
|
|
|
let ((item_state_proxy, transaction), _) = ItemStateAction::default()
|
|
|
|
.act(take_meseta_from_inventory(*character_id, meseta.0))
|
|
|
|
.commit((item_state_proxy, transaction))
|
|
|
|
.await?;
|
|
|
@ -1270,7 +1243,7 @@ pub async fn enemy_drops_item<'a, EG> ( |
|
|
|
where
|
|
|
|
EG: EntityGateway,
|
|
|
|
{
|
|
|
|
entity_gateway.with_transaction(|mut transaction| async move {
|
|
|
|
entity_gateway.with_transaction(|transaction| async move {
|
|
|
|
let item_state_proxy = ItemStateProxy::new(item_state);
|
|
|
|
let ((item_state_proxy, transaction), floor_item) = ItemStateAction::default()
|
|
|
|
.act(convert_item_drop_to_floor_item(character_id, item_drop))
|
|
|
@ -1283,17 +1256,17 @@ where |
|
|
|
}).await
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_modifier_to_inventory_item(character_id: CharacterEntityId, modifier: ItemModifier)
|
|
|
|
fn apply_modifier_to_inventory_item(modifier: ItemModifier)
|
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(item_state, mut transaction), mut inventory_item| {
|
|
|
|
let modifier = modifier.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
match (&inventory_item.item, modifier) {
|
|
|
|
(InventoryItemDetail::Individual(IndividualItemDetail{entity_id, item: ItemDetail::Weapon(mut weapon), ..}), ItemModifier::WeaponModifier(modifier)) => {
|
|
|
|
match (&mut inventory_item.item, modifier) {
|
|
|
|
(InventoryItemDetail::Individual(IndividualItemDetail{entity_id, item: ItemDetail::Weapon(ref mut weapon), ..}), ItemModifier::WeaponModifier(modifier)) => {
|
|
|
|
weapon.apply_modifier(&modifier);
|
|
|
|
transaction.gateway().add_weapon_modifier(&entity_id, modifier).await?;
|
|
|
|
transaction.gateway().add_weapon_modifier(entity_id, modifier).await?;
|
|
|
|
},
|
|
|
|
_ => return Err(ItemStateError::InvalidModifier)
|
|
|
|
}
|
|
|
@ -1334,7 +1307,7 @@ where |
|
|
|
let item_state_proxy = ItemStateProxy::new(item_state);
|
|
|
|
let ((item_state_proxy, transaction), item) = ItemStateAction::default()
|
|
|
|
.act(take_item_from_inventory(character.id, item_id, 1))
|
|
|
|
.act(apply_modifier_to_inventory_item(character.id, modifier))
|
|
|
|
.act(apply_modifier_to_inventory_item(modifier))
|
|
|
|
.act(add_item_to_inventory(character.clone()))
|
|
|
|
.act(as_individual_item())
|
|
|
|
.commit((item_state_proxy, transaction))
|
|
|
|