diff --git a/src/dbmanager/avatar_reliquary.rs b/src/dbmanager/avatar_reliquary.rs new file mode 100644 index 0000000..6751e9e --- /dev/null +++ b/src/dbmanager/avatar_reliquary.rs @@ -0,0 +1,25 @@ +// Database Manager + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "avatar_reliquary")] +pub struct Model { + #[sea_orm(primary_key)] + pub avatar_guid: i64, + pub reliquary_guid: i64, +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + _ => panic!("Unknown relation type!"), + } + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/dbmanager/avatar_weapon.rs b/src/dbmanager/avatar_weapon.rs new file mode 100644 index 0000000..f28ca0b --- /dev/null +++ b/src/dbmanager/avatar_weapon.rs @@ -0,0 +1,25 @@ +// Database Manager + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "avatar_weapon")] +pub struct Model { + #[sea_orm(primary_key)] + pub avatar_guid: i64, + pub weapon_guid: i64, +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + _ => panic!("Unknown relation type!"), + } + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/dbmanager/database_manager.rs b/src/dbmanager/database_manager.rs index 612477c..a69444e 100644 --- a/src/dbmanager/database_manager.rs +++ b/src/dbmanager/database_manager.rs @@ -1,6 +1,9 @@ // Database Manager use std::collections::HashMap; +#[macro_use] +use packet_processor::*; + use crate::collection; use sea_orm::{entity::*, error::*, query::*, DbConn, FromQueryResult, Database}; @@ -15,6 +18,12 @@ use super::player_info::Entity as PlayerInfoEntity; pub use super::avatar_info::Model as AvatarInfo; use super::avatar_info::Entity as AvatarInfoEntity; +pub use super::avatar_weapon::Model as AvatarWeapon; +use super::avatar_weapon::Entity as AvatarWeaponEntity; + +pub use super::avatar_reliquary::Model as AvatarReliquary; +use super::avatar_reliquary::Entity as AvatarReliquaryEntity; + pub use super::scene_info::Model as SceneInfo; use super::scene_info::Entity as SceneInfoEntity; @@ -189,7 +198,26 @@ impl DatabaseManager { } pub fn get_avatar_equip(&self, guid: i64) -> Option> { - let equip = vec![IdManager::get_guid_by_uid_and_id(AuthManager::SPOOFED_PLAYER_UID, Self::SPOOFED_WEAPON_ID) as i64]; + //let equip = vec![IdManager::get_guid_by_uid_and_id(AuthManager::SPOOFED_PLAYER_UID, Self::SPOOFED_WEAPON_ID) as i64]; + let weapons = match AvatarWeaponEntity::find_by_id(guid).one(&self.db).wait() { + Err(_) => { panic!("DB ERROR!") }, + Ok(weapon) => match weapon { + None => { + println!("WARNING: no weapon for avatar {}!", guid); + vec![] + }, + Some(weapon) => vec![weapon.weapon_guid], + }, + }; + + let relics = match AvatarReliquaryEntity::find_by_id(guid).all(&self.db).wait() { + Err(_) => { panic!("DB ERROR!") }, + Ok(relics) => relics, + }; + + let relics = relics.into_iter().map(|r| r.reliquary_guid); + + let equip = relics.chain(weapons.into_iter()).collect(); return Some(equip); } @@ -324,13 +352,12 @@ impl DatabaseManager { return Some(states); } - - pub fn get_inventory(&self, uid: u32) -> Option> { - +/* + pub fn _get_inventory(&self, uid: u32) -> Option> { let mut weapon = proto::Weapon::default(); weapon.level = 70; weapon.promote_level = 4; - weapon.affix_map = collection!{111406 => 0}; + weapon.affix_map = collection! {111406 => 0}; let mut equip = proto::Equip::default(); equip.is_locked = true; @@ -342,9 +369,9 @@ impl DatabaseManager { item.detail = Some(proto::item::Detail::Equip(equip)); return Some(vec![item]); + }*/ - - + pub fn get_inventory(&self, uid: u32) -> Option> { /* Inventory item can be of three types: material, equip and furniture Equip is further divided into relic and weapon @@ -354,7 +381,7 @@ impl DatabaseManager { 3) Relics (+their properties) 4) Weapons (+their affices) */ - /* + let items = match ItemInfoEntity::find_by_id(uid).all(&self.db).wait() { Err(e) => { panic!("DB ERROR: {}!", e) }, Ok(items) => items, @@ -366,7 +393,80 @@ impl DatabaseManager { let equip: Vec<(ItemInfo, EquipInfo)> = self.find_related_to_items(&items, EquipInfoEntity); - return None;*/ + let materials = materials.into_iter().map(|(ii, mi)| { + build!(Item { + item_id: ii.item_id, + guid: ii.guid as u64, // TODO: figure out the correct type for goddamn GUIDs! + detail: Some(proto::item::Detail::Material(build!(Material { + count: mi.count, + // TODO: MaterialDeleteInfo! + }))), + }) + }); + + let furniture = furniture.into_iter().map(|(ii, fi)| { + build!(Item { + item_id: ii.item_id, + guid: ii.guid as u64, // TODO: figure out the correct type for goddamn GUIDs! + detail: Some(proto::item::Detail::Furniture(build!(Furniture { + count: fi.count, + }))), + }) + }); + + let equip = equip.into_iter().map(|(ii, ei)| { + let reliquary = match ei.find_related(ReliquaryInfoEntity).one(&self.db).wait() { + Err(e) => { panic!("DB ERROR: {}!", e) }, + Ok(data) => match data { + None => None, + Some(data) => { + let props = match data.find_related(ReliquaryPropEntity).all(&self.db).wait() { + Err(e) => { panic!("DB ERROR: {}!", e) }, + Ok(data) => data.into_iter().map(|rp| rp.prop_id).collect(), + }; + + Some(build!(Reliquary { + level: ei.level, + promote_level: ei.promote_level, + exp: ei.exp, + main_prop_id: data.main_prop_id, + append_prop_id_list: props, + })) + } + }, + }; + + let weapon = match ei.find_related(WeaponAffixInfoEntity).all(&self.db).wait() { + Err(e) => { panic!("DB ERROR: {}!", e) }, + Ok(data) => Some(build!(Weapon { + level: ei.level, + promote_level: ei.promote_level, + exp: ei.exp, + affix_map: data.into_iter().map(|wai| (wai.affix_id, wai.affix_value)).collect(), + })) + }; + + let detail = if reliquary != None { + Some(proto::equip::Detail::Reliquary(reliquary.unwrap())) + } else if weapon != None { + Some(proto::equip::Detail::Weapon(weapon.unwrap())) + } else { + panic!("Equip item {} is not recognized as weapon or relic!", ii.guid) + }; + + build!(Item { + item_id: ii.item_id, + guid: ii.guid as u64, // TODO: figure out the correct type for goddamn GUIDs! + detail: Some(proto::item::Detail::Equip(build!(Equip { + is_locked: ei.is_locked, + detail: detail, + }))), + }) + }); + + return Some( + materials.chain(furniture).chain(equip).collect() + ); } fn find_related_to_items(&self, items: &Vec, entity_type: T) -> Vec<(ItemInfo, T::Model)> diff --git a/src/dbmanager/equip_info.rs b/src/dbmanager/equip_info.rs index 9396aec..a282a67 100644 --- a/src/dbmanager/equip_info.rs +++ b/src/dbmanager/equip_info.rs @@ -18,6 +18,12 @@ pub enum Relation { Item, } +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum RelationOuter { + WeaponAffix, + Reliquary, +} + impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { @@ -30,4 +36,32 @@ impl RelationTrait for Relation { } } +impl RelationTrait for RelationOuter { + fn def(&self) -> RelationDef { + match self { + Self::WeaponAffix => Entity::belongs_to(super::weapon_affix_info::Entity) + .from(Column::Guid) + .to(super::weapon_affix_info::Column::Guid) + .into(), + Self::Reliquary => Entity::belongs_to(super::reliquary_info::Entity) + .from(Column::Guid) + .to(super::reliquary_info::Column::Guid) + .into(), + _ => panic!("Unknown relation type!"), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + RelationOuter::WeaponAffix.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + RelationOuter::Reliquary.def() + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/src/dbmanager/item_info.rs b/src/dbmanager/item_info.rs index 2a90edd..f452634 100644 --- a/src/dbmanager/item_info.rs +++ b/src/dbmanager/item_info.rs @@ -21,6 +21,18 @@ pub enum Relation { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { + Self::Material => Entity::belongs_to(super::material_info::Entity) + .from(Column::Guid) + .to(super::material_info::Column::Guid) + .into(), + Self::Equip => Entity::belongs_to(super::equip_info::Entity) + .from(Column::Guid) + .to(super::equip_info::Column::Guid) + .into(), + Self::Furniture => Entity::belongs_to(super::furniture_info::Entity) + .from(Column::Guid) + .to(super::furniture_info::Column::Guid) + .into(), _ => panic!("Unknown relation type!"), } } diff --git a/src/dbmanager/mod.rs b/src/dbmanager/mod.rs index 4fb1921..957d4e8 100644 --- a/src/dbmanager/mod.rs +++ b/src/dbmanager/mod.rs @@ -4,6 +4,8 @@ pub use self::database_manager::DatabaseManager; mod player_info; mod avatar_info; +mod avatar_weapon; +mod avatar_reliquary; mod scene_info; mod avatar_team_info; mod team_info; diff --git a/src/dbmanager/reliquary_info.rs b/src/dbmanager/reliquary_info.rs index ddeac26..7586938 100644 --- a/src/dbmanager/reliquary_info.rs +++ b/src/dbmanager/reliquary_info.rs @@ -12,14 +12,42 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { + Equip, +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum RelationOuter { + ReliquaryProp, } impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { + Self::Equip => Entity::belongs_to(super::equip_info::Entity) + .from(Column::Guid) + .to(super::equip_info::Column::Guid) + .into(), _ => panic!("Unknown relation type!"), } } } +impl RelationTrait for RelationOuter { + fn def(&self) -> RelationDef { + match self { + Self::ReliquaryProp => Entity::belongs_to(super::reliquary_prop::Entity) + .from(Column::Guid) + .to(super::reliquary_prop::Column::Guid) + .into(), + _ => panic!("Unknown relation type!"), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + RelationOuter::ReliquaryProp.def() + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/src/dbmanager/reliquary_prop.rs b/src/dbmanager/reliquary_prop.rs index 328fabf..ed107b3 100644 --- a/src/dbmanager/reliquary_prop.rs +++ b/src/dbmanager/reliquary_prop.rs @@ -12,11 +12,16 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { + Reliquary, } impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { + Self::Reliquary => Entity::belongs_to(super::reliquary_info::Entity) + .from(Column::Guid) + .to(super::reliquary_info::Column::Guid) + .into(), _ => panic!("Unknown relation type!"), } } diff --git a/src/dbmanager/weapon_affix_info.rs b/src/dbmanager/weapon_affix_info.rs index 0177c28..24d92ce 100644 --- a/src/dbmanager/weapon_affix_info.rs +++ b/src/dbmanager/weapon_affix_info.rs @@ -13,11 +13,16 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { + Equip, } impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { + Self::Equip => Entity::belongs_to(super::equip_info::Entity) + .from(Column::Guid) + .to(super::equip_info::Column::Guid) + .into(), _ => panic!("Unknown relation type!"), } }