Position update for add
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2025-12-14 20:23:34 +01:00
parent 9a6c3ff058
commit 79f28377c6
5 changed files with 39 additions and 66 deletions

View File

@ -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<Position>,
// /// Place the new entity using the map’s position rules.
// #[arg(long, short = 'p', group = "position")]
// pub pos: Option<Position>,
// /// 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<EntityIdentifier>,
/// 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,

View File

@ -65,7 +65,7 @@ pub(crate) fn vec_entities<'a>(
) -> &'a Vec<Entity> {
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<Entity> {
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))
}

View File

@ -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 {

View File

@ -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")]

View File

@ -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;