Browse Source

add kill counter field to weapons and increment it when a client kills a monster

kill_counters
andy 3 years ago
parent
commit
b0eb494660
  1. 2
      src/bin/main.rs
  2. 11
      src/entity/gateway/entitygateway.rs
  3. 15
      src/entity/gateway/inmemory.rs
  4. 1
      src/entity/item/mod.rs
  5. 6
      src/entity/item/weapon.rs
  6. 1
      src/ship/drops/rare_drop_table.rs
  7. 1
      src/ship/items/inventory.rs
  8. 35
      src/ship/items/manager.rs
  9. 3
      src/ship/packet/handler/message.rs

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

11
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<TradeEntity, GatewayError> {
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!();
}
}

15
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!");
}
}

1
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,

6
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);
}
}
}

1
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 {

1
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()

35
src/ship/items/manager.rs

@ -1415,6 +1415,41 @@ impl<EG: EntityGateway> ItemAction<EG> 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<EG: EntityGateway>( &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(())
}
}

3
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?
}

Loading…
Cancel
Save