Browse Source

in memory entity access, change entitygateway api a bit

pbs
jake 5 years ago
parent
commit
9ebabca1bb
  1. 2
      src/entity/account.rs
  2. 5
      src/entity/character.rs
  3. 6
      src/entity/gateway/entitygateway.rs
  4. 103
      src/entity/gateway/inmemory.rs
  5. 2
      src/entity/gateway/mod.rs
  6. 6
      src/login/character.rs
  7. 5
      src/login/main.rs

2
src/entity/account.rs

@ -19,12 +19,14 @@ pub struct UserAccount {
pub flags: u32, // TODO: is this used for anything other than character creation?
}
#[derive(Clone, Debug, Default)]
pub struct UserSettings {
pub id: u32,
pub user_id: u32,
pub settings: settings::UserSettings,
}
#[derive(Clone, Default)]
pub struct GuildCardData {
pub id: u32,
pub user_id: u32,

5
src/entity/character.rs

@ -1,8 +1,9 @@
use libpso::character::character;
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Default)]
pub struct Character {
pub id: u32,
pub user_id: u32,
pub slot: u32,
pub character: character::Character,
}
}

6
src/entity/gateway/entitygateway.rs

@ -26,7 +26,11 @@ pub trait EntityGateway {
unimplemented!();
}
fn set_character_by_user(&mut self, _user: &UserAccount, _slot: u32, _char: Character) {
fn new_character_by_user(&mut self, _user: &UserAccount) -> Character {
unimplemented!();
}
fn set_character(&mut self, _char: &Character) {
unimplemented!();
}

103
src/entity/gateway/inmemory.rs

@ -0,0 +1,103 @@
use std::collections::HashMap;
use crate::entity::account::*;
use crate::entity::character::*;
use crate::entity::gateway::EntityGateway;
use libpso::character::settings;
use std::sync::{Arc, Mutex};
pub struct InMemoryGateway {
users: Arc<Mutex<HashMap<u32, UserAccount>>>,
user_settings: Arc<Mutex<HashMap<u32, UserSettings>>>,
//guildcard: Arc<Mutex<HashMap<u32, GuildCardData>>>,
characters: Arc<Mutex<HashMap<u32, Character>>>,
}
impl InMemoryGateway {
pub fn new() -> InMemoryGateway {
InMemoryGateway {
users: Arc::new(Mutex::new(HashMap::new())),
user_settings: Arc::new(Mutex::new(HashMap::new())),
//guildcard: Arc::new(Mutex::new(HashMap::new())),
characters: Arc::new(Mutex::new(HashMap::new())),
}
}
}
impl EntityGateway for InMemoryGateway {
fn get_user_by_id(&self, id: u32) -> Option<UserAccount> {
let users = self.users.lock().unwrap();
users.get(&id).map(|k| k.clone())
}
fn get_user_by_name(&self, username: String) -> Option<UserAccount> {
let users = self.users.lock().unwrap();
users
.iter()
.find(|(_, k)| k.username == username)
.map(|(_, k)| k.clone())
}
fn set_user(&mut self, user: &UserAccount) {
let mut users = self.users.lock().unwrap();
users.insert(user.id, user.clone());
}
fn get_user_settings_by_user(&self, user: &UserAccount) -> Option<UserSettings> {
let user_settings = self.user_settings.lock().unwrap();
user_settings
.iter()
.find(|(_, k)| k.id == user.id)
.map(|(_, k)| k.clone())
}
fn create_user_settings_by_user(&self, user: &UserAccount) -> UserSettings {
let mut user_settings = self.user_settings.lock().unwrap();
let id = user_settings
.iter()
.fold(0, |sum, (i, _)| std::cmp::max(sum, *i))
+ 1;
let new_settings = UserSettings {
id: id,
user_id: user.id,
settings: settings::UserSettings::default(),
};
user_settings.insert(id, new_settings.clone());
new_settings
}
fn get_characters_by_user(&self, user: &UserAccount) -> [Option<Character>; 4] {
let characters = self.characters.lock().unwrap();
let mut chars = [None; 4];
characters
.iter()
.filter(|(_, c)| c.user_id == user.id)
.for_each(|(_, c)| chars[c.slot as usize] = Some(c.clone()));
chars
}
fn new_character_by_user(&mut self, user: &UserAccount) -> Character {
let mut characters = self.characters.lock().unwrap();
let id = characters
.iter()
.fold(0, |sum, (i, _)| std::cmp::max(sum, *i))
+ 1;
let mut c = Character::default();
c.id = id;
c.user_id = user.id;
characters.insert(id, c.clone());
c
}
fn set_character(&mut self, char: &Character) {
let mut characters = self.characters.lock().unwrap();
characters.insert(char.id, char.clone());
}
fn get_guild_card_data_by_user(&self, _user: &UserAccount) -> GuildCardData {
GuildCardData::default()
}
}

2
src/entity/gateway/mod.rs

@ -1,3 +1,5 @@
pub mod entitygateway;
pub mod inmemory;
pub use entitygateway::EntityGateway;
pub use inmemory::InMemoryGateway;

6
src/login/character.rs

@ -383,12 +383,16 @@ impl<EG: EntityGateway> ServerState for CharacterServerState<EG> {
let client = self.clients.get_mut(&id).ok_or(CharacterError::ClientNotFound(id))?;
let mut user = client.user.as_mut().unwrap();
if user.flags == USERFLAG_NEWCHAR {
let mut char = self.entity_gateway.new_character_by_user(&user);
char.slot = preview.slot;
char.character = preview.character.as_character();
let char = Character {
id: 9,
slot: preview.slot,
user_id: user.id,
character: preview.character.as_character()
};
self.entity_gateway.set_character_by_user(&user, preview.slot, char);
self.entity_gateway.set_character(&char);
}
if user.flags == USERFLAG_DRESSINGROOM {
// TODO: dressing room stuff

5
src/login/main.rs

@ -49,6 +49,7 @@ impl LoginStubData {
characters: [Some(Character {
id: 1,
slot: 0,
user_id: 1,
character: c,
}),
@ -78,8 +79,8 @@ impl EntityGateway for LoginStubData {
self.characters
}
fn set_character_by_user(&mut self, _user: &UserAccount, slot: u32, char: Character) {
self.characters[slot as usize] = Some(char);
fn set_character(&mut self, char: &Character) {
self.characters[char.slot as usize] = Some(char.clone());
}
fn get_guild_card_data_by_user(&self, user: &UserAccount) -> GuildCardData {

Loading…
Cancel
Save