Browse Source

weapons can have tek modifiers

pbs
jake 4 years ago
parent
commit
e4cdd2b95a
  1. 5
      src/entity/gateway/entitygateway.rs
  2. 20
      src/entity/gateway/inmemory.rs
  3. 6
      src/entity/gateway/postgres/models.rs
  4. 17
      src/entity/gateway/postgres/postgres.rs
  5. 44
      src/entity/item/weapon.rs

5
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<Vec<ItemEntity>, GatewayError> {
unimplemented!();

20
src/entity/gateway/inmemory.rs

@ -18,6 +18,7 @@ pub struct InMemoryGateway {
banks: Arc<Mutex<BTreeMap<CharacterEntityId, BankEntity>>>,
equips: Arc<Mutex<BTreeMap<CharacterEntityId, EquippedEntity>>>,
mag_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<mag::MagModifier>>>>,
weapon_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<weapon::WeaponModifier>>>>,
}
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<InventoryEntity, GatewayError> {
println!("getting inv");
let inventories = self.inventories.lock().unwrap();

6
src/entity/gateway/postgres/models.rs

@ -320,6 +320,12 @@ impl Into<weapon::Weapon> for PgWeapon {
}
}
#[derive(Debug, sqlx::FromRow)]
pub struct PgWeaponModifier {
pub weapon: i32,
pub modifier: sqlx::types::Json<weapon::WeaponModifier>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PgArmor {
armor: armor::ArmorType,

17
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

44
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());

Loading…
Cancel
Save