From b39ee6c0f378384914439118739793f5ebafdcf0 Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 29 Mar 2020 22:00:07 -0700 Subject: [PATCH] use Option for entity primary keys --- src/entity/account.rs | 38 ++++-- src/entity/character.rs | 10 +- src/entity/gateway/entitygateway.rs | 16 +-- src/entity/gateway/inmemory.rs | 96 +++++++-------- src/entity/item/mod.rs | 2 +- src/login/character.rs | 180 +++++++++++++++------------- src/login/login.rs | 8 +- src/main.rs | 149 +++++++---------------- src/ship/items.rs | 20 ++-- src/ship/ship.rs | 16 +-- 10 files changed, 248 insertions(+), 287 deletions(-) diff --git a/src/entity/account.rs b/src/entity/account.rs index 032d80b..1c3afbc 100644 --- a/src/entity/account.rs +++ b/src/entity/account.rs @@ -6,17 +6,16 @@ use libpso::character::guildcard; pub const USERFLAG_NEWCHAR: u32 = 0x00000001; pub const USERFLAG_DRESSINGROOM: u32 = 0x00000002; -// TOOD: these should not derive default -#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Default)] +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] pub struct UserAccountId(pub u32); -#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Default)] +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] pub struct UserSettingsId(pub u32); -#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Default)] +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] pub struct GuildCardDataId(pub u32); #[derive(Clone, Debug)] pub struct UserAccountEntity { - pub id: UserAccountId, + pub id: Option, pub username: String, pub password: String, pub guildcard: u32, @@ -27,16 +26,37 @@ pub struct UserAccountEntity { pub flags: u32, // TODO: is this used for anything other than character creation? } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug)] pub struct UserSettingsEntity { - pub id: UserSettingsId, + pub id: Option, pub user_id: UserAccountId, pub settings: settings::UserSettings, } -#[derive(Clone, Default)] +impl UserSettingsEntity { + pub fn new(user_id: UserAccountId) -> UserSettingsEntity { + UserSettingsEntity { + id: None, + user_id: user_id, + settings: settings::UserSettings::default(), + } + } +} + + +#[derive(Clone)] pub struct GuildCardDataEntity { - pub id: GuildCardDataId, + pub id: Option, pub user_id: UserAccountId, pub guildcard: guildcard::GuildCardData, } + +impl GuildCardDataEntity { + pub fn new(user_id: UserAccountId) -> GuildCardDataEntity { + GuildCardDataEntity { + id: None, + user_id: user_id, + guildcard: guildcard::GuildCardData::default(), + } + } +} diff --git a/src/entity/character.rs b/src/entity/character.rs index 3e49631..11f431a 100644 --- a/src/entity/character.rs +++ b/src/entity/character.rs @@ -234,7 +234,7 @@ pub struct CharacterEntityId(pub u32); #[derive(Clone)] pub struct CharacterEntity { - pub id: CharacterEntityId, + pub id: Option, pub user_id: UserAccountId, pub slot: u32, @@ -251,11 +251,11 @@ pub struct CharacterEntity { pub guildcard: CharacterGuildCard, } -impl std::default::Default for CharacterEntity { - fn default() -> CharacterEntity { +impl CharacterEntity { + pub fn new(user: UserAccountId) -> CharacterEntity { CharacterEntity { - id: CharacterEntityId(0), - user_id: UserAccountId(0), + id: None, + user_id: user, slot: 0, name: "".into(), exp: 0, diff --git a/src/entity/gateway/entitygateway.rs b/src/entity/gateway/entitygateway.rs index eb15535..3b90e08 100644 --- a/src/entity/gateway/entitygateway.rs +++ b/src/entity/gateway/entitygateway.rs @@ -13,7 +13,7 @@ pub trait EntityGateway { unimplemented!(); } - fn set_user(&mut self, _user: &UserAccountEntity) { + fn set_user(&mut self, _user: &mut UserAccountEntity) { unimplemented!(); } @@ -21,7 +21,7 @@ pub trait EntityGateway { unimplemented!(); } - fn create_user_settings_by_user(&self, _user: &UserAccountEntity) -> UserSettingsEntity { + fn set_user_settings(&mut self, _settings: &mut UserSettingsEntity) { unimplemented!(); } @@ -29,11 +29,7 @@ pub trait EntityGateway { unimplemented!(); } - fn set_character(&mut self, _char: &CharacterEntity) { - unimplemented!(); - } - - fn new_character_by_user(&mut self, _user: &UserAccountEntity) -> CharacterEntity { + fn set_character(&mut self, _char: &mut CharacterEntity) { unimplemented!(); } @@ -41,11 +37,7 @@ pub trait EntityGateway { unimplemented!(); } - fn new_item(&mut self, _item: ItemDetail, _location: ItemLocation) -> ItemEntity { - unimplemented!(); - } - - fn set_item(&self, _item: &ItemEntity) { + fn set_item(&mut self, _item: &mut ItemEntity) { unimplemented!(); } diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index 5e04f99..5f85b49 100644 --- a/src/entity/gateway/inmemory.rs +++ b/src/entity/gateway/inmemory.rs @@ -44,32 +44,37 @@ impl EntityGateway for InMemoryGateway { .map(|(_, k)| k.clone()) } - fn set_user(&mut self, user: &UserAccountEntity) { + fn set_user(&mut self, user: &mut UserAccountEntity) { let mut users = self.users.lock().unwrap(); - users.insert(user.id, user.clone()); + if let None = user.id { + let id = users + .iter() + .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) + + 1; + user.id = Some(UserAccountId(id)); + } + users.insert(user.id.unwrap(), user.clone()); } fn get_user_settings_by_user(&self, user: &UserAccountEntity) -> Option { let user_settings = self.user_settings.lock().unwrap(); user_settings .iter() - .find(|(_, k)| k.user_id == user.id) + .find(|(_, k)| k.user_id == user.id.unwrap()) .map(|(_, k)| k.clone()) } - fn create_user_settings_by_user(&self, user: &UserAccountEntity) -> UserSettingsEntity { + + fn set_user_settings(&mut self, user_setting: &mut UserSettingsEntity) { let mut user_settings = self.user_settings.lock().unwrap(); - let id = user_settings - .iter() - .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) - + 1; - let new_settings = UserSettingsEntity { - id: UserSettingsId(id), - user_id: user.id, - settings: settings::UserSettings::default(), - }; - user_settings.insert(new_settings.id, new_settings.clone()); - new_settings + if let None = user_setting.id { + let id = user_settings + .iter() + .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) + + 1; + user_setting.id = Some(UserSettingsId(id)); + } + user_settings.insert(user_setting.id.unwrap(), user_setting.clone()); } fn get_characters_by_user(&self, user: &UserAccountEntity) -> [Option; 4] { @@ -77,52 +82,37 @@ impl EntityGateway for InMemoryGateway { let mut chars = [None; 4]; characters .iter() - .filter(|(_, c)| c.user_id == user.id) + .filter(|(_, c)| c.user_id == user.id.unwrap()) .for_each(|(_, c)| chars[c.slot as usize] = Some(c.clone())); chars } - fn new_character_by_user(&mut self, user: &UserAccountEntity) -> CharacterEntity { - let mut characters = self.characters.lock().unwrap(); - let id = characters - .iter() - .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) - + 1; - - let mut c = CharacterEntity::default(); - c.id = CharacterEntityId(id); - c.user_id = user.id; - characters.insert(CharacterEntityId(id), c.clone()); - c - } - - fn set_character(&mut self, char: &CharacterEntity) { + fn set_character(&mut self, char: &mut CharacterEntity) { let mut characters = self.characters.lock().unwrap(); - characters.insert(char.id, char.clone()); + if let None = char.id { + let id = characters + .iter() + .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) + + 1; + char.id = Some(CharacterEntityId(id)); + } + characters.insert(char.id.unwrap(), char.clone()); } - fn get_guild_card_data_by_user(&self, _user: &UserAccountEntity) -> GuildCardDataEntity { - GuildCardDataEntity::default() + fn get_guild_card_data_by_user(&self, user: &UserAccountEntity) -> GuildCardDataEntity { + GuildCardDataEntity::new(user.id.unwrap()) } - fn new_item(&mut self, item: ItemDetail, location: ItemLocation) -> ItemEntity { + fn set_item(&mut self, item: &mut ItemEntity) { let mut items = self.items.lock().unwrap(); - let id = items - .iter() - .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) - + 1; - let new_item = ItemEntity { - id: ItemEntityId(id), - location: location, - item: item, - }; - items.insert(ItemEntityId(id), new_item.clone()); - new_item - } - - fn set_item(&self, item: &ItemEntity) { - let mut items = self.items.lock().unwrap(); - items.insert(item.id, item.clone()); + if let None = item.id { + let id = items + .iter() + .fold(0, |sum, (i, _)| std::cmp::max(sum, i.0)) + + 1; + item.id = Some(ItemEntityId(id)); + } + items.insert(item.id.unwrap(), item.clone()); } fn get_items_by_character(&self, character: &CharacterEntity) -> Vec { @@ -131,8 +121,8 @@ impl EntityGateway for InMemoryGateway { .iter() .filter(|(_, k)| { match k.location { - ItemLocation::Inventory{character_id, ..} => character_id == character.id, - ItemLocation::Bank{character_id, ..} => character_id == character.id, + ItemLocation::Inventory{character_id, ..} => character_id == character.id.unwrap(), + ItemLocation::Bank{character_id, ..} => character_id == character.id.unwrap(), _ => false } }) diff --git a/src/entity/item/mod.rs b/src/entity/item/mod.rs index 1cdb96b..aeb4a5c 100644 --- a/src/entity/item/mod.rs +++ b/src/entity/item/mod.rs @@ -88,7 +88,7 @@ impl ItemDetail { #[derive(Clone, Debug, PartialEq)] pub struct ItemEntity { - pub id: ItemEntityId, + pub id: Option, pub location: ItemLocation, pub item: ItemDetail, } diff --git a/src/login/character.rs b/src/login/character.rs index 71934f8..4a5cff5 100644 --- a/src/login/character.rs +++ b/src/login/character.rs @@ -16,8 +16,8 @@ use crate::common::leveltable::CharacterLevelTable; use libpso::{utf8_to_array, utf8_to_utf16_array}; use crate::entity::gateway::EntityGateway; -use crate::entity::account::{UserAccountEntity, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM}; -use crate::entity::item::{ItemDetail, ItemLocation}; +use crate::entity::account::{UserAccountEntity, UserSettingsEntity, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM}; +use crate::entity::item::{ItemEntity, ItemDetail, ItemLocation}; use crate::entity::item::weapon::Weapon; use crate::entity::item::armor::Armor; use crate::entity::item::tech::Technique; @@ -190,89 +190,101 @@ pub struct CharacterServerState { fn new_character(entity_gateway: &mut EG, user: &UserAccountEntity, preview: &CharacterPreview) { - let mut char = entity_gateway.new_character_by_user(&user); - new_character_from_preview(&mut char, preview); - match char.char_class { - CharacterClass::FOmar | CharacterClass::FOmarl| CharacterClass::FOnewm | CharacterClass::FOnewearl => char.techs.set_tech(Technique::Foie, TechLevel(1)), + //let mut character = entity_gateway.new_character_by_user(&user); + //new_character_from_preview(&mut char, preview); + let mut character = new_character_from_preview(user, preview); + match character.char_class { + CharacterClass::FOmar | CharacterClass::FOmarl| CharacterClass::FOnewm | CharacterClass::FOnewearl => character.techs.set_tech(Technique::Foie, TechLevel(1)), _ => {} } - entity_gateway.set_character(&char); + entity_gateway.set_character(&mut character); - let new_weapon = match char.char_class { + let new_weapon = match character.char_class { CharacterClass::HUmar | CharacterClass::HUnewearl | CharacterClass::HUcast | CharacterClass::HUcaseal => item::weapon::WeaponType::Saber, CharacterClass::RAmar | CharacterClass::RAmarl | CharacterClass::RAcast | CharacterClass::RAcaseal => item::weapon::WeaponType::Handgun, CharacterClass::FOmar | CharacterClass::FOmarl | CharacterClass::FOnewm | CharacterClass::FOnewearl => item::weapon::WeaponType::Cane, }; - entity_gateway.new_item( - ItemDetail::Weapon( - Weapon { - weapon: new_weapon, - grind: 0, - special: None, - attrs: [None; 3], - tekked: true, - }), - ItemLocation::Inventory { - character_id: char.id, - index: 0, - equipped: true, - }); - entity_gateway.new_item( - ItemDetail::Armor ( - Armor { - armor: item::armor::ArmorType::Frame, - dfp: 0, - evp: 0, - slots: 0, + entity_gateway.set_item( + &mut ItemEntity { + id: None, + item : ItemDetail::Weapon( + Weapon { + weapon: new_weapon, + grind: 0, + special: None, + attrs: [None; 3], + tekked: true, }), - ItemLocation::Inventory { - character_id: char.id, - index: 1, + location: ItemLocation::Inventory { + character_id: character.id.unwrap(), + index: 0, + equipped: true, + }}); + + entity_gateway.set_item( + &mut ItemEntity { + id: None, + item: ItemDetail::Armor ( + Armor { + armor: item::armor::ArmorType::Frame, + dfp: 0, + evp: 0, + slots: 0, + }), + location: ItemLocation::Inventory { + character_id: character.id.unwrap(), + index: 1, equipped: true, - }); - - entity_gateway.new_item( - ItemDetail::Mag( - Mag { - mag: MagType::Mag, - def: 500, - pow: 0, - dex: 0, - mnd: 0, - synchro: 20, - iq: 0, - photon_blast: [None; 3], - }), - ItemLocation::Inventory { - character_id: char.id, + }}); + + entity_gateway.set_item( + &mut ItemEntity { + id: None, + item: ItemDetail::Mag( + Mag { + mag: MagType::Mag, + def: 500, + pow: 0, + dex: 0, + mnd: 0, + synchro: 20, + iq: 0, + photon_blast: [None; 3], + }), + location: ItemLocation::Inventory { + character_id: character.id.unwrap(), index: 2, equipped: true, - }); + }}); for _ in 0..4 { - entity_gateway.new_item( - ItemDetail::Tool ( - Tool { - tool: item::tool::ToolType::Monomate, - }), - ItemLocation::Inventory { - character_id: char.id, - index: 3, - equipped: false, - }); - entity_gateway.new_item( - ItemDetail::Tool ( - Tool { - tool: item::tool::ToolType::Monofluid, - }), - ItemLocation::Inventory { - character_id: char.id, - index: 4, - equipped: false, - }); + entity_gateway.set_item( + &mut ItemEntity { + id: None, + item: ItemDetail::Tool ( + Tool { + tool: item::tool::ToolType::Monomate, + }), + location: ItemLocation::Inventory { + character_id: character.id.unwrap(), + index: 3, + equipped: false, + }}); + entity_gateway.set_item( + &mut ItemEntity { + id: None, + item: ItemDetail::Tool ( + Tool { + tool: item::tool::ToolType::Monofluid, + }), + location: ItemLocation::Inventory { + character_id: character.id.unwrap(), + index: 4, + equipped: false, + }}); } } @@ -333,7 +345,11 @@ impl CharacterServerState { // TODO: this should error (data should be added on account creation, why did I copy this silly sylv logic?) let settings = match self.entity_gateway.get_user_settings_by_user(&user) { Some(settings) => settings, - None => self.entity_gateway.create_user_settings_by_user(&user), + None => { + let mut user_settings = UserSettingsEntity::new(user.id.unwrap()); + self.entity_gateway.set_user_settings(&mut user_settings); + user_settings + } }; let pkt = SendKeyAndTeamSettings::new(settings.settings.key_config, @@ -421,7 +437,7 @@ impl CharacterServerState { let client = self.clients.get_mut(&id).ok_or(CharacterError::ClientNotFound(id))?; let mut user = client.user.as_mut().unwrap(); user.flags = setflag.flags; - self.entity_gateway.set_user(&user); + self.entity_gateway.set_user(&mut user); Ok(None.into_iter()) } @@ -459,7 +475,7 @@ impl CharacterServerState { client.session.action = SessionAction::SelectCharacter; client.session.character_slot = preview.slot as u8; user.flags = 0; - self.entity_gateway.set_user(&user); + self.entity_gateway.set_user(&mut user); Ok(vec![SendCharacterPacket::LoginResponse(LoginResponse::by_char_select(user.guildcard, user.team_id.unwrap_or(1), client.session)), @@ -552,7 +568,8 @@ impl ServerState for CharacterServerState { } -fn new_character_from_preview(character: &mut CharacterEntity, preview: &CharacterPreview) { +fn new_character_from_preview(user: &UserAccountEntity, preview: &CharacterPreview) -> CharacterEntity { + let mut character = CharacterEntity::new(user.id.unwrap()); character.slot = preview.slot; character.name = String::from_utf16_lossy(&preview.character.name).trim_matches(char::from(0)).into(); character.section_id = preview.character.section_id.into(); @@ -567,6 +584,7 @@ fn new_character_from_preview(character: &mut CharacterEntity, preview: &Charact character.appearance.hair_b = preview.character.hair_b; character.appearance.prop_x = preview.character.prop_x; character.appearance.prop_y = preview.character.prop_y; + character } @@ -636,7 +654,7 @@ impl<'a> SelectScreenCharacterBuilder<'a> { #[cfg(test)] mod test { use super::*; - use crate::entity::account::UserSettings; + use crate::entity::account::*; use libpso::character::{settings, character}; use std::time::SystemTime; use crate::entity::gateway::{InMemoryGateway}; @@ -647,10 +665,10 @@ mod test { } impl EntityGateway for TestData { - fn get_user_settings_by_user(&self, _user: &UserAccountEntity) -> Option { - Some(UserSettings { - id: 0, - user_id: 0, + fn get_user_settings_by_user(&self, user: &UserAccountEntity) -> Option { + Some(UserSettingsEntity { + id: Some(UserSettingsId(0)), + user_id: user.id.unwrap(), settings: settings::UserSettings::default() }) } @@ -659,7 +677,7 @@ mod test { let mut server = CharacterServerState::new(TestData {}); let mut clientstate = ClientState::new(); clientstate.user = Some(UserAccountEntity { - id: 1, + id: Some(UserAccountId(1)), username: "testuser".to_owned(), password: bcrypt::hash("mypassword", 5).unwrap(), guildcard: 0, @@ -701,7 +719,7 @@ mod test { let TestData = InMemoryGateway::new(); let mut fake_user = ClientState::new(); fake_user.user = Some(UserAccountEntity { - id: 3, + id: Some(UserAccountId(3)), username: "hi3".to_string(), password: bcrypt::hash("qwer", 5).unwrap(), guildcard: 3, @@ -715,7 +733,7 @@ mod test { let mut server = CharacterServerState::new(TestData.clone()); server.clients.insert(ClientId(1), fake_user.clone()); let mut send = server.handle(ClientId(1), &RecvCharacterPacket::SetFlag(SetFlag {flags: 1})).unwrap().collect::>(); - assert!(TestData.get_user_by_id(3).unwrap().flags == 1); + assert!(TestData.get_user_by_id(UserAccountId(3)).unwrap().flags == 1); send = server.handle(ClientId(1), &RecvCharacterPacket::CharacterPreview(CharacterPreview {slot: 1, character: character::SelectScreenCharacter { exp: 0, level: 0, diff --git a/src/login/login.rs b/src/login/login.rs index 6237dfe..349b745 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -14,7 +14,7 @@ use crate::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY}; use crate::common::serverstate::{SendServerPacket, RecvServerPacket, ServerState, OnConnect, ClientId}; use crate::entity::gateway::EntityGateway; -use crate::entity::account::UserAccountEntity; +use crate::entity::account::{UserAccountId, UserAccountEntity}; pub const LOGIN_PORT: u16 = 12000; @@ -175,7 +175,7 @@ mod test { fn get_user_by_name(&self, name: String) -> Option { assert!(name == "testuser"); Some(UserAccountEntity { - id: 1, + id: Some(UserAccountId(1)), username: "testuser".to_owned(), password: bcrypt::hash("mypassword", 5).unwrap(), guildcard: 0, @@ -255,7 +255,7 @@ mod test { fn get_user_by_name(&self, name: String) -> Option { assert!(name == "testuser"); Some(UserAccountEntity { - id: 1, + id: Some(UserAccountId(1)), username: "testuser".to_owned(), password: bcrypt::hash("notpassword", 5).unwrap(), guildcard: 0, @@ -298,7 +298,7 @@ mod test { fn get_user_by_name(&self, name: String) -> Option { assert!(name == "testuser"); Some(UserAccountEntity { - id: 1, + id: Some(UserAccountId(1)), username: "testuser".to_owned(), password: bcrypt::hash("mypassword", 5).unwrap(), guildcard: 0, diff --git a/src/main.rs b/src/main.rs index 4035914..a6a41f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,9 +18,10 @@ use patch::patch::{PatchServerState, generate_patch_tree, load_config, load_motd use login::login::LoginServerState; use login::character::CharacterServerState; use ship::ship::ShipServerState; -use entity::account::{UserAccountEntity, UserAccountId}; +use entity::account::{UserAccountEntity, UserSettingsEntity}; use entity::gateway::{EntityGateway, InMemoryGateway}; -use entity::item::ItemLocation; +use entity::character::CharacterEntity; +use entity::item::{ItemEntity, ItemDetail, ItemLocation}; use crate::entity::item; @@ -58,108 +59,48 @@ fn main() { setup_logger(); let mut entity_gateway = InMemoryGateway::new(); - let fake_user = UserAccountEntity { - id: UserAccountId(1), - username: "hi".to_string(), - password: bcrypt::hash("qwer", 5).unwrap(), - guildcard: 1u32, - team_id: None, - banned: false, - muted_until: SystemTime::now(), - created_at: SystemTime::now(), - flags: 0, - }; - entity_gateway.set_user(&fake_user); - entity_gateway.create_user_settings_by_user(&fake_user); - let mut character = entity_gateway.new_character_by_user(&fake_user); - character.name = "Test Char 1".into(); - entity_gateway.set_character(&character); - let mut character = entity_gateway.new_character_by_user(&fake_user); - character.slot = 2; - character.name = "\tE12345678".into(); - character.exp = 80000000; - entity_gateway.set_character(&character); - - entity_gateway.new_item( - item::ItemDetail::Weapon( - item::weapon::Weapon { - weapon: item::weapon::WeaponType::Handgun, - grind: 5, - special: None, - attrs: [None; 3], - tekked: true, - } - ), - ItemLocation::Inventory { - character_id: character.id, - index: 0, - equipped: true, - } - ); - - - let fake_user2 = UserAccountEntity { - id: UserAccountId(2), - username: "hi2".to_string(), - password: bcrypt::hash("qwer", 5).unwrap(), - guildcard: 2u32, - team_id: None, - banned: false, - muted_until: SystemTime::now(), - created_at: SystemTime::now(), - flags: 0, - }; - entity_gateway.set_user(&fake_user2); - entity_gateway.create_user_settings_by_user(&fake_user2); - let mut character = entity_gateway.new_character_by_user(&fake_user2); - character.name = "Test Char 3".into(); - entity_gateway.set_character(&character); - let mut character = entity_gateway.new_character_by_user(&fake_user2); - character.slot = 2; - character.name = "Test Char 4".into(); - entity_gateway.set_character(&character); - - let fake_user3 = UserAccountEntity { - id: UserAccountId(3), - username: "hi3".to_string(), - password: bcrypt::hash("qwer", 5).unwrap(), - guildcard: 3u32, - team_id: None, - banned: false, - muted_until: SystemTime::now(), - created_at: SystemTime::now(), - flags: 0, - }; - entity_gateway.set_user(&fake_user3); - entity_gateway.create_user_settings_by_user(&fake_user3); - let mut character = entity_gateway.new_character_by_user(&fake_user3); - character.name = "Test Char 5".into(); - entity_gateway.set_character(&character); - let mut character = entity_gateway.new_character_by_user(&fake_user3); - character.slot = 2; - character.name = "Test Char 6".into(); - entity_gateway.set_character(&character); - - let fake_user4 = UserAccountEntity { - id: UserAccountId(4), - username: "hi4".to_string(), - password: bcrypt::hash("qwer", 5).unwrap(), - guildcard: 4u32, - team_id: None, - banned: false, - muted_until: SystemTime::now(), - created_at: SystemTime::now(), - flags: 0, - }; - entity_gateway.set_user(&fake_user4); - entity_gateway.create_user_settings_by_user(&fake_user4); - let mut character = entity_gateway.new_character_by_user(&fake_user4); - character.name = "Test Char 7".into(); - entity_gateway.set_character(&character); - let mut character = entity_gateway.new_character_by_user(&fake_user4); - character.slot = 2; - character.name = "Test Char 8".into(); - entity_gateway.set_character(&character); + for i in 0..5 { + let mut fake_user = UserAccountEntity { + id: None, + username: if i == 0 { "hi".to_string() } else { format!("hi{}", i+1) }, + password: bcrypt::hash("qwer", 5).unwrap(), + guildcard: i + 1, + team_id: None, + banned: false, + muted_until: SystemTime::now(), + created_at: SystemTime::now(), + flags: 0, + }; + entity_gateway.set_user(&mut fake_user); + entity_gateway.set_user_settings(&mut UserSettingsEntity::new(fake_user.id.unwrap())); + let mut character = CharacterEntity::new(fake_user.id.unwrap()); + character.name = format!("Test Char {}", i*2); + entity_gateway.set_character(&mut character); + let mut character = CharacterEntity::new(fake_user.id.unwrap()); + character.slot = 2; + character.name = "\tE12345678".into(); + character.exp = 80000000; + entity_gateway.set_character(&mut character); + + entity_gateway.set_item( + &mut ItemEntity { + id: None, + item: ItemDetail::Weapon( + item::weapon::Weapon { + weapon: item::weapon::WeaponType::Handgun, + grind: 5, + special: None, + attrs: [None; 3], + tekked: true, + } + ), + location: ItemLocation::Inventory { + character_id: character.id.unwrap(), + index: 0, + equipped: true, + } + }); + } async_std::task::block_on(async move { let thread_entity_gateway = entity_gateway.clone(); diff --git a/src/ship/items.rs b/src/ship/items.rs index 77fd88f..617a699 100644 --- a/src/ship/items.rs +++ b/src/ship/items.rs @@ -178,7 +178,7 @@ mod test { #[test] fn test_stack_items() { let item1 = ItemEntity { - id: ItemEntityId(1), + id: Some(ItemEntityId(1)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 0, @@ -193,7 +193,7 @@ mod test { }) }; let item2 = ItemEntity { - id: ItemEntityId(2), + id: Some(ItemEntityId(2)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 1, @@ -204,7 +204,7 @@ mod test { }) }; let item3 = ItemEntity { - id: ItemEntityId(3), + id: Some(ItemEntityId(3)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 2, @@ -218,8 +218,8 @@ mod test { tekked: true, }) }; - let item4 = ItemEntity { - id: ItemEntityId(4), + let item4 = ItemEntity { + id: Some(ItemEntityId(4)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 1, @@ -230,7 +230,7 @@ mod test { }) }; let item5 = ItemEntity { - id: ItemEntityId(5), + id: Some(ItemEntityId(5)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 1, @@ -241,7 +241,7 @@ mod test { }) }; let item6 = ItemEntity { - id: ItemEntityId(6), + id: Some(ItemEntityId(6)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 3, @@ -256,7 +256,7 @@ mod test { }) }; let item7 = ItemEntity { - id: ItemEntityId(7), + id: Some(ItemEntityId(7)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 4, @@ -267,7 +267,7 @@ mod test { }) }; let item8 = ItemEntity { - id: ItemEntityId(8), + id: Some(ItemEntityId(8)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 4, @@ -278,7 +278,7 @@ mod test { }) }; let item9 = ItemEntity { - id: ItemEntityId(9), + id: Some(ItemEntityId(9)), location: ItemLocation::Inventory { character_id: CharacterEntityId(0), index: 4, diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 3ce13f8..fe1fcb4 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -171,7 +171,7 @@ impl ShipServerState { Ok(match get_login_status(&self.entity_gateway, pkt) { Ok(user) => { let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); - response.guildcard = user.id.0 as u32; + response.guildcard = user.id.unwrap().0 as u32; response.team_id = user.team_id.map_or(31, |ti| ti) as u32; let characters = self.entity_gateway.get_characters_by_user(&user); let character = characters @@ -233,7 +233,7 @@ impl ShipServerState { PlayerInfo { header: PlayerHeader { tag: 0x100, - guildcard: client.user.id.0, + guildcard: client.user.id.unwrap().0, _unknown1: [0; 5], client_id: room_client.index as u32, name: c.name, @@ -289,7 +289,7 @@ impl ShipServerState { playerinfo: PlayerInfo { header: PlayerHeader { tag: 0x100, - guildcard: client.user.id.0, + guildcard: client.user.id.unwrap().0, _unknown1: [0; 5], client_id: client_id as u32, name: c.name, @@ -336,7 +336,7 @@ impl ShipServerState { msg: GameMessage::GuildcardRecv(GuildcardRecv { client: guildcard_send.client, target: guildcard_send.target, - guildcard: client.user.id.0, + guildcard: client.user.id.unwrap().0, name: utf8_to_utf16_array!(client.character.name, 0x18), team: [0; 0x10], // TODO: teams not yet implemented desc: utf8_to_utf16_array!(client.character.guildcard.description, 0x58), @@ -365,7 +365,7 @@ impl ShipServerState { fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Result + Send>, ShipError> { let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; - let cmsg = PlayerChat::new(client.user.id.0, msg.message.clone()); + let cmsg = PlayerChat::new(client.user.id.unwrap().0, msg.message.clone()); Ok(Box::new(self.client_location.get_area_by_user(id).clients().iter() .map(move |client| { @@ -392,7 +392,7 @@ impl ShipServerState { let client = self.clients.get_mut(&id).unwrap();//.ok_or(ShipError::ClientNotFound(id)).unwrap(); let players = [PlayerHeader { tag: 0x00010000, - guildcard: client.user.id.0, + guildcard: client.user.id.unwrap().0, _unknown1: [0; 5], client_id: 0, name: libpso::utf8_to_utf16_array!(client.character.name, 16), @@ -434,7 +434,7 @@ impl ShipServerState { fn update_config(&mut self, id: ClientId, update_config: &UpdateConfig) -> Box + Send> { let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); client.character.config.update(update_config); - self.entity_gateway.set_character(&client.character); + self.entity_gateway.set_character(&mut client.character); Box::new(None.into_iter()) } @@ -460,7 +460,7 @@ impl ShipServerState { fn write_infoboard(&mut self, id: ClientId, new_infoboard: &WriteInfoboard) -> Box + Send> { let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); client.character.info_board.update_infoboard(new_infoboard); - self.entity_gateway.set_character(&client.character); + self.entity_gateway.set_character(&mut client.character); Box::new(None.into_iter()) }