Moved bot inventory to the db
+ Refactored item and shop handling a bit + fmt + clippy
This commit is contained in:
+22
-4
@@ -34,6 +34,14 @@ pub enum ItemType {
|
||||
Nft,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Hash, Eq, PartialEq, Copy)]
|
||||
#[allow(dead_code)]
|
||||
pub enum Operation {
|
||||
Sell,
|
||||
Buy,
|
||||
Custom(i64),
|
||||
}
|
||||
|
||||
impl FromStr for ItemType {
|
||||
type Err = InventoryError;
|
||||
|
||||
@@ -117,7 +125,7 @@ pub struct InventoryManager {
|
||||
}
|
||||
|
||||
impl InventoryManager {
|
||||
pub fn get_item(&mut self, item_type: ItemType) -> Option<&mut InventorySlot> {
|
||||
pub fn get_item_mut(&mut self, item_type: ItemType) -> Option<&mut InventorySlot> {
|
||||
if let Some(inv_ndx) = self.inventory.iter().position(|i| i.item_type == item_type) {
|
||||
Some(&mut self.inventory[inv_ndx])
|
||||
} else {
|
||||
@@ -125,8 +133,16 @@ impl InventoryManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_item(&self, item_type: ItemType) -> Option<&InventorySlot> {
|
||||
if let Some(inv_ndx) = self.inventory.iter().position(|i| i.item_type == item_type) {
|
||||
Some(&self.inventory[inv_ndx])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn give_item(&mut self, item_type: ItemType, quantity: i64, item_data: Option<ItemData>) {
|
||||
match self.get_item(item_type) {
|
||||
match self.get_item_mut(item_type) {
|
||||
None => self.inventory.push(InventorySlot {
|
||||
quantity,
|
||||
item_type,
|
||||
@@ -144,7 +160,7 @@ impl InventoryManager {
|
||||
item: ItemType,
|
||||
quantity: i64,
|
||||
) -> Result<Option<ItemData>, InventoryError> {
|
||||
let slot = self.get_item(item);
|
||||
let slot = self.get_item_mut(item);
|
||||
|
||||
if let Some(slot) = slot {
|
||||
if slot.quantity < quantity {
|
||||
@@ -179,7 +195,9 @@ impl InventoryManager {
|
||||
}
|
||||
|
||||
pub fn try_use_item(&mut self, item: ItemType) -> Result<Option<ItemData>, InventoryError> {
|
||||
let item_slot = self.get_item(item).ok_or(InventoryError::NotEnoughItems)?;
|
||||
let item_slot = self
|
||||
.get_item_mut(item)
|
||||
.ok_or(InventoryError::NotEnoughItems)?;
|
||||
|
||||
if item_slot.quantity < item_slot.use_cost() {
|
||||
return Err(InventoryError::NotEnoughItems);
|
||||
|
||||
Reference in New Issue
Block a user