Browse Source

item entity

pbs
jake 5 years ago
parent
commit
41e7f1c41a
  1. 17
      src/entity/gateway/entitygateway.rs
  2. 45
      src/entity/gateway/inmemory.rs
  3. 27
      src/entity/item.rs

17
src/entity/gateway/entitygateway.rs

@ -1,5 +1,8 @@
use crate::entity::account::*;
use crate::entity::character::*;
use crate::entity::item::*;
use libpso::item;
pub trait EntityGateway {
fn get_user_by_id(&self, _id: u32) -> Option<UserAccount> {
@ -30,11 +33,23 @@ pub trait EntityGateway {
unimplemented!();
}
fn new_character_by_user(&mut self, _user: &UserAccount) -> Character{
fn new_character_by_user(&mut self, _user: &UserAccount) -> Character {
unimplemented!();
}
fn get_guild_card_data_by_user(&self, _user: &UserAccount) -> GuildCardData {
unimplemented!();
}
fn new_item(&mut self, _item: item::Item, _location: ItemLocation) -> Item {
unimplemented!();
}
fn set_item(&self, _item: &Item) {
unimplemented!();
}
fn get_items_by_character(&self, _char: &Character) -> Vec<Item> {
unimplemented!();
}
}

45
src/entity/gateway/inmemory.rs

@ -1,10 +1,13 @@
use std::collections::HashMap;
use std::default::Default;
use crate::entity::account::*;
use crate::entity::character::*;
use crate::entity::gateway::EntityGateway;
use crate::entity::item::*;
use libpso::character::settings;
use libpso::item;
use std::sync::{Arc, Mutex};
@ -14,6 +17,7 @@ pub struct InMemoryGateway {
user_settings: Arc<Mutex<HashMap<u32, UserSettings>>>,
//guildcard: Arc<Mutex<HashMap<u32, GuildCardData>>>,
characters: Arc<Mutex<HashMap<u32, Character>>>,
items: Arc<Mutex<HashMap<u32, Item>>>,
}
impl InMemoryGateway {
@ -21,8 +25,8 @@ impl 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())),
items: Arc::new(Mutex::new(HashMap::new())),
}
}
}
@ -101,4 +105,43 @@ impl EntityGateway for InMemoryGateway {
fn get_guild_card_data_by_user(&self, _user: &UserAccount) -> GuildCardData {
GuildCardData::default()
}
fn new_item(&mut self, item: item::Item, location: ItemLocation) -> Item {
let mut items = self.items.lock().unwrap();
let id = items
.iter()
.fold(0, |sum, (i, _)| std::cmp::max(sum, *i))
+ 1;
let new_item = Item {
id: id,
location: location,
item: item,
};
items.insert(id, new_item.clone());
new_item
}
fn set_item(&self, item: &Item) {
let mut items = self.items.lock().unwrap();
items.insert(item.id, item.clone());
}
fn get_items_by_character(&self, character: &Character) -> Vec<Item> {
let items = self.items.lock().unwrap();
items
.iter()
.filter(|(_, k)| {
if let ItemLocation::Inventory{character_id, index} = k.location {
character_id == character.id
}
else {
false
}
})
.map(|(_, k)| {
k.clone()
})
.collect()
}
}

27
src/entity/item.rs

@ -0,0 +1,27 @@
use libpso::item;
#[derive(Clone, Debug)]
pub enum ItemLocation {
Inventory {
character_id: u32,
index: usize,
},
Bank {
character_id: u32,
slot: usize
},
Floor {
// floor: eventually
// x y z: ?????
},
}
#[derive(Clone, Debug)]
pub struct Item {
pub id: u32,
pub location: ItemLocation,
pub item: item::Item,
}
Loading…
Cancel
Save