2022-07-19 12:34:07 -06:00
|
|
|
// TODO: replace various u32s and usizes denoting item amounts for ItemAmount(u32) for consistency
|
2022-04-27 20:57:47 -06:00
|
|
|
use crate::ship::items::ClientItemId;
|
2022-04-30 21:40:46 -06:00
|
|
|
use crate::entity::item::{Meseta, ItemNote};
|
2022-04-27 20:57:47 -06:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
2022-04-30 11:41:24 -06:00
|
|
|
use crate::ship::map::MapArea;
|
2022-04-27 23:53:26 -06:00
|
|
|
use crate::entity::character::{CharacterEntity, CharacterEntityId};
|
2022-07-19 21:26:00 -06:00
|
|
|
use crate::entity::gateway::EntityGatewayTransaction;
|
|
|
|
use crate::ship::items::state::{ItemStateProxy, ItemStateError, AddItemResult, StackedItemDetail, IndividualItemDetail};
|
2022-07-19 20:48:55 -06:00
|
|
|
use crate::ship::items::bank::{BankItem, BankItemDetail};
|
2022-07-19 19:39:58 -06:00
|
|
|
use crate::ship::items::inventory::{InventoryItem, InventoryItemDetail};
|
2022-07-19 20:06:43 -06:00
|
|
|
use crate::ship::items::floor::{FloorItem, FloorItemDetail};
|
2022-05-27 01:38:49 -06:00
|
|
|
use crate::ship::items::apply_item::apply_item;
|
2022-07-18 18:42:44 -06:00
|
|
|
use crate::entity::item::{ItemDetail, NewItemEntity, TradeId};
|
2022-07-18 20:51:35 -06:00
|
|
|
use crate::entity::item::tool::Tool;
|
2022-07-18 23:57:54 -06:00
|
|
|
use crate::entity::item::ItemModifier;
|
2022-06-21 00:09:52 -06:00
|
|
|
use crate::ship::shops::ShopItem;
|
2022-07-18 20:51:35 -06:00
|
|
|
use crate::ship::drops::{ItemDrop, ItemDropType};
|
2022-04-27 20:57:47 -06:00
|
|
|
|
2022-04-30 23:51:26 -06:00
|
|
|
pub enum TriggerCreateItem {
|
2022-04-27 20:57:47 -06:00
|
|
|
Yes,
|
|
|
|
No
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn take_item_from_floor(character_id: CharacterEntityId, item_id: ClientItemId)
|
2022-04-27 20:57:47 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
2022-07-18 18:42:44 -06:00
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem), ItemStateError>> + Send + 'a>>
|
2022-04-27 20:57:47 -06:00
|
|
|
{
|
2022-07-18 18:42:44 -06:00
|
|
|
move |(mut item_state, transaction): (ItemStateProxy<'_>, Box<dyn EntityGatewayTransaction + '_>) , _| {
|
2022-04-27 20:57:47 -06:00
|
|
|
Box::pin(async move {
|
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
|
let item = floor.take_item(&item_id).ok_or(ItemStateError::NoFloorItem(item_id))?;
|
|
|
|
item_state.set_floor(floor);
|
2022-04-30 11:41:24 -06:00
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
Ok(((item_state, transaction), item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_floor_item_to_inventory(character: &CharacterEntity)
|
2022-04-27 20:57:47 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem)
|
2022-04-30 18:19:11 -06:00
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), TriggerCreateItem), ItemStateError>> + Send + 'a>>
|
2022-04-27 20:57:47 -06:00
|
|
|
{
|
|
|
|
let character = character.clone();
|
|
|
|
move |(mut item_state, transaction), floor_item| {
|
|
|
|
let character = character.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character.id)?;
|
|
|
|
|
|
|
|
let character_id = character.id;
|
2022-05-30 23:47:58 -06:00
|
|
|
let transaction = floor_item.with_entity_id(transaction, |mut transaction, entity_id| {
|
2022-04-27 20:57:47 -06:00
|
|
|
async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::Pickup {
|
|
|
|
character_id
|
|
|
|
}).await?;
|
|
|
|
Ok(transaction)
|
2022-04-27 20:57:47 -06:00
|
|
|
}}).await?;
|
|
|
|
|
2022-05-30 23:47:58 -06:00
|
|
|
let mut transaction = floor_item.with_mag(transaction, |mut transaction, entity_id, _mag| {
|
2022-04-27 20:57:47 -06:00
|
|
|
let character = character.clone();
|
|
|
|
async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
transaction.gateway().change_mag_owner(&entity_id, &character).await?;
|
|
|
|
Ok(transaction)
|
2022-04-27 20:57:47 -06:00
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
let add_result = inventory.add_floor_item(floor_item)?;
|
2022-04-30 11:40:21 -06:00
|
|
|
transaction.gateway().set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
2022-05-19 22:22:54 -06:00
|
|
|
transaction.gateway().set_character_meseta(&character_id, inventory.meseta).await?;
|
2022-04-27 20:57:47 -06:00
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction),
|
|
|
|
match add_result {
|
|
|
|
AddItemResult::NewItem => TriggerCreateItem::Yes,
|
|
|
|
AddItemResult::AddToStack => TriggerCreateItem::No,
|
|
|
|
AddItemResult::Meseta => TriggerCreateItem::No,
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 11:41:24 -06:00
|
|
|
|
2022-04-30 18:18:05 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn take_item_from_inventory(character_id: CharacterEntityId, item_id: ClientItemId, amount: u32)
|
2022-04-30 11:41:24 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
2022-04-30 18:19:11 -06:00
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem), ItemStateError>> + Send + 'a>>
|
2022-04-30 11:41:24 -06:00
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
2022-07-19 12:34:07 -06:00
|
|
|
let item = inventory.take_item(&item_id, amount).ok_or (ItemStateError::NoFloorItem(item_id))?;
|
2022-04-30 11:41:24 -06:00
|
|
|
|
|
|
|
transaction.gateway().set_character_inventory(&character_id, &inventory.as_inventory_entity(&character_id)).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_inventory_item_to_shared_floor(character_id: CharacterEntityId, map_area: MapArea, drop_position: (f32, f32, f32))
|
2022-04-30 11:41:24 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
2022-05-14 13:06:40 -06:00
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem), ItemStateError>> + Send + 'a>>
|
2022-04-30 11:41:24 -06:00
|
|
|
{
|
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
|
|
|
Box::pin(async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
let transaction = inventory_item.with_entity_id(transaction, |mut transaction, entity_id| {
|
2022-04-30 11:41:24 -06:00
|
|
|
async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
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?;
|
|
|
|
Ok(transaction)
|
2022-04-30 11:41:24 -06:00
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
2022-05-14 13:06:40 -06:00
|
|
|
let floor_item = floor.add_inventory_item(inventory_item, map_area, drop_position).clone();
|
2022-04-30 21:40:46 -06:00
|
|
|
item_state.set_floor(floor);
|
2022-04-30 11:41:24 -06:00
|
|
|
|
2022-05-14 13:06:40 -06:00
|
|
|
Ok(((item_state, transaction), floor_item))
|
2022-04-30 11:41:24 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 21:40:46 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn take_meseta_from_inventory(character_id: CharacterEntityId, amount: u32)
|
2022-05-14 13:06:40 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
2022-04-30 21:40:46 -06:00
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
2022-05-14 13:06:40 -06:00
|
|
|
inventory.remove_meseta(amount)?;
|
|
|
|
transaction.gateway().set_character_meseta(&character_id, inventory.meseta).await?;
|
2022-07-18 18:42:44 -06:00
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_meseta_to_inventory(character_id: CharacterEntityId, amount: u32)
|
2022-07-18 18:42:44 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
|
|
|
inventory.add_meseta(amount)?;
|
|
|
|
transaction.gateway().set_character_meseta(&character_id, inventory.meseta).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
2022-04-30 21:40:46 -06:00
|
|
|
|
2022-05-14 13:06:40 -06:00
|
|
|
Ok(((item_state, transaction), ()))
|
2022-04-30 21:40:46 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_meseta_to_shared_floor(character_id: CharacterEntityId, amount: u32, map_area: MapArea, drop_position: (f32, f32))
|
2022-05-14 13:06:40 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem), ItemStateError>> + Send + 'a>>
|
2022-04-30 21:40:46 -06:00
|
|
|
{
|
2022-05-14 13:06:40 -06:00
|
|
|
|
|
|
|
move |(mut item_state, transaction), _| {
|
2022-04-30 21:40:46 -06:00
|
|
|
Box::pin(async move {
|
|
|
|
let floor_item = FloorItem {
|
|
|
|
item_id: item_state.new_item_id()?,
|
2022-05-14 13:06:40 -06:00
|
|
|
item: FloorItemDetail::Meseta(Meseta(amount)),
|
2022-07-19 12:34:07 -06:00
|
|
|
map_area,
|
2022-04-30 21:40:46 -06:00
|
|
|
x: drop_position.0,
|
|
|
|
y: 0.0,
|
|
|
|
z: drop_position.1,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
2022-07-18 20:51:35 -06:00
|
|
|
let floor_item = floor.add_shared_item(floor_item).clone();
|
2022-04-30 21:40:46 -06:00
|
|
|
item_state.set_floor(floor);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), floor_item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn take_meseta_from_bank(character_id: CharacterEntityId, amount: u32)
|
2022-05-14 13:06:40 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut bank = item_state.bank(&character_id)?;
|
|
|
|
bank.remove_meseta(amount)?;
|
|
|
|
transaction.gateway().set_bank_meseta(&character_id, &bank.name, bank.meseta).await?;
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_meseta_from_bank_to_inventory(character_id: CharacterEntityId, amount: u32)
|
2022-05-14 13:06:40 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
2022-04-30 21:40:46 -06:00
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
2022-05-14 13:06:40 -06:00
|
|
|
inventory.add_meseta_no_overflow(amount)?;
|
2022-04-30 21:40:46 -06:00
|
|
|
transaction.gateway().set_character_meseta(&character_id, inventory.meseta).await?;
|
|
|
|
|
2022-05-14 13:06:40 -06:00
|
|
|
Ok(((item_state, transaction), ()))
|
2022-04-30 21:40:46 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 13:06:40 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_meseta_to_bank(character_id: CharacterEntityId, amount: u32)
|
2022-05-14 13:06:40 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
2022-04-30 21:40:46 -06:00
|
|
|
Box::pin(async move {
|
2022-05-14 13:06:40 -06:00
|
|
|
let mut bank = item_state.bank(&character_id)?;
|
|
|
|
bank.add_meseta(amount)?;
|
|
|
|
transaction.gateway().set_bank_meseta(&character_id, &bank.name, bank.meseta).await?;
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn take_item_from_bank(character_id: CharacterEntityId, item_id: ClientItemId, amount: u32)
|
2022-05-14 13:06:40 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), BankItem), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut bank = item_state.bank(&character_id)?;
|
2022-07-19 12:34:07 -06:00
|
|
|
let item = bank.take_item(&item_id, amount).ok_or(ItemStateError::NoBankItem(item_id))?;
|
2022-05-14 13:06:40 -06:00
|
|
|
transaction.gateway().set_character_bank(&character_id, &bank.as_bank_entity(), &bank.name).await?;
|
|
|
|
item_state.set_bank(bank);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_bank_item_to_inventory(character: &CharacterEntity)
|
2022-05-14 13:06:40 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), BankItem)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
let character = character.clone();
|
|
|
|
move |(mut item_state, transaction), bank_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;
|
2022-05-30 23:47:58 -06:00
|
|
|
let transaction = bank_item.with_entity_id(transaction, |mut transaction, entity_id| {
|
2022-05-14 13:06:40 -06:00
|
|
|
let bank_name = bank_name.clone();
|
|
|
|
async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::Withdraw {
|
|
|
|
character_id,
|
|
|
|
bank: bank_name,
|
|
|
|
}).await?;
|
|
|
|
Ok(transaction)
|
2022-05-14 13:06:40 -06:00
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
let inventory_item = InventoryItem {
|
|
|
|
item_id: bank_item.item_id,
|
|
|
|
item: match bank_item.item {
|
|
|
|
BankItemDetail::Individual(individual_item) => InventoryItemDetail::Individual(individual_item),
|
|
|
|
BankItemDetail::Stacked(stacked_item) => InventoryItemDetail::Stacked(stacked_item),
|
|
|
|
},
|
2022-04-30 21:40:46 -06:00
|
|
|
};
|
|
|
|
|
2022-05-30 23:47:58 -06:00
|
|
|
let mut transaction = inventory_item.with_mag(transaction, |mut transaction, entity_id, _mag| {
|
2022-05-14 13:06:40 -06:00
|
|
|
let character = character.clone();
|
|
|
|
async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
transaction.gateway().change_mag_owner(&entity_id, &character).await?;
|
|
|
|
Ok(transaction)
|
2022-05-14 13:06:40 -06:00
|
|
|
}}).await?;
|
2022-04-30 21:40:46 -06:00
|
|
|
|
2022-05-14 13:06:40 -06:00
|
|
|
inventory.add_item(inventory_item.clone())?;
|
|
|
|
transaction.gateway().set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), inventory_item))
|
2022-04-30 21:40:46 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 13:06:40 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_inventory_item_to_bank(character_id: CharacterEntityId)
|
2022-05-14 13:06:40 -06:00
|
|
|
-> 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>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut bank = item_state.bank(&character_id)?;
|
|
|
|
let bank_name = bank.name.clone();
|
2022-05-30 23:47:58 -06:00
|
|
|
let mut transaction = inventory_item.with_entity_id(transaction, move |mut transaction, entity_id| {
|
2022-05-14 13:06:40 -06:00
|
|
|
let bank_name = bank_name.clone();
|
|
|
|
async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::Deposit {
|
|
|
|
character_id,
|
|
|
|
bank: bank_name,
|
|
|
|
}).await?;
|
|
|
|
Ok(transaction)
|
2022-05-14 13:06:40 -06:00
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
bank.add_inventory_item(inventory_item)?;
|
|
|
|
|
|
|
|
transaction.gateway().set_character_bank(&character_id, &bank.as_bank_entity(), &bank.name).await?;
|
|
|
|
item_state.set_bank(bank);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:34:06 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn equip_inventory_item(character_id: CharacterEntityId, item_id: ClientItemId, equip_slot: u8)
|
2022-05-15 00:34:06 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
|
|
|
inventory.equip(&item_id, equip_slot);
|
|
|
|
transaction.gateway().set_character_equips(&character_id, &inventory.as_equipped_entity()).await?;
|
2022-05-15 00:55:09 -06:00
|
|
|
item_state.set_inventory(inventory);
|
2022-05-15 00:34:06 -06:00
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn unequip_inventory_item(character_id: CharacterEntityId, item_id: ClientItemId)
|
2022-05-15 00:34:06 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
|
|
|
inventory.unequip(&item_id);
|
|
|
|
transaction.gateway().set_character_equips(&character_id, &inventory.as_equipped_entity()).await?;
|
2022-05-15 00:55:09 -06:00
|
|
|
item_state.set_inventory(inventory);
|
2022-05-15 00:34:06 -06:00
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-15 19:59:38 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn sort_inventory_items(character_id: CharacterEntityId, item_ids: Vec<ClientItemId>)
|
2022-05-15 18:59:49 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ()), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
let item_ids = item_ids.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
|
|
|
inventory.sort(&item_ids);
|
|
|
|
transaction.gateway().set_character_inventory(&character_id, &inventory.as_inventory_entity(&character_id)).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), ()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 01:38:49 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn use_consumed_item(character: CharacterEntity)
|
2022-05-27 01:38:49 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), CharacterEntity), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
2022-06-20 15:40:30 -06:00
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
2022-05-27 01:38:49 -06:00
|
|
|
let mut character = character.clone();
|
|
|
|
Box::pin(async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
let mut transaction = inventory_item.with_entity_id(transaction, |mut transaction, entity_id| {
|
2022-05-27 01:38:49 -06:00
|
|
|
async move {
|
2022-05-30 23:47:58 -06:00
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::Consumed).await?;
|
|
|
|
Ok(transaction)
|
2022-05-27 01:38:49 -06:00
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
apply_item(&mut item_state, transaction.gateway(), &mut character, inventory_item).await?;
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), character))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:40:30 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn feed_mag_item(character: CharacterEntity, mag_item_id: ClientItemId)
|
2022-06-20 15:40:30 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), CharacterEntity), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, transaction), tool| {
|
|
|
|
let character = character.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character.id)?;
|
|
|
|
let mag_entity = inventory.get_by_client_id_mut(&mag_item_id)
|
2022-07-19 12:34:07 -06:00
|
|
|
.ok_or(ItemStateError::InvalidItemId(mag_item_id))?
|
2022-06-20 15:40:30 -06:00
|
|
|
.item
|
|
|
|
.as_individual_mut()
|
2022-07-19 12:34:07 -06:00
|
|
|
.ok_or(ItemStateError::NotAMag(mag_item_id))?;
|
2022-06-20 15:40:30 -06:00
|
|
|
let mag_entity_id = mag_entity.entity_id;
|
|
|
|
|
|
|
|
let mut transaction = tool.with_entity_id(transaction, |mut transaction, entity_id| {
|
|
|
|
async move {
|
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::FedToMag {
|
|
|
|
//character_id: character.id,
|
|
|
|
mag: mag_entity_id,
|
|
|
|
}).await?;
|
|
|
|
transaction.gateway().feed_mag(&mag_entity_id, &entity_id).await?;
|
|
|
|
Ok(transaction)
|
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
let food_tool = tool
|
|
|
|
.item
|
|
|
|
.stacked()
|
2022-07-19 12:34:07 -06:00
|
|
|
.ok_or(ItemStateError::NotMagFood(tool.item_id))?
|
2022-06-20 15:40:30 -06:00
|
|
|
.tool
|
|
|
|
.tool;
|
|
|
|
|
|
|
|
let mag_entity = mag_entity
|
|
|
|
.as_mag_mut()
|
2022-07-19 12:34:07 -06:00
|
|
|
.ok_or(ItemStateError::NotAMag(mag_item_id))?;
|
2022-06-20 15:40:30 -06:00
|
|
|
|
|
|
|
mag_entity.feed(food_tool);
|
|
|
|
|
|
|
|
transaction.gateway().set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), character))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_bought_item_to_inventory<'a>(character_id: CharacterEntityId,
|
2022-06-21 00:09:52 -06:00
|
|
|
shop_item: &'a (dyn ShopItem + Send + Sync),
|
|
|
|
item_id: ClientItemId,
|
|
|
|
amount: u32)
|
|
|
|
-> impl Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, 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 bought_item = shop_item.as_item();
|
|
|
|
|
|
|
|
let inventory_item = match bought_item {
|
|
|
|
ItemDetail::Tool(tool) if tool.is_stackable() => {
|
|
|
|
let mut item_entities = Vec::new();
|
|
|
|
for _ in 0..amount {
|
|
|
|
let item_entity = transaction.gateway().create_item(NewItemEntity {
|
|
|
|
item: ItemDetail::Tool(tool),
|
|
|
|
}).await?;
|
|
|
|
transaction.gateway().add_item_note(&item_entity.id, ItemNote::BoughtAtShop {
|
2022-07-19 12:34:07 -06:00
|
|
|
character_id,
|
2022-06-21 00:09:52 -06:00
|
|
|
}).await?;
|
|
|
|
item_entities.push(item_entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
let inventory_item = InventoryItem {
|
|
|
|
item_id,
|
|
|
|
item: InventoryItemDetail::Stacked(StackedItemDetail {
|
|
|
|
entity_ids: item_entities.into_iter().map(|i| i.id).collect(),
|
2022-07-19 12:34:07 -06:00
|
|
|
tool,
|
2022-06-21 00:09:52 -06:00
|
|
|
})
|
|
|
|
};
|
|
|
|
inventory.add_item(inventory_item)?.1
|
|
|
|
},
|
|
|
|
item_detail => {
|
|
|
|
let item_entity = transaction.gateway().create_item(NewItemEntity {
|
|
|
|
item: item_detail.clone(),
|
|
|
|
}).await?;
|
|
|
|
transaction.gateway().add_item_note(&item_entity.id, ItemNote::BoughtAtShop {
|
2022-07-19 12:34:07 -06:00
|
|
|
character_id,
|
2022-06-21 00:09:52 -06:00
|
|
|
}).await?;
|
|
|
|
|
|
|
|
let inventory_item = InventoryItem {
|
|
|
|
item_id,
|
|
|
|
item: InventoryItemDetail::Individual(IndividualItemDetail {
|
|
|
|
entity_id: item_entity.id,
|
|
|
|
item: item_detail,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
inventory.add_item(inventory_item)?.1
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
transaction.gateway().set_character_inventory(&character_id, &inventory.as_inventory_entity(&character_id)).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
Ok(((item_state, transaction), inventory_item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 00:57:46 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn sell_inventory_item<'a>(character_id: CharacterEntityId)
|
2022-06-21 00:57:46 -06:00
|
|
|
-> 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>>
|
|
|
|
{
|
2022-07-19 12:34:07 -06:00
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
2022-06-21 00:57:46 -06:00
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character_id)?;
|
|
|
|
let price = inventory_item.item.sell_price()?;
|
|
|
|
inventory.add_meseta_no_overflow(price)?;
|
|
|
|
|
|
|
|
let mut transaction = inventory_item.with_entity_id(transaction, |mut transaction, entity_id| {
|
|
|
|
async move {
|
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::SoldToShop).await?;
|
|
|
|
Ok(transaction)
|
|
|
|
}}).await?;
|
|
|
|
transaction.gateway().set_character_meseta(&character_id, inventory.meseta).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
Ok(((item_state, transaction), inventory_item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 18:42:44 -06:00
|
|
|
|
|
|
|
#[async_recursion::async_recursion]
|
|
|
|
async fn iterate_inner<'a, I, O, T, F, FR>(state: (ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>),
|
|
|
|
mut input: Vec<I>,
|
|
|
|
func: F,
|
|
|
|
arg: T)
|
|
|
|
-> Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), Vec<O>), ItemStateError>
|
|
|
|
where
|
|
|
|
'a: 'async_recursion,
|
|
|
|
I: Send,
|
|
|
|
O: Send,
|
|
|
|
T: Clone + Send + Sync,
|
|
|
|
F: Fn(I) -> FR + Send + Sync + Clone + 'static,
|
|
|
|
FR: Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), T)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), O), ItemStateError>> + Send + 'a>> + Send + Sync,
|
|
|
|
{
|
|
|
|
let item = match input.pop() {
|
|
|
|
Some(item) => item,
|
|
|
|
None => return Ok((state, Vec::new()))
|
|
|
|
};
|
|
|
|
|
|
|
|
let (state, mut output) = iterate_inner(state, input, func.clone(), arg.clone()).await.unwrap();
|
|
|
|
let rfunc = func(item);
|
|
|
|
let (state, result) = rfunc(state, arg.clone()).await.unwrap();
|
|
|
|
|
|
|
|
output.push(result);
|
|
|
|
|
|
|
|
Ok((state, output))
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn iterate<'k, I, O, T, F, FR>(
|
2022-07-18 18:42:44 -06:00
|
|
|
input: Vec<I>,
|
|
|
|
func: F)
|
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), T)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), Vec<O>), ItemStateError>> + Send + 'a>>
|
|
|
|
where
|
|
|
|
O: Send,
|
|
|
|
I: Send + Clone + 'static + std::fmt::Debug,
|
|
|
|
T: Send + Clone + 'static + std::fmt::Debug,
|
|
|
|
F: Fn(I) -> FR + Send + Sync + Clone + 'static,
|
|
|
|
FR: for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), T)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), O), ItemStateError>> + Send + 'a>> + Send + Sync,
|
|
|
|
T: Clone + Send + Sync,
|
|
|
|
{
|
2022-07-19 12:34:07 -06:00
|
|
|
move |(item_state, transaction), arg| {
|
2022-07-18 18:42:44 -06:00
|
|
|
let input = input.clone();
|
|
|
|
let func = func.clone();
|
|
|
|
println!("i {:?} {:?}", input, arg);
|
|
|
|
Box::pin(async move {
|
|
|
|
let (state, result) = iterate_inner((item_state, transaction), input, func, arg.clone()).await?;
|
|
|
|
Ok((state, result))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[async_recursion::async_recursion]
|
|
|
|
async fn foreach_inner<'a, O, T, F>(state: (ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>),
|
|
|
|
mut input: Vec<T>,
|
|
|
|
func: F)
|
|
|
|
-> Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), Vec<O>), ItemStateError>
|
|
|
|
where
|
|
|
|
'a: 'async_recursion,
|
|
|
|
O: Send,
|
|
|
|
T: Clone + Send,
|
|
|
|
F: Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), T)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), O), ItemStateError>> + Send + 'a>> + Send + Sync,
|
|
|
|
F: Clone,
|
|
|
|
{
|
|
|
|
let item = match input.pop() {
|
|
|
|
Some(item) => item,
|
|
|
|
None => return Ok((state, Vec::new()))
|
|
|
|
};
|
|
|
|
|
|
|
|
let (state, mut output) = foreach_inner(state, input, func.clone()).await?;
|
|
|
|
let (state, result) = func(state, item).await?;
|
|
|
|
|
|
|
|
output.push(result);
|
|
|
|
|
|
|
|
Ok((state, output))
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn foreach<'k, O, T, F>(func: F)
|
2022-07-18 18:42:44 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), Vec<T>)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), Vec<O>), ItemStateError>> + Send + 'a>>
|
|
|
|
where
|
|
|
|
O: Send,
|
|
|
|
T: Send + Clone + 'static + std::fmt::Debug,
|
|
|
|
F: for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), T)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), O), ItemStateError>> + Send + 'a>> + Send + Sync + 'static,
|
|
|
|
F: Clone,
|
|
|
|
T: Clone + Send + Sync,
|
|
|
|
{
|
|
|
|
move |(item_state, transaction), items| {
|
|
|
|
println!("fe {:?}", items);
|
|
|
|
let func = func.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
let (state, result) = foreach_inner((item_state, transaction), items, func).await?;
|
|
|
|
Ok((state, result))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn insert<'a, T: Send + Clone + 'a>(element: T)
|
2022-07-18 18:42:44 -06:00
|
|
|
-> impl Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), T), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |state, _| {
|
|
|
|
let element = element.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
Ok((state, element))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_item_to_inventory(character: CharacterEntity)
|
2022-07-18 18:42:44 -06:00
|
|
|
-> 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
|
|
|
|
{
|
|
|
|
move |(mut item_state, transaction), inventory_item| {
|
|
|
|
let character = character.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut inventory = item_state.inventory(&character.id)?;
|
|
|
|
let mut transaction = inventory_item.with_mag(transaction, |mut transaction, entity_id, _mag| {
|
|
|
|
let character = character.clone();
|
|
|
|
async move {
|
|
|
|
transaction.gateway().change_mag_owner(&entity_id, &character).await?;
|
|
|
|
Ok(transaction)
|
|
|
|
}}).await?;
|
|
|
|
|
|
|
|
inventory.add_item(inventory_item.clone())?;
|
|
|
|
transaction.gateway().set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
|
|
|
item_state.set_inventory(inventory);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), inventory_item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn record_trade(trade_id: TradeId, character_to: CharacterEntityId, character_from: CharacterEntityId)
|
2022-07-18 18:42:44 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), Vec<InventoryItem>)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), Vec<InventoryItem>), ItemStateError>> + Send + 'a>> + Clone
|
|
|
|
{
|
|
|
|
move |(item_state, mut transaction), traded_items| {
|
|
|
|
Box::pin(async move {
|
|
|
|
for item in &traded_items {
|
|
|
|
transaction = item.with_entity_id(transaction, |mut transaction, entity_id| {
|
|
|
|
async move {
|
|
|
|
transaction.gateway().add_item_note(&entity_id, ItemNote::Trade {
|
|
|
|
trade_id,
|
|
|
|
character_to,
|
|
|
|
character_from,
|
|
|
|
}).await?;
|
|
|
|
Ok(transaction)
|
|
|
|
}}).await?;
|
|
|
|
}
|
|
|
|
Ok(((item_state, transaction), traded_items))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn assign_new_item_id()
|
2022-07-18 18:42:44 -06:00
|
|
|
-> 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
|
|
|
|
{
|
|
|
|
move |(mut item_state, transaction), mut inventory_item| {
|
|
|
|
Box::pin(async move {
|
|
|
|
inventory_item.item_id = item_state.new_item_id()?;
|
|
|
|
Ok(((item_state, transaction), inventory_item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 20:51:35 -06:00
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn convert_item_drop_to_floor_item(character_id: CharacterEntityId, item_drop: ItemDrop)
|
2022-07-18 20:51:35 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), ())
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem), ItemStateError>> + Send + 'a>> + Clone
|
|
|
|
{
|
|
|
|
move |(mut item_state, mut transaction), _| {
|
|
|
|
let item_drop = item_drop.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
enum ItemOrMeseta {
|
|
|
|
Individual(ItemDetail),
|
|
|
|
Stacked(Tool),
|
|
|
|
Meseta(Meseta)
|
|
|
|
}
|
|
|
|
|
|
|
|
let item = match item_drop.item {
|
|
|
|
ItemDropType::Weapon(w) => ItemOrMeseta::Individual(ItemDetail::Weapon(w)),
|
|
|
|
ItemDropType::Armor(w) => ItemOrMeseta::Individual(ItemDetail::Armor(w)),
|
|
|
|
ItemDropType::Shield(w) => ItemOrMeseta::Individual(ItemDetail::Shield(w)),
|
|
|
|
ItemDropType::Unit(w) => ItemOrMeseta::Individual(ItemDetail::Unit(w)),
|
|
|
|
ItemDropType::TechniqueDisk(w) => ItemOrMeseta::Individual(ItemDetail::TechniqueDisk(w)),
|
|
|
|
ItemDropType::Mag(w) => ItemOrMeseta::Individual(ItemDetail::Mag(w)),
|
|
|
|
ItemDropType::Tool(t) => {
|
|
|
|
if t.tool.is_stackable() {
|
|
|
|
ItemOrMeseta::Stacked(t)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ItemOrMeseta::Individual(ItemDetail::Tool(t))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ItemDropType::Meseta(m) => ItemOrMeseta::Meseta(Meseta(m)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_id = item_state.new_item_id()?;
|
|
|
|
|
|
|
|
let floor_item = match item {
|
|
|
|
ItemOrMeseta::Individual(item_detail) => {
|
|
|
|
let entity = transaction.gateway().create_item(NewItemEntity {
|
|
|
|
item: item_detail.clone(),
|
|
|
|
}).await?;
|
|
|
|
transaction.gateway().add_item_note(&entity.id, ItemNote::EnemyDrop {
|
|
|
|
character_id,
|
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
}).await?;
|
|
|
|
FloorItem {
|
|
|
|
item_id,
|
|
|
|
item: FloorItemDetail::Individual(IndividualItemDetail {
|
|
|
|
entity_id: entity.id,
|
|
|
|
item: item_detail,
|
|
|
|
}),
|
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ItemOrMeseta::Stacked(tool) => {
|
|
|
|
let entity = transaction.gateway().create_item(NewItemEntity {
|
|
|
|
item: ItemDetail::Tool(tool),
|
|
|
|
}).await?;
|
|
|
|
transaction.gateway().add_item_note(&entity.id, ItemNote::EnemyDrop {
|
|
|
|
character_id,
|
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
}).await?;
|
|
|
|
FloorItem {
|
|
|
|
item_id,
|
|
|
|
item: FloorItemDetail::Stacked(StackedItemDetail{
|
|
|
|
entity_ids: vec![entity.id],
|
|
|
|
tool,
|
|
|
|
}),
|
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ItemOrMeseta::Meseta(meseta) => {
|
|
|
|
FloorItem {
|
|
|
|
item_id,
|
|
|
|
item: FloorItemDetail::Meseta(meseta),
|
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), floor_item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn add_item_to_local_floor(character_id: CharacterEntityId)
|
2022-07-18 20:51:35 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), FloorItem), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(mut item_state, transaction) , floor_item| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut floor = item_state.floor(&character_id)?;
|
|
|
|
let item = floor.add_local_item(floor_item).clone();
|
|
|
|
item_state.set_floor(floor);
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn apply_modifier_to_inventory_item(modifier: ItemModifier)
|
2022-07-18 23:57:54 -06:00
|
|
|
-> 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 {
|
2022-07-19 12:34:07 -06:00
|
|
|
match (&mut inventory_item.item, modifier) {
|
|
|
|
(InventoryItemDetail::Individual(IndividualItemDetail{entity_id, item: ItemDetail::Weapon(ref mut weapon), ..}), ItemModifier::WeaponModifier(modifier)) => {
|
2022-07-18 23:57:54 -06:00
|
|
|
weapon.apply_modifier(&modifier);
|
2022-07-19 12:34:07 -06:00
|
|
|
transaction.gateway().add_weapon_modifier(entity_id, modifier).await?;
|
2022-07-18 23:57:54 -06:00
|
|
|
},
|
|
|
|
_ => return Err(ItemStateError::InvalidModifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), inventory_item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:26:00 -06:00
|
|
|
pub(super) fn as_individual_item()
|
2022-07-18 23:57:54 -06:00
|
|
|
-> impl for<'a> Fn((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), InventoryItem)
|
|
|
|
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction + 'a>), IndividualItemDetail), ItemStateError>> + Send + 'a>>
|
|
|
|
{
|
|
|
|
move |(item_state, transaction), inventory_item| {
|
|
|
|
Box::pin(async move {
|
|
|
|
let item = match inventory_item.item {
|
|
|
|
InventoryItemDetail::Individual(individual_item) => individual_item,
|
|
|
|
_ => return Err(ItemStateError::WrongItemType(inventory_item.item_id))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(((item_state, transaction), item))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|