Browse Source

update inmemory gateway to reflect how inventory actually works

pull/128/head
jake 2 years ago
parent
commit
6a2703ed6c
  1. 52
      src/entity/gateway/inmemory.rs

52
src/entity/gateway/inmemory.rs

@ -213,6 +213,12 @@ impl EntityGatewayTransaction for InMemoryGatewayTransaction {
} }
} }
#[derive(Clone)]
enum InventoryItemElement {
Individual(ItemEntityId),
Stacked(Vec<ItemEntityId>),
}
#[derive(Clone)] #[derive(Clone)]
pub struct InMemoryGateway { pub struct InMemoryGateway {
users: Arc<Mutex<BTreeMap<UserAccountId, UserAccountEntity>>>, users: Arc<Mutex<BTreeMap<UserAccountId, UserAccountEntity>>>,
@ -221,7 +227,7 @@ pub struct InMemoryGateway {
character_meseta: Arc<Mutex<BTreeMap<CharacterEntityId, Meseta>>>, character_meseta: Arc<Mutex<BTreeMap<CharacterEntityId, Meseta>>>,
bank_meseta: Arc<Mutex<BTreeMap<(CharacterEntityId, BankName), Meseta>>>, bank_meseta: Arc<Mutex<BTreeMap<(CharacterEntityId, BankName), Meseta>>>,
items: Arc<Mutex<BTreeMap<ItemEntityId, ItemEntity>>>, items: Arc<Mutex<BTreeMap<ItemEntityId, ItemEntity>>>,
inventories: Arc<Mutex<BTreeMap<CharacterEntityId, InventoryEntity>>>,
inventories: Arc<Mutex<BTreeMap<CharacterEntityId, Vec<InventoryItemElement>>>>,
banks: Arc<Mutex<BTreeMap<CharacterEntityId, BankEntity>>>, banks: Arc<Mutex<BTreeMap<CharacterEntityId, BankEntity>>>,
equips: Arc<Mutex<BTreeMap<CharacterEntityId, EquippedEntity>>>, equips: Arc<Mutex<BTreeMap<CharacterEntityId, EquippedEntity>>>,
mag_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<mag::MagModifier>>>>, mag_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<mag::MagModifier>>>>,
@ -538,7 +544,29 @@ impl EntityGateway for InMemoryGateway {
Ok(inventories Ok(inventories
.iter() .iter()
.find(|(id, _)| **id == *char_id) .find(|(id, _)| **id == *char_id)
.map(|(_, inv)| inv.clone())
.map(|(_, inv)| {
InventoryEntity {
items: inv
.iter()
.map(|inv_item_id| {
match inv_item_id {
InventoryItemElement::Individual(individual_id) => {
InventoryItemEntity::Individual(items.get(individual_id).unwrap().clone())
},
InventoryItemElement::Stacked(stacked_ids) => {
InventoryItemEntity::Stacked(
stacked_ids.iter()
.map(|stacked_id| {
items.get(stacked_id).unwrap().clone()
})
.collect()
)
}
}
})
.collect()
}
})
.map(|inv| apply_modifiers(&items, &weapon_modifiers, &mag_modifiers, inv)) .map(|inv| apply_modifiers(&items, &weapon_modifiers, &mag_modifiers, inv))
.unwrap_or_default()) .unwrap_or_default())
} }
@ -554,7 +582,25 @@ impl EntityGateway for InMemoryGateway {
async fn set_character_inventory(&mut self, char_id: &CharacterEntityId, inventory: &InventoryEntity) -> Result<(), GatewayError> { async fn set_character_inventory(&mut self, char_id: &CharacterEntityId, inventory: &InventoryEntity) -> Result<(), GatewayError> {
let mut inventories = self.inventories.lock().await; let mut inventories = self.inventories.lock().await;
inventories.insert(*char_id, inventory.clone());
inventories.insert(*char_id, inventory.items
.iter()
.map(|inventory_item| {
match inventory_item {
InventoryItemEntity::Individual(individual) => {
InventoryItemElement::Individual(individual.id)
},
InventoryItemEntity::Stacked(stacked) => {
InventoryItemElement::Stacked(
stacked.iter()
.map(|stacked| {
stacked.id
})
.collect()
)
}
}
})
.collect());
Ok(()) Ok(())
} }

Loading…
Cancel
Save