From e61901d0ea873d41df473582ab29dfc9c0558e04 Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 30 May 2022 23:47:58 -0600 Subject: [PATCH] clean these functions up a bit --- src/ship/items/actions.rs | 84 ++++++++++++++++----------------------- src/ship/items/state.rs | 47 +++++++++++----------- 2 files changed, 58 insertions(+), 73 deletions(-) diff --git a/src/ship/items/actions.rs b/src/ship/items/actions.rs index 5fdd398..f01c7b5 100644 --- a/src/ship/items/actions.rs +++ b/src/ship/items/actions.rs @@ -45,23 +45,19 @@ fn add_floor_item_to_inventory(character: &CharacterEntity) let mut inventory = item_state.inventory(&character.id)?; let character_id = character.id; - let transaction = floor_item.with_entity_id(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id| { + let transaction = floor_item.with_entity_id(transaction, |mut transaction, entity_id| { async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().add_item_note(&entity_id, ItemNote::Pickup { - character_id - }).await?; - } - transaction + transaction.gateway().add_item_note(&entity_id, ItemNote::Pickup { + character_id + }).await?; + Ok(transaction) }}).await?; - let mut transaction = floor_item.with_mag(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id, _mag| { + let mut transaction = floor_item.with_mag(transaction, |mut transaction, entity_id, _mag| { let character = character.clone(); async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().change_mag_owner(&entity_id, &character).await?; - } - transaction + transaction.gateway().change_mag_owner(&entity_id, &character).await?; + Ok(transaction) }}).await?; let add_result = inventory.add_floor_item(floor_item)?; @@ -125,18 +121,16 @@ fn add_inventory_item_to_shared_floor(character_id: CharacterEntityId, map_area: { move |(mut item_state, transaction), inventory_item| { Box::pin(async move { - let transaction = inventory_item.with_entity_id(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id| { + let transaction = inventory_item.with_entity_id(transaction, |mut transaction, entity_id| { async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().add_item_note(&entity_id, ItemNote::PlayerDrop { - character_id, - map_area, - x: drop_position.0, - y: drop_position.1, - z: drop_position.2, - }).await?; - } - transaction + transaction.gateway().add_item_note(&entity_id, ItemNote::PlayerDrop { + character_id, + map_area, + x: drop_position.0, + y: drop_position.1, + z: drop_position.2, + }).await?; + Ok(transaction) }}).await?; let mut floor = item_state.floor(&character_id)?; @@ -376,16 +370,14 @@ fn add_bank_item_to_inventory(character: &CharacterEntity) let mut inventory = item_state.inventory(&character.id)?; let character_id = character.id; - let transaction = bank_item.with_entity_id(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id| { + let transaction = bank_item.with_entity_id(transaction, |mut transaction, entity_id| { let bank_name = bank_name.clone(); async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().add_item_note(&entity_id, ItemNote::Withdraw { - character_id, - bank: bank_name, - }).await?; - } - transaction + transaction.gateway().add_item_note(&entity_id, ItemNote::Withdraw { + character_id, + bank: bank_name, + }).await?; + Ok(transaction) }}).await?; let inventory_item = InventoryItem { @@ -396,13 +388,11 @@ fn add_bank_item_to_inventory(character: &CharacterEntity) }, }; - let mut transaction = inventory_item.with_mag(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id, _mag| { + let mut transaction = inventory_item.with_mag(transaction, |mut transaction, entity_id, _mag| { let character = character.clone(); async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().change_mag_owner(&entity_id, &character).await?; - } - transaction + transaction.gateway().change_mag_owner(&entity_id, &character).await?; + Ok(transaction) }}).await?; inventory.add_item(inventory_item.clone())?; @@ -444,16 +434,14 @@ fn add_inventory_item_to_bank(character_id: CharacterEntityId) Box::pin(async move { let mut bank = item_state.bank(&character_id)?; let bank_name = bank.name.clone(); - let mut transaction = inventory_item.with_entity_id(Ok(transaction), move |mut transaction: Result<_, ItemStateError>, entity_id| { + let mut transaction = inventory_item.with_entity_id(transaction, move |mut transaction, entity_id| { let bank_name = bank_name.clone(); async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().add_item_note(&entity_id, ItemNote::Deposit { - character_id, - bank: bank_name, - }).await?; - } - transaction + transaction.gateway().add_item_note(&entity_id, ItemNote::Deposit { + character_id, + bank: bank_name, + }).await?; + Ok(transaction) }}).await?; bank.add_inventory_item(inventory_item)?; @@ -609,12 +597,10 @@ fn use_consumed_item(character: CharacterEntity) move |(mut item_state, mut transaction), inventory_item| { let mut character = character.clone(); Box::pin(async move { - let mut transaction = inventory_item.with_entity_id(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id| { + let mut transaction = inventory_item.with_entity_id(transaction, |mut transaction, entity_id| { async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().add_item_note(&entity_id, ItemNote::Consumed).await?; - } - transaction + transaction.gateway().add_item_note(&entity_id, ItemNote::Consumed).await?; + Ok(transaction) }}).await?; apply_item(&mut item_state, transaction.gateway(), &mut character, inventory_item).await?; diff --git a/src/ship/items/state.rs b/src/ship/items/state.rs index df226f1..f65a6d9 100644 --- a/src/ship/items/state.rs +++ b/src/ship/items/state.rs @@ -268,36 +268,36 @@ pub struct InventoryItem { } impl InventoryItem { - pub async fn with_entity_id(&self, mut param: T, mut func: F) -> T + pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId) -> Fut, - Fut: Future, + Fut: Future>, { match &self.item { InventoryItemDetail::Individual(individual_item) => { - param = func(param, individual_item.entity_id).await; + param = func(param, individual_item.entity_id).await?; }, InventoryItemDetail::Stacked(stacked_item) => { for entity_id in &stacked_item.entity_ids { - param = func(param, *entity_id).await; + param = func(param, *entity_id).await?; } } } - param + Ok(param) } - pub async fn with_mag(&self, mut param: T, mut func: F) -> T + pub async fn with_mag(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId, Mag) -> Fut, - Fut: Future, + Fut: Future>, { if let InventoryItemDetail::Individual(individual_item) = &self.item { if let ItemDetail::Mag(mag) = &individual_item.item { - param = func(param, individual_item.entity_id, mag.clone()).await; + param = func(param, individual_item.entity_id, mag.clone()).await?; } } - param + Ok(param) } } @@ -344,23 +344,22 @@ pub struct BankItem { } impl BankItem { - pub async fn with_entity_id(&self, mut param: T, mut func: F) -> T + pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId) -> Fut, - Fut: Future, + Fut: Future>, { match &self.item { BankItemDetail::Individual(individual_item) => { - param = func(param, individual_item.entity_id).await; + param = func(param, individual_item.entity_id).await?; }, BankItemDetail::Stacked(stacked_item) => { for entity_id in &stacked_item.entity_ids { - param = func(param, *entity_id).await; + param = func(param, *entity_id).await?; } } } - - param + Ok(param) } } @@ -391,37 +390,37 @@ pub struct FloorItem { } impl FloorItem { - pub async fn with_entity_id(&self, mut param: T, mut func: F) -> T + pub async fn with_entity_id(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId) -> Fut, - Fut: Future, + Fut: Future>, { match &self.item { FloorItemDetail::Individual(individual_item) => { - param = func(param, individual_item.entity_id).await; + param = func(param, individual_item.entity_id).await?; }, FloorItemDetail::Stacked(stacked_item) => { for entity_id in &stacked_item.entity_ids { - param = func(param, *entity_id).await; + param = func(param, *entity_id).await?; } }, FloorItemDetail::Meseta(_meseta) => {}, } - param + Ok(param) } - pub async fn with_mag(&self, mut param: T, mut func: F) -> T + pub async fn with_mag(&self, mut param: T, mut func: F) -> Result where F: FnMut(T, ItemEntityId, Mag) -> Fut, - Fut: Future, + Fut: Future>, { if let FloorItemDetail::Individual(individual_item) = &self.item { if let ItemDetail::Mag(mag) = &individual_item.item { - param = func(param, individual_item.entity_id, mag.clone()).await; + param = func(param, individual_item.entity_id, mag.clone()).await?; } } - param + Ok(param) } pub fn as_client_bytes(&self) -> [u8; 16] {