diff --git a/bin_efo/src/commands/entity_generic/helper_functions.rs b/bin_efo/src/commands/entity_generic/helper_functions.rs index 5974a52..5836c46 100644 --- a/bin_efo/src/commands/entity_generic/helper_functions.rs +++ b/bin_efo/src/commands/entity_generic/helper_functions.rs @@ -5,7 +5,7 @@ use lib_efo::{ epoch::EpochInfo, error::EpochError, naming::NAMING, - stats::{self, CharacterId, EntityType, StatPair, Symbols, TypedLink}, + stats::{self, EntityType, Symbols}, utils::RngLevel, }; use log::info; @@ -17,7 +17,11 @@ use crate::{ }; /// Update the given stats with the given argumenbts /// Note that this function will not change name & id due to the usage variance between caller -pub(crate) fn update_stats(pair_list: &Vec, delta: stats::Modification, e: &mut Entity) { +pub(crate) fn update_stats( + pair_list: &Vec, + delta: stats::Modification, + e: &mut Entity, +) { for stat in pair_list { e.change_stat_fuzzy(stat.clone(), delta); } @@ -145,54 +149,156 @@ pub fn update_advanced_values( 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)?; - remove_link_from_old(entity, epoch_info, old_id, stats::Role::Govern); - add_link_to_new(entity, epoch_info, new_id, stats::Role::Govern)?; + 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); + remove_link_from_old( + entity, + epoch_info, + Some(*old_gov), + stats::Role::Govern, + &EntityType::Character, + ); } - // if let Some(capital) = &entity_args.capital { - // let capital = epoch_info.find_entity_of_type(&Some(capital.clone()), &EntityType::Town)?; - // let old_cap = entity.change_capital(stats::TownId(capital.id)); - // let new_cap = capital.id; + 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, + )?; - // if let Some(cap) = old_cap { - // epoch_info.del_from_links(*cap, &EntityType::Character, entity.id)?; - // } - - // epoch_info.add_to_links(new_cap, &EntityType::Town, entity.id)?; - // } else if entity_args.no_capital - // && let Some(c) = entity.remove_capital() - // { - // epoch_info.del_from_links(*c, &EntityType::Town, entity.id)?; - // } - // // Remove links if needed, updating two ways + 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, + ); + } Ok(()) } +/// Update a single “link” (governor, capital, …) in a DRY way. +/// +/// * `opt_new` – `Some(&T)` if the caller supplied a new value, `None` otherwise. +/// * `no_flag` – `entity_args.no_xxx` (true if we are allowed to delete the old link). +/// * `remove_old` – closure that removes the old value and returns `Option`. +/// * `add_new` – closure that inserts the new value and returns `AddingDoneStruct`. +/// * `role` – the `stats::Role` belonging to this link. +/// * `etype` – the `EntityType` belonging to this link. +/// +/// All operations are performed on the supplied `entity` and `epoch_info`. +/// +/// Returns `Ok(())` or propagates any `EpochError` that occurs. +#[allow(clippy::too_many_arguments)] +fn update_link( + opt_new: Option<&T>, + no_flag: bool, + remove_old: FRemove, + add_new: FAdd, + entity: &mut Entity, + epoch_info: &mut EpochInfo, + role: stats::Role, + etype: &EntityType, +) -> Result<(), EpochError> +where + FRemove: FnOnce(&mut Entity) -> Option, + FAdd: Fn( + &mut Entity, + &mut EpochInfo, + &T, + &EntityType, + stats::Role, + ) -> 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); + } + } + + Ok(()) +} /// Structure existing only to have sane stranfer #[allow(clippy::missing_docs_in_private_items)] struct AddingDoneStruct { - old_id: Option, + old_id: Option, new_id: Uuid, } /// Add the entity id to the entity, swapping the old one fn add_entity( entity: &mut Entity, epoch_info: &mut EpochInfo, - governor: &lib_efo::entity::EntityIdentifier, + target: &lib_efo::entity::EntityIdentifier, + target_t: &EntityType, + action: stats::Role, ) -> Result { - let governor = - epoch_info.find_entity_of_type(&Some(governor.clone()), &EntityType::Character)?; - let new_id = governor.id; - let old_id = entity.change_governor(stats::CharacterId(new_id)); + let target = epoch_info.find_entity_of_type(&Some(target.clone()), target_t)?; + let new_id = target.id; + let old_id = match action { + stats::Role::Govern => entity + .change_governor(stats::CharacterId(new_id)) + .map(|v| *v), + stats::Role::Capital => entity.change_capital(stats::TownId(new_id)).map(|v| *v), + stats::Role::Undefined => None, + }; info!( - "Adding {}{} to {}{}", - governor.name, - governor.id.to_symbol(), + "Adding {}{} [{}] to {}{}", + target.name, + target.id.to_symbol(), + action.to_symbol(), entity.name, entity.id.to_symbol(), ); @@ -203,27 +309,30 @@ fn add_entity( fn remove_link_from_old( entity: &mut Entity, epoch_info: &mut EpochInfo, - old_id: Option, + old_id: Option, role: stats::Role, + target_t: &EntityType, ) { if let Some(old_id) = old_id { info!( - "Deleting {old_id} link to town Town {}{}", - entity.id, entity.name + "Deleting {} link to {} {}{}", + old_id.to_symbol(), + entity.id.to_symbol(), + entity.entity_type.to_symbol(), + entity.name ); - let destroy_link = TypedLink { + let destroy_link = stats::TypedLink { other_id: entity.id, role, other_type: entity.entity_type, }; - // If the data structure is corrupted, print a message and continue let _ = epoch_info - .del_from_links(*old_id, &EntityType::Character, destroy_link) + .del_from_links(old_id, target_t, destroy_link) .inspect_err(|_| { display_warning(&WarningMessages::EntityNotFound { id: old_id.to_symbol(), - e_type: &EntityType::Character, + e_type: target_t, }) }); } @@ -235,8 +344,9 @@ fn add_link_to_new( epoch_info: &mut EpochInfo, new_gov: Uuid, action: stats::Role, + target_t: &EntityType, ) -> Result<(), EpochError> { - let link_back = TypedLink { + let link_back = stats::TypedLink { other_id: entity.id, role: action, other_type: entity.entity_type, @@ -245,7 +355,7 @@ fn add_link_to_new( "Creating link-back {link_back} for target {}", new_gov.to_symbol() ); - epoch_info.add_to_links(new_gov, &EntityType::Character, link_back)?; + epoch_info.add_to_links(new_gov, target_t, link_back)?; Ok(()) } diff --git a/lib_efo/src/entity/implementations/methods.rs b/lib_efo/src/entity/implementations/methods.rs index eeb77a7..8afe655 100644 --- a/lib_efo/src/entity/implementations/methods.rs +++ b/lib_efo/src/entity/implementations/methods.rs @@ -208,7 +208,7 @@ impl Entity { implementations::uuid_to_symbol( link.other_id, epoch_info, - &link.role.infered_type_of_target() + &link.other_type ) .yellow() ), diff --git a/lib_efo/src/epoch/epoch_info.rs b/lib_efo/src/epoch/epoch_info.rs index b43e843..f499f6b 100644 --- a/lib_efo/src/epoch/epoch_info.rs +++ b/lib_efo/src/epoch/epoch_info.rs @@ -159,9 +159,10 @@ impl EpochInfo { .vec_entities_mut(target_t) .iter_mut() .find(|entity| entity.id == target_id) - .ok_or(EpochError::EntityNotFoundById(EntityIdentifier::Id( - target_id.to_string(), - )))?; + .ok_or(EpochError::EntityNotFoundById( + EntityIdentifier::Id(target_id.to_string()), + Some(*target_t), + ))?; e.add_to_links(link_back); Ok(()) } @@ -182,9 +183,10 @@ impl EpochInfo { .vec_entities_mut(target_t) .iter_mut() .find(|entity| entity.id == target_id) - .ok_or(EpochError::EntityNotFoundById(EntityIdentifier::Id( - target_id.to_string(), - )))?; + .ok_or(EpochError::EntityNotFoundById( + EntityIdentifier::Id(target_id.to_string()), + Some(*target_t), + ))?; e.del_from_links(link_back); Ok(()) @@ -218,7 +220,7 @@ impl EpochInfo { .or(self.towns.iter().find(predicate)) .or(self.blockers.iter().find(predicate)) .or(self.calamities.iter().find(predicate)) - .ok_or(EpochError::EntityNotFoundById(id)) + .ok_or(EpochError::EntityNotFoundById(id, None)) } /// Find an entity from an Uuid pub fn find_entity_strict( diff --git a/lib_efo/src/error.rs b/lib_efo/src/error.rs index 089534d..693793d 100644 --- a/lib_efo/src/error.rs +++ b/lib_efo/src/error.rs @@ -253,12 +253,12 @@ pub enum EpochError { EpochHashingImpossible(&'static str, u32), /// The entity with id **`{0}`** could not be found. - #[error("Entity of type with id {0:?} not found")] + #[error("Entity of type {1:?} with id {0:?} not found")] #[diagnostic(code(epoch::entity_not_found_by_id))] #[diagnostic(help( "Verify that the identifier is correct and that the entity exists in the current epoch." ))] - EntityNotFoundById(EntityIdentifier), + EntityNotFoundById(EntityIdentifier, Option), /// The entity could not be found #[error("Entity of type {0:?} with name {1:?} not found")] diff --git a/lib_efo/src/stats/implementations.rs b/lib_efo/src/stats/implementations.rs index b6fb6ba..a1f3f7a 100644 --- a/lib_efo/src/stats/implementations.rs +++ b/lib_efo/src/stats/implementations.rs @@ -1,7 +1,7 @@ //! All traits implementatition for the main structs are deffered here #![allow(dead_code)] -use crate::stats::{self, EntityType, GameStat, GenericStat, ListStats, OverallStats, Role}; +use crate::stats::{GameStat, GenericStat, ListStats, OverallStats}; mod from_impl; mod symb_impl; @@ -294,24 +294,5 @@ macro_rules! thin_newtype { }; } -impl Role { - /// The role inferred of the entity targeted by the role - pub fn infered_type_of_target(&self) -> stats::EntityType { - match self { - Role::Govern => EntityType::Town, - Role::Capital => EntityType::Character, - Role::Undefined => EntityType::Event, - } - } - /// The role inferred of the entity since it has this role - pub fn infered_type_of_self(&self) -> stats::EntityType { - match self { - Role::Govern => EntityType::Character, - Role::Capital => EntityType::Town, - Role::Undefined => EntityType::Event, - } - } -} - #[cfg(test)] pub(crate) mod test;