diff --git a/.run_status.toml b/.run_status.toml index 4208424..2bba8dc 100644 --- a/.run_status.toml +++ b/.run_status.toml @@ -1,8 +1,8 @@ [last_run_clean] run_status = "Success" -lines_parsed = 102 -coverage = 47.96 +lines_parsed = 108 +coverage = 52.45 [last_run_clean.run_duration] secs = 26 -nanos = 496094925 +nanos = 130055380 diff --git a/badges/coverage.svg b/badges/coverage.svg index cbbfba6..39652f9 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -test-coverage: 48%test-coverage48% \ No newline at end of file +test-coverage: 52%test-coverage52% \ No newline at end of file diff --git a/bin_efo/src/commands/entity_generic/helper_functions.rs b/bin_efo/src/commands/entity_generic/helper_functions.rs index 19187b8..a55d75d 100644 --- a/bin_efo/src/commands/entity_generic/helper_functions.rs +++ b/bin_efo/src/commands/entity_generic/helper_functions.rs @@ -143,12 +143,17 @@ pub fn update_advanced_values( info!("Adding {} to {}{}", governor, entity.id, entity.name); let governor = epoch_info.find_entity_of_type(&Some(governor.clone()), &EntityType::Character)?; - entity.change_governor(stats::CharacterId(governor.id)); + let old_gov = entity.change_governor(stats::CharacterId(governor.id)); + let new_gov = governor.id; + // If there was a governor, tell it it's no longer the big boy in town + if let Some(gov) = old_gov { + epoch_info.del_from_links(*gov, &EntityType::Town, entity.id)?; + } // Since I added a governor, I now need to add the link to the governor, // note that if you add an entity to itself this will subtly break but the // link usage being only for deletion, this breakage will not matter // This double lookup is only done since the update is not done by the owning structure - epoch_info.add_to_links(governor.id, &EntityType::Character, entity.id)?; + epoch_info.add_to_links(new_gov, &EntityType::Character, entity.id)?; } else if entity_args.no_governor && let Some(g) = entity.remove_governor() { @@ -157,8 +162,14 @@ pub fn update_advanced_values( } if let Some(capital) = &entity_args.capital { let capital = epoch_info.find_entity_of_type(&Some(capital.clone()), &EntityType::Town)?; - entity.change_capital(stats::TownId(capital.id)); - epoch_info.add_to_links(capital.id, &EntityType::Town, entity.id)?; + let old_cap = entity.change_capital(stats::TownId(capital.id)); + let new_cap = capital.id; + + 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() { diff --git a/lib_efo/src/entity/implementations/methods.rs b/lib_efo/src/entity/implementations/methods.rs index 15d0af7..682c421 100644 --- a/lib_efo/src/entity/implementations/methods.rs +++ b/lib_efo/src/entity/implementations/methods.rs @@ -82,6 +82,24 @@ impl Entity { self.stat_to_table(items) } + /// Reset the temporary stats to a given base value. + /// + /// Useful before performing an update: you can set the temporary stats to + /// whatever snapshot you want to start from (or `None` to clear it). + pub fn reset_temporary_stats_to(&mut self, other: Option) { + self.temp_stats = other; + } + + /// Return an immutable reference to the entity’s live statistics. + pub fn get_stats(&self) -> &OverallStats { + &self.stats + } + /// Return an immutable reference to the temporary statistics snapshot, + /// if one is currently stored. + pub fn get_temp_stats(&self) -> &Option { + &self.temp_stats + } + /// Display the entity's statistics in a pretty table (same format as /// `full_stats_to_table`). /// @@ -107,7 +125,6 @@ impl Entity { /// /// # Returns /// A `String` containing the formatted tree. - // TODO: Deref ids pub fn to_pretty_tree(&self, epoch_info: &EpochInfo) -> String { // Helper that prefixes a line with the correct amount of vertical bars. pub(crate) fn indent(level: usize, line: &str) -> String { @@ -464,10 +481,10 @@ impl Entity { } } - /// Add a governor to the entity if the id exists - pub fn change_governor(&mut self, governor: stats::CharacterId) { + /// Add a governor to the entity if the id exists and return the old governor if it existed + pub fn change_governor(&mut self, governor: stats::CharacterId) -> Option { debug!("Swapping the governor with {}", governor.to_symbol()); - self.metadata.governor = Some(governor); + self.metadata.governor.replace(governor) } /// Remove the entity's governor and return it pub fn remove_governor(&mut self) -> Option { @@ -475,9 +492,9 @@ impl Entity { } /// Add a governor to the entity if the id exists - pub fn change_capital(&mut self, capital: stats::TownId) { + pub fn change_capital(&mut self, capital: stats::TownId) -> Option { debug!("Swapping the capital with {}", capital.to_symbol()); - self.metadata.capital = Some(capital); + self.metadata.capital.replace(capital) } /// Remove the entity's governor and return it pub fn remove_capital(&mut self) -> Option { diff --git a/lib_efo/src/epoch/epoch_config.rs b/lib_efo/src/epoch/epoch_config.rs index ecaef1c..69ad5ad 100644 --- a/lib_efo/src/epoch/epoch_config.rs +++ b/lib_efo/src/epoch/epoch_config.rs @@ -10,9 +10,12 @@ //! The world data and all event logs belong to the individual epoch //! directories and are **not** part of this file. -use crate::epoch::{ - EpochId, EpochIdentifier, EpochInfo, MAIN_NAME, SUBFOLDER_NAMING, SaveMode, TagList, - epoch_traits::ConfigWritable, +use crate::{ + epoch::{ + EpochId, EpochIdentifier, EpochInfo, MAIN_NAME, SUBFOLDER_NAMING, SaveMode, TagList, + epoch_traits::ConfigWritable, + }, + stats, }; use std::{ collections::HashMap, @@ -301,7 +304,12 @@ impl EpochConfig { /// with the ID immediately following the current one. The actual logic /// is deferred to the `EpochInfo -> World` subsystem. pub fn compute_next(&mut self) -> Result { - // unimplemented!() + // unimplemented!(); + // Iter on events, increment time + // Iter on character, increment time + // create a new_towns_values vector, update it with base stats, then all events modifying underlying value + // create_new_character_base_v vector, do the same + // calculate the temps values (via function) // TODO(1): Implement this warn!("DEBUG LINE: COMPUTING NEXT EPOCH"); // New fake epoch @@ -312,6 +320,41 @@ impl EpochConfig { Ok(new_epoch) } + /// Update **all** temporary values of every entity in the current epoch. + /// + /// # Safety + /// * The caller must guarantee that the current epoch is **loaded** and + /// its storage is still valid for the duration of the call. + /// * The function performs raw pointer arithmetic / `unsafe` reads/writes + /// on the internal data structures. No other thread may be mutably + /// borrowing the same data at the same time. + pub unsafe fn update_all_temporary_values(&mut self) {} + + /// Reset **all** temporary values to the current live values. + /// + /// # Safety + /// * The caller must ensure that the current epoch is loaded and that + /// no other mutable references to the same data exist. + /// * This method writes directly into the entity structs; any concurrent + /// access will lead to undefined behaviour. + pub unsafe fn reset_temporary_stats(&mut self) -> Result<(), EpochError> { + let current_epoch = self.try_load_current_as_mut()?; + current_epoch + .vec_entities_mut(&stats::EntityType::Town) + .iter_mut() + .for_each(|entity| entity.reset_temporary_stats_to(Some(entity.get_stats().clone()))); + + Ok(()) + } + + /// Update temporary values for every entity assuming the current epoch + /// is loaded. + /// + /// # Safety + /// * Same as `update_all_temporary_values`: the current epoch must be + /// loaded and no other mutable references may exist. + pub unsafe fn update_temps(&mut self) {} + /// Insert a new epoch into the configuration and persist it. /// /// The function creates the epoch's directory, writes its metadata,