jake
5 years ago
7 changed files with 123 additions and 6 deletions
-
2src/entity/account.rs
-
3src/entity/character.rs
-
6src/entity/gateway/entitygateway.rs
-
103src/entity/gateway/inmemory.rs
-
2src/entity/gateway/mod.rs
-
6src/login/character.rs
-
5src/login/main.rs
@ -1,8 +1,9 @@ |
|||||
use libpso::character::character;
|
use libpso::character::character;
|
||||
|
|
||||
#[derive(Copy, Clone, Debug)]
|
|
||||
|
#[derive(Copy, Clone, Debug, Default)]
|
||||
pub struct Character {
|
pub struct Character {
|
||||
pub id: u32,
|
pub id: u32,
|
||||
pub user_id: u32,
|
pub user_id: u32,
|
||||
|
pub slot: u32,
|
||||
pub character: character::Character,
|
pub character: character::Character,
|
||||
}
|
}
|
@ -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()
|
||||
|
}
|
||||
|
}
|
@ -1,3 +1,5 @@ |
|||||
pub mod entitygateway;
|
pub mod entitygateway;
|
||||
|
pub mod inmemory;
|
||||
|
|
||||
pub use entitygateway::EntityGateway;
|
pub use entitygateway::EntityGateway;
|
||||
|
pub use inmemory::InMemoryGateway;
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue