Changed many helpers to be methods
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good
Details
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good
Details
This commit is contained in:
parent
2f65050039
commit
73a93ba8c2
|
|
@ -268,18 +268,18 @@ pub(crate) fn handle_create_entity(
|
|||
let epoch_info = main_epoch.try_load_current_as_mut()?;
|
||||
let ident = entity_args.identifier.clone().map(EntityIdentifier::Name);
|
||||
|
||||
if helper_functions::find_entity(&ident, e_type, epoch_info).is_ok() {
|
||||
if epoch_info.find_entity_of_type(&ident, e_type).is_ok() {
|
||||
Err(EpochError::EntityAlreadyExist(ident))?;
|
||||
};
|
||||
|
||||
let mut entity = Entity::create_rng(e_type, &entity_args.rng)?;
|
||||
if let Some(position) = &entity_args.position {
|
||||
helper_functions::get_pos(position, epoch_info)?;
|
||||
epoch_info.resolve_position(position)?;
|
||||
}
|
||||
helper_functions::update_stats(&entity_args.stats, false, &mut entity);
|
||||
helper_functions::update_defaults(entity_args, &mut entity);
|
||||
|
||||
let vec_entities: &mut Vec<Entity> = helper_functions::vec_entities_mut(e_type, epoch_info);
|
||||
let vec_entities: &mut Vec<Entity> = epoch_info.vec_entities_mut(e_type);
|
||||
info!("Created character: {:#?}", entity);
|
||||
entity.print_full();
|
||||
let message = &crate::success::SuccessMessages::CreatedEntity(
|
||||
|
|
@ -305,10 +305,10 @@ fn handle_delete_entity(
|
|||
let epoch_info = main_epoch.try_load_current_as_mut()?;
|
||||
|
||||
let entity_identifier = Some(entity_args.identifier.clone());
|
||||
let vec_entities: &mut Vec<Entity> = helper_functions::vec_entities_mut(e_type, epoch_info);
|
||||
let vec_entities: &mut Vec<Entity> = epoch_info.vec_entities_mut(e_type);
|
||||
let index = vec_entities
|
||||
.iter()
|
||||
.position(|entity| helper_functions::find_by_args(entity, &entity_identifier))
|
||||
.position(|entity| entity.match_identifier(&entity_identifier))
|
||||
.ok_or(EpochError::EntityNotFound(
|
||||
e_type.clone(),
|
||||
entity_identifier,
|
||||
|
|
@ -344,8 +344,8 @@ fn handle_diff_entity(
|
|||
let epoch_current = unsafe { main_epoch.try_load_id(id) }?;
|
||||
let epoch_past = unsafe { main_epoch.try_load_id(id2) }?;
|
||||
let binding = Some(entity_args.identifier.clone());
|
||||
let current_entity = helper_functions::find_entity(&binding, e_type, epoch_current)?;
|
||||
let past_entity = helper_functions::find_entity(&binding, e_type, epoch_past)?;
|
||||
let current_entity = epoch_current.find_entity_of_type(&binding, e_type)?;
|
||||
let past_entity = epoch_past.find_entity_of_type(&binding, e_type)?;
|
||||
|
||||
let diff = current_entity.delta(past_entity);
|
||||
display_success(&crate::success::SuccessMessages::DiffBetweenEpoch(id, id2));
|
||||
|
|
@ -362,11 +362,11 @@ fn handle_show_entity(
|
|||
) -> Result<(), EpochError> {
|
||||
let epoch_info = main_epoch.try_load_current()?;
|
||||
if entity_args.identifier.is_none() {
|
||||
for entity in helper_functions::vec_entities(e_type, epoch_info) {
|
||||
for entity in epoch_info.vec_entities(e_type) {
|
||||
entity.print_oneline();
|
||||
}
|
||||
} else {
|
||||
let entity = helper_functions::find_entity(&entity_args.identifier, e_type, epoch_info)?;
|
||||
let entity = epoch_info.find_entity_of_type(&entity_args.identifier, e_type)?;
|
||||
entity.print_full();
|
||||
}
|
||||
Ok(())
|
||||
|
|
@ -389,26 +389,25 @@ fn handle_edit_entity(
|
|||
) -> Result<(), EpochError> {
|
||||
let epoch_info = main_epoch.try_load_current_as_mut()?;
|
||||
let position = if let Some(position) = &entity_args.position {
|
||||
helper_functions::get_pos(position, epoch_info).ok().clone()
|
||||
Some(epoch_info.resolve_position(position)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let entity = helper_functions::vec_entities_mut(e_type, epoch_info)
|
||||
|
||||
let entity = epoch_info
|
||||
.vec_entities_mut(e_type)
|
||||
.iter_mut()
|
||||
.find(|entity| {
|
||||
helper_functions::find_by_args(entity, &Some(entity_args.identifier.clone()))
|
||||
})
|
||||
.find(|entity| entity.match_identifier(&Some(entity_args.identifier.clone())))
|
||||
.ok_or(EpochError::EntityNotFound(
|
||||
e_type.clone(),
|
||||
Some(entity_args.identifier.clone()),
|
||||
))?;
|
||||
|
||||
helper_functions::update_stats(&entity_args.stats, entity_args.delta, entity);
|
||||
// TODO: Cleanup this mess with better methords
|
||||
|
||||
if let Some(position) = position {
|
||||
entity.modify_position(position);
|
||||
}
|
||||
|
||||
if let Some(duration) = &entity_args.duration {
|
||||
entity.modify_duration(duration.clone());
|
||||
}
|
||||
|
|
@ -421,11 +420,6 @@ fn handle_edit_entity(
|
|||
if let Some(bounds) = &entity_args.bounds {
|
||||
entity.modify_effect_bounds(bounds.clone());
|
||||
}
|
||||
// if let Some(pos) = &entity_args.pos_exact {
|
||||
// entity.modify_position(pos.clone());
|
||||
// } else if let Some(pos) = &entity_args.pos {
|
||||
// entity.modify_position_grid(pos.clone());
|
||||
// }
|
||||
|
||||
entity.print_full();
|
||||
let message = &crate::success::SuccessMessages::EditedEntity(
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
//! All helper functions
|
||||
|
||||
use lib_efo::{
|
||||
entity::{EffectBounds, Entity, EntityIdentifier},
|
||||
epoch::EpochInfo,
|
||||
error::EpochError,
|
||||
entity::{EffectBounds, Entity},
|
||||
naming::NAMING,
|
||||
stats::{EntityType, Position, PositionIdentifier, StatPair},
|
||||
stats::{EntityType, StatPair},
|
||||
};
|
||||
|
||||
use crate::commands::entity_generic::AddArgs;
|
||||
|
|
@ -17,114 +15,107 @@ pub(crate) fn update_stats(pair_list: &Vec<StatPair>, is_a_delta: bool, e: &mut
|
|||
}
|
||||
}
|
||||
|
||||
/// Looks up an entity inside the given `epoch_info` according to the
|
||||
/// supplied arguments.
|
||||
///
|
||||
/// The function returns a reference to the matching entity or an
|
||||
/// `EpochError::EntityNotFound` if no entity matches.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `entity_args` - The arguments that identify the entity.
|
||||
/// * `e_type` - The type of entity to look for.
|
||||
/// * `epoch_info` - The epoch that contains all entities.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Ok(&Entity)` - The entity that matches the arguments.
|
||||
/// * `Err(EpochError::EntityNotFound)` - No matching entity was found.
|
||||
pub(crate) fn find_entity<'a>(
|
||||
entity_to_find: &'a Option<EntityIdentifier>,
|
||||
e_type: &'a EntityType,
|
||||
epoch_info: &'a lib_efo::epoch::EpochInfo,
|
||||
) -> Result<&'a Entity, EpochError> {
|
||||
vec_entities(e_type, epoch_info)
|
||||
.iter()
|
||||
.find(|&entity| find_by_args(entity, entity_to_find))
|
||||
.ok_or(EpochError::EntityNotFound(
|
||||
e_type.clone(),
|
||||
entity_to_find.clone(),
|
||||
))
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// inside `epoch_info`. For unimplemented entity types the call will panic
|
||||
/// because of the `todo!()` placeholder.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `e_type` - The type of entities to retrieve.
|
||||
/// * `epoch_info` - The epoch information containing the collections.
|
||||
///
|
||||
/// # Returns
|
||||
/// A reference to the vector containing the requested entities.
|
||||
/// The lifetime of the returned slice is tied to that of `epoch_info`.
|
||||
#[inline]
|
||||
pub(crate) fn vec_entities<'a>(
|
||||
e_type: &'a EntityType,
|
||||
epoch_info: &'a lib_efo::epoch::EpochInfo,
|
||||
) -> &'a Vec<Entity> {
|
||||
match e_type {
|
||||
EntityType::Character => &epoch_info.characters,
|
||||
EntityType::Player => unimplemented!("No players for now"),
|
||||
EntityType::Town => &epoch_info.towns,
|
||||
EntityType::Monster => &epoch_info.calamities,
|
||||
EntityType::Blocker => &epoch_info.blockers,
|
||||
EntityType::Event => &epoch_info.events,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns a mutable slice over the entities of the requested type from the given
|
||||
/// `EpochInfo`.
|
||||
///
|
||||
/// Like `vec_entities`, this function selects the appropriate collection
|
||||
/// based on `e_type`. Unimplemented variants will panic due to the `todo!()`.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `e_type` - The type of entities to retrieve.
|
||||
/// * `epoch_info` - The epoch information containing the collections.
|
||||
///
|
||||
/// # Returns
|
||||
/// A mutable reference to the vector containing the requested entities.
|
||||
/// The lifetime of the returned slice is tied to that of `epoch_info`.
|
||||
pub(crate) fn vec_entities_mut<'a>(
|
||||
e_type: &'a EntityType,
|
||||
epoch_info: &'a mut lib_efo::epoch::EpochInfo,
|
||||
) -> &'a mut Vec<Entity> {
|
||||
match e_type {
|
||||
EntityType::Character => &mut epoch_info.characters,
|
||||
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,
|
||||
EntityType::Event => &mut epoch_info.events,
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper function finding by name, note that a lot more helpers could be done to find by other criterias
|
||||
pub(crate) fn find_by_args(
|
||||
entity_to_match: &Entity,
|
||||
entity_identifier: &Option<EntityIdentifier>,
|
||||
) -> bool {
|
||||
match entity_identifier {
|
||||
Some(EntityIdentifier::Name(name)) => entity_to_match.name.starts_with(name),
|
||||
Some(EntityIdentifier::Id(id)) => {
|
||||
entity_to_match.id.hyphenated().to_string().starts_with(id)
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
// /// Find an entity matching a tag
|
||||
// pub(crate) fn find_by_id<'a>(entities: &'a [Entity], id: &'a String) -> Option<&'a Entity> {
|
||||
// entities
|
||||
// /// Looks up an entity inside the given `epoch_info` according to the
|
||||
// /// supplied arguments.
|
||||
// ///
|
||||
// /// The function returns a reference to the matching entity or an
|
||||
// /// `EpochError::EntityNotFound` if no entity matches.
|
||||
// ///
|
||||
// /// # Arguments
|
||||
// /// * `entity_args` - The arguments that identify the entity.
|
||||
// /// * `e_type` - The type of entity to look for.
|
||||
// /// * `epoch_info` - The epoch that contains all entities.
|
||||
// ///
|
||||
// /// # Returns
|
||||
// /// * `Ok(&Entity)` - The entity that matches the arguments.
|
||||
// /// * `Err(EpochError::EntityNotFound)` - No matching entity was found.
|
||||
// #[deprecated(note = "Use epoch_info.find_entity_of_type instead")]
|
||||
// pub(crate) fn find_entity<'a>(
|
||||
// entity_to_find: &'a Option<EntityIdentifier>,
|
||||
// e_type: &'a EntityType,
|
||||
// epoch_info: &'a lib_efo::epoch::EpochInfo,
|
||||
// ) -> Result<&'a Entity, EpochError> {
|
||||
// epoch_info
|
||||
// .vec_entities(e_type)
|
||||
// .iter()
|
||||
// .find(|e| e.id.hyphenated().to_string().starts_with(id))
|
||||
// .find(|&entity| entity.match_identifier(entity_to_find))
|
||||
// .ok_or(EpochError::EntityNotFound(
|
||||
// e_type.clone(),
|
||||
// entity_to_find.clone(),
|
||||
// ))
|
||||
// }
|
||||
|
||||
// /// Find an entity matching a name
|
||||
// pub(crate) fn find_by_name<'a>(entities: &'a [Entity], id: &'a String) -> Option<&'a Entity> {
|
||||
// entities.iter().find(|e| e.name.starts_with(id))
|
||||
// /// 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
|
||||
// /// inside `epoch_info`. For unimplemented entity types the call will panic
|
||||
// /// because of the `todo!()` placeholder.
|
||||
// ///
|
||||
// /// # Arguments
|
||||
// /// * `e_type` - The type of entities to retrieve.
|
||||
// /// * `epoch_info` - The epoch information containing the collections.
|
||||
// ///
|
||||
// /// # Returns
|
||||
// /// A reference to the vector containing the requested entities.
|
||||
// /// The lifetime of the returned slice is tied to that of `epoch_info`.
|
||||
// #[inline]
|
||||
// #[deprecated(note = "Use epoch_info.vec_entities instead")]
|
||||
// pub(crate) fn vec_entities<'a>(
|
||||
// e_type: &'a EntityType,
|
||||
// epoch_info: &'a lib_efo::epoch::EpochInfo,
|
||||
// ) -> &'a Vec<Entity> {
|
||||
// match e_type {
|
||||
// EntityType::Character => &epoch_info.characters,
|
||||
// EntityType::Player => unimplemented!("No players for now"),
|
||||
// EntityType::Town => &epoch_info.towns,
|
||||
// EntityType::Monster => &epoch_info.calamities,
|
||||
// EntityType::Blocker => &epoch_info.blockers,
|
||||
// EntityType::Event => &epoch_info.events,
|
||||
// }
|
||||
// }
|
||||
|
||||
// #[inline]
|
||||
// /// Returns a mutable slice over the entities of the requested type from the given
|
||||
// /// `EpochInfo`.
|
||||
// ///
|
||||
// /// Like `vec_entities`, this function selects the appropriate collection
|
||||
// /// based on `e_type`. Unimplemented variants will panic due to the `todo!()`.
|
||||
// ///
|
||||
// /// # Arguments
|
||||
// /// * `e_type` - The type of entities to retrieve.
|
||||
// /// * `epoch_info` - The epoch information containing the collections.
|
||||
// ///
|
||||
// /// # Returns
|
||||
// /// A mutable reference to the vector containing the requested entities.
|
||||
// /// The lifetime of the returned slice is tied to that of `epoch_info`.
|
||||
// #[deprecated(note = "Use epoch_info.vec_entities_mut instead")]
|
||||
// pub(crate) fn vec_entities_mut<'a>(
|
||||
// e_type: &'a EntityType,
|
||||
// epoch_info: &'a mut lib_efo::epoch::EpochInfo,
|
||||
// ) -> &'a mut Vec<Entity> {
|
||||
// match e_type {
|
||||
// EntityType::Character => &mut epoch_info.characters,
|
||||
// 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,
|
||||
// EntityType::Event => &mut epoch_info.events,
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// Helper function finding by name, note that a lot more helpers could be done to find by other criterias
|
||||
// #[deprecated(note = "Use entity.match_identifier instead")]
|
||||
// pub(crate) fn find_by_args(
|
||||
// entity_to_match: &Entity,
|
||||
// entity_identifier: &Option<EntityIdentifier>,
|
||||
// ) -> bool {
|
||||
// match entity_identifier {
|
||||
// Some(EntityIdentifier::Name(name)) => entity_to_match.name.starts_with(name),
|
||||
// Some(EntityIdentifier::Id(id)) => {
|
||||
// entity_to_match.id.hyphenated().to_string().starts_with(id)
|
||||
// }
|
||||
// None => false,
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Update the default values of the entity with sensible defaults
|
||||
|
|
@ -197,50 +188,49 @@ pub(crate) fn update_defaults(entity_args: &AddArgs, e: &mut Entity) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Get the position of an entity either directly or cloning one from an epoch
|
||||
// TODO: This should be a method of epochinfo to avoid all borrow shenenigans
|
||||
pub(crate) fn get_pos(
|
||||
position: &PositionIdentifier,
|
||||
epoch: &EpochInfo,
|
||||
) -> Result<Position, EpochError> {
|
||||
Ok(match position {
|
||||
PositionIdentifier::Exact(pos) => pos.clone(),
|
||||
PositionIdentifier::Grid(pos) => pos.to_position(),
|
||||
PositionIdentifier::Id(ident) => {
|
||||
let entity_source = find_any_entity(epoch, ident.clone())?;
|
||||
entity_source.position().clone()
|
||||
}
|
||||
})
|
||||
}
|
||||
/// 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))
|
||||
}
|
||||
// /// Get the position of an entity either directly or cloning one from an epoch
|
||||
// // TODO: This should be a method of epochinfo to avoid all borrow shenenigans
|
||||
// #[deprecated(note = "Use epoch.resolve_position instead")]
|
||||
// pub(crate) fn get_pos(
|
||||
// position: &PositionIdentifier,
|
||||
// epoch: &EpochInfo,
|
||||
// ) -> Result<Position, EpochError> {
|
||||
// Ok(match position {
|
||||
// PositionIdentifier::Exact(pos) => pos.clone(),
|
||||
// PositionIdentifier::Grid(pos) => pos.to_position(),
|
||||
// PositionIdentifier::Id(ident) => epoch.find_any_entity(ident.clone())?.position().clone(),
|
||||
// })
|
||||
// }
|
||||
// /// 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.
|
||||
// #[deprecated(note = "Use epoch_info.find_any_entity instead")]
|
||||
// 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))
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use rand::distr::Distribution;
|
|||
use rand::seq::{IndexedRandom, IteratorRandom};
|
||||
use uuid::Uuid; // [3] - colored-rs examples
|
||||
|
||||
use crate::entity::{EffectApplication, EffectBounds, Entity, EntityDuration};
|
||||
use crate::entity::{EffectApplication, EffectBounds, Entity, EntityDuration, EntityIdentifier};
|
||||
use crate::epoch::epoch_traits::Diffable;
|
||||
use crate::error::EpochError;
|
||||
use crate::stats::{
|
||||
|
|
@ -440,6 +440,15 @@ impl Entity {
|
|||
pub fn position(&self) -> &Position {
|
||||
&self.metadata.position
|
||||
}
|
||||
|
||||
/// Returns whether the entity match an optionnal identifier
|
||||
pub fn match_identifier(&self, entity_identifier: &Option<EntityIdentifier>) -> bool {
|
||||
match entity_identifier {
|
||||
Some(EntityIdentifier::Name(name)) => self.name.starts_with(name),
|
||||
Some(EntityIdentifier::Id(id)) => self.id.hyphenated().to_string().starts_with(id),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// impl Entity {
|
||||
|
|
|
|||
|
|
@ -12,13 +12,13 @@ use serde::{Deserialize, Serialize};
|
|||
use std::fs::create_dir_all;
|
||||
|
||||
use crate::{
|
||||
entity::Entity,
|
||||
entity::{Entity, EntityIdentifier},
|
||||
epoch::{
|
||||
EpochId, SUB_NAME, SUBFOLDER_NAMING, TagList,
|
||||
epoch_traits::{ConfigWritable, Prettiable},
|
||||
},
|
||||
error::EpochError,
|
||||
stats::EntityType,
|
||||
stats::{EntityType, Position, PositionIdentifier},
|
||||
};
|
||||
|
||||
/// The metadata file stored inside each epoch directory
|
||||
|
|
@ -118,40 +118,116 @@ impl ConfigWritable for EpochInfo {
|
|||
}
|
||||
|
||||
impl EpochInfo {
|
||||
// pub(crate) fn find_entity_of_type<'a>(
|
||||
// &self,
|
||||
// entity_to_find: &'a Option<EntityIdentifier>,
|
||||
// e_type: &'a EntityType,
|
||||
// ) -> Result<&'a Entity, EpochError> {
|
||||
// self.vec_entities(e_type)
|
||||
// .iter()
|
||||
// .find(|&entity| find_by_args(entity, entity_to_find))
|
||||
// .ok_or(EpochError::EntityNotFound(
|
||||
// e_type.clone(),
|
||||
// entity_to_find.clone(),
|
||||
// ))
|
||||
// }
|
||||
// //
|
||||
// /// 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
|
||||
// /// inside `epoch_info`. For unimplemented entity types the call will panic
|
||||
// /// because of the `todo!()` placeholder.
|
||||
// ///
|
||||
// /// # Arguments
|
||||
// /// * `epoch_info` - The epoch information containing the collections.
|
||||
// ///
|
||||
// /// # Returns
|
||||
// /// A reference to the vector containing the requested entities.
|
||||
// /// The lifetime of the returned slice is tied to that of `epoch_info`.
|
||||
// pub(crate) fn vec_entities(&self, e_type: &EntityType) -> &Vec<Entity> {
|
||||
// match e_type {
|
||||
// EntityType::Character => &self.characters,
|
||||
// EntityType::Player => unimplemented!("No players for now"),
|
||||
// EntityType::Town => &self.towns,
|
||||
// EntityType::Monster => &self.calamities,
|
||||
// EntityType::Blocker => &self.blockers,
|
||||
// EntityType::Event => &self.events,
|
||||
// }
|
||||
// }
|
||||
/// Looks up an entity inside the given `epoch_info` according to the
|
||||
/// supplied arguments.
|
||||
///
|
||||
/// The function returns a reference to the matching entity or an
|
||||
/// `EpochError::EntityNotFound` if no entity matches.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `entity_args` - The arguments that identify the entity.
|
||||
/// * `e_type` - The type of entity to look for.
|
||||
/// * `epoch_info` - The epoch that contains all entities.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Ok(&Entity)` - The entity that matches the arguments.
|
||||
/// * `Err(EpochError::EntityNotFound)` - No matching entity was found.
|
||||
pub fn find_entity_of_type(
|
||||
&self,
|
||||
entity_to_find: &Option<EntityIdentifier>,
|
||||
e_type: &EntityType,
|
||||
) -> Result<&Entity, EpochError> {
|
||||
self.vec_entities(e_type)
|
||||
.iter()
|
||||
.find(|&entity| entity.match_identifier(entity_to_find))
|
||||
.ok_or(EpochError::EntityNotFound(
|
||||
e_type.clone(),
|
||||
entity_to_find.clone(),
|
||||
))
|
||||
}
|
||||
/// 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(&self, 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),
|
||||
};
|
||||
self.events
|
||||
.iter()
|
||||
.find(predicate)
|
||||
.or(self.characters.iter().find(predicate))
|
||||
.or(self.towns.iter().find(predicate))
|
||||
.or(self.blockers.iter().find(predicate))
|
||||
.or(self.calamities.iter().find(predicate))
|
||||
.ok_or(EpochError::EntityNotFoundById(id))
|
||||
}
|
||||
/// 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
|
||||
/// inside `epoch_info`. For unimplemented entity types the call will panic
|
||||
/// because of the `todo!()` placeholder.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `epoch_info` - The epoch information containing the collections.
|
||||
///
|
||||
/// # Returns
|
||||
/// A reference to the vector containing the requested entities.
|
||||
/// The lifetime of the returned slice is tied to that of `epoch_info`.
|
||||
pub fn vec_entities(&self, e_type: &EntityType) -> &Vec<Entity> {
|
||||
match e_type {
|
||||
EntityType::Character => &self.characters,
|
||||
EntityType::Player => unimplemented!("No players for now"),
|
||||
EntityType::Town => &self.towns,
|
||||
EntityType::Monster => &self.calamities,
|
||||
EntityType::Blocker => &self.blockers,
|
||||
EntityType::Event => &self.events,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a mutable slice over the entities of the requested type from the given
|
||||
/// `EpochInfo`.
|
||||
///
|
||||
/// Like `vec_entities`, this function selects the appropriate collection
|
||||
/// based on `e_type`. Unimplemented variants will panic due to the `todo!()`.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `e_type` - The type of entities to retrieve.
|
||||
/// * `epoch_info` - The epoch information containing the collections.
|
||||
///
|
||||
/// # Returns
|
||||
/// A mutable reference to the vector containing the requested entities.
|
||||
/// The lifetime of the returned slice is tied to that of `epoch_info`.
|
||||
pub fn vec_entities_mut(&mut self, e_type: &EntityType) -> &mut Vec<Entity> {
|
||||
match e_type {
|
||||
EntityType::Character => &mut self.characters,
|
||||
EntityType::Player => unimplemented!("No players for now"),
|
||||
EntityType::Town => &mut self.towns,
|
||||
EntityType::Monster => &mut self.calamities,
|
||||
EntityType::Blocker => &mut self.blockers,
|
||||
EntityType::Event => &mut self.events,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the position of an entity either directly or cloning one from an epoch
|
||||
pub fn resolve_position(&self, position: &PositionIdentifier) -> Result<Position, EpochError> {
|
||||
Ok(match position {
|
||||
PositionIdentifier::Exact(pos) => pos.clone(),
|
||||
PositionIdentifier::Grid(pos) => pos.to_position(),
|
||||
PositionIdentifier::Id(ident) => {
|
||||
self.find_any_entity(ident.clone())?.position().clone()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue