|
|
@ -1,4 +1,3 @@ |
|
|
|
use log::warn;
|
|
|
|
use crate::ship::items::ClientItemId;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::cell::RefCell;
|
|
|
@ -6,7 +5,7 @@ use thiserror::Error; |
|
|
|
use crate::entity::gateway::{EntityGateway, GatewayError};
|
|
|
|
use crate::entity::character::{CharacterEntity, CharacterEntityId, TechLevel};
|
|
|
|
use crate::entity::item::{ItemDetail, ItemNote, BankName};
|
|
|
|
use crate::entity::item::{Meseta, NewItemEntity, ItemEntity, ItemEntityId, InventoryItemEntity, BankItemEntity};
|
|
|
|
use crate::entity::item::{Meseta, NewItemEntity, ItemEntity, InventoryItemEntity, BankItemEntity};
|
|
|
|
use crate::entity::item::tool::{Tool, ToolType};
|
|
|
|
use crate::entity::item::weapon;
|
|
|
|
use crate::ship::map::MapArea;
|
|
|
@ -239,38 +238,36 @@ impl ItemManager { |
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_character_bank(&self, character: &CharacterEntity) -> Result<&CharacterBank, anyhow::Error> {
|
|
|
|
Ok(self.character_bank
|
|
|
|
self.character_bank
|
|
|
|
.get(&character.id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(character.id))?)
|
|
|
|
//.get(&BankName("".to_string()))
|
|
|
|
//.ok_or(ItemManagerError::InvalidBankName(BankName("".to_string())))?)
|
|
|
|
.ok_or_else(|| ItemManagerError::NoCharacter(character.id).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
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))?)
|
|
|
|
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))?)
|
|
|
|
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))?)
|
|
|
|
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))?)
|
|
|
|
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)
|
|
|
|
self.character_meseta.get_mut(character_id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(*character_id))?,
|
|
|
|
self.bank_meseta.get_mut(&character_id)
|
|
|
|
self.bank_meseta.get_mut(character_id)
|
|
|
|
.ok_or(ItemManagerError::NoCharacter(*character_id))?
|
|
|
|
))
|
|
|
|
}
|
|
|
@ -299,7 +296,7 @@ impl ItemManager { |
|
|
|
|
|
|
|
pub async fn character_picks_up_item<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, item_id: ClientItemId)
|
|
|
|
-> Result<TriggerCreateItem, anyhow::Error> {
|
|
|
|
let it = ItemTransaction::new(&self, (character, item_id))
|
|
|
|
let it = ItemTransaction::new(self, (character, item_id))
|
|
|
|
.act(|it, (character, item_id)| -> Result<_, ItemManagerError> {
|
|
|
|
let local_floor = it.manager.character_floor.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
let inventory = it.manager.character_inventory.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
@ -310,7 +307,7 @@ impl ItemManager { |
|
|
|
Some(floor_item) => {
|
|
|
|
it.action(Box::new(RemoveFromLocalFloor {
|
|
|
|
character_id: character.id,
|
|
|
|
item_id: item_id.clone()
|
|
|
|
item_id: *item_id
|
|
|
|
}));
|
|
|
|
floor_item
|
|
|
|
},
|
|
|
@ -319,12 +316,12 @@ impl ItemManager { |
|
|
|
Some(floor_item) => {
|
|
|
|
it.action(Box::new(RemoveFromSharedFloor {
|
|
|
|
room_id: *room_id,
|
|
|
|
item_id: item_id.clone()
|
|
|
|
item_id: *item_id
|
|
|
|
}));
|
|
|
|
floor_item
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
return Err(ItemManagerError::NoSuchItemId(item_id.clone())).into()
|
|
|
|
return Err(ItemManagerError::NoSuchItemId(*item_id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -339,7 +336,7 @@ impl ItemManager { |
|
|
|
}))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Err(ItemManagerError::CouldNotAddToInventory(*item_id).into());
|
|
|
|
return Err(ItemManagerError::CouldNotAddToInventory(*item_id));
|
|
|
|
}
|
|
|
|
TriggerCreateItem::Yes
|
|
|
|
},
|
|
|
@ -360,14 +357,14 @@ impl ItemManager { |
|
|
|
TriggerCreateItem::No
|
|
|
|
},
|
|
|
|
SpaceForStack::No(_) => {
|
|
|
|
return Err(ItemManagerError::CouldNotAddToInventory(*item_id).into());
|
|
|
|
return Err(ItemManagerError::CouldNotAddToInventory(*item_id));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
FloorItem::Meseta(meseta_floor_item) => {
|
|
|
|
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());
|
|
|
|
return Err(ItemManagerError::CouldNotAddToInventory(*item_id));
|
|
|
|
}
|
|
|
|
it.action(Box::new(AddMesetaFloorItemToInventory {
|
|
|
|
character_id: character.id,
|
|
|
@ -615,7 +612,7 @@ impl ItemManager { |
|
|
|
.ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
|
|
|
|
let item_to_deposit = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
|
let bank_item = bank.deposit_item(item_to_deposit, amount).ok_or(ItemManagerError::Idunnoman)?;
|
|
|
|
let _bank_item = bank.deposit_item(item_to_deposit, amount).ok_or(ItemManagerError::Idunnoman)?;
|
|
|
|
|
|
|
|
entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
|
|
|
entity_gateway.set_character_bank(&character.id, &bank.as_bank_entity(&character.id, &BankName("".into())), BankName("".into())).await?;
|
|
|
@ -972,7 +969,7 @@ impl ItemManager { |
|
|
|
p1: (&AreaClient, &CharacterEntity, &Vec<TradeItem>, usize),
|
|
|
|
p2: (&AreaClient, &CharacterEntity, &Vec<TradeItem>, usize))
|
|
|
|
-> Result<Vec<ItemToTrade>, anyhow::Error> {
|
|
|
|
let it = ItemTransaction::new(&self, (p1, p2, room_id))
|
|
|
|
let it = ItemTransaction::new(self, (p1, p2, room_id))
|
|
|
|
.act(|it, (p1, p2, room_id)| -> Result<_, anyhow::Error> {
|
|
|
|
let p1_inventory = it.manager.get_character_inventory(p1.1)?;
|
|
|
|
let p2_inventory = it.manager.get_character_inventory(p2.1)?;
|
|
|
@ -1049,27 +1046,27 @@ impl ItemManager { |
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
|
|
|
|
let trade_items = [(p1, p2, p1_inventory, p2_inventory), (p2, p1, p2_inventory, p1_inventory)]
|
|
|
|
.map(|(src_client, dest_client, src_inventory, dest_inventory)| {
|
|
|
|
let trade_items = [(p1, p2, p1_inventory), (p2, p1, p2_inventory)]
|
|
|
|
.map(|(src_client, dest_client, src_inventory)| {
|
|
|
|
src_client.2.iter()
|
|
|
|
.map(|item| -> Option<(Option<ItemToTrade>, Box<dyn ItemAction<EG>>)> {
|
|
|
|
match item {
|
|
|
|
TradeItem::Individual(item_id) => {
|
|
|
|
let item = src_inventory.get_item_by_id(*item_id)?.individual()?;
|
|
|
|
let new_item_id = it.manager.room_item_id_counter.borrow_mut().get_mut(&room_id)?();
|
|
|
|
let new_item_id = it.manager.room_item_id_counter.borrow_mut().get_mut(room_id)?();
|
|
|
|
Some((
|
|
|
|
Some(ItemToTrade {
|
|
|
|
add_to: *dest_client.0,
|
|
|
|
remove_from: *src_client.0,
|
|
|
|
current_item_id: *item_id,
|
|
|
|
new_item_id: new_item_id,
|
|
|
|
new_item_id,
|
|
|
|
item_detail: ItemToTradeDetail::Individual(item.item.clone())
|
|
|
|
}),
|
|
|
|
Box::new(TradeIndividualItem {
|
|
|
|
src_character_id: src_client.1.id,
|
|
|
|
dest_character_id: dest_client.1.id,
|
|
|
|
current_item_id: *item_id,
|
|
|
|
new_item_id: new_item_id,
|
|
|
|
new_item_id,
|
|
|
|
}),
|
|
|
|
))
|
|
|
|
},
|
|
|
@ -1079,13 +1076,13 @@ impl ItemManager { |
|
|
|
None
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let new_item_id = it.manager.room_item_id_counter.borrow_mut().get_mut(&room_id)?();
|
|
|
|
let new_item_id = it.manager.room_item_id_counter.borrow_mut().get_mut(room_id)?();
|
|
|
|
Some((
|
|
|
|
Some(ItemToTrade {
|
|
|
|
add_to: *dest_client.0,
|
|
|
|
remove_from: *src_client.0,
|
|
|
|
current_item_id: *item_id,
|
|
|
|
new_item_id: new_item_id,
|
|
|
|
new_item_id,
|
|
|
|
item_detail: ItemToTradeDetail::Stacked(item.tool, *amount)
|
|
|
|
}),
|
|
|
|
Box::new(TradeStackedItem {
|
|
|
@ -1093,7 +1090,7 @@ impl ItemManager { |
|
|
|
dest_character_id: dest_client.1.id,
|
|
|
|
//item_ids: item.entity_ids.iter().cloned().take(*amount).collect(),
|
|
|
|
current_item_id: *item_id,
|
|
|
|
new_item_id: new_item_id,
|
|
|
|
new_item_id,
|
|
|
|
amount: *amount,
|
|
|
|
}),
|
|
|
|
))
|
|
|
@ -1125,8 +1122,8 @@ impl ItemManager { |
|
|
|
|
|
|
|
|
|
|
|
if let [Some(p1_trades), Some(p2_trades)] = trade_items {
|
|
|
|
let (p1_item_trades, p1_item_actions): (Vec<Option<ItemToTrade>>, Vec<Box<dyn ItemAction<EG>>>) = p1_trades.into_iter().unzip();
|
|
|
|
let (p2_item_trades, p2_item_actions): (Vec<Option<ItemToTrade>>, Vec<Box<dyn ItemAction<EG>>>) = p2_trades.into_iter().unzip();
|
|
|
|
let (p1_item_trades, p1_item_actions): (Vec<_>, Vec<_>) = p1_trades.into_iter().unzip();
|
|
|
|
let (p2_item_trades, p2_item_actions): (Vec<_>, Vec<_>) = p2_trades.into_iter().unzip();
|
|
|
|
|
|
|
|
let item_trades = p1_item_trades.into_iter().flatten().chain(p2_item_trades.into_iter().flatten());
|
|
|
|
let item_actions = p1_item_actions.into_iter().chain(p2_item_actions.into_iter());
|
|
|
|