Browse Source

bankname -> bankidentifier

pull/130/head
jake 2 years ago
parent
commit
0bf84db189
  1. 6
      src/bin/main.rs
  2. 8
      src/entity/gateway/entitygateway.rs
  3. 139
      src/entity/gateway/inmemory.rs
  4. 12
      src/entity/gateway/postgres/models.rs
  5. 225
      src/entity/gateway/postgres/postgres.rs
  6. 10
      src/entity/item/mod.rs
  7. 6
      src/login/character.rs
  8. 2
      src/ship/client.rs
  9. 20
      src/ship/items/actions.rs
  10. 8
      src/ship/items/bank.rs
  11. 29
      src/ship/items/state.rs
  12. 4
      tests/common.rs
  13. 88
      tests/test_bank.rs
  14. 2
      tests/test_item_id.rs

6
src/bin/main.rs

@ -68,14 +68,14 @@ fn main() {
character.name = format!("Test Char {}", i*2); character.name = format!("Test Char {}", i*2);
let character = entity_gateway.create_character(character).await.unwrap(); let character = entity_gateway.create_character(character).await.unwrap();
entity_gateway.set_character_meseta(&character.id, item::Meseta(999999)).await.unwrap(); entity_gateway.set_character_meseta(&character.id, item::Meseta(999999)).await.unwrap();
entity_gateway.set_bank_meseta(&character.id, &item::BankName("".into()), item::Meseta(999999)).await.unwrap();
entity_gateway.set_bank_meseta(&character.id, &item::BankIdentifier::Character, item::Meseta(999999)).await.unwrap();
let mut character = NewCharacterEntity::new(fake_user.id); let mut character = NewCharacterEntity::new(fake_user.id);
character.slot = 2; character.slot = 2;
character.name = "ItemRefactor".into(); character.name = "ItemRefactor".into();
character.exp = 80000000; character.exp = 80000000;
let character = entity_gateway.create_character(character).await.unwrap(); let character = entity_gateway.create_character(character).await.unwrap();
entity_gateway.set_character_meseta(&character.id, item::Meseta(999999)).await.unwrap(); entity_gateway.set_character_meseta(&character.id, item::Meseta(999999)).await.unwrap();
entity_gateway.set_bank_meseta(&character.id, &item::BankName("".into()), item::Meseta(999999)).await.unwrap();
entity_gateway.set_bank_meseta(&character.id, &item::BankIdentifier::Character, item::Meseta(999999)).await.unwrap();
for _ in 0..3 { for _ in 0..3 {
entity_gateway.create_item( entity_gateway.create_item(
@ -329,7 +329,7 @@ fn main() {
item8_s.into(), item9_u0.into(), item10_u1.into(), item11_u2.into(), item12_u3.into(), item8_s.into(), item9_u0.into(), item10_u1.into(), item11_u2.into(), item12_u3.into(),
item13.into(), item14.into(), monomates.into()]); item13.into(), item14.into(), monomates.into()]);
entity_gateway.set_character_inventory(&character.id, &inventory).await.unwrap(); entity_gateway.set_character_inventory(&character.id, &inventory).await.unwrap();
entity_gateway.set_character_bank(&character.id, &item::BankEntity::default(), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&character.id, &item::BankEntity::default(), &item::BankIdentifier::Character).await.unwrap();
} }
info!("[patch] starting server"); info!("[patch] starting server");

8
src/entity/gateway/entitygateway.rs

@ -104,7 +104,7 @@ pub trait EntityGateway: Send + Sync {
unimplemented!(); unimplemented!();
} }
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_identifier: &BankIdentifier) -> Result<BankEntity, GatewayError> {
unimplemented!(); unimplemented!();
} }
@ -112,7 +112,7 @@ pub trait EntityGateway: Send + Sync {
unimplemented!(); unimplemented!();
} }
async fn set_character_bank(&mut self, _char_id: &CharacterEntityId, _inventory: &BankEntity, _bank_name: &BankName) -> Result<(), GatewayError> {
async fn set_character_bank(&mut self, _char_id: &CharacterEntityId, _bank: &BankEntity, _bank_identifier: &BankIdentifier) -> Result<(), GatewayError> {
unimplemented!(); unimplemented!();
} }
@ -132,11 +132,11 @@ pub trait EntityGateway: Send + Sync {
unimplemented!(); unimplemented!();
} }
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_identifier: &BankIdentifier) -> Result<Meseta, GatewayError> {
unimplemented!(); unimplemented!();
} }
async fn set_bank_meseta(&mut self, _char_id: &CharacterEntityId, _bank: &BankName, _amount: Meseta) -> Result<(), GatewayError> {
async fn set_bank_meseta(&mut self, _char_id: &CharacterEntityId, _bank_identifier: &BankIdentifier, _amount: Meseta) -> Result<(), GatewayError> {
unimplemented!(); unimplemented!();
} }

139
src/entity/gateway/inmemory.rs

@ -117,11 +117,11 @@ impl EntityGateway for InMemoryGatewayTransaction {
} }
} }
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 {
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<BankEntity, GatewayError> {
match self.working_gateway.get_character_bank(char_id, bank_identifier).await {
Ok(bank) => Ok(bank), Ok(bank) => Ok(bank),
Err(_) => { Err(_) => {
self.original_gateway.get_character_bank(char_id, bank_name).await
self.original_gateway.get_character_bank(char_id, bank_identifier).await
} }
} }
} }
@ -130,11 +130,10 @@ impl EntityGateway for InMemoryGatewayTransaction {
self.working_gateway.set_character_inventory(char_id, inventory).await 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 set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, bank_identifier: &BankIdentifier) -> Result<(), GatewayError> {
self.working_gateway.set_character_bank(char_id, bank, bank_identifier).await
} }
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> {
match self.working_gateway.get_character_equips(char_id).await { match self.working_gateway.get_character_equips(char_id).await {
Ok(equips) => Ok(equips), Ok(equips) => Ok(equips),
@ -161,15 +160,15 @@ impl EntityGateway for InMemoryGatewayTransaction {
} }
} }
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 set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier, meseta: Meseta) -> Result<(), GatewayError> {
self.working_gateway.set_bank_meseta(char_id, bank_identifier, 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 {
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<Meseta, GatewayError> {
match self.working_gateway.get_bank_meseta(char_id, bank_identifier).await {
Ok(meseta) => Ok(meseta), Ok(meseta) => Ok(meseta),
Err(_) => { Err(_) => {
self.original_gateway.get_bank_meseta(char_id, bank).await
self.original_gateway.get_bank_meseta(char_id, bank_identifier).await
} }
} }
} }
@ -203,7 +202,8 @@ impl EntityGatewayTransaction for InMemoryGatewayTransaction {
self.original_gateway.bank_meseta.lock().await.extend(self.working_gateway.bank_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.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.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.character_banks.lock().await.extend(self.working_gateway.character_banks.lock().await.clone());
self.original_gateway.shared_banks.lock().await.extend(self.working_gateway.shared_banks.lock().await.clone());
self.original_gateway.equips.lock().await.extend(self.working_gateway.equips.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.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.weapon_modifiers.lock().await.extend(self.working_gateway.weapon_modifiers.lock().await.clone());
@ -225,10 +225,12 @@ pub struct InMemoryGateway {
user_settings: Arc<Mutex<BTreeMap<UserSettingsId, UserSettingsEntity>>>, user_settings: Arc<Mutex<BTreeMap<UserSettingsId, UserSettingsEntity>>>,
characters: Arc<Mutex<BTreeMap<CharacterEntityId, CharacterEntity>>>, characters: Arc<Mutex<BTreeMap<CharacterEntityId, CharacterEntity>>>,
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, Meseta>>>,
shared_bank_meseta: Arc<Mutex<BTreeMap<(UserAccountId, BankName), Meseta>>>,
items: Arc<Mutex<BTreeMap<ItemEntityId, ItemEntity>>>, items: Arc<Mutex<BTreeMap<ItemEntityId, ItemEntity>>>,
inventories: Arc<Mutex<BTreeMap<CharacterEntityId, Vec<InventoryItemElement>>>>, inventories: Arc<Mutex<BTreeMap<CharacterEntityId, Vec<InventoryItemElement>>>>,
banks: Arc<Mutex<BTreeMap<CharacterEntityId, BankEntity>>>,
character_banks: Arc<Mutex<BTreeMap<CharacterEntityId, BankEntity>>>,
shared_banks: Arc<Mutex<BTreeMap<(UserAccountId, BankName), 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>>>>,
weapon_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<weapon::WeaponModifier>>>>, weapon_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<weapon::WeaponModifier>>>>,
@ -243,9 +245,11 @@ impl Default for InMemoryGateway {
characters: Arc::new(Mutex::new(BTreeMap::new())), characters: Arc::new(Mutex::new(BTreeMap::new())),
character_meseta: Arc::new(Mutex::new(BTreeMap::new())), character_meseta: Arc::new(Mutex::new(BTreeMap::new())),
bank_meseta: Arc::new(Mutex::new(BTreeMap::new())), bank_meseta: Arc::new(Mutex::new(BTreeMap::new())),
shared_bank_meseta: Arc::new(Mutex::new(BTreeMap::new())),
items: Arc::new(Mutex::new(BTreeMap::new())), items: Arc::new(Mutex::new(BTreeMap::new())),
inventories: Arc::new(Mutex::new(BTreeMap::new())), inventories: Arc::new(Mutex::new(BTreeMap::new())),
banks: Arc::new(Mutex::new(BTreeMap::new())),
character_banks: Arc::new(Mutex::new(BTreeMap::new())),
shared_banks: Arc::new(Mutex::new(BTreeMap::new())),
equips: Arc::new(Mutex::new(BTreeMap::new())), equips: Arc::new(Mutex::new(BTreeMap::new())),
mag_modifiers: Arc::new(Mutex::new(BTreeMap::new())), mag_modifiers: Arc::new(Mutex::new(BTreeMap::new())),
weapon_modifiers: Arc::new(Mutex::new(BTreeMap::new())), weapon_modifiers: Arc::new(Mutex::new(BTreeMap::new())),
@ -325,9 +329,11 @@ impl EntityGateway for InMemoryGateway {
let characters = self.characters.lock().await.clone(); let characters = self.characters.lock().await.clone();
let character_meseta = self.character_meseta.lock().await.clone(); let character_meseta = self.character_meseta.lock().await.clone();
let bank_meseta = self.bank_meseta.lock().await.clone(); let bank_meseta = self.bank_meseta.lock().await.clone();
let shared_bank_meseta = self.shared_bank_meseta.lock().await.clone();
let items = self.items.lock().await.clone(); let items = self.items.lock().await.clone();
let inventories = self.inventories.lock().await.clone(); let inventories = self.inventories.lock().await.clone();
let banks = self.banks.lock().await.clone();
let character_banks = self.character_banks.lock().await.clone();
let shared_banks = self.shared_banks.lock().await.clone();
let equips = self.equips.lock().await.clone(); let equips = self.equips.lock().await.clone();
let mag_modifiers = self.mag_modifiers.lock().await.clone(); let mag_modifiers = self.mag_modifiers.lock().await.clone();
let weapon_modifiers = self.weapon_modifiers.lock().await.clone(); let weapon_modifiers = self.weapon_modifiers.lock().await.clone();
@ -339,9 +345,11 @@ impl EntityGateway for InMemoryGateway {
characters: Arc::new(Mutex::new(characters)), characters: Arc::new(Mutex::new(characters)),
character_meseta: Arc::new(Mutex::new(character_meseta)), character_meseta: Arc::new(Mutex::new(character_meseta)),
bank_meseta: Arc::new(Mutex::new(bank_meseta)), bank_meseta: Arc::new(Mutex::new(bank_meseta)),
shared_bank_meseta: Arc::new(Mutex::new(shared_bank_meseta)),
items: Arc::new(Mutex::new(items)), items: Arc::new(Mutex::new(items)),
inventories: Arc::new(Mutex::new(inventories)), inventories: Arc::new(Mutex::new(inventories)),
banks: Arc::new(Mutex::new(banks)),
character_banks: Arc::new(Mutex::new(character_banks)),
shared_banks: Arc::new(Mutex::new(shared_banks)),
equips: Arc::new(Mutex::new(equips)), equips: Arc::new(Mutex::new(equips)),
mag_modifiers: Arc::new(Mutex::new(mag_modifiers)), mag_modifiers: Arc::new(Mutex::new(mag_modifiers)),
weapon_modifiers: Arc::new(Mutex::new(weapon_modifiers)), weapon_modifiers: Arc::new(Mutex::new(weapon_modifiers)),
@ -571,14 +579,34 @@ impl EntityGateway for InMemoryGateway {
.unwrap_or_default()) .unwrap_or_default())
} }
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, _bank_name: &BankName) -> Result<BankEntity, GatewayError> {
let banks = self.banks.lock().await;
Ok(banks
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<BankEntity, GatewayError> {
match bank_identifier {
BankIdentifier::Character => {
let character_banks = self.character_banks.lock().await;
Ok(character_banks
.iter()
.find(|(id, _)| **id == *char_id)
.map(|(_, b)| b.clone())
.unwrap_or_default())
},
BankIdentifier::Shared(bank_name) => {
let user_id = self.characters
.lock()
.await
.iter() .iter()
.find(|(id, _)| **id == *char_id) .find(|(id, _)| **id == *char_id)
.unwrap()
.1
.user_id;
let shared_banks = self.shared_banks.lock().await;
Ok(shared_banks
.iter()
.find(|((id, name), _)| *id == user_id && *name == *bank_name)
.map(|(_, b)| b.clone()) .map(|(_, b)| b.clone())
.unwrap_or_default()) .unwrap_or_default())
} }
}
}
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;
@ -604,10 +632,26 @@ impl EntityGateway for InMemoryGateway {
Ok(()) Ok(())
} }
// TOOD: impl bank name
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, _bank_name: &BankName) -> Result<(), GatewayError> {
let mut banks = self.banks.lock().await;
banks.insert(*char_id, bank.clone());
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, bank_identifier: &BankIdentifier) -> Result<(), GatewayError> {
match bank_identifier {
BankIdentifier::Character => {
let mut character_banks = self.character_banks.lock().await;
character_banks.insert(*char_id, bank.clone());
},
BankIdentifier::Shared(bank_name) => {
let user_id = self.characters
.lock()
.await
.iter()
.find(|(id, _)| **id == *char_id)
.unwrap()
.1
.user_id;
let mut shared_banks = self.shared_banks.lock().await;
shared_banks.insert((user_id, bank_name.clone()), bank.clone());
}
}
Ok(()) Ok(())
} }
@ -642,20 +686,59 @@ 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_identifier: &BankIdentifier, meseta: Meseta) -> Result<(), GatewayError> {
match bank_identifier {
BankIdentifier::Character => {
let mut bank_meseta = self.bank_meseta.lock().await; let mut bank_meseta = self.bank_meseta.lock().await;
bank_meseta.insert((*char_id, bank.clone()), meseta);
bank_meseta.insert(*char_id, meseta);
}
BankIdentifier::Shared(bank_name) => {
let user_id = self.characters
.lock()
.await
.iter()
.find(|(id, _)| **id == *char_id)
.unwrap()
.1
.user_id;
self.shared_bank_meseta
.lock()
.await
.insert((user_id, bank_name.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_identifier: &BankIdentifier) -> Result<Meseta, GatewayError> {
match bank_identifier {
BankIdentifier::Character => {
let mut bank_meseta = self.bank_meseta.lock().await; 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) {
Ok(*meseta) Ok(*meseta)
} }
else { else {
Err(GatewayError::Error) Err(GatewayError::Error)
} }
},
BankIdentifier::Shared(bank_name) => {
let mut shared_bank_meseta = self.shared_bank_meseta.lock().await;
let user_id = self.characters
.lock()
.await
.iter()
.find(|(id, _)| **id == *char_id)
.unwrap()
.1
.user_id;
if let Some(meseta) = shared_bank_meseta.get_mut(&(user_id, bank_name.clone())) {
Ok(*meseta)
}
else {
Err(GatewayError::Error)
}
}
}
} }
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> {

12
src/entity/gateway/postgres/models.rs

@ -607,11 +607,11 @@ pub enum PgItemNoteDetail {
}, },
Withdraw { Withdraw {
character_id: u32, character_id: u32,
bank: String,
bank: BankIdentifier,
}, },
Deposit { Deposit {
character_id: u32, character_id: u32,
bank: String,
bank: BankIdentifier,
} }
} }
@ -650,13 +650,13 @@ impl From<ItemNote> for PgItemNoteDetail {
ItemNote::Withdraw{character_id, bank} => { ItemNote::Withdraw{character_id, bank} => {
PgItemNoteDetail::Withdraw { PgItemNoteDetail::Withdraw {
character_id: character_id.0, character_id: character_id.0,
bank: bank.0,
bank: bank,
} }
}, },
ItemNote::Deposit{character_id, bank} => { ItemNote::Deposit{character_id, bank} => {
PgItemNoteDetail::Deposit { PgItemNoteDetail::Deposit {
character_id: character_id.0, character_id: character_id.0,
bank: bank.0,
bank: bank,
} }
} }
} }
@ -697,11 +697,11 @@ impl From<PgItemNoteDetail> for ItemNote {
}, },
PgItemNoteDetail::Withdraw{character_id, bank} => ItemNote::Withdraw { PgItemNoteDetail::Withdraw{character_id, bank} => ItemNote::Withdraw {
character_id: CharacterEntityId(character_id), character_id: CharacterEntityId(character_id),
bank: BankName(bank),
bank: bank,
}, },
PgItemNoteDetail::Deposit{character_id, bank} => ItemNote::Deposit { PgItemNoteDetail::Deposit{character_id, bank} => ItemNote::Deposit {
character_id: CharacterEntityId(character_id), character_id: CharacterEntityId(character_id),
bank: BankName(bank),
bank: bank,
}, },
} }
} }

225
src/entity/gateway/postgres/postgres.rs

@ -3,7 +3,7 @@
use std::convert::{From, TryFrom, Into}; use std::convert::{From, TryFrom, Into};
use futures::Future; use futures::Future;
use async_std::stream::StreamExt;
use futures::stream::{StreamExt, FuturesOrdered};
use async_std::sync::{Arc, Mutex}; use async_std::sync::{Arc, Mutex};
use libpso::character::guildcard; use libpso::character::guildcard;
use crate::entity::account::*; use crate::entity::account::*;
@ -93,7 +93,7 @@ async fn apply_item_modifications(conn: &mut sqlx::PgConnection, item: ItemEntit
.bind(id.0 as i32) .bind(id.0 as i32)
.fetch(conn); .fetch(conn);
weapon_modifiers.for_each(|modifier| {
weapon_modifiers.for_each(|modifier| async move {
if let Ok(modifier) = modifier { if let Ok(modifier) = modifier {
weapon.apply_modifier(&modifier.modifier); weapon.apply_modifier(&modifier.modifier);
} }
@ -101,7 +101,7 @@ async fn apply_item_modifications(conn: &mut sqlx::PgConnection, item: ItemEntit
ItemDetail::Weapon(weapon) ItemDetail::Weapon(weapon)
}, },
ItemDetail::Mag(mut mag) => {
ItemDetail::Mag(mag) => {
let q = r#"select mag, modifier, item.item -> 'Tool' as feed, item2.item -> 'Tool' as cell let q = r#"select mag, modifier, item.item -> 'Tool' as feed, item2.item -> 'Tool' as cell
from mag_modifier from mag_modifier
left join item on item.id = cast (modifier ->> 'FeedMag' as integer) left join item on item.id = cast (modifier ->> 'FeedMag' as integer)
@ -111,7 +111,7 @@ async fn apply_item_modifications(conn: &mut sqlx::PgConnection, item: ItemEntit
.bind(id.0 as i32) .bind(id.0 as i32)
.fetch(conn); .fetch(conn);
mag_modifiers.for_each(|modifier| {
let mag = mag_modifiers.fold(mag, |mut mag, modifier| async {
let PgMagModifierWithParameters {modifier, feed, cell, ..} = modifier.unwrap(); let PgMagModifierWithParameters {modifier, feed, cell, ..} = modifier.unwrap();
let modifier: mag::MagModifier = modifier.0.into(); let modifier: mag::MagModifier = modifier.0.into();
match modifier { match modifier {
@ -128,6 +128,8 @@ async fn apply_item_modifications(conn: &mut sqlx::PgConnection, item: ItemEntit
mag.change_owner(class, section_id) mag.change_owner(class, section_id)
}, },
} }
mag
}).await; }).await;
ItemDetail::Mag(mag) ItemDetail::Mag(mag)
@ -141,6 +143,31 @@ async fn apply_item_modifications(conn: &mut sqlx::PgConnection, item: ItemEntit
} }
} }
async fn fetch_item<T>(conn: &mut sqlx::PgConnection, item: PgInventoryItemEntity, individual: fn(ItemEntity) -> T, stacked: fn(Vec<ItemEntity>) -> T) -> Result<T, GatewayError>
{
match item {
PgInventoryItemEntity::Individual(item) => {
let entity = sqlx::query_as::<_, PgItemEntity>("select item.id, item.item from item where id = $1")
.bind(item)
.fetch_one(&mut *conn).await
.map(|item| item.into())
.map(|item| apply_item_modifications(&mut *conn, item))?
.await;
Ok(individual(entity))
},
PgInventoryItemEntity::Stacked(items) => {
let mut stacked_item = Vec::new();
for s_item in items {
stacked_item.push(sqlx::query_as::<_, PgItemEntity>("select item.id, item.item from item where id = $1")
.bind(s_item)
.fetch_one(&mut *conn).await
.map(|item| item.into())?)
}
Ok(stacked(stacked_item))
}
}
}
async fn create_user(conn: &mut sqlx::PgConnection, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError> async fn create_user(conn: &mut sqlx::PgConnection, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError>
{ {
let new_user = sqlx::query_as::<_, PgUserAccount>("insert into user_accounts (email, username, password, activated) values ($1, $2, $3, $4) returning *;") let new_user = sqlx::query_as::<_, PgUserAccount>("insert into user_accounts (email, username, password, activated) values ($1, $2, $3, $4) returning *;")
@ -280,7 +307,7 @@ async fn get_characters_by_user(conn: &mut sqlx::PgConnection, user: &UserAccoun
.bind(user.id.0) .bind(user.id.0)
.fetch(conn); .fetch(conn);
Ok(stream.fold(core::array::from_fn(|_| None), |mut acc, char| {
Ok(stream.fold(core::array::from_fn(|_| None), |mut acc, char| async move {
if let Ok(char) = char { if let Ok(char) = char {
let slot = char.slot as usize; let slot = char.slot as usize;
acc[slot] = Some(char.into()) acc[slot] = Some(char.into())
@ -391,76 +418,65 @@ async fn add_weapon_modifier(conn: &mut sqlx::PgConnection, item_id: &ItemEntity
async fn get_character_inventory(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId) -> Result<InventoryEntity, GatewayError> async fn get_character_inventory(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId) -> Result<InventoryEntity, GatewayError>
{ {
let mut t = conn.begin().await?;
// this is some degen shit
let conn = Arc::new(Mutex::new(conn.begin().await?));
let inventory = sqlx::query_as::<_, PgInventoryEntity>("select * from inventory where pchar = $1") let inventory = sqlx::query_as::<_, PgInventoryEntity>("select * from inventory where pchar = $1")
.bind(char_id.0) .bind(char_id.0)
.fetch_one(&mut t).await?;
.fetch_one(&mut **conn.lock().await).await?;
// TODO: inefficient
let mut real_inventory = Vec::new();
for inv_item in inventory.items.0.into_iter() {
match inv_item {
PgInventoryItemEntity::Individual(item) => {
let entity = sqlx::query_as::<_, PgItemEntity>("select item.id, item.item from item where id = $1")
.bind(item)
.fetch_one(&mut t).await
.map(|item| item.into())
.map(|item| apply_item_modifications(&mut t, item))?
.await;
real_inventory.push(InventoryItemEntity::Individual(entity));
},
PgInventoryItemEntity::Stacked(items) => {
let mut stacked_item = Vec::new();
for s_item in items {
stacked_item.push(sqlx::query_as::<_, PgItemEntity>("select item.id, item.item from item where id = $1")
.bind(s_item)
.fetch_one(&mut t).await
.map(|item| item.into())?)
Ok(InventoryEntity::new(
inventory.items.0
.into_iter()
.map(move |item| {
let conn = conn.clone();
async move {
fetch_item(&mut **conn.lock().await, item, InventoryItemEntity::Individual, InventoryItemEntity::Stacked).await
} }
real_inventory.push(InventoryItemEntity::Stacked(stacked_item));
}
}
}
Ok(InventoryEntity::new(real_inventory))
})
.collect::<FuturesOrdered<_>>()
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?))
} }
async fn get_character_bank(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, bank_name: &BankName) -> Result<BankEntity, GatewayError>
async fn get_character_bank(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<BankEntity, GatewayError>
{ {
let mut t = conn.begin().await?;
let bank = sqlx::query_as::<_, PgInventoryEntity>("select * from bank where pchar = $1 and name = $2")
// this is some degen shit
let conn = Arc::new(Mutex::new(conn.begin().await?));
let bank = match bank_identifier {
BankIdentifier::Character => {
sqlx::query_as::<_, PgInventoryEntity>("select * from bank where pchar = $1")
.bind(char_id.0) .bind(char_id.0)
.bind(bank_name.0.clone())
.fetch_one(&mut t).await?;
// TODO: inefficient
let mut real_bank = Vec::new();
for bank_item in bank.items.0.into_iter() {
match bank_item {
PgInventoryItemEntity::Individual(item) => {
let entity = sqlx::query_as::<_, PgItemEntity>("select item.id, item.item from item where id = $1")
.bind(item)
.fetch_one(&mut t).await
.map(|item| item.into())
.map(|item| apply_item_modifications(&mut t, item))?
.await;
real_bank.push(BankItemEntity::Individual(entity));
.fetch_one(&mut **conn.lock().await).await?
}, },
PgInventoryItemEntity::Stacked(items) => {
let mut stacked_item = Vec::new();
for s_item in items {
stacked_item.push(sqlx::query_as::<_, PgItemEntity>("select item.id, item.item from item where id = $1")
.bind(s_item)
.fetch_one(&mut t).await
.map(|item| item.into())
.map(|item| apply_item_modifications(&mut t, item))?
.await)
}
real_bank.push(BankItemEntity::Stacked(stacked_item));
}
}
BankIdentifier::Shared(bank_name) => {
todo!();
/*
sqlx::query_as::<_, PgInventoryEntity>("select shared_bank.* from shared_bank
left join player_character on shared_bank.user = player_character.user
where player_character.id = $1 and bank.name = $2")
.bind(char_id.0)
.bind(&bank_name.0)
.fetch_one(&mut **conn.lock().await).await?
*/
} }
};
Ok(BankEntity::new(real_bank))
Ok(BankEntity::new(
bank.items.0
.into_iter()
.map(move |item| {
let conn = conn.clone();
async move {
fetch_item(&mut **conn.lock().await, item, BankItemEntity::Individual, BankItemEntity::Stacked).await
}
})
.collect::<FuturesOrdered<_>>()
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?))
} }
async fn set_character_inventory(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, inventory: &InventoryEntity) -> Result<(), GatewayError> async fn set_character_inventory(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, inventory: &InventoryEntity) -> Result<(), GatewayError>
@ -486,8 +502,8 @@ async fn set_character_inventory(conn: &mut sqlx::PgConnection, char_id: &Charac
Ok(()) Ok(())
} }
async fn set_character_bank(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, bank: &BankEntity, bank_name: &BankName) -> Result<(), GatewayError>
{
async fn set_character_bank(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, bank: &BankEntity, bank_identifier: &BankIdentifier) -> Result<(), GatewayError> {
let bank = bank.items.iter() let bank = bank.items.iter()
.map(|item| { .map(|item| {
match item { match item {
@ -501,12 +517,25 @@ async fn set_character_bank(conn: &mut sqlx::PgConnection, char_id: &CharacterEn
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
sqlx::query("insert into bank (pchar, items, name) values ($1, $2, $3) on conflict (pchar, name) do update set items = $2")
match bank_identifier {
BankIdentifier::Character => {
sqlx::query("insert into bank (pchar, items) values ($1, $2) on conflict (pchar, name) do update set items = $2")
.bind(char_id.0) .bind(char_id.0)
.bind(sqlx::types::Json(bank)) .bind(sqlx::types::Json(bank))
.bind(bank_name.0.clone())
.execute(conn) .execute(conn)
.await?; .await?;
},
BankIdentifier::Shared(bank_name) => {
// TODO!
/*
sqlx::query("insert into shared_bank (pchar, items) values ($1, $2) on conflict (pchar, name) do update set items = $2")
.bind(char_id.0)
.bind(sqlx::types::Json(bank))
.execute(conn)
.await?;
*/
}
}
Ok(()) Ok(())
} }
@ -540,7 +569,7 @@ async fn set_character_equips(conn: &mut sqlx::PgConnection, char_id: &Character
async fn set_character_meseta(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, meseta: Meseta) -> Result<(), GatewayError> async fn set_character_meseta(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, meseta: Meseta) -> Result<(), GatewayError>
{ {
sqlx::query("insert into character_meseta values ($1, $2) on conflict (pchar) do update set meseta = $2")
sqlx::query("insert into character_meseta values ($1, '', $2) on conflict (pchar) do update set meseta = $2")
.bind(char_id.0) .bind(char_id.0)
.bind(meseta.0 as i32) .bind(meseta.0 as i32)
.execute(conn) .execute(conn)
@ -559,27 +588,41 @@ async fn get_character_meseta(conn: &mut sqlx::PgConnection, char_id: &Character
Ok(Meseta(meseta.0 as u32)) Ok(Meseta(meseta.0 as u32))
} }
async fn set_bank_meseta(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, bank: &BankName, meseta: Meseta) -> Result<(), GatewayError>
async fn set_bank_meseta(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier, meseta: Meseta) -> Result<(), GatewayError>
{ {
sqlx::query("insert into bank_meseta values ($1, $2, $3) on conflict (pchar, bank) do update set meseta = $3")
match bank_identifier {
BankIdentifier::Character => {
sqlx::query("insert into bank_meseta values ($1, '', $2) on conflict pchar do update set meseta = $2")
.bind(char_id.0) .bind(char_id.0)
.bind(bank.0.clone())
.bind(meseta.0 as i32) .bind(meseta.0 as i32)
.execute(conn) .execute(conn)
.await?; .await?;
},
BankIdentifier::Shared(bank_name) => {
todo!();
}
}
Ok(()) Ok(())
} }
async fn get_bank_meseta(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError>
async fn get_bank_meseta(conn: &mut sqlx::PgConnection, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<Meseta, GatewayError>
{ {
#[derive(sqlx::FromRow)] #[derive(sqlx::FromRow)]
struct PgMeseta(i32); struct PgMeseta(i32);
let meseta = sqlx::query_as::<_, PgMeseta>(r#"select meseta from bank_meseta where pchar = $1 and bank = $2"#)
match bank_identifier {
BankIdentifier::Character => {
let meseta = sqlx::query_as::<_, PgMeseta>(r#"select meseta from bank_meseta where pchar = $1"#)
.bind(char_id.0) .bind(char_id.0)
.bind(bank.0.clone())
.fetch_one(conn) .fetch_one(conn)
.await?; .await?;
Ok(Meseta(meseta.0 as u32)) Ok(Meseta(meseta.0 as u32))
},
BankIdentifier::Shared(bank_name) => {
todo!();
}
}
} }
async fn create_trade(conn: &mut sqlx::PgConnection, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result<TradeEntity, GatewayError> async fn create_trade(conn: &mut sqlx::PgConnection, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result<TradeEntity, GatewayError>
@ -696,16 +739,16 @@ impl<'t> EntityGateway for PostgresGateway<'t> {
get_character_inventory(&mut *self.pool.acquire().await?, char_id).await get_character_inventory(&mut *self.pool.acquire().await?, char_id).await
} }
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, bank_name: &BankName) -> Result<BankEntity, GatewayError> {
get_character_bank(&mut *self.pool.acquire().await?, char_id, bank_name).await
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<BankEntity, GatewayError> {
get_character_bank(&mut *self.pool.acquire().await?, char_id, bank_identifier).await
} }
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> {
set_character_inventory(&mut *self.pool.acquire().await?, char_id, inventory).await set_character_inventory(&mut *self.pool.acquire().await?, char_id, inventory).await
} }
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, bank_name: &BankName) -> Result<(), GatewayError> {
set_character_bank(&mut *self.pool.acquire().await?, char_id, bank, bank_name).await
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, bank_identifier: &BankIdentifier) -> Result<(), GatewayError> {
set_character_bank(&mut *self.pool.acquire().await?, char_id, bank, bank_identifier).await
} }
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> {
@ -724,12 +767,12 @@ impl<'t> EntityGateway for PostgresGateway<'t> {
get_character_meseta(&mut *self.pool.acquire().await?, char_id).await get_character_meseta(&mut *self.pool.acquire().await?, char_id).await
} }
async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName, meseta: Meseta) -> Result<(), GatewayError> {
set_bank_meseta(&mut *self.pool.acquire().await?, char_id, bank, meseta).await
async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier, meseta: Meseta) -> Result<(), GatewayError> {
set_bank_meseta(&mut *self.pool.acquire().await?, char_id, bank_identifier, meseta).await
} }
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError> {
get_bank_meseta(&mut *self.pool.acquire().await?, char_id, bank).await
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<Meseta, GatewayError> {
get_bank_meseta(&mut *self.pool.acquire().await?, char_id, bank_identifier).await
} }
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> {
@ -822,16 +865,16 @@ impl<'c> EntityGateway for PostgresTransaction<'c> {
get_character_inventory(&mut *self.pgtransaction.lock().await, char_id).await get_character_inventory(&mut *self.pgtransaction.lock().await, char_id).await
} }
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, bank_name: &BankName) -> Result<BankEntity, GatewayError> {
get_character_bank(&mut *self.pgtransaction.lock().await, char_id, bank_name).await
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<BankEntity, GatewayError> {
get_character_bank(&mut *self.pgtransaction.lock().await, char_id, bank_identifier).await
} }
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> {
set_character_inventory(&mut *self.pgtransaction.lock().await, char_id, inventory).await set_character_inventory(&mut *self.pgtransaction.lock().await, char_id, inventory).await
} }
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, bank_name: &BankName) -> Result<(), GatewayError> {
set_character_bank(&mut *self.pgtransaction.lock().await, char_id, bank, bank_name).await
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, bank_identifier: &BankIdentifier) -> Result<(), GatewayError> {
set_character_bank(&mut *self.pgtransaction.lock().await, char_id, bank, bank_identifier).await
} }
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> {
@ -850,12 +893,12 @@ impl<'c> EntityGateway for PostgresTransaction<'c> {
get_character_meseta(&mut *self.pgtransaction.lock().await, char_id).await get_character_meseta(&mut *self.pgtransaction.lock().await, char_id).await
} }
async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName, meseta: Meseta) -> Result<(), GatewayError> {
set_bank_meseta(&mut *self.pgtransaction.lock().await, char_id, bank, meseta).await
async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier, meseta: Meseta) -> Result<(), GatewayError> {
set_bank_meseta(&mut *self.pgtransaction.lock().await, char_id, bank_identifier, meseta).await
} }
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError> {
get_bank_meseta(&mut *self.pgtransaction.lock().await, char_id, bank).await
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank_identifier: &BankIdentifier) -> Result<Meseta, GatewayError> {
get_bank_meseta(&mut *self.pgtransaction.lock().await, char_id, bank_identifier).await
} }
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> {

10
src/entity/item/mod.rs

@ -22,6 +22,12 @@ pub struct BankName(pub String);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TradeId(pub u32); pub struct TradeId(pub u32);
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, derive_more::Display)]
pub enum BankIdentifier {
Character,
Shared(BankName),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ItemNote { pub enum ItemNote {
CharacterCreation { CharacterCreation {
@ -62,11 +68,11 @@ pub enum ItemNote {
}, },
Withdraw { Withdraw {
character_id: CharacterEntityId, character_id: CharacterEntityId,
bank: BankName,
bank: BankIdentifier,
}, },
Deposit { Deposit {
character_id: CharacterEntityId, character_id: CharacterEntityId,
bank: BankName,
bank: BankIdentifier,
}, },
} }

6
src/login/character.rs

@ -23,7 +23,7 @@ use libpso::{utf8_to_array, utf8_to_utf16_array};
use crate::entity::gateway::{EntityGateway, GatewayError}; use crate::entity::gateway::{EntityGateway, GatewayError};
use crate::entity::account::{UserAccountId, UserAccountEntity, NewUserSettingsEntity, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM}; use crate::entity::account::{UserAccountId, UserAccountEntity, NewUserSettingsEntity, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM};
use crate::entity::item::{NewItemEntity, ItemDetail, ItemNote, InventoryItemEntity, InventoryEntity, BankEntity, BankName, EquippedEntity, Meseta};
use crate::entity::item::{NewItemEntity, ItemDetail, ItemNote, InventoryItemEntity, InventoryEntity, BankEntity, BankName, BankIdentifier, EquippedEntity, Meseta};
use crate::entity::item::weapon::Weapon; use crate::entity::item::weapon::Weapon;
use crate::entity::item::armor::Armor; use crate::entity::item::armor::Armor;
use crate::entity::item::tech::Technique; use crate::entity::item::tech::Technique;
@ -211,7 +211,7 @@ async fn new_character<EG: EntityGateway + Clone>(entity_gateway: &mut EG, user:
let character = entity_gateway.create_character(character).await?; let character = entity_gateway.create_character(character).await?;
entity_gateway.set_character_meseta(&character.id, Meseta(300)).await?; entity_gateway.set_character_meseta(&character.id, Meseta(300)).await?;
entity_gateway.set_bank_meseta(&character.id, &BankName("".into()), Meseta(0)).await?;
entity_gateway.set_bank_meseta(&character.id, &BankIdentifier::Character, Meseta(0)).await?;
let new_weapon = match character.char_class { let new_weapon = match character.char_class {
CharacterClass::HUmar | CharacterClass::HUnewearl | CharacterClass::HUcast | CharacterClass::HUcaseal => item::weapon::WeaponType::Saber, CharacterClass::HUmar | CharacterClass::HUnewearl | CharacterClass::HUcast | CharacterClass::HUcaseal => item::weapon::WeaponType::Saber,
@ -305,7 +305,7 @@ async fn new_character<EG: EntityGateway + Clone>(entity_gateway: &mut EG, user:
InventoryItemEntity::Stacked(monomates), InventoryItemEntity::Stacked(monofluids)], InventoryItemEntity::Stacked(monomates), InventoryItemEntity::Stacked(monofluids)],
}; };
entity_gateway.set_character_inventory(&character.id, &inventory).await?; entity_gateway.set_character_inventory(&character.id, &inventory).await?;
entity_gateway.set_character_bank(&character.id, &BankEntity::default(), &BankName("".into())).await?;
entity_gateway.set_character_bank(&character.id, &BankEntity::default(), &BankIdentifier::Character).await?;
let equipped = EquippedEntity { let equipped = EquippedEntity {
weapon: Some(weapon.id), weapon: Some(weapon.id),
armor: Some(armor.id), armor: Some(armor.id),

2
src/ship/client.rs

@ -137,6 +137,7 @@ pub struct ClientState {
pub tek: Option<(items::ClientItemId, item::weapon::TekSpecialModifier, item::weapon::TekPercentModifier, i32)>, pub tek: Option<(items::ClientItemId, item::weapon::TekSpecialModifier, item::weapon::TekPercentModifier, i32)>,
pub character_playtime: chrono::Duration, pub character_playtime: chrono::Duration,
pub log_on_time: chrono::DateTime<chrono::Utc>, pub log_on_time: chrono::DateTime<chrono::Utc>,
pub current_bank: item::BankIdentifier,
} }
impl ClientState { impl ClientState {
@ -160,6 +161,7 @@ impl ClientState {
tek: None, tek: None,
character_playtime, character_playtime,
log_on_time: chrono::Utc::now(), log_on_time: chrono::Utc::now(),
current_bank: item::BankIdentifier::Character,
} }
} }

20
src/ship/items/actions.rs

@ -270,7 +270,7 @@ where
Box::pin(async move { Box::pin(async move {
let mut bank = item_state.bank(&character_id).await?; let mut bank = item_state.bank(&character_id).await?;
bank.remove_meseta(amount)?; bank.remove_meseta(amount)?;
transaction.gateway().set_bank_meseta(&character_id, &bank.name, bank.meseta).await?;
transaction.gateway().set_bank_meseta(&character_id, &bank.identifier, bank.meseta).await?;
item_state.set_bank(bank).await; item_state.set_bank(bank).await;
Ok(((item_state, transaction), ())) Ok(((item_state, transaction), ()))
@ -313,7 +313,7 @@ where
Box::pin(async move { Box::pin(async move {
let mut bank = item_state.bank(&character_id).await?; let mut bank = item_state.bank(&character_id).await?;
bank.add_meseta(amount)?; bank.add_meseta(amount)?;
transaction.gateway().set_bank_meseta(&character_id, &bank.name, bank.meseta).await?;
transaction.gateway().set_bank_meseta(&character_id, &bank.identifier, bank.meseta).await?;
item_state.set_bank(bank).await; item_state.set_bank(bank).await;
Ok(((item_state, transaction), ())) Ok(((item_state, transaction), ()))
@ -338,7 +338,7 @@ where
let item = bank.take_item(&item_id, amount) let item = bank.take_item(&item_id, amount)
.await .await
.ok_or_else(|| ItemStateError::NoBankItem(item_id))?; .ok_or_else(|| ItemStateError::NoBankItem(item_id))?;
transaction.gateway().set_character_bank(&character_id, &bank.as_bank_entity(), &bank.name).await?;
transaction.gateway().set_character_bank(&character_id, &bank.as_bank_entity(), &bank.identifier).await?;
item_state.set_bank(bank).await; item_state.set_bank(bank).await;
Ok(((item_state, transaction), item)) Ok(((item_state, transaction), item))
@ -358,16 +358,16 @@ where
move |(mut item_state, transaction), bank_item| { move |(mut item_state, transaction), bank_item| {
let character = character.clone(); let character = character.clone();
Box::pin(async move { Box::pin(async move {
let bank_name = item_state.bank(&character.id).await?.name;
let bank_identifier = item_state.bank(&character.id).await?.identifier;
let mut inventory = item_state.inventory(&character.id).await?; let mut inventory = item_state.inventory(&character.id).await?;
let character_id = character.id; let character_id = character.id;
let transaction = bank_item.with_entity_id(transaction, |mut transaction, entity_id| { let transaction = bank_item.with_entity_id(transaction, |mut transaction, entity_id| {
let bank_name = bank_name.clone();
let bank_identifier = bank_identifier.clone();
async move { async move {
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_identifier,
}).await?; }).await?;
Ok(transaction) Ok(transaction)
}}).await?; }}).await?;
@ -408,20 +408,20 @@ where
move |(mut item_state, transaction), inventory_item| { move |(mut item_state, transaction), inventory_item| {
Box::pin(async move { Box::pin(async move {
let mut bank = item_state.bank(&character_id).await?; let mut bank = item_state.bank(&character_id).await?;
let bank_name = bank.name.clone();
let bank_identifier = bank.identifier.clone();
let mut transaction = inventory_item.with_entity_id(transaction, move |mut transaction, entity_id| { let mut transaction = inventory_item.with_entity_id(transaction, move |mut transaction, entity_id| {
let bank_name = bank_name.clone();
let bank_identifier = bank_identifier.clone();
async move { async move {
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_identifier,
}).await?; }).await?;
Ok(transaction) Ok(transaction)
}}).await?; }}).await?;
bank.add_inventory_item(inventory_item)?; bank.add_inventory_item(inventory_item)?;
transaction.gateway().set_character_bank(&character_id, &bank.as_bank_entity(), &bank.name).await?;
transaction.gateway().set_character_bank(&character_id, &bank.as_bank_entity(), &bank.identifier).await?;
item_state.set_bank(bank).await; item_state.set_bank(bank).await;

8
src/ship/items/bank.rs

@ -6,6 +6,7 @@ use std::future::Future;
use async_std::sync::{Arc, Mutex}; use async_std::sync::{Arc, Mutex};
use crate::entity::character::CharacterEntityId; use crate::entity::character::CharacterEntityId;
use crate::entity::item::BankIdentifier;
use crate::ship::items::state::ItemStateError; use crate::ship::items::state::ItemStateError;
use crate::ship::items::state::{IndividualItemDetail, StackedItemDetail, AddItemResult}; use crate::ship::items::state::{IndividualItemDetail, StackedItemDetail, AddItemResult};
use crate::ship::items::inventory::{InventoryItem, InventoryItemDetail}; use crate::ship::items::inventory::{InventoryItem, InventoryItemDetail};
@ -22,6 +23,7 @@ pub enum BankError {
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum BankItemDetail { pub enum BankItemDetail {
Individual(IndividualItemDetail), Individual(IndividualItemDetail),
@ -98,7 +100,7 @@ impl Bank {
pub struct BankState { pub struct BankState {
pub character_id: CharacterEntityId, pub character_id: CharacterEntityId,
pub item_id_counter: Arc<Mutex<u32>>, pub item_id_counter: Arc<Mutex<u32>>,
pub name: BankName,
pub identifier: BankIdentifier,
pub bank: Bank, pub bank: Bank,
pub meseta: Meseta, pub meseta: Meseta,
} }
@ -112,12 +114,12 @@ async fn new_item_id(item_id_counter: &Arc<Mutex<u32>>) -> ClientItemId {
} }
impl BankState { impl BankState {
pub fn new(character_id: CharacterEntityId, name: BankName, mut bank: Bank, meseta: Meseta) -> BankState {
pub fn new(character_id: CharacterEntityId, identifier: BankIdentifier, mut bank: Bank, meseta: Meseta) -> BankState {
bank.0.sort(); bank.0.sort();
BankState { BankState {
character_id, character_id,
item_id_counter: Arc::new(Mutex::new(0)), item_id_counter: Arc::new(Mutex::new(0)),
name,
identifier,
bank, bank,
meseta, meseta,
} }

29
src/ship/items/state.rs

@ -5,7 +5,7 @@ use anyhow::Context;
use crate::entity::gateway::{EntityGateway, GatewayError}; use crate::entity::gateway::{EntityGateway, GatewayError};
use crate::entity::character::{CharacterEntity, CharacterEntityId}; use crate::entity::character::{CharacterEntity, CharacterEntityId};
use crate::entity::item::{ItemEntityId, ItemDetail, ItemEntity, InventoryItemEntity, BankItemEntity, BankName};
use crate::entity::item::{ItemEntityId, ItemDetail, ItemEntity, InventoryItemEntity, BankItemEntity, BankIdentifier};
use crate::entity::item::tool::Tool; use crate::entity::item::tool::Tool;
use crate::entity::item::weapon::Weapon; use crate::entity::item::weapon::Weapon;
use crate::entity::item::mag::Mag; use crate::entity::item::mag::Mag;
@ -218,9 +218,8 @@ impl ItemState {
Ok(ClientItemId(*self.room_item_id_counter.read().await)) Ok(ClientItemId(*self.room_item_id_counter.read().await))
} }
pub async fn load_character<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> Result<(), anyhow::Error> {
pub async fn load_character_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> Result<(), anyhow::Error> {
let inventory = entity_gateway.get_character_inventory(&character.id).await?; let inventory = entity_gateway.get_character_inventory(&character.id).await?;
let bank = entity_gateway.get_character_bank(&character.id, &BankName("".into())).await?;
let equipped = entity_gateway.get_character_equips(&character.id).await?; let equipped = entity_gateway.get_character_equips(&character.id).await?;
let inventory_items = inventory.items.into_iter() let inventory_items = inventory.items.into_iter()
@ -262,6 +261,15 @@ impl ItemState {
meseta: character_meseta, meseta: character_meseta,
}; };
self.character_inventory
.write()
.await
.insert(character.id, RwLock::new(inventory_state));
Ok(())
}
pub async fn load_character_bank<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity, bank_identifier: BankIdentifier) -> Result<(), anyhow::Error> {
let bank = entity_gateway.get_character_bank(&character.id, &bank_identifier).await?;
let bank_items = join_all( let bank_items = join_all(
bank.items.into_iter() bank.items.into_iter()
.map(|item| { .map(|item| {
@ -298,13 +306,9 @@ impl ItemState {
.into_iter() .into_iter()
.collect::<Result<Vec<_>, anyhow::Error>>()?; .collect::<Result<Vec<_>, anyhow::Error>>()?;
let bank_meseta = entity_gateway.get_bank_meseta(&character.id, &BankName("".into())).await?;
let bank_state = BankState::new(character.id, BankName("".into()), Bank::new(bank_items), bank_meseta);
let bank_meseta = entity_gateway.get_bank_meseta(&character.id, &bank_identifier).await?;
let bank_state = BankState::new(character.id, bank_identifier, Bank::new(bank_items), bank_meseta);
self.character_inventory
.write()
.await
.insert(character.id, RwLock::new(inventory_state));
self.character_bank self.character_bank
.write() .write()
.await .await
@ -312,6 +316,13 @@ impl ItemState {
Ok(()) Ok(())
} }
pub async fn load_character<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> Result<(), anyhow::Error> {
self.load_character_inventory(entity_gateway, character).await?;
self.load_character_bank(entity_gateway, character, BankIdentifier::Character).await?;
Ok(())
}
pub async fn add_character_to_room(&mut self, room_id: RoomId, character: &CharacterEntity, area_client: AreaClient) { pub async fn add_character_to_room(&mut self, room_id: RoomId, character: &CharacterEntity, area_client: AreaClient) {
let mut base_item_ids = self.room_gem_item_ids let mut base_item_ids = self.room_gem_item_ids
.write() .write()

4
tests/common.rs

@ -4,7 +4,7 @@ use elseware::common::serverstate::{ClientId, ServerState};
use elseware::entity::gateway::EntityGateway; use elseware::entity::gateway::EntityGateway;
use elseware::entity::account::{UserAccountEntity, NewUserAccountEntity, NewUserSettingsEntity}; use elseware::entity::account::{UserAccountEntity, NewUserAccountEntity, NewUserSettingsEntity};
use elseware::entity::character::{CharacterEntity, NewCharacterEntity}; use elseware::entity::character::{CharacterEntity, NewCharacterEntity};
use elseware::entity::item::{Meseta, BankName};
use elseware::entity::item::{Meseta, BankName, BankIdentifier};
use elseware::ship::ship::{ShipServerState, RecvShipPacket}; use elseware::ship::ship::{ShipServerState, RecvShipPacket};
use elseware::ship::room::Difficulty; use elseware::ship::room::Difficulty;
@ -30,7 +30,7 @@ pub async fn new_user_character<EG: EntityGateway + Clone>(entity_gateway: &mut
let new_character = NewCharacterEntity::new(user.id); let new_character = NewCharacterEntity::new(user.id);
let character = entity_gateway.create_character(new_character).await.unwrap(); let character = entity_gateway.create_character(new_character).await.unwrap();
entity_gateway.set_character_meseta(&character.id, Meseta(0)).await.unwrap(); entity_gateway.set_character_meseta(&character.id, Meseta(0)).await.unwrap();
entity_gateway.set_bank_meseta(&character.id, &BankName("".into()), Meseta(0)).await.unwrap();
entity_gateway.set_bank_meseta(&character.id, &BankIdentifier::Character, Meseta(0)).await.unwrap();
(user, character) (user, character)
} }

88
tests/test_bank.rs

@ -31,7 +31,7 @@ async fn test_bank_items_sent_in_character_login() {
), ),
}).await.unwrap(); }).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![item]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![item]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -69,7 +69,7 @@ async fn test_request_bank_items() {
}).await.unwrap()); }).await.unwrap());
} }
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -113,7 +113,7 @@ async fn test_request_stacked_bank_items() {
}).await.unwrap()); }).await.unwrap());
} }
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -178,7 +178,7 @@ async fn test_request_bank_items_sorted() {
}).await.unwrap(); }).await.unwrap();
let bank = vec![item::BankItemEntity::Individual(item1), vec![monomate].into(), item2.into()]; let bank = vec![item::BankItemEntity::Individual(item1), vec![monomate].into(), item2.into()];
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -275,7 +275,7 @@ async fn test_deposit_individual_item() {
assert_eq!(item.id, item::ItemEntityId(1)); assert_eq!(item.id, item::ItemEntityId(1));
}).unwrap(); }).unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
bank_items.items[0].with_individual(|item| { bank_items.items[0].with_individual(|item| {
assert_eq!(item.id, item::ItemEntityId(2)); assert_eq!(item.id, item::ItemEntityId(2));
@ -335,7 +335,7 @@ async fn test_deposit_stacked_item() {
&& player_no_longer_has_item.amount == 3 && player_no_longer_has_item.amount == 3
)); ));
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
bank_items.items[0].with_stacked(|items| { bank_items.items[0].with_stacked(|items| {
assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(), assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
@ -397,7 +397,7 @@ async fn test_deposit_partial_stacked_item() {
)); ));
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
bank_items.items[0].with_stacked(|items| { bank_items.items[0].with_stacked(|items| {
assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(), assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
@ -443,7 +443,7 @@ async fn test_deposit_stacked_item_with_stack_already_in_bank() {
} }
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -477,7 +477,7 @@ async fn test_deposit_stacked_item_with_stack_already_in_bank() {
&& player_no_longer_has_item.amount == 2 && player_no_longer_has_item.amount == 2
)); ));
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
bank_items.items[0].with_stacked(|items| { bank_items.items[0].with_stacked(|items| {
assert_eq!(items.iter().map(|i| i.id).collect::<BTreeSet<_>>(), assert_eq!(items.iter().map(|i| i.id).collect::<BTreeSet<_>>(),
@ -516,7 +516,7 @@ async fn test_deposit_stacked_item_with_full_stack_in_bank() {
} }
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -543,7 +543,7 @@ async fn test_deposit_stacked_item_with_full_stack_in_bank() {
assert!(packets.is_err()); assert!(packets.is_err());
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
bank_items.items[0].with_stacked(|items| { bank_items.items[0].with_stacked(|items| {
assert_eq!(items.len(), 10); assert_eq!(items.len(), 10);
@ -594,7 +594,7 @@ async fn test_deposit_individual_item_in_full_bank() {
} }
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -621,7 +621,7 @@ async fn test_deposit_individual_item_in_full_bank() {
assert!(packets.is_err()); assert!(packets.is_err());
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 200); assert_eq!(bank_items.items.len(), 200);
let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap(); let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
@ -666,7 +666,7 @@ async fn test_deposit_stacked_item_in_full_bank() {
} }
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(full_bank), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(full_bank), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -693,7 +693,7 @@ async fn test_deposit_stacked_item_in_full_bank() {
assert!(packets.is_err()); assert!(packets.is_err());
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 200); assert_eq!(bank_items.items.len(), 200);
let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap(); let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
@ -752,7 +752,7 @@ async fn test_deposit_stacked_item_in_full_bank_with_partial_stack() {
almost_full_bank.push(bank_monomates.into()); almost_full_bank.push(bank_monomates.into());
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(almost_full_bank), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(almost_full_bank), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -777,7 +777,7 @@ async fn test_deposit_stacked_item_in_full_bank_with_partial_stack() {
unknown: 0, unknown: 0,
})))).await.unwrap(); })))).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 200); assert_eq!(bank_items.items.len(), 200);
bank_items.items[199].with_stacked(|items| { bank_items.items[199].with_stacked(|items| {
assert_eq!(items.len(), 4); assert_eq!(items.len(), 4);
@ -818,7 +818,7 @@ async fn test_deposit_meseta() {
})))).await.unwrap(); })))).await.unwrap();
let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap(); let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankName("".into())).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert!(c1_meseta.0 == 277); assert!(c1_meseta.0 == 277);
assert!(c1_bank_meseta.0 == 23); assert!(c1_bank_meseta.0 == 23);
} }
@ -829,7 +829,7 @@ async fn test_deposit_too_much_meseta() {
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap(); entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankName("".into()), item::Meseta(999980)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(999980)).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -857,7 +857,7 @@ async fn test_deposit_too_much_meseta() {
assert!(packets.is_err()); assert!(packets.is_err());
let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap(); let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankName("".into())).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert!(c1_meseta.0 == 300); assert!(c1_meseta.0 == 300);
assert!(c1_bank_meseta.0 == 999980); assert!(c1_bank_meseta.0 == 999980);
} }
@ -868,7 +868,7 @@ async fn test_deposit_meseta_when_bank_is_maxed() {
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap(); entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankName("".into()), item::Meseta(999999)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(999999)).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -896,7 +896,7 @@ async fn test_deposit_meseta_when_bank_is_maxed() {
assert!(packets.is_err()); assert!(packets.is_err());
let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap(); let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankName("".into())).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert!(c1_meseta.0 == 300); assert!(c1_meseta.0 == 300);
assert!(c1_bank_meseta.0 == 999999); assert!(c1_bank_meseta.0 == 999999);
} }
@ -923,7 +923,7 @@ async fn test_withdraw_individual_item() {
), ),
}).await.unwrap()); }).await.unwrap());
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -983,7 +983,7 @@ async fn test_withdraw_stacked_item() {
}).await.unwrap()); }).await.unwrap());
} }
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1042,7 +1042,7 @@ async fn test_withdraw_partial_stacked_item() {
), ),
}).await.unwrap()); }).await.unwrap());
} }
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1075,7 +1075,7 @@ async fn test_withdraw_partial_stacked_item() {
if create_item.item_id == 0x20001 if create_item.item_id == 0x20001
)); ));
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
bank_items.items[0].with_stacked(|items| { bank_items.items[0].with_stacked(|items| {
assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(), assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
@ -1120,7 +1120,7 @@ async fn test_withdraw_stacked_item_with_stack_already_in_inventory() {
} }
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1153,7 +1153,7 @@ async fn test_withdraw_stacked_item_with_stack_already_in_inventory() {
if create_item.item_id == 0x20000 if create_item.item_id == 0x20000
)); ));
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 0); assert_eq!(bank_items.items.len(), 0);
let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap(); let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
@ -1195,7 +1195,7 @@ async fn test_withdraw_stacked_item_with_full_stack_in_inventory() {
} }
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1222,7 +1222,7 @@ async fn test_withdraw_stacked_item_with_full_stack_in_inventory() {
assert!(packets.is_err()); assert!(packets.is_err());
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
bank_items.items[0].with_stacked(|items| { bank_items.items[0].with_stacked(|items| {
assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(), assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
@ -1273,7 +1273,7 @@ async fn test_withdraw_individual_item_in_full_inventory() {
} }
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1299,7 +1299,7 @@ async fn test_withdraw_individual_item_in_full_inventory() {
})))).await; })))).await;
assert!(packets.is_err()); assert!(packets.is_err());
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap(); let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
@ -1341,7 +1341,7 @@ async fn test_withdraw_stacked_item_in_full_inventory() {
} }
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1369,7 +1369,7 @@ async fn test_withdraw_stacked_item_in_full_inventory() {
assert!(packets.is_err()); assert!(packets.is_err());
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert_eq!(bank_items.items.len(), 1); assert_eq!(bank_items.items.len(), 1);
bank_items.items[0].with_stacked(|items| { bank_items.items[0].with_stacked(|items| {
assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(), assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
@ -1397,7 +1397,7 @@ async fn test_withdraw_stacked_item_in_full_inventory_with_partial_stack() {
), ),
}).await.unwrap()); }).await.unwrap());
} }
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_item]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_item]), &item::BankIdentifier::Character).await.unwrap();
let mut items = Vec::new(); let mut items = Vec::new();
for _i in 0..29usize { for _i in 0..29usize {
@ -1453,7 +1453,7 @@ async fn test_withdraw_stacked_item_in_full_inventory_with_partial_stack() {
unknown: 0, unknown: 0,
})))).await.unwrap(); })))).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankName("".into())).await.unwrap();
let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert!(bank_items.items.len() == 0); assert!(bank_items.items.len() == 0);
let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap(); let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
@ -1471,7 +1471,7 @@ async fn test_withdraw_meseta() {
let mut entity_gateway = InMemoryGateway::default(); let mut entity_gateway = InMemoryGateway::default();
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
entity_gateway.set_bank_meseta(&char1.id, &item::BankName("".into()), item::Meseta(300)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1497,7 +1497,7 @@ async fn test_withdraw_meseta() {
})))).await.unwrap(); })))).await.unwrap();
let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap(); let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankName("".into())).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert!(c1_meseta.0 == 23); assert!(c1_meseta.0 == 23);
assert!(c1_bank_meseta.0 == 277); assert!(c1_bank_meseta.0 == 277);
} }
@ -1508,7 +1508,7 @@ async fn test_withdraw_too_much_meseta() {
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
entity_gateway.set_character_meseta(&char1.id, item::Meseta(999980)).await.unwrap(); entity_gateway.set_character_meseta(&char1.id, item::Meseta(999980)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankName("".into()), item::Meseta(300)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1536,7 +1536,7 @@ async fn test_withdraw_too_much_meseta() {
assert!(packet.is_err()); assert!(packet.is_err());
let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap(); let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankName("".into())).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert!(c1_meseta.0 == 999980); assert!(c1_meseta.0 == 999980);
assert!(c1_bank_meseta.0 == 300); assert!(c1_bank_meseta.0 == 300);
} }
@ -1547,7 +1547,7 @@ async fn test_withdraw_meseta_inventory_is_maxed() {
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
entity_gateway.set_character_meseta(&char1.id, item::Meseta(999999)).await.unwrap(); entity_gateway.set_character_meseta(&char1.id, item::Meseta(999999)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankName("".into()), item::Meseta(300)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1575,7 +1575,7 @@ async fn test_withdraw_meseta_inventory_is_maxed() {
assert!(packet.is_err()); assert!(packet.is_err());
let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap(); let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankName("".into())).await.unwrap();
let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
assert!(c1_meseta.0 == 999999); assert!(c1_meseta.0 == 999999);
assert!(c1_bank_meseta.0 == 300); assert!(c1_bank_meseta.0 == 300);
} }
@ -1588,7 +1588,7 @@ async fn test_withdraw_meseta_and_buy_a_few_monomates_with_it() {
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
entity_gateway.set_character_meseta(&char1.id, item::Meseta(100)).await.unwrap(); entity_gateway.set_character_meseta(&char1.id, item::Meseta(100)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankName("".into()), item::Meseta(300)).await.unwrap();
entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())
@ -1629,7 +1629,7 @@ async fn test_withdraw_meseta_and_buy_a_few_monomates_with_it() {
})))).await.unwrap(); })))).await.unwrap();
//let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap(); //let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
//let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankName("".into())).await.unwrap();
//let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
//assert!(c1_meseta.0 == 23); //assert!(c1_meseta.0 == 23);
//assert!(c1_bank_meseta.0 == 277); //assert!(c1_bank_meseta.0 == 277);
} }

2
tests/test_item_id.rs

@ -330,7 +330,7 @@ async fn test_depositing_a_full_stack_then_withdrawing_part() {
), ),
}).await.unwrap()); }).await.unwrap());
} }
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankName("".into())).await.unwrap();
entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
let mut ship = Box::new(ShipServerState::builder() let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone()) .gateway(entity_gateway.clone())

Loading…
Cancel
Save