Browse Source

use async mutexes in inmemorygateway and reimplement inmemorygateway transactions

pull/117/head
jake 2 years ago
parent
commit
d6c2e289ff
  1. 319
      src/entity/gateway/inmemory.rs

319
src/entity/gateway/inmemory.rs

@ -7,57 +7,199 @@ use crate::entity::character::*;
use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction, GatewayError}; use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction, GatewayError};
use crate::entity::item::*; use crate::entity::item::*;
use std::sync::{Arc, Mutex};
use async_std::sync::{Arc, Mutex};
// TODO: implement multiple banks // TODO: implement multiple banks
pub struct InMemoryGatewayTransaction<'a> { pub struct InMemoryGatewayTransaction<'a> {
working_gateway: InMemoryGateway, working_gateway: InMemoryGateway,
original_gateway: &'a mut InMemoryGateway, original_gateway: &'a mut InMemoryGateway,
} }
fn copy_if_needed<K, V>(working_table: &mut BTreeMap<K, V>, original_table: &BTreeMap<K, V>, key: K) -> Option<()>
where
K: Ord + Copy,
V: Clone,
{
if !working_table.contains_key(&key) {
working_table.insert(key, original_table.get(&key)?.clone());
}
Some(())
}
// functions here have been skipped as they are not used in transactions, add as needed
#[async_trait::async_trait] #[async_trait::async_trait]
impl<'a> EntityGatewayTransaction for InMemoryGatewayTransaction<'a> {
fn gateway(&mut self) -> &mut dyn EntityGateway {
&mut self.working_gateway
impl<'a> EntityGateway for InMemoryGatewayTransaction<'a> {
async fn create_user(&mut self, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError> {
self.working_gateway.create_user(user).await
} }
async fn commit(mut self: Box<Self>) -> Result<(), GatewayError> {
self.original_gateway.users.lock().unwrap().clear();
self.original_gateway.users.lock().unwrap().extend(self.working_gateway.users.lock().unwrap().clone());
async fn get_user_by_id(&mut self, id: UserAccountId) -> Result<UserAccountEntity, GatewayError> {
match self.working_gateway.get_user_by_id(id).await {
Ok(user) => Ok(user),
Err(_) => {
self.original_gateway.get_user_by_id(id).await
}
}
}
async fn get_user_by_name(&mut self, username: String) -> Result<UserAccountEntity, GatewayError> {
match self.working_gateway.get_user_by_name(username.clone()).await {
Ok(user) => Ok(user),
Err(_) => {
self.original_gateway.get_user_by_name(username).await
}
}
}
self.original_gateway.user_settings.lock().unwrap().clear();
self.original_gateway.user_settings.lock().unwrap().extend(self.working_gateway.user_settings.lock().unwrap().clone());
async fn save_user(&mut self, user: &UserAccountEntity) -> Result<(), GatewayError> {
self.working_gateway.save_user(user).await
}
self.original_gateway.characters.lock().unwrap().clear();
self.original_gateway.characters.lock().unwrap().extend(self.working_gateway.characters.lock().unwrap().clone());
async fn create_user_settings(&mut self, settings: NewUserSettingsEntity) -> Result<UserSettingsEntity, GatewayError> {
self.working_gateway.create_user_settings(settings).await
}
self.original_gateway.character_meseta.lock().unwrap().clear();
self.original_gateway.character_meseta.lock().unwrap().extend(self.working_gateway.character_meseta.lock().unwrap().clone());
async fn get_user_settings_by_user(&mut self, user: &UserAccountEntity) -> Result<UserSettingsEntity, GatewayError> {
match self.working_gateway.get_user_settings_by_user(user).await {
Ok(user) => Ok(user),
Err(_) => {
self.original_gateway.get_user_settings_by_user(user).await
}
}
}
self.original_gateway.bank_meseta.lock().unwrap().clear();
self.original_gateway.bank_meseta.lock().unwrap().extend(self.working_gateway.bank_meseta.lock().unwrap().clone());
async fn save_character(&mut self, char: &CharacterEntity) -> Result<(), GatewayError> {
copy_if_needed(&mut *self.working_gateway.characters.lock().await,
&*self.original_gateway.characters.lock().await,
char.id
);
self.working_gateway.save_character(char).await
}
self.original_gateway.items.lock().unwrap().clear();
self.original_gateway.items.lock().unwrap().extend(self.working_gateway.items.lock().unwrap().clone());
async fn create_item(&mut self, item: NewItemEntity) -> Result<ItemEntity, GatewayError> {
self.working_gateway.create_item(item).await
}
self.original_gateway.inventories.lock().unwrap().clear();
self.original_gateway.inventories.lock().unwrap().extend(self.working_gateway.inventories.lock().unwrap().clone());
async fn add_item_note(&mut self, item_id: &ItemEntityId, item_note: ItemNote) -> Result<(), GatewayError> {
self.working_gateway.add_item_note(item_id, item_note).await
}
self.original_gateway.banks.lock().unwrap().clear();
self.original_gateway.banks.lock().unwrap().extend(self.working_gateway.banks.lock().unwrap().clone());
async fn feed_mag(&mut self, mag_item_id: &ItemEntityId, tool_item_id: &ItemEntityId) -> Result<(), GatewayError> {
self.working_gateway.feed_mag(mag_item_id, tool_item_id).await
}
self.original_gateway.equips.lock().unwrap().clear();
self.original_gateway.equips.lock().unwrap().extend(self.working_gateway.equips.lock().unwrap().clone());
async fn change_mag_owner(&mut self, mag_item_id: &ItemEntityId, character: &CharacterEntity) -> Result<(), GatewayError> {
self.working_gateway.change_mag_owner(mag_item_id, character).await
}
self.original_gateway.mag_modifiers.lock().unwrap().clear();
self.original_gateway.mag_modifiers.lock().unwrap().extend(self.working_gateway.mag_modifiers.lock().unwrap().clone());
async fn use_mag_cell(&mut self, mag_item_id: &ItemEntityId, mag_cell_id: &ItemEntityId) -> Result<(), GatewayError> {
self.working_gateway.use_mag_cell(mag_item_id, mag_cell_id).await
}
self.original_gateway.weapon_modifiers.lock().unwrap().clear();
self.original_gateway.weapon_modifiers.lock().unwrap().extend(self.working_gateway.weapon_modifiers.lock().unwrap().clone());
async fn add_weapon_modifier(&mut self, item_id: &ItemEntityId, modifier: weapon::WeaponModifier) -> Result<(), GatewayError> {
self.working_gateway.add_weapon_modifier(item_id, modifier).await
}
self.original_gateway.trades.lock().unwrap().clear();
self.original_gateway.trades.lock().unwrap().extend(self.working_gateway.trades.lock().unwrap().clone());
async fn get_character_inventory(&mut self, char_id: &CharacterEntityId) -> Result<InventoryEntity, GatewayError> {
match self.working_gateway.get_character_inventory(char_id).await {
Ok(inventory) => Ok(inventory),
Err(_) => {
self.original_gateway.get_character_inventory(char_id).await
}
}
}
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, bank_name: &BankName) -> Result<BankEntity, GatewayError> {
match self.working_gateway.get_character_bank(char_id, bank_name).await {
Ok(bank) => Ok(bank),
Err(_) => {
self.original_gateway.get_character_bank(char_id, bank_name).await
}
}
}
async fn set_character_inventory(&mut self, char_id: &CharacterEntityId, inventory: &InventoryEntity) -> Result<(), GatewayError> {
self.working_gateway.set_character_inventory(char_id, inventory).await
}
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, bank_name: &BankName) -> Result<(), GatewayError> {
self.working_gateway.set_character_bank(char_id, bank, bank_name).await
}
async fn get_character_equips(&mut self, char_id: &CharacterEntityId) -> Result<EquippedEntity, GatewayError> {
match self.working_gateway.get_character_equips(char_id).await {
Ok(equips) => Ok(equips),
Err(_) => {
self.original_gateway.get_character_equips(char_id).await
}
}
}
async fn set_character_equips(&mut self, char_id: &CharacterEntityId, equipped: &EquippedEntity) -> Result<(), GatewayError> {
self.working_gateway.set_character_equips(char_id, equipped).await
}
async fn set_character_meseta(&mut self, char_id: &CharacterEntityId, meseta: Meseta) -> Result<(), GatewayError> {
self.working_gateway.set_character_meseta(char_id, meseta).await
}
async fn get_character_meseta(&mut self, char_id: &CharacterEntityId) -> Result<Meseta, GatewayError> {
match self.working_gateway.get_character_meseta(char_id).await {
Ok(meseta) => Ok(meseta),
Err(_) => {
self.original_gateway.get_character_meseta(char_id).await
}
}
}
async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName, meseta: Meseta) -> Result<(), GatewayError> {
self.working_gateway.set_bank_meseta(char_id, bank, meseta).await
}
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError> {
match self.working_gateway.get_bank_meseta(char_id, bank).await {
Ok(meseta) => Ok(meseta),
Err(_) => {
self.original_gateway.get_bank_meseta(char_id, bank).await
}
}
}
async fn create_trade(&mut self, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result<TradeEntity, GatewayError> {
self.working_gateway.create_trade(char_id1, char_id2).await
}
async fn set_character_playtime(&mut self, char_id: &CharacterEntityId, playtime: u32) -> Result<(), GatewayError> {
copy_if_needed(&mut *self.working_gateway.characters.lock().await,
&*self.original_gateway.characters.lock().await,
*char_id
);
self.working_gateway.set_character_playtime(char_id, playtime).await
}
}
#[async_trait::async_trait]
impl<'a> EntityGatewayTransaction for InMemoryGatewayTransaction<'a> {
fn gateway(&mut self) -> &mut dyn EntityGateway {
self
}
async fn commit(mut self: Box<Self>) -> Result<(), GatewayError> {
self.original_gateway.users.lock().await.extend(self.working_gateway.users.lock().await.clone());
self.original_gateway.user_settings.lock().await.extend(self.working_gateway.user_settings.lock().await.clone());
self.original_gateway.characters.lock().await.extend(self.working_gateway.characters.lock().await.clone());
self.original_gateway.character_meseta.lock().await.extend(self.working_gateway.character_meseta.lock().await.clone());
self.original_gateway.bank_meseta.lock().await.extend(self.working_gateway.bank_meseta.lock().await.clone());
self.original_gateway.items.lock().await.extend(self.working_gateway.items.lock().await.clone());
self.original_gateway.inventories.lock().await.extend(self.working_gateway.inventories.lock().await.clone());
self.original_gateway.banks.lock().await.extend(self.working_gateway.banks.lock().await.clone());
self.original_gateway.equips.lock().await.extend(self.working_gateway.equips.lock().await.clone());
self.original_gateway.mag_modifiers.lock().await.extend(self.working_gateway.mag_modifiers.lock().await.clone());
self.original_gateway.weapon_modifiers.lock().await.extend(self.working_gateway.weapon_modifiers.lock().await.clone());
self.original_gateway.trades.lock().await.extend(self.working_gateway.trades.lock().await.clone());
Ok(()) Ok(())
} }
@ -98,15 +240,16 @@ impl Default for InMemoryGateway {
} }
} }
impl InMemoryGateway {
fn apply_modifiers(&self, inventory: InventoryEntity ) -> InventoryEntity {
let items = self.items.lock().unwrap();
fn apply_modifiers(items: &BTreeMap<ItemEntityId, ItemEntity>,
weapon_modifiers: &BTreeMap<ItemEntityId, Vec<weapon::WeaponModifier>>,
mag_modifiers: &BTreeMap<ItemEntityId, Vec<mag::MagModifier>>,
inventory: InventoryEntity ) -> InventoryEntity {
let inventory_items = inventory.items.into_iter() let inventory_items = inventory.items.into_iter()
.map(|item| { .map(|item| {
item.map_individual(|mut item| { item.map_individual(|mut item| {
item.item = match item.item { item.item = match item.item {
ItemDetail::Weapon(mut weapon) => { ItemDetail::Weapon(mut weapon) => {
if let Some(weapon_modifiers) = self.weapon_modifiers.lock().unwrap().get(&item.id) {
if let Some(weapon_modifiers) = weapon_modifiers.get(&item.id) {
for weapon_modifier in weapon_modifiers.iter() { for weapon_modifier in weapon_modifiers.iter() {
weapon.apply_modifier(weapon_modifier); weapon.apply_modifier(weapon_modifier);
} }
@ -115,7 +258,7 @@ impl InMemoryGateway {
}, },
ItemDetail::Mag(mag) => { ItemDetail::Mag(mag) => {
let mut mag = mag::Mag::baby_mag(mag.color as u16); let mut mag = mag::Mag::baby_mag(mag.color as u16);
if let Some(mag_modifiers) = self.mag_modifiers.lock().unwrap().get(&item.id) {
if let Some(mag_modifiers) = mag_modifiers.get(&item.id) {
for mag_modifier in mag_modifiers.iter() { for mag_modifier in mag_modifiers.iter() {
match mag_modifier { match mag_modifier {
mag::MagModifier::FeedMag {food} => { mag::MagModifier::FeedMag {food} => {
@ -152,25 +295,24 @@ impl InMemoryGateway {
.collect(); .collect();
InventoryEntity::new(inventory_items) InventoryEntity::new(inventory_items)
} }
}
#[async_trait::async_trait] #[async_trait::async_trait]
impl EntityGateway for InMemoryGateway { impl EntityGateway for InMemoryGateway {
async fn transaction<'a>(&'a mut self) -> Result<Box<dyn EntityGatewayTransaction + 'a>, GatewayError> async fn transaction<'a>(&'a mut self) -> Result<Box<dyn EntityGatewayTransaction + 'a>, GatewayError>
{ {
let working_gateway = { let working_gateway = {
let users = self.users.lock().unwrap().clone();
let user_settings = self.user_settings.lock().unwrap().clone();
let characters = self.characters.lock().unwrap().clone();
let character_meseta = self.character_meseta.lock().unwrap().clone();
let bank_meseta = self.bank_meseta.lock().unwrap().clone();
let items = self.items.lock().unwrap().clone();
let inventories = self.inventories.lock().unwrap().clone();
let banks = self.banks.lock().unwrap().clone();
let equips = self.equips.lock().unwrap().clone();
let mag_modifiers = self.mag_modifiers.lock().unwrap().clone();
let weapon_modifiers = self.weapon_modifiers.lock().unwrap().clone();
let trades = self.trades.lock().unwrap().clone();
let users = self.users.lock().await.clone();
let user_settings = self.user_settings.lock().await.clone();
let characters = self.characters.lock().await.clone();
let character_meseta = self.character_meseta.lock().await.clone();
let bank_meseta = self.bank_meseta.lock().await.clone();
let items = self.items.lock().await.clone();
let inventories = self.inventories.lock().await.clone();
let banks = self.banks.lock().await.clone();
let equips = self.equips.lock().await.clone();
let mag_modifiers = self.mag_modifiers.lock().await.clone();
let weapon_modifiers = self.weapon_modifiers.lock().await.clone();
let trades = self.trades.lock().await.clone();
InMemoryGateway { InMemoryGateway {
users: Arc::new(Mutex::new(users)), users: Arc::new(Mutex::new(users)),
@ -202,18 +344,18 @@ impl EntityGateway for InMemoryGateway {
R: Send, R: Send,
E: From<GatewayError>, E: From<GatewayError>,
{ {
let users = self.users.lock().unwrap().clone();
let user_settings = self.user_settings.lock().unwrap().clone();
let characters = self.characters.lock().unwrap().clone();
let character_meseta = self.character_meseta.lock().unwrap().clone();
let bank_meseta = self.bank_meseta.lock().unwrap().clone();
let items = self.items.lock().unwrap().clone();
let inventories = self.inventories.lock().unwrap().clone();
let banks = self.banks.lock().unwrap().clone();
let equips = self.equips.lock().unwrap().clone();
let mag_modifiers = self.mag_modifiers.lock().unwrap().clone();
let weapon_modifiers = self.weapon_modifiers.lock().unwrap().clone();
let trades = self.trades.lock().unwrap().clone();
let users = self.users.lock().await.clone();
let user_settings = self.user_settings.lock().await.clone();
let characters = self.characters.lock().await.clone();
let character_meseta = self.character_meseta.lock().await.clone();
let bank_meseta = self.bank_meseta.lock().await.clone();
let items = self.items.lock().await.clone();
let inventories = self.inventories.lock().await.clone();
let banks = self.banks.lock().await.clone();
let equips = self.equips.lock().await.clone();
let mag_modifiers = self.mag_modifiers.lock().await.clone();
let weapon_modifiers = self.weapon_modifiers.lock().await.clone();
let trades = self.trades.lock().await.clone();
let working_gateway = InMemoryGateway { let working_gateway = InMemoryGateway {
users: Arc::new(Mutex::new(users)), users: Arc::new(Mutex::new(users)),
@ -242,7 +384,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn create_user(&mut self, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError> { async fn create_user(&mut self, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError> {
let mut users = self.users.lock().unwrap();
let mut users = self.users.lock().await;
let id = users let id = users
.iter() .iter()
.fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0))
@ -267,12 +409,12 @@ impl EntityGateway for InMemoryGateway {
} }
async fn get_user_by_id(&mut self, id: UserAccountId) -> Result<UserAccountEntity, GatewayError> { async fn get_user_by_id(&mut self, id: UserAccountId) -> Result<UserAccountEntity, GatewayError> {
let users = self.users.lock().unwrap();
let users = self.users.lock().await;
users.get(&id).cloned().ok_or(GatewayError::Error) users.get(&id).cloned().ok_or(GatewayError::Error)
} }
async fn get_user_by_name(&mut self, username: String) -> Result<UserAccountEntity, GatewayError> { async fn get_user_by_name(&mut self, username: String) -> Result<UserAccountEntity, GatewayError> {
let users = self.users.lock().unwrap();
let users = self.users.lock().await;
users users
.iter() .iter()
.find(|(_, k)| k.username == username) .find(|(_, k)| k.username == username)
@ -281,13 +423,13 @@ impl EntityGateway for InMemoryGateway {
} }
async fn save_user(&mut self, user: &UserAccountEntity) -> Result<(), GatewayError> { async fn save_user(&mut self, user: &UserAccountEntity) -> Result<(), GatewayError> {
let mut users = self.users.lock().unwrap();
let mut users = self.users.lock().await;
users.insert(user.id, user.clone()); users.insert(user.id, user.clone());
Ok(()) Ok(())
} }
async fn create_user_settings(&mut self, settings: NewUserSettingsEntity) -> Result<UserSettingsEntity, GatewayError> { async fn create_user_settings(&mut self, settings: NewUserSettingsEntity) -> Result<UserSettingsEntity, GatewayError> {
let mut user_settings = self.user_settings.lock().unwrap();
let mut user_settings = self.user_settings.lock().await;
let id = user_settings let id = user_settings
.iter() .iter()
.fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0))
@ -302,7 +444,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn get_user_settings_by_user(&mut self, user: &UserAccountEntity) -> Result<UserSettingsEntity, GatewayError> { async fn get_user_settings_by_user(&mut self, user: &UserAccountEntity) -> Result<UserSettingsEntity, GatewayError> {
let user_settings = self.user_settings.lock().unwrap();
let user_settings = self.user_settings.lock().await;
user_settings user_settings
.iter() .iter()
.find(|(_, k)| k.user_id == user.id) .find(|(_, k)| k.user_id == user.id)
@ -311,7 +453,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn get_characters_by_user(&mut self, user: &UserAccountEntity) -> Result<[Option<CharacterEntity>; 4], GatewayError> { async fn get_characters_by_user(&mut self, user: &UserAccountEntity) -> Result<[Option<CharacterEntity>; 4], GatewayError> {
let characters = self.characters.lock().unwrap();
let characters = self.characters.lock().await;
const NONE: Option<CharacterEntity> = None; const NONE: Option<CharacterEntity> = None;
let mut chars = [NONE; 4]; let mut chars = [NONE; 4];
characters characters
@ -322,7 +464,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn create_character(&mut self, character: NewCharacterEntity) -> Result<CharacterEntity, GatewayError> { async fn create_character(&mut self, character: NewCharacterEntity) -> Result<CharacterEntity, GatewayError> {
let mut characters = self.characters.lock().unwrap();
let mut characters = self.characters.lock().await;
let id = characters let id = characters
.iter() .iter()
.fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0))
@ -353,7 +495,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn save_character(&mut self, char: &CharacterEntity) -> Result<(), GatewayError> { async fn save_character(&mut self, char: &CharacterEntity) -> Result<(), GatewayError> {
let mut characters = self.characters.lock().unwrap();
let mut characters = self.characters.lock().await;
characters.insert(char.id, char.clone()); characters.insert(char.id, char.clone());
Ok(()) Ok(())
} }
@ -363,7 +505,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn create_item(&mut self, item: NewItemEntity) -> Result<ItemEntity, GatewayError> { async fn create_item(&mut self, item: NewItemEntity) -> Result<ItemEntity, GatewayError> {
let mut items = self.items.lock().unwrap();
let mut items = self.items.lock().await;
let id = items let id = items
.iter() .iter()
.fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0))
@ -381,7 +523,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn feed_mag(&mut self, mag_item_id: &ItemEntityId, tool_item_id: &ItemEntityId) -> Result<(), GatewayError> { async fn feed_mag(&mut self, mag_item_id: &ItemEntityId, tool_item_id: &ItemEntityId) -> Result<(), GatewayError> {
self.mag_modifiers.lock().unwrap()
self.mag_modifiers.lock().await
.entry(*mag_item_id) .entry(*mag_item_id)
.or_insert_with(Vec::new) .or_insert_with(Vec::new)
.push(mag::MagModifier::FeedMag { .push(mag::MagModifier::FeedMag {
@ -391,7 +533,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn change_mag_owner(&mut self, mag_item_id: &ItemEntityId, character: &CharacterEntity) -> Result<(), GatewayError> { async fn change_mag_owner(&mut self, mag_item_id: &ItemEntityId, character: &CharacterEntity) -> Result<(), GatewayError> {
self.mag_modifiers.lock().unwrap()
self.mag_modifiers.lock().await
.entry(*mag_item_id) .entry(*mag_item_id)
.or_insert_with(Vec::new) .or_insert_with(Vec::new)
.push(mag::MagModifier::OwnerChange(character.char_class, character.section_id)); .push(mag::MagModifier::OwnerChange(character.char_class, character.section_id));
@ -399,7 +541,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn use_mag_cell(&mut self, mag_item_id: &ItemEntityId, mag_cell_id: &ItemEntityId) -> Result<(), GatewayError> { async fn use_mag_cell(&mut self, mag_item_id: &ItemEntityId, mag_cell_id: &ItemEntityId) -> Result<(), GatewayError> {
self.mag_modifiers.lock().unwrap()
self.mag_modifiers.lock().await
.entry(*mag_item_id) .entry(*mag_item_id)
.or_insert_with(Vec::new) .or_insert_with(Vec::new)
.push(mag::MagModifier::MagCell(*mag_cell_id)); .push(mag::MagModifier::MagCell(*mag_cell_id));
@ -407,7 +549,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn add_weapon_modifier(&mut self, item_id: &ItemEntityId, modifier: weapon::WeaponModifier) -> Result<(), GatewayError> { async fn add_weapon_modifier(&mut self, item_id: &ItemEntityId, modifier: weapon::WeaponModifier) -> Result<(), GatewayError> {
self.weapon_modifiers.lock().unwrap()
self.weapon_modifiers.lock().await
.entry(*item_id) .entry(*item_id)
.or_insert_with(Vec::new) .or_insert_with(Vec::new)
.push(modifier); .push(modifier);
@ -415,17 +557,20 @@ impl EntityGateway for InMemoryGateway {
} }
async fn get_character_inventory(&mut self, char_id: &CharacterEntityId) -> Result<InventoryEntity, GatewayError> { async fn get_character_inventory(&mut self, char_id: &CharacterEntityId) -> Result<InventoryEntity, GatewayError> {
let inventories = self.inventories.lock().unwrap();
let inventories = self.inventories.lock().await;
let items = self.items.lock().await;
let weapon_modifiers = self.weapon_modifiers.lock().await;
let mag_modifiers = self.mag_modifiers.lock().await;
Ok(inventories Ok(inventories
.iter() .iter()
.find(|(id, _)| **id == *char_id) .find(|(id, _)| **id == *char_id)
.map(|(_, inv)| inv.clone()) .map(|(_, inv)| inv.clone())
.map(|inv| self.apply_modifiers(inv))
.map(|inv| apply_modifiers(&items, &weapon_modifiers, &mag_modifiers, inv))
.unwrap_or_default()) .unwrap_or_default())
} }
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, _bank_name: &BankName) -> Result<BankEntity, GatewayError> { async fn get_character_bank(&mut self, char_id: &CharacterEntityId, _bank_name: &BankName) -> Result<BankEntity, GatewayError> {
let banks = self.banks.lock().unwrap();
let banks = self.banks.lock().await;
Ok(banks Ok(banks
.iter() .iter()
.find(|(id, _)| **id == *char_id) .find(|(id, _)| **id == *char_id)
@ -434,20 +579,20 @@ 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().unwrap();
let mut inventories = self.inventories.lock().await;
inventories.insert(*char_id, inventory.clone()); inventories.insert(*char_id, inventory.clone());
Ok(()) Ok(())
} }
// TOOD: impl bank name // TOOD: impl bank name
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, _bank_name: &BankName) -> Result<(), GatewayError> { async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, _bank_name: &BankName) -> Result<(), GatewayError> {
let mut banks = self.banks.lock().unwrap();
let mut banks = self.banks.lock().await;
banks.insert(*char_id, bank.clone()); banks.insert(*char_id, bank.clone());
Ok(()) Ok(())
} }
async fn get_character_equips(&mut self, char_id: &CharacterEntityId) -> Result<EquippedEntity, GatewayError> { async fn get_character_equips(&mut self, char_id: &CharacterEntityId) -> Result<EquippedEntity, GatewayError> {
let equips = self.equips.lock().unwrap();
let equips = self.equips.lock().await;
Ok(equips Ok(equips
.iter() .iter()
.find(|(id, _)| **id == *char_id) .find(|(id, _)| **id == *char_id)
@ -456,19 +601,19 @@ impl EntityGateway for InMemoryGateway {
} }
async fn set_character_equips(&mut self, char_id: &CharacterEntityId, equipped: &EquippedEntity) -> Result<(), GatewayError> { async fn set_character_equips(&mut self, char_id: &CharacterEntityId, equipped: &EquippedEntity) -> Result<(), GatewayError> {
let mut equips = self.equips.lock().unwrap();
let mut equips = self.equips.lock().await;
equips.insert(*char_id, equipped.clone()); equips.insert(*char_id, equipped.clone());
Ok(()) Ok(())
} }
async fn set_character_meseta(&mut self, char_id: &CharacterEntityId, meseta: Meseta) -> Result<(), GatewayError> { async fn set_character_meseta(&mut self, char_id: &CharacterEntityId, meseta: Meseta) -> Result<(), GatewayError> {
let mut character_meseta = self.character_meseta.lock().unwrap();
let mut character_meseta = self.character_meseta.lock().await;
character_meseta.insert(*char_id, meseta); character_meseta.insert(*char_id, meseta);
Ok(()) Ok(())
} }
async fn get_character_meseta(&mut self, char_id: &CharacterEntityId) -> Result<Meseta, GatewayError> { async fn get_character_meseta(&mut self, char_id: &CharacterEntityId) -> Result<Meseta, GatewayError> {
let mut character_meseta = self.character_meseta.lock().unwrap();
let mut character_meseta = self.character_meseta.lock().await;
if let Some(meseta) = character_meseta.get_mut(char_id) { if let Some(meseta) = character_meseta.get_mut(char_id) {
Ok(*meseta) Ok(*meseta)
} }
@ -478,13 +623,13 @@ impl EntityGateway for InMemoryGateway {
} }
async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName, meseta: Meseta) -> Result<(), GatewayError> { async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName, meseta: Meseta) -> Result<(), GatewayError> {
let mut bank_meseta = self.bank_meseta.lock().unwrap();
let mut bank_meseta = self.bank_meseta.lock().await;
bank_meseta.insert((*char_id, bank.clone()), meseta); bank_meseta.insert((*char_id, bank.clone()), meseta);
Ok(()) Ok(())
} }
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError> { async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError> {
let mut bank_meseta = self.bank_meseta.lock().unwrap();
let mut bank_meseta = self.bank_meseta.lock().await;
if let Some(meseta) = bank_meseta.get_mut(&(*char_id, bank.clone())) { if let Some(meseta) = bank_meseta.get_mut(&(*char_id, bank.clone())) {
Ok(*meseta) Ok(*meseta)
} }
@ -494,7 +639,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn create_trade(&mut self, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result<TradeEntity, GatewayError> { async fn create_trade(&mut self, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result<TradeEntity, GatewayError> {
let mut trades = self.trades.lock().unwrap();
let mut trades = self.trades.lock().await;
let id = trades.len() as u32; let id = trades.len() as u32;
let new_trade = TradeEntity { let new_trade = TradeEntity {
id: TradeId(id), id: TradeId(id),
@ -506,7 +651,7 @@ impl EntityGateway for InMemoryGateway {
} }
async fn set_character_playtime(&mut self, char_id: &CharacterEntityId, playtime: u32) -> Result<(), GatewayError> { async fn set_character_playtime(&mut self, char_id: &CharacterEntityId, playtime: u32) -> Result<(), GatewayError> {
let mut characters = self.characters.lock().unwrap();
let mut characters = self.characters.lock().await;
if let Some(character) = characters.get_mut(char_id) { if let Some(character) = characters.get_mut(char_id) {
character.playtime = playtime; character.playtime = playtime;
Ok(()) Ok(())

Loading…
Cancel
Save