|
@ -60,7 +60,7 @@ impl InventoryItemDetail { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// TODO: this should probably go somewhere a bit more fundamental like ItemDetail
|
|
|
// TODO: this should probably go somewhere a bit more fundamental like ItemDetail
|
|
|
pub fn sell_price(&self) -> Result<u32, ItemStateError> {
|
|
|
|
|
|
|
|
|
pub fn sell_price(&self) -> Result<u32, anyhow::Error> {
|
|
|
match self {
|
|
|
match self {
|
|
|
InventoryItemDetail::Individual(individual_item) => {
|
|
|
InventoryItemDetail::Individual(individual_item) => {
|
|
|
match &individual_item.item {
|
|
|
match &individual_item.item {
|
|
@ -102,7 +102,7 @@ impl InventoryItemDetail { |
|
|
Ok((ToolShopItem::from(d).price() / 8) as u32)
|
|
|
Ok((ToolShopItem::from(d).price() / 8) as u32)
|
|
|
},
|
|
|
},
|
|
|
ItemDetail::Mag(_m) => {
|
|
|
ItemDetail::Mag(_m) => {
|
|
|
Err(ItemStateError::ItemNotSellable)
|
|
|
|
|
|
|
|
|
Err(ItemStateError::ItemNotSellable.into())
|
|
|
},
|
|
|
},
|
|
|
ItemDetail::ESWeapon(_e) => {
|
|
|
ItemDetail::ESWeapon(_e) => {
|
|
|
Ok(10u32)
|
|
|
Ok(10u32)
|
|
@ -126,10 +126,10 @@ pub struct InventoryItem { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl InventoryItem {
|
|
|
impl InventoryItem {
|
|
|
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
|
|
|
|
|
|
|
|
|
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, anyhow::Error>
|
|
|
where
|
|
|
where
|
|
|
F: FnMut(T, ItemEntityId) -> Fut,
|
|
|
F: FnMut(T, ItemEntityId) -> Fut,
|
|
|
Fut: Future<Output=Result<T, ItemStateError>>,
|
|
|
|
|
|
|
|
|
Fut: Future<Output=Result<T, anyhow::Error>>,
|
|
|
{
|
|
|
{
|
|
|
match &self.item {
|
|
|
match &self.item {
|
|
|
InventoryItemDetail::Individual(individual_item) => {
|
|
|
InventoryItemDetail::Individual(individual_item) => {
|
|
@ -145,10 +145,10 @@ impl InventoryItem { |
|
|
Ok(param)
|
|
|
Ok(param)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
|
|
|
|
|
|
|
|
|
pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, anyhow::Error>
|
|
|
where
|
|
|
where
|
|
|
F: FnMut(T, ItemEntityId, Mag) -> Fut,
|
|
|
F: FnMut(T, ItemEntityId, Mag) -> Fut,
|
|
|
Fut: Future<Output=Result<T, ItemStateError>>,
|
|
|
|
|
|
|
|
|
Fut: Future<Output=Result<T, anyhow::Error>>,
|
|
|
{
|
|
|
{
|
|
|
if let InventoryItemDetail::Individual(individual_item) = &self.item {
|
|
|
if let InventoryItemDetail::Individual(individual_item) = &self.item {
|
|
|
if let ItemDetail::Mag(mag) = &individual_item.item {
|
|
|
if let ItemDetail::Mag(mag) = &individual_item.item {
|
|
@ -205,11 +205,11 @@ impl InventoryState { |
|
|
self.inventory.0.len()
|
|
|
self.inventory.0.len()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn add_floor_item(&mut self, item: FloorItem) -> Result<AddItemResult, InventoryError> {
|
|
|
|
|
|
|
|
|
pub fn add_floor_item(&mut self, item: FloorItem) -> Result<AddItemResult, anyhow::Error> {
|
|
|
match item.item {
|
|
|
match item.item {
|
|
|
FloorItemDetail::Individual(iitem) => {
|
|
|
FloorItemDetail::Individual(iitem) => {
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
Err(InventoryError::InventoryFull)
|
|
|
|
|
|
|
|
|
Err(InventoryError::InventoryFull.into())
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
|
self.inventory.0.push(InventoryItem {
|
|
|
self.inventory.0.push(InventoryItem {
|
|
@ -229,7 +229,7 @@ impl InventoryState { |
|
|
match existing_stack {
|
|
|
match existing_stack {
|
|
|
Some(existing_stack) => {
|
|
|
Some(existing_stack) => {
|
|
|
if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() {
|
|
|
if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() {
|
|
|
Err(InventoryError::StackFull)
|
|
|
|
|
|
|
|
|
Err(InventoryError::StackFull.into())
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
|
existing_stack.entity_ids.append(&mut sitem.entity_ids.clone());
|
|
|
existing_stack.entity_ids.append(&mut sitem.entity_ids.clone());
|
|
@ -238,7 +238,7 @@ impl InventoryState { |
|
|
},
|
|
|
},
|
|
|
None => {
|
|
|
None => {
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
Err(InventoryError::InventoryFull)
|
|
|
|
|
|
|
|
|
Err(InventoryError::InventoryFull.into())
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
|
self.inventory.0.push(InventoryItem {
|
|
|
self.inventory.0.push(InventoryItem {
|
|
@ -253,7 +253,7 @@ impl InventoryState { |
|
|
},
|
|
|
},
|
|
|
FloorItemDetail::Meseta(meseta) => {
|
|
|
FloorItemDetail::Meseta(meseta) => {
|
|
|
if self.meseta == Meseta(999999) {
|
|
|
if self.meseta == Meseta(999999) {
|
|
|
Err(InventoryError::MesetaFull)
|
|
|
|
|
|
|
|
|
Err(InventoryError::MesetaFull.into())
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
|
self.meseta.0 = std::cmp::min(self.meseta.0 + meseta.0, 999999);
|
|
|
self.meseta.0 = std::cmp::min(self.meseta.0 + meseta.0, 999999);
|
|
@ -263,11 +263,11 @@ impl InventoryState { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn add_item(&mut self, item: InventoryItem) -> Result<(AddItemResult, InventoryItem), InventoryError> {
|
|
|
|
|
|
|
|
|
pub fn add_item(&mut self, item: InventoryItem) -> Result<(AddItemResult, InventoryItem), anyhow::Error> {
|
|
|
match &item.item {
|
|
|
match &item.item {
|
|
|
InventoryItemDetail::Individual(_) => {
|
|
|
InventoryItemDetail::Individual(_) => {
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
Err(InventoryError::InventoryFull)
|
|
|
|
|
|
|
|
|
Err(InventoryError::InventoryFull.into())
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
|
self.inventory.0.push(item);
|
|
|
self.inventory.0.push(item);
|
|
@ -290,7 +290,7 @@ impl InventoryState { |
|
|
match existing_stack {
|
|
|
match existing_stack {
|
|
|
Some(existing_stack) => {
|
|
|
Some(existing_stack) => {
|
|
|
if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() {
|
|
|
if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() {
|
|
|
Err(InventoryError::StackFull)
|
|
|
|
|
|
|
|
|
Err(InventoryError::StackFull.into())
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
|
existing_stack.entity_ids.append(&mut sitem.entity_ids.clone());
|
|
|
existing_stack.entity_ids.append(&mut sitem.entity_ids.clone());
|
|
@ -307,7 +307,7 @@ impl InventoryState { |
|
|
},
|
|
|
},
|
|
|
None => {
|
|
|
None => {
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
Err(InventoryError::InventoryFull)
|
|
|
|
|
|
|
|
|
Err(InventoryError::InventoryFull.into())
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
|
self.inventory.0.push(item);
|
|
|
self.inventory.0.push(item);
|
|
@ -370,25 +370,25 @@ impl InventoryState { |
|
|
.find(|i| i.item_id == *item_id)
|
|
|
.find(|i| i.item_id == *item_id)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn add_meseta(&mut self, amount: u32) -> Result<(), ItemStateError> {
|
|
|
|
|
|
|
|
|
pub fn add_meseta(&mut self, amount: u32) -> Result<(), anyhow::Error> {
|
|
|
if self.meseta.0 == 999999 {
|
|
|
if self.meseta.0 == 999999 {
|
|
|
return Err(ItemStateError::FullOfMeseta)
|
|
|
|
|
|
|
|
|
return Err(ItemStateError::FullOfMeseta.into())
|
|
|
}
|
|
|
}
|
|
|
self.meseta.0 = std::cmp::min(self.meseta.0 + amount, 999999);
|
|
|
self.meseta.0 = std::cmp::min(self.meseta.0 + amount, 999999);
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn add_meseta_no_overflow(&mut self, amount: u32) -> Result<(), ItemStateError> {
|
|
|
|
|
|
|
|
|
pub fn add_meseta_no_overflow(&mut self, amount: u32) -> Result<(), anyhow::Error> {
|
|
|
if self.meseta.0 + amount > 999999 {
|
|
|
if self.meseta.0 + amount > 999999 {
|
|
|
return Err(ItemStateError::FullOfMeseta)
|
|
|
|
|
|
|
|
|
return Err(ItemStateError::FullOfMeseta.into())
|
|
|
}
|
|
|
}
|
|
|
self.meseta.0 += amount;
|
|
|
self.meseta.0 += amount;
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn remove_meseta(&mut self, amount: u32) -> Result<(), ItemStateError> {
|
|
|
|
|
|
|
|
|
pub fn remove_meseta(&mut self, amount: u32) -> Result<(), anyhow::Error> {
|
|
|
if amount > self.meseta.0 {
|
|
|
if amount > self.meseta.0 {
|
|
|
return Err(ItemStateError::InvalidMesetaRemoval(amount))
|
|
|
|
|
|
|
|
|
return Err(ItemStateError::InvalidMesetaRemoval(amount).into())
|
|
|
}
|
|
|
}
|
|
|
self.meseta.0 -= amount;
|
|
|
self.meseta.0 -= amount;
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|