diff --git a/bin_efo/src/commands/entity_generic.rs b/bin_efo/src/commands/entity_generic.rs index 0ddcc49..51c269c 100644 --- a/bin_efo/src/commands/entity_generic.rs +++ b/bin_efo/src/commands/entity_generic.rs @@ -154,20 +154,6 @@ pub struct AddArgs { #[arg(value_enum, long, short, default_value_t = RngLevel::Med)] pub rng: RngLevel, - // /// Place the new entity at an exact position – this ignores - // /// any map‑specific coordinate transformations. - // #[arg(long, short = 'P', group = "position")] - // pub pos_exact: Option, - - // /// Place the new entity using the map’s position rules. - // #[arg(long, short = 'p', group = "position")] - // pub pos: Option, - - // /// Update the position with an entity position - // /// - // /// Note that if a name is supplied instead of a tag, only towns will be looked over - // #[arg(long, short = 'e', group = "position")] - // pub entity_position: Option, /// A position Identifier, either a grid position, and exact position or a tag #[arg(short, long)] pub position: PositionIdentifier, @@ -377,7 +363,6 @@ fn handle_diff_entity( } /// Function handler for the show subcommand -// TODO: Add filters else this will become very clustered fn handle_show_entity( entity_args: &ShowArgs, e_type: &EntityType, diff --git a/bin_efo/src/commands/entity_generic/helper_functions.rs b/bin_efo/src/commands/entity_generic/helper_functions.rs index 0429037..29f1cde 100644 --- a/bin_efo/src/commands/entity_generic/helper_functions.rs +++ b/bin_efo/src/commands/entity_generic/helper_functions.rs @@ -65,7 +65,7 @@ pub(crate) fn vec_entities<'a>( ) -> &'a Vec { match e_type { EntityType::Character => &epoch_info.characters, - EntityType::Player => todo!(), + EntityType::Player => unimplemented!("No players for now"), EntityType::Town => &epoch_info.towns, EntityType::Monster => &epoch_info.calamities, EntityType::Blocker => &epoch_info.blockers, @@ -93,7 +93,7 @@ pub(crate) fn vec_entities_mut<'a>( ) -> &'a mut Vec { match e_type { EntityType::Character => &mut epoch_info.characters, - EntityType::Player => todo!(), + EntityType::Player => unimplemented!("No players for now"), EntityType::Town => &mut epoch_info.towns, EntityType::Monster => &mut epoch_info.calamities, EntityType::Blocker => &mut epoch_info.blockers, @@ -208,11 +208,41 @@ pub(crate) fn update_position( PositionIdentifier::Grid(pos) => entity.modify_position_grid(pos.clone()), PositionIdentifier::Id(ident) => { - // TODO: Consider another than find that could return any - let entity_identifier = Some(ident.clone()); - let entity_source = find_entity(&entity_identifier, &EntityType::Town, epoch)?; + let entity_source = find_any_entity(epoch, ident.clone())?; entity.modify_position(entity_source.position().clone()); } } Ok(()) } +/// Find an entity by its UUID. +/// +/// This function searches the currently loaded epoch for an entity whose +/// `id` matches the supplied `Uuid`. It looks in both the `events`, `towns` +/// and `characters` collections of the epoch. +/// +/// # Arguments +/// * `id` - The `Uuid` of the entity to locate. +/// +/// # Returns +/// * `Ok(&Entity)` - Reference to the entity if it is present in the +/// current epoch. +/// * `Err(EpochError::EntityNotFoundById(id))` - No entity with the +/// supplied UUID exists in the epoch. +pub fn find_any_entity( + epoch_info: &EpochInfo, + id: EntityIdentifier, +) -> Result<&Entity, EpochError> { + let predicate = |entity: &&Entity| match &id { + EntityIdentifier::Name(name) => entity.name.starts_with(name), + EntityIdentifier::Id(id) => entity.id.to_string().starts_with(id), + }; + epoch_info + .events + .iter() + .find(predicate) + .or(epoch_info.characters.iter().find(predicate)) + .or(epoch_info.towns.iter().find(predicate)) + .or(epoch_info.blockers.iter().find(predicate)) + .or(epoch_info.calamities.iter().find(predicate)) + .ok_or(EpochError::EntityNotFoundById(id)) +} diff --git a/lib_efo/src/epoch/epoch_config.rs b/lib_efo/src/epoch/epoch_config.rs index d501510..6be91a3 100644 --- a/lib_efo/src/epoch/epoch_config.rs +++ b/lib_efo/src/epoch/epoch_config.rs @@ -10,12 +10,9 @@ //! The world data and all event logs belong to the individual epoch //! directories and are **not** part of this file. -use crate::{ - entity::Entity, - epoch::{ - EpochId, EpochIdentifier, EpochInfo, MAIN_NAME, SUBFOLDER_NAMING, SaveMode, - epoch_traits::ConfigWritable, - }, +use crate::epoch::{ + EpochId, EpochIdentifier, EpochInfo, MAIN_NAME, SUBFOLDER_NAMING, SaveMode, + epoch_traits::ConfigWritable, }; use std::{ collections::HashMap, @@ -25,7 +22,6 @@ use std::{ use log::{info, warn}; use serde::{Deserialize, Serialize}; -use uuid::Uuid; use crate::{epoch::epoch_traits::Prettiable, error::EpochError}; @@ -368,43 +364,6 @@ impl EpochConfig { println!("{}", EpochInfo::as_table(items_to_log)?); Ok(()) } - - /// Find an entity by its UUID. - /// - /// This function searches the currently loaded epoch for an entity whose - /// `id` matches the supplied `Uuid`. It looks in both the `events`, `towns` - /// and `characters` collections of the epoch. - /// - /// # Safety - /// The caller must guarantee that the epoch identified by `self.epoch_id` - /// has already been loaded into memory (for example by calling - /// `try_load` first). The function performs an unchecked reference - /// conversion via `try_load_id`; invoking it when the epoch is not - /// cached results in **undefined behaviour**. - /// - /// # Arguments - /// * `id` - The `Uuid` of the entity to locate. - /// - /// # Returns - /// * `Ok(&Entity)` - Reference to the entity if it is present in the - /// current epoch. - /// * `Err(EpochError::EntityNotFoundById(id))` - No entity with the - /// supplied UUID exists in the epoch. - /// - #[allow(dead_code)] - unsafe fn find_entity(&self, id: Uuid) -> Result<&Entity, EpochError> { - let epoch_info = unsafe { self.try_load_id(self.epoch_id) }?; - let predicate = |entity: &&Entity| entity.id == id; - epoch_info - .events - .iter() - .find(predicate) - .or(epoch_info.characters.iter().find(predicate)) - .or(epoch_info.towns.iter().find(predicate)) - .or(epoch_info.blockers.iter().find(predicate)) - .or(epoch_info.calamities.iter().find(predicate)) - .ok_or(EpochError::EntityNotFoundById(id)) - } } impl ConfigWritable for EpochConfig { diff --git a/lib_efo/src/error.rs b/lib_efo/src/error.rs index 290fe63..1f24a79 100644 --- a/lib_efo/src/error.rs +++ b/lib_efo/src/error.rs @@ -254,7 +254,7 @@ pub enum EpochError { #[diagnostic(help( "Verify that the identifier is correct and that the entity exists in the current epoch." ))] - EntityNotFoundById(Uuid), + EntityNotFoundById(EntityIdentifier), /// The entity could not be found #[error("Entity of type {0:?} with name {1:?} not found")] diff --git a/lib_efo/src/stats.rs b/lib_efo/src/stats.rs index 77a3dcb..0219ac2 100644 --- a/lib_efo/src/stats.rs +++ b/lib_efo/src/stats.rs @@ -10,7 +10,6 @@ //! * **Source** - Formula or function that generates the stat value. #![deny(clippy::missing_docs_in_private_items)] -#![allow(dead_code)] use crate::{entity::EntityIdentifier, error::EpochError, thin_newtype, utils::RngLevel}; use cli_table::{CellStruct, Color, Style, Table}; use colored::Colorize;