Browse Source

clean these functions up a bit

pull/112/head
jake 3 years ago
committed by andy
parent
commit
5be796cb5c
  1. 42
      src/ship/items/actions.rs
  2. 47
      src/ship/items/state.rs

42
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 mut inventory = item_state.inventory(&character.id)?;
let character_id = 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 { async move {
if let Ok(transaction) = &mut transaction {
transaction.gateway().add_item_note(&entity_id, ItemNote::Pickup { transaction.gateway().add_item_note(&entity_id, ItemNote::Pickup {
character_id character_id
}).await?; }).await?;
}
transaction
Ok(transaction)
}}).await?; }}).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(); let character = character.clone();
async move { async move {
if let Ok(transaction) = &mut transaction {
transaction.gateway().change_mag_owner(&entity_id, &character).await?; transaction.gateway().change_mag_owner(&entity_id, &character).await?;
}
transaction
Ok(transaction)
}}).await?; }}).await?;
let add_result = inventory.add_floor_item(floor_item)?; let add_result = inventory.add_floor_item(floor_item)?;
@ -125,9 +121,8 @@ fn add_inventory_item_to_shared_floor(character_id: CharacterEntityId, map_area:
{ {
move |(mut item_state, transaction), inventory_item| { move |(mut item_state, transaction), inventory_item| {
Box::pin(async move { 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 { async move {
if let Ok(transaction) = &mut transaction {
transaction.gateway().add_item_note(&entity_id, ItemNote::PlayerDrop { transaction.gateway().add_item_note(&entity_id, ItemNote::PlayerDrop {
character_id, character_id,
map_area, map_area,
@ -135,8 +130,7 @@ fn add_inventory_item_to_shared_floor(character_id: CharacterEntityId, map_area:
y: drop_position.1, y: drop_position.1,
z: drop_position.2, z: drop_position.2,
}).await?; }).await?;
}
transaction
Ok(transaction)
}}).await?; }}).await?;
let mut floor = item_state.floor(&character_id)?; 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 mut inventory = item_state.inventory(&character.id)?;
let character_id = 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(); let bank_name = bank_name.clone();
async move { async move {
if let Ok(transaction) = &mut transaction {
transaction.gateway().add_item_note(&entity_id, ItemNote::Withdraw { transaction.gateway().add_item_note(&entity_id, ItemNote::Withdraw {
character_id, character_id,
bank: bank_name, bank: bank_name,
}).await?; }).await?;
}
transaction
Ok(transaction)
}}).await?; }}).await?;
let inventory_item = InventoryItem { 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(); let character = character.clone();
async move { async move {
if let Ok(transaction) = &mut transaction {
transaction.gateway().change_mag_owner(&entity_id, &character).await?; transaction.gateway().change_mag_owner(&entity_id, &character).await?;
}
transaction
Ok(transaction)
}}).await?; }}).await?;
inventory.add_item(inventory_item.clone())?; inventory.add_item(inventory_item.clone())?;
@ -444,16 +434,14 @@ fn add_inventory_item_to_bank(character_id: CharacterEntityId)
Box::pin(async move { Box::pin(async move {
let mut bank = item_state.bank(&character_id)?; let mut bank = item_state.bank(&character_id)?;
let bank_name = bank.name.clone(); 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(); let bank_name = bank_name.clone();
async move { async move {
if let Ok(transaction) = &mut transaction {
transaction.gateway().add_item_note(&entity_id, ItemNote::Deposit { transaction.gateway().add_item_note(&entity_id, ItemNote::Deposit {
character_id, character_id,
bank: bank_name, bank: bank_name,
}).await?; }).await?;
}
transaction
Ok(transaction)
}}).await?; }}).await?;
bank.add_inventory_item(inventory_item)?; bank.add_inventory_item(inventory_item)?;
@ -609,12 +597,10 @@ fn use_consumed_item(character: CharacterEntity)
move |(mut item_state, mut transaction), inventory_item| { move |(mut item_state, mut transaction), inventory_item| {
let mut character = character.clone(); let mut character = character.clone();
Box::pin(async move { 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 { async move {
if let Ok(transaction) = &mut transaction {
transaction.gateway().add_item_note(&entity_id, ItemNote::Consumed).await?; transaction.gateway().add_item_note(&entity_id, ItemNote::Consumed).await?;
}
transaction
Ok(transaction)
}}).await?; }}).await?;
apply_item(&mut item_state, transaction.gateway(), &mut character, inventory_item).await?; apply_item(&mut item_state, transaction.gateway(), &mut character, inventory_item).await?;

47
src/ship/items/state.rs

@ -268,36 +268,36 @@ pub struct InventoryItem {
} }
impl InventoryItem { impl InventoryItem {
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> T
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where where
F: FnMut(T, ItemEntityId) -> Fut, F: FnMut(T, ItemEntityId) -> Fut,
Fut: Future<Output=T>,
Fut: Future<Output=Result<T, ItemStateError>>,
{ {
match &self.item { match &self.item {
InventoryItemDetail::Individual(individual_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) => { InventoryItemDetail::Stacked(stacked_item) => {
for entity_id in &stacked_item.entity_ids { 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<F, Fut, T>(&self, mut param: T, mut func: F) -> T
pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where where
F: FnMut(T, ItemEntityId, Mag) -> Fut, F: FnMut(T, ItemEntityId, Mag) -> Fut,
Fut: Future<Output=T>,
Fut: Future<Output=Result<T, ItemStateError>>,
{ {
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 {
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 { impl BankItem {
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> T
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where where
F: FnMut(T, ItemEntityId) -> Fut, F: FnMut(T, ItemEntityId) -> Fut,
Fut: Future<Output=T>,
Fut: Future<Output=Result<T, ItemStateError>>,
{ {
match &self.item { match &self.item {
BankItemDetail::Individual(individual_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) => { BankItemDetail::Stacked(stacked_item) => {
for entity_id in &stacked_item.entity_ids { 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 { impl FloorItem {
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> T
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where where
F: FnMut(T, ItemEntityId) -> Fut, F: FnMut(T, ItemEntityId) -> Fut,
Fut: Future<Output=T>,
Fut: Future<Output=Result<T, ItemStateError>>,
{ {
match &self.item { match &self.item {
FloorItemDetail::Individual(individual_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) => { FloorItemDetail::Stacked(stacked_item) => {
for entity_id in &stacked_item.entity_ids { for entity_id in &stacked_item.entity_ids {
param = func(param, *entity_id).await;
param = func(param, *entity_id).await?;
} }
}, },
FloorItemDetail::Meseta(_meseta) => {}, FloorItemDetail::Meseta(_meseta) => {},
} }
param
Ok(param)
} }
pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> T
pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> Result<T, ItemStateError>
where where
F: FnMut(T, ItemEntityId, Mag) -> Fut, F: FnMut(T, ItemEntityId, Mag) -> Fut,
Fut: Future<Output=T>,
Fut: Future<Output=Result<T, ItemStateError>>,
{ {
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 {
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] { pub fn as_client_bytes(&self) -> [u8; 16] {

Loading…
Cancel
Save