diff --git a/.run_status.toml b/.run_status.toml index 2bba8dc..bb2f884 100644 --- a/.run_status.toml +++ b/.run_status.toml @@ -1,8 +1,8 @@ [last_run_clean] run_status = "Success" -lines_parsed = 108 -coverage = 52.45 +lines_parsed = 110 +coverage = 49.76 [last_run_clean.run_duration] secs = 26 -nanos = 130055380 +nanos = 637749582 diff --git a/badges/coverage.svg b/badges/coverage.svg index 39652f9..d697e70 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -test-coverage: 52%test-coverage52% \ No newline at end of file +test-coverage: 50%test-coverage50% \ No newline at end of file diff --git a/bin_efo/src/commands/entity_generic.rs b/bin_efo/src/commands/entity_generic.rs index 7b2e398..a85cb2c 100644 --- a/bin_efo/src/commands/entity_generic.rs +++ b/bin_efo/src/commands/entity_generic.rs @@ -392,8 +392,9 @@ fn handle_delete_entity( .ok_or(EpochError::EntityNotFound(*e_type, entity_identifier))?; let entity = vec_entities.swap_remove(index); - + entity.remove_all_links(epoch_info)?; // TODO: Clean all the links from it's presence + // for l in entity.links(){ // // Figure out what the link is // // Delete either a governor or capital diff --git a/bin_efo/src/commands/entity_generic/helper_functions.rs b/bin_efo/src/commands/entity_generic/helper_functions.rs index 5836c46..05695de 100644 --- a/bin_efo/src/commands/entity_generic/helper_functions.rs +++ b/bin_efo/src/commands/entity_generic/helper_functions.rs @@ -148,77 +148,30 @@ pub fn update_advanced_values( entity: &mut Entity, epoch_info: &mut EpochInfo, ) -> Result<(), EpochError> { - if let Some(governor) = &entity_args.governor { - let AddingDoneStruct { old_id, new_id } = add_entity( - entity, - epoch_info, - governor, - &EntityType::Character, - stats::Role::Govern, - )?; - remove_link_from_old( - entity, - epoch_info, - old_id, - stats::Role::Govern, - &EntityType::Character, - ); - add_link_to_new( - entity, - epoch_info, - new_id, - stats::Role::Govern, - &EntityType::Character, - )?; - } else if entity_args.no_governor - && let Some(old_gov) = entity.remove_governor() - { - remove_link_from_old( - entity, - epoch_info, - Some(*old_gov), - stats::Role::Govern, - &EntityType::Character, - ); - } + update_link( + entity_args.governor.as_ref(), + entity_args.no_governor, + |e| e.remove_governor().map(|i| *i), + add_entity, + entity, + epoch_info, + stats::Role::Govern, + &EntityType::Character, + )?; - if let Some(capital) = &entity_args.capital { - info!("About to add"); - let AddingDoneStruct { old_id, new_id } = add_entity( - entity, - epoch_info, - capital, - &EntityType::Town, - stats::Role::Capital, - )?; - - remove_link_from_old( - entity, - epoch_info, - old_id, - stats::Role::Capital, - &EntityType::Town, - ); - add_link_to_new( - entity, - epoch_info, - new_id, - stats::Role::Capital, - &EntityType::Town, - )?; - } else if entity_args.no_capital - && let Some(capital) = entity.remove_capital() - { - remove_link_from_old( - entity, - epoch_info, - Some(*capital), - stats::Role::Capital, - &EntityType::Town, - ); - } + update_link( + entity_args.capital.as_ref(), + entity_args.no_capital, + |e| e.remove_capital().map(|i| *i), + add_entity, + entity, + epoch_info, + stats::Role::Capital, + &EntityType::Town, + )?; Ok(()) } + /// Update a single “link” (governor, capital, …) in a DRY way. /// /// * `opt_new` – `Some(&T)` if the caller supplied a new value, `None` otherwise. @@ -253,20 +206,12 @@ where ) -> Result, { if let Some(new_val) = opt_new { - // 1️⃣ add the new entity let AddingDoneStruct { old_id, new_id } = add_new(entity, epoch_info, new_val, etype, role)?; - - // 2️⃣ unlink the old one (if any) remove_link_from_old(entity, epoch_info, old_id, role, etype); - - // 3️⃣ link the new one add_link_to_new(entity, epoch_info, new_id, role, etype)?; - } else if no_flag { - // only remove the old link if we are allowed to - if let Some(old_id) = remove_old(entity) { - remove_link_from_old(entity, epoch_info, Some(old_id), role, etype); - } + } else if no_flag && let Some(old_id) = remove_old(entity) { + remove_link_from_old(entity, epoch_info, Some(old_id), role, etype); } Ok(()) diff --git a/lib_efo/src/entity.rs b/lib_efo/src/entity.rs index d57cf1d..a7545ea 100644 --- a/lib_efo/src/entity.rs +++ b/lib_efo/src/entity.rs @@ -19,7 +19,7 @@ use uuid::Uuid; use crate::{ error::EpochError, - stats::{EntityType, MetaStats, OverallStats}, + stats::{self, EntityType, MetaStats, OverallStats, Symbols, TypedLink}, }; pub mod implementations; @@ -80,7 +80,40 @@ pub struct Entity { /// Limits that clamp any stat changes caused by the entity's effect. effect_bounds: EffectBounds, } -impl Entity {} +impl Entity { + /// Iterate over all the links and do the appropriate action + pub fn remove_all_links( + &self, + epoch_info: &mut crate::epoch::EpochInfo, + ) -> Result<(), EpochError> { + for TypedLink { + other_id, + role, + other_type, + } in &self.metadata.links + { + let entity: &mut Entity = + epoch_info.find_entity_strict_as_mut(*other_id, Some(*other_type))?; + let id = match role { + stats::Role::Govern => entity.remove_governor().map(|i| *i), + stats::Role::Capital => entity.remove_capital().map(|i| *i), + stats::Role::Undefined => None, + }; + // TODO: Add the other way cleaning, not useful for now but if ever links can be two way, + // it needs to be done + log::info!( + "Removed entity {}{} [{}] from {}{}{} -> {id:?}", + other_id.to_symbol(), + other_type.to_symbol(), + role.to_symbol(), + entity.name, + entity.id.to_symbol(), + entity.entity_type.to_symbol() + ); + } + Ok(()) + } +} /// Limits for the maximum and minimum value that an effect may /// modify a statistic. The bounds are applied after the effect has diff --git a/lib_efo/src/entity/test.rs b/lib_efo/src/entity/test.rs index cbe7588..eba3dae 100644 --- a/lib_efo/src/entity/test.rs +++ b/lib_efo/src/entity/test.rs @@ -244,18 +244,18 @@ fn full_entity() -> Entity { links: vec![ stats::TypedLink::new( Uuid::from_u128_le(1212), - stats::Role::Govern, - EntityType::Character, + stats::Role::Capital, + EntityType::Town, ), stats::TypedLink::new( Uuid::from_u128_le(121), - stats::Role::Govern, - EntityType::Character, + stats::Role::Capital, + EntityType::Town, ), stats::TypedLink::new( Uuid::from_u128_le(121), - stats::Role::Govern, - EntityType::Character, + stats::Role::Capital, + EntityType::Town, ), ], reach: stats::Reach::Global, diff --git a/lib_efo/src/epoch/epoch_info.rs b/lib_efo/src/epoch/epoch_info.rs index f499f6b..8a2db4a 100644 --- a/lib_efo/src/epoch/epoch_info.rs +++ b/lib_efo/src/epoch/epoch_info.rs @@ -251,6 +251,36 @@ impl EpochInfo { )) } } + /// Find an entity from an Uuid + // TODO: Siplify this with the better iterator + pub fn find_entity_strict_as_mut( + &mut self, + id: Uuid, + strict: Option, + ) -> Result<&mut Entity, EpochError> { + let predicate = |entity: &&mut Entity| entity.id == id; + if let Some(e_type) = strict { + self.vec_entities_mut(&e_type) + .iter_mut() + .find(|entity| entity.id == id) + .ok_or(EpochError::EntityNotFound( + e_type, + Some(EntityIdentifier::Id(id.to_string())), + )) + } else { + self.events + .iter_mut() + .find(predicate) + .or(self.characters.iter_mut().find(predicate)) + .or(self.towns.iter_mut().find(predicate)) + .or(self.blockers.iter_mut().find(predicate)) + .or(self.calamities.iter_mut().find(predicate)) + .ok_or(EpochError::EntityNotFound( + EntityType::Player, + Some(EntityIdentifier::Id(id.to_string())), + )) + } + } /// Returns a slice over the entities of the requested type from the given `EpochInfo`. /// /// The function matches the supplied `e_type` to the appropriate collection