|
|
@ -106,6 +106,8 @@ pub struct ItemManager { |
|
|
|
pub(super) id_counter: u32,
|
|
|
|
|
|
|
|
pub(self) character_inventory: HashMap<CharacterEntityId, CharacterInventory>,
|
|
|
|
pub(self) character_meseta: HashMap<CharacterEntityId, Meseta>,
|
|
|
|
pub(self) bank_meseta: HashMap<CharacterEntityId, Meseta>,
|
|
|
|
//character_bank: HashMap<CharacterEntityId, BTreeMap<BankName, CharacterBank>>,
|
|
|
|
pub(self) character_bank: HashMap<CharacterEntityId, CharacterBank>,
|
|
|
|
pub(self) character_floor: HashMap<CharacterEntityId, RoomFloorItems>,
|
|
|
@ -120,6 +122,8 @@ impl Default for ItemManager { |
|
|
|
ItemManager {
|
|
|
|
id_counter: 0,
|
|
|
|
character_inventory: HashMap::new(),
|
|
|
|
character_meseta: HashMap::new(),
|
|
|
|
bank_meseta: HashMap::new(),
|
|
|
|
character_bank: HashMap::new(),
|
|
|
|
character_floor: HashMap::new(),
|
|
|
|
character_room: HashMap::new(),
|
|
|
@ -194,8 +198,13 @@ impl ItemManager { |
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
let character_bank = CharacterBank::new(bank_items);
|
|
|
|
|
|
|
|
let character_meseta = entity_gateway.get_character_meseta(&character.id).await?;
|
|
|
|
let bank_meseta = entity_gateway.get_bank_meseta(&character.id, BankName("".into())).await?;
|
|
|
|
|
|
|
|
self.character_inventory.insert(character.id, character_inventory);
|
|
|
|
self.character_bank.insert(character.id, character_bank);
|
|
|
|
self.character_meseta.insert(character.id, character_meseta);
|
|
|
|
self.bank_meseta.insert(character.id, bank_meseta);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
@ -237,6 +246,35 @@ impl ItemManager { |
|
|
|
//.ok_or(ItemManagerError::InvalidBankName(BankName("".to_string())))?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_character_meseta(&self, character_id: &CharacterEntityId) -> Result<&Meseta, ItemManagerError> {
|
|
|
|
Ok(self.character_meseta.get(&character_id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(*character_id))?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_character_meseta_mut<'a>(&'a mut self, character_id: &CharacterEntityId) -> Result<&'a mut Meseta, ItemManagerError> {
|
|
|
|
Ok(self.character_meseta.get_mut(&character_id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(*character_id))?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_bank_meseta(&self, character_id: &CharacterEntityId) -> Result<&Meseta, ItemManagerError> {
|
|
|
|
Ok(self.bank_meseta.get(&character_id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(*character_id))?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_bank_meseta_mut<'a>(&'a mut self, character_id: &CharacterEntityId) -> Result<&'a mut Meseta, ItemManagerError> {
|
|
|
|
Ok(self.bank_meseta.get_mut(&character_id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(*character_id))?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_character_and_bank_meseta_mut<'a>(&'a mut self, character_id: &CharacterEntityId) -> Result<(&'a mut Meseta, &'a mut Meseta), ItemManagerError> {
|
|
|
|
Ok((
|
|
|
|
self.character_meseta.get_mut(&character_id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(*character_id))?,
|
|
|
|
self.bank_meseta.get_mut(&character_id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(*character_id))?
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
/*pub fn get_character_bank_mut(&mut self, character: &CharacterEntity) -> Result<&CharacterBank, ItemManagerError> {
|
|
|
|
Ok(self.character_bank
|
|
|
|
.get_mut(&character.id)
|
|
|
@ -336,11 +374,12 @@ impl ItemManager { |
|
|
|
}
|
|
|
|
},
|
|
|
|
FloorItem::Meseta(meseta_floor_item) => {
|
|
|
|
if character.meseta >= 999999 {
|
|
|
|
let character_meseta = it.manager.character_meseta.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
if character_meseta.0 >= 999999 {
|
|
|
|
return Err(ItemManagerError::CouldNotAddToInventory(*item_id).into());
|
|
|
|
}
|
|
|
|
it.action(Box::new(AddMesetaFloorItemToInventory {
|
|
|
|
character: (**character).clone(),
|
|
|
|
character_id: character.id,
|
|
|
|
item: meseta_floor_item.clone()
|
|
|
|
}));
|
|
|
|
|
|
|
@ -493,11 +532,12 @@ impl ItemManager { |
|
|
|
-> Result<FloorItem, anyhow::Error> {
|
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
if character.meseta < amount {
|
|
|
|
let character_meseta = self.character_meseta.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
if character_meseta.0 < amount {
|
|
|
|
return Err(ItemManagerError::CouldNotDropMeseta.into())
|
|
|
|
}
|
|
|
|
character.meseta -= amount;
|
|
|
|
entity_gateway.save_character(character).await?;
|
|
|
|
character_meseta.0 -= amount;
|
|
|
|
entity_gateway.set_character_meseta(&character.id, *character_meseta).await?;
|
|
|
|
|
|
|
|
let item_id = self.room_item_id_counter.borrow_mut().get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?();
|
|
|
|
let floor_item = FloorItem::Meseta(MesetaFloorItem {
|
|
|
@ -1183,16 +1223,16 @@ impl<EG: EntityGateway> ItemAction<EG> for AddStackedFloorItemToInventory { |
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct AddMesetaFloorItemToInventory{
|
|
|
|
character: CharacterEntity,
|
|
|
|
character_id: CharacterEntityId,
|
|
|
|
item: MesetaFloorItem,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<EG: EntityGateway> ItemAction<EG> for AddMesetaFloorItemToInventory {
|
|
|
|
async fn commit(&self, _item_manager: &mut ItemManager, entity_gateway: &mut EG) -> Result<(), TransactionCommitError> {
|
|
|
|
let mut nchar = self.character.clone();
|
|
|
|
nchar.meseta = std::cmp::min(self.character.meseta + self.item.meseta.0, 999999);
|
|
|
|
entity_gateway.save_character(&nchar).await?;
|
|
|
|
async fn commit(&self, item_manager: &mut ItemManager, entity_gateway: &mut EG) -> Result<(), TransactionCommitError> {
|
|
|
|
let character_meseta = item_manager.character_meseta.get_mut(&self.character_id).ok_or(ItemManagerError::NoCharacter(self.character_id))?;
|
|
|
|
character_meseta.0 = std::cmp::min(character_meseta.0 + self.item.meseta.0, 999999);
|
|
|
|
entity_gateway.set_character_meseta(&self.character_id, *character_meseta).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1254,7 +1294,17 @@ struct TradeMeseta { |
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<EG: EntityGateway> ItemAction<EG> for TradeMeseta {
|
|
|
|
async fn commit(&self, _item_manager: &mut ItemManager, entity_gateway: &mut EG) -> Result<(), TransactionCommitError> {
|
|
|
|
async fn commit(&self, item_manager: &mut ItemManager, entity_gateway: &mut EG) -> Result<(), TransactionCommitError> {
|
|
|
|
{
|
|
|
|
let src_meseta = item_manager.get_character_meseta_mut(&self.src_character_id)?;
|
|
|
|
src_meseta.0 -= self.amount as u32;
|
|
|
|
entity_gateway.set_character_meseta(&self.src_character_id, *src_meseta).await?;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let dest_meseta = item_manager.get_character_meseta_mut(&self.dest_character_id)?;
|
|
|
|
dest_meseta.0 += self.amount as u32;
|
|
|
|
entity_gateway.set_character_meseta(&self.dest_character_id, *dest_meseta).await?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|