From b0eb494660711e729ad8d5655098f84f3807ab5b Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 10 Aug 2021 00:23:14 +0000 Subject: [PATCH] add kill counter field to weapons and increment it when a client kills a monster --- src/bin/main.rs | 2 +- src/entity/gateway/entitygateway.rs | 11 +++++++++ src/entity/gateway/inmemory.rs | 15 +++++++++++++ src/entity/item/mod.rs | 1 - src/entity/item/weapon.rs | 6 +++++ src/ship/drops/rare_drop_table.rs | 1 + src/ship/items/inventory.rs | 1 - src/ship/items/manager.rs | 35 +++++++++++++++++++++++++++++ src/ship/packet/handler/message.rs | 3 ++- 9 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index aa8a73a..4fe4b1c 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -116,7 +116,7 @@ fn main() { Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, - kills: Some(22999), + kills: Some(22995), } ), }).await.unwrap(); diff --git a/src/entity/gateway/entitygateway.rs b/src/entity/gateway/entitygateway.rs index 36bc0a1..5c3c43b 100644 --- a/src/entity/gateway/entitygateway.rs +++ b/src/entity/gateway/entitygateway.rs @@ -157,6 +157,17 @@ pub trait EntityGateway: Send + Sync { async fn create_trade(&mut self, _char_id1: &CharacterEntityId, _char_id2: &CharacterEntityId) -> Result { unimplemented!(); } + async fn increment_kill_counter(&mut self, _item_entity_id: &ItemEntityId) -> Result<(), GatewayError> { + unimplemented!(); + } + + async fn get_kill_counter() { + unimplemented!(); + } + + async fn set_kill_counter() { + unimplemented!(); + } } diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index bdca6d2..7ebee44 100644 --- a/src/entity/gateway/inmemory.rs +++ b/src/entity/gateway/inmemory.rs @@ -527,4 +527,19 @@ impl EntityGateway for InMemoryGateway { trades.push(new_trade.clone()); Ok(new_trade) } + + async fn increment_kill_counter(&mut self, item_id: &ItemEntityId) -> Result<(), GatewayError> { + if let Some(item_entity) = self.items.lock().unwrap().get_mut(item_id) { + item_entity.increase_kill_counter(); + } + Ok(()) + } + + async fn get_kill_counter() { + println!("src/entity/gateway/inmemory.rs::get_kill_counter() - unimplemented!"); + } + + async fn set_kill_counter() { + println!("src/entity/gateway/inmemory.rs::set_kill_counter() - unimplemented!"); + } } diff --git a/src/entity/item/mod.rs b/src/entity/item/mod.rs index ac7b229..8198da7 100644 --- a/src/entity/item/mod.rs +++ b/src/entity/item/mod.rs @@ -186,7 +186,6 @@ impl ItemDetail { ItemDetail::Armor(_a) => false, ItemDetail::Shield(_s) => false, ItemDetail::Unit(u) => u.kills.is_some(), - // ItemDetail::Unit(_u) => false, ItemDetail::Tool(_t) => false, ItemDetail::TechniqueDisk(_d) => false, ItemDetail::Mag(_m) => false, diff --git a/src/entity/item/weapon.rs b/src/entity/item/weapon.rs index 7243836..20c6049 100644 --- a/src/entity/item/weapon.rs +++ b/src/entity/item/weapon.rs @@ -1684,4 +1684,10 @@ impl Weapon { | WeaponType::Scepter ) } + + pub fn increment_kill_counter(&mut self) { + if let Some(kills) = self.kills { + self.kills = Some(kills + 1); + } + } } diff --git a/src/ship/drops/rare_drop_table.rs b/src/ship/drops/rare_drop_table.rs index fc44b5b..1e6578f 100644 --- a/src/ship/drops/rare_drop_table.rs +++ b/src/ship/drops/rare_drop_table.rs @@ -110,6 +110,7 @@ impl RareDropTable { dropped_weapon.kills = Some(0); }; ItemDropType::Weapon(dropped_weapon) + }) }, RareDropItem::Armor(armor) => { ItemDropType::Armor(Armor { diff --git a/src/ship/items/inventory.rs b/src/ship/items/inventory.rs index 4024fc8..a11721a 100644 --- a/src/ship/items/inventory.rs +++ b/src/ship/items/inventory.rs @@ -498,7 +498,6 @@ impl InventoryState { self.equipped.clone() } - pub fn as_client_inventory_items(&self) -> [character::InventoryItem; 30] { self.inventory.0.iter() .enumerate() diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index dcd1dae..c511085 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -1415,6 +1415,41 @@ impl ItemAction for TradeMeseta { dest_meseta.0 += self.amount as u32; entity_gateway.set_character_meseta(&self.dest_character_id, *dest_meseta).await?; } + } + + pub async fn increase_kill_counters( &mut self, + entity_gateway: &mut EG, + character: &CharacterEntity, + equipped_items: &EquippedEntity) + -> Result<(), anyhow::Error> { + let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; + if let Some(weapon_entity) = equipped_items.weapon { + println!("updating weapon kill counter for weapon {:?}", weapon_entity); + // weapon_entity = &InventoryItem + + // let weapon_id = weapon_entity.item_id(); + let weapon_id = inventory.get_item_by_entity_id(weapon_entity).ok_or(ItemManagerError::EntityIdNotInInventory(weapon_entity))?.item_id(); + let mut weapon_handle = inventory.get_item_handle_by_id(weapon_id).ok_or(ItemManagerError::NoSuchItemId(weapon_id))?; + // weapon_handle = InventoryItemHandle + let individual_item = weapon_handle.item_mut() + .ok_or(ItemManagerError::NoSuchItemId(weapon_id))? + .individual_mut() + .ok_or(ItemManagerError::WrongItemType(weapon_id))?; + let weapon = individual_item + .weapon_mut() + .ok_or(ItemManagerError::WrongItemType(weapon_id))?; + + weapon.increment_kill_counter(); + entity_gateway.increment_kill_counter(&weapon_entity).await?; + entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?; + } + // for units in equipped_items.unit { + // if let Some(unit_id) = units { + // println!("UNIMPLEMENTED - updating unit kill counter for unit {:?}", unit_id); + // // entity_gateway.increase_kill_counter(&unit_id).await?; + // // let unit = inventory.get_item_by_entity_id(&unit_id) + // } + // } Ok(()) } } diff --git a/src/ship/packet/handler/message.rs b/src/ship/packet/handler/message.rs index 2515aef..af79d03 100644 --- a/src/ship/packet/handler/message.rs +++ b/src/ship/packet/handler/message.rs @@ -405,7 +405,8 @@ where EG: EntityGateway { let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; - item_manager.player_sells_item(entity_gateway, &mut client.character, ClientItemId(sold_item.item_id), sold_item.amount as usize).await?; + sell_item(item_state, entity_gateway, &client.character, ClientItemId(sold_item.item_id), sold_item.amount as u32).await?; + // TODO: send the packet to other clients Ok(Box::new(None.into_iter())) // TODO: Do clients care about the order of other clients items? }