From e4cdd2b95a4bc389845361f271de38d5f5b7855e Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 2 Dec 2020 19:20:33 -0700 Subject: [PATCH] weapons can have tek modifiers --- src/entity/gateway/entitygateway.rs | 5 +++ src/entity/gateway/inmemory.rs | 20 +++++++++-- src/entity/gateway/postgres/models.rs | 6 ++++ src/entity/gateway/postgres/postgres.rs | 17 ++++++++++ src/entity/item/weapon.rs | 44 +++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/entity/gateway/entitygateway.rs b/src/entity/gateway/entitygateway.rs index 7aeb2c3..261aadc 100644 --- a/src/entity/gateway/entitygateway.rs +++ b/src/entity/gateway/entitygateway.rs @@ -81,6 +81,11 @@ pub trait EntityGateway: Send + Sync + Clone { unimplemented!(); } + async fn add_weapon_modifier(&mut self, _item_id: &ItemEntityId, _modifier: weapon::WeaponModifier) -> Result<(), GatewayError> { + unimplemented!(); + } + + /* async fn get_items_by_character(&self, _char_id: &CharacterEntityId) -> Result, GatewayError> { unimplemented!(); diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index e3a1c36..cedeaf6 100644 --- a/src/entity/gateway/inmemory.rs +++ b/src/entity/gateway/inmemory.rs @@ -18,6 +18,7 @@ pub struct InMemoryGateway { banks: Arc>>, equips: Arc>>, mag_modifiers: Arc>>>, + weapon_modifiers: Arc>>>, } impl InMemoryGateway { @@ -31,6 +32,7 @@ impl InMemoryGateway { banks: Arc::new(Mutex::new(BTreeMap::new())), equips: Arc::new(Mutex::new(BTreeMap::new())), mag_modifiers: Arc::new(Mutex::new(BTreeMap::new())), + weapon_modifiers: Arc::new(Mutex::new(BTreeMap::new())), } } } @@ -42,9 +44,16 @@ impl InMemoryGateway { .map(|item| { item.map_individual(|mut item| { item.item = match item.item { + ItemDetail::Weapon(mut weapon) => { + if let Some(weapon_modifiers) = self.weapon_modifiers.lock().unwrap().get(&item.id) { + for weapon_modifier in weapon_modifiers.iter() { + weapon.apply_modifier(&weapon_modifier); + } + } + ItemDetail::Weapon(weapon) + }, ItemDetail::Mag(mag) => { let mut mag = mag::Mag::baby_mag(mag.color as u16); - println!("mag! {:?}", mag); if let Some(mag_modifiers) = self.mag_modifiers.lock().unwrap().get(&item.id) { for mag_modifier in mag_modifiers.iter() { match mag_modifier { @@ -69,7 +78,6 @@ impl InMemoryGateway { }, _ => {} } - println!("{:?} -> {:?}", mag_modifier, mag); } } ItemDetail::Mag(mag) @@ -257,6 +265,14 @@ impl EntityGateway for InMemoryGateway { Ok(()) } + async fn add_weapon_modifier(&mut self, item_id: &ItemEntityId, modifier: weapon::WeaponModifier) -> Result<(), GatewayError> { + self.weapon_modifiers.lock().unwrap() + .entry(*item_id) + .or_insert(Vec::new()) + .push(modifier); + Ok(()) + } + async fn get_character_inventory(&mut self, char_id: &CharacterEntityId) -> Result { println!("getting inv"); let inventories = self.inventories.lock().unwrap(); diff --git a/src/entity/gateway/postgres/models.rs b/src/entity/gateway/postgres/models.rs index 0409955..892fa24 100644 --- a/src/entity/gateway/postgres/models.rs +++ b/src/entity/gateway/postgres/models.rs @@ -320,6 +320,12 @@ impl Into for PgWeapon { } } +#[derive(Debug, sqlx::FromRow)] +pub struct PgWeaponModifier { + pub weapon: i32, + pub modifier: sqlx::types::Json, +} + #[derive(Debug, Serialize, Deserialize)] pub struct PgArmor { armor: armor::ArmorType, diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index b041790..1239f1d 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/src/entity/gateway/postgres/postgres.rs @@ -48,6 +48,23 @@ impl PostgresGateway { let ItemEntity {id, item, location} = item; let item = match item { + ItemDetail::Weapon(mut weapon) => { + let q = r#"select weapon, modifier + from weapon_modifier + where weapon = $1 + order by created_at"#; + let weapon_modifiers = sqlx::query_as::<_, PgWeaponModifier>(q) + .bind(id.0 as i32) + .fetch(&self.pool); + + weapon_modifiers.for_each(|modifier| { + if let Ok(modifier) = modifier { + weapon.apply_modifier(&modifier.modifier); + } + }).await; + + ItemDetail::Weapon(weapon) + }, ItemDetail::Mag(mut mag) => { let q = r#"select mag, modifier, item.item -> 'Tool' as feed, item2.item -> 'Tool' as cell from mag_modifier diff --git a/src/entity/item/weapon.rs b/src/entity/item/weapon.rs index b689d43..2cdfcc4 100644 --- a/src/entity/item/weapon.rs +++ b/src/entity/item/weapon.rs @@ -1474,6 +1474,50 @@ impl Weapon { } } + pub fn apply_modifier(&mut self, modifier: &WeaponModifier) { + match modifier { + WeaponModifier::Tekked{special, percents} => { + match special { + TekSpecialModifier::Plus => { + self.special = self.special.map(|special| { + special.rank_up() + }); + }, + TekSpecialModifier::Minus => { + self.special = self.special.map(|special| { + special.rank_down() + }); + }, + TekSpecialModifier::Neutral => { + }, + } + for i in 0..3 { + self.attrs[i] = self.attrs[i].map(|mut attr| { + match percents { + TekPercentModifier::PlusPlus => { + attr.value += 10; + }, + TekPercentModifier::Plus => { + attr.value += 5; + }, + TekPercentModifier::MinusMinus => { + attr.value -= 10; + }, + TekPercentModifier::Minus => { + attr.value -= 5; + }, + TekPercentModifier::Neutral => { + } + } + attr + }); + } + self.tekked = true; + }, + _ => {} + } + } + pub fn as_bytes(&self) -> [u8; 16] { let mut result = [0u8; 16]; result[0..3].copy_from_slice(&self.weapon.value());