From 41e7f1c41a6d597694598a7cfd5f8dbdf75f42cb Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 9 Dec 2019 23:11:27 -0800 Subject: [PATCH] item entity --- src/entity/gateway/entitygateway.rs | 17 ++++++++++- src/entity/gateway/inmemory.rs | 45 ++++++++++++++++++++++++++++- src/entity/item.rs | 27 +++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/entity/gateway/entitygateway.rs b/src/entity/gateway/entitygateway.rs index 482091f..3e5f6c0 100644 --- a/src/entity/gateway/entitygateway.rs +++ b/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 { @@ -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 { + unimplemented!(); + } } diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index 5fd3532..2a0c97f 100644 --- a/src/entity/gateway/inmemory.rs +++ b/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>>, //guildcard: Arc>>, characters: Arc>>, + items: Arc>>, } 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 { + 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() + } + } diff --git a/src/entity/item.rs b/src/entity/item.rs index e69de29..ff5f058 100644 --- a/src/entity/item.rs +++ b/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, +}