|
@ -900,29 +900,35 @@ impl ItemManager { |
|
|
item_id: ClientItemId,
|
|
|
item_id: ClientItemId,
|
|
|
amount: usize)
|
|
|
amount: usize)
|
|
|
-> Result<(), anyhow::Error> {
|
|
|
-> Result<(), anyhow::Error> {
|
|
|
|
|
|
let character_meseta = self.get_character_meseta(&character.id)?.0;
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let sold_item_handle = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
let sold_item_handle = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
if let Some(item_sold) = sold_item_handle.item() {
|
|
|
if let Some(item_sold) = sold_item_handle.item() {
|
|
|
let unit_price = item_sold.get_sell_price()?; {
|
|
|
let unit_price = item_sold.get_sell_price()?; {
|
|
|
let total_sale = unit_price * amount as u32;
|
|
|
let total_sale = unit_price * amount as u32;
|
|
|
match item_sold {
|
|
|
|
|
|
InventoryItem::Individual(i) => {
|
|
|
|
|
|
entity_gateway.add_item_note(&i.entity_id, ItemNote::SoldToShop).await?;
|
|
|
|
|
|
inventory.remove_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
|
|
|
},
|
|
|
|
|
|
InventoryItem::Stacked(s) => {
|
|
|
|
|
|
match amount.cmp(&s.count()) {
|
|
|
|
|
|
Ordering::Less | Ordering::Equal => {
|
|
|
|
|
|
sold_item_handle.consume(amount)?;
|
|
|
|
|
|
},
|
|
|
|
|
|
Ordering::Greater => return Err(ItemManagerError::InvalidSale.into()),
|
|
|
|
|
|
};
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
if character_meseta + total_sale <= 999999 {
|
|
|
|
|
|
match item_sold {
|
|
|
|
|
|
InventoryItem::Individual(i) => {
|
|
|
|
|
|
entity_gateway.add_item_note(&i.entity_id, ItemNote::SoldToShop).await?;
|
|
|
|
|
|
inventory.remove_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
|
|
|
},
|
|
|
|
|
|
InventoryItem::Stacked(s) => {
|
|
|
|
|
|
match amount.cmp(&s.count()) {
|
|
|
|
|
|
Ordering::Less | Ordering::Equal => {
|
|
|
|
|
|
sold_item_handle.consume(amount)?;
|
|
|
|
|
|
},
|
|
|
|
|
|
Ordering::Greater => return Err(ItemManagerError::InvalidSale.into()),
|
|
|
|
|
|
};
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
|
|
|
|
|
let character_meseta = self.get_character_meseta_mut(&character.id)?;
|
|
|
|
|
|
character_meseta.0 += total_sale;
|
|
|
|
|
|
entity_gateway.set_character_meseta(&character.id, *character_meseta).await?;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
return Err(ItemManagerError::WalletFull.into())
|
|
|
}
|
|
|
}
|
|
|
entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
|
|
|
|
|
let character_meseta = self.get_character_meseta_mut(&character.id)?;
|
|
|
|
|
|
character_meseta.0 += total_sale;
|
|
|
|
|
|
entity_gateway.set_character_meseta(&character.id, *character_meseta).await?;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
} else {
|
|
|
return Err(ItemManagerError::ItemIdNotInInventory(item_id).into())
|
|
|
return Err(ItemManagerError::ItemIdNotInInventory(item_id).into())
|
|
|