Browse Source

anyhowing continues

pull/127/head
jake 2 years ago
parent
commit
7bd385d580
  1. 20
      src/ship/items/bank.rs
  2. 6
      src/ship/items/floor.rs
  3. 42
      src/ship/items/inventory.rs
  4. 2
      src/ship/items/state.rs

20
src/ship/items/bank.rs

@ -64,10 +64,10 @@ pub struct BankItem {
} }
impl BankItem { impl BankItem {
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 {
BankItemDetail::Individual(individual_item) => { BankItemDetail::Individual(individual_item) => {
@ -125,27 +125,27 @@ impl BankState {
self.item_id_counter = base_item_id + self.bank.0.len() as u32; self.item_id_counter = base_item_id + self.bank.0.len() as u32;
} }
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 + 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(())
} }
pub fn add_inventory_item(&mut self, item: InventoryItem) -> Result<AddItemResult, BankError> {
pub fn add_inventory_item(&mut self, item: InventoryItem) -> Result<AddItemResult, anyhow::Error> {
match item.item { match item.item {
InventoryItemDetail::Individual(iitem) => { InventoryItemDetail::Individual(iitem) => {
if self.bank.0.len() >= 30 { if self.bank.0.len() >= 30 {
Err(BankError::BankFull)
Err(BankError::BankFull.into())
} }
else { else {
self.bank.0.push(BankItem { self.bank.0.push(BankItem {
@ -166,7 +166,7 @@ impl BankState {
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(BankError::StackFull)
Err(BankError::StackFull.into())
} }
else { else {
existing_stack.entity_ids.append(&mut sitem.entity_ids.clone()); existing_stack.entity_ids.append(&mut sitem.entity_ids.clone());
@ -175,7 +175,7 @@ impl BankState {
}, },
None => { None => {
if self.bank.0.len() >= 30 { if self.bank.0.len() >= 30 {
Err(BankError::BankFull)
Err(BankError::BankFull.into())
} }
else { else {
self.bank.0.push(BankItem { self.bank.0.push(BankItem {

6
src/ship/items/floor.rs

@ -33,7 +33,7 @@ pub struct FloorItem {
} }
impl FloorItem { impl FloorItem {
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, ItemStateError>>,
@ -53,10 +53,10 @@ impl FloorItem {
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 FloorItemDetail::Individual(individual_item) = &self.item { if let FloorItemDetail::Individual(individual_item) = &self.item {
if let ItemDetail::Mag(mag) = &individual_item.item { if let ItemDetail::Mag(mag) = &individual_item.item {

42
src/ship/items/inventory.rs

@ -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(())

2
src/ship/items/state.rs

@ -22,6 +22,8 @@ pub enum ItemStateError {
NoCharacter(CharacterEntityId), NoCharacter(CharacterEntityId),
#[error("room {0} not found")] #[error("room {0} not found")]
NoRoom(RoomId), NoRoom(RoomId),
#[error("inventory item {0} not found")]
NoInventoryItem(ClientItemId),
#[error("floor item {0} not found")] #[error("floor item {0} not found")]
NoFloorItem(ClientItemId), NoFloorItem(ClientItemId),
#[error("expected {0} to be a tool")] #[error("expected {0} to be a tool")]

Loading…
Cancel
Save