diff --git a/bin_efo/src/commands/chr.rs b/bin_efo/src/commands/chr.rs index 0203db2..1acbe3f 100644 --- a/bin_efo/src/commands/chr.rs +++ b/bin_efo/src/commands/chr.rs @@ -47,6 +47,10 @@ pub struct EntityArgs { #[arg(long, group = "identifier")] pub name: Option, + /// Applies a delta to stats instead of setting the values + #[arg(long, short, action = clap::ArgAction::SetTrue, default_value_t = false)] + pub delta: bool, + /// An optional Uuid to either set or get #[arg(long, group = "identifier")] pub id: Option, @@ -158,8 +162,12 @@ fn handle_edit_entity( entity_args.name.clone(), entity_args.id.clone(), ))?; - // TODO[new]: Modify all values - // + + for stat in &entity_args.stats { + entity.change_stat(stat.clone()); + } + // TODO: Changing name ? + display_success(&crate::success::SuccessMessages::EditedEntity( entity.name.clone(), entity.entity_type.to_shortest(), @@ -209,14 +217,12 @@ fn handle_create_entity( // TODO: Add name, add stats repartition to clap // TODO: Load stats to entity from clap let mut e = Entity::create_rng(e_type, &RngLevel::Flawed)?; - for stat in &entity_args.stats { - e.change_stat(stat.clone()); - } + update_stats(entity_args, &mut e); + // Specifying a name means the name needs to be changed + // Specifying an id however means nothing and should be ignored if let Some(name) = &entity_args.name { e.name = name.clone(); } - // TODO find id - // if let Some(id) = info!("Created character: {:#?}", e); let message = &crate::success::SuccessMessages::CreatedEntity( @@ -233,6 +239,21 @@ fn handle_create_entity( Ok(()) } +/// Update the given stats with the given argumenbts +/// Note that this function will not change name & id due to the usage variance between caller +/// +fn update_stats(entity_args: &EntityArgs, e: &mut Entity) { + if entity_args.delta { + for stat in &entity_args.stats { + e.delta_stat(stat.clone()); + } + } else { + for stat in &entity_args.stats { + e.change_stat(stat.clone()); + } + } +} + /// Looks up an entity inside the given `epoch_info` according to the /// supplied arguments. /// @@ -330,9 +351,14 @@ fn find_by_args(entity: &Entity, entity_args: &EntityArgs) -> bool { } } -/// Find a complete id in a list of entities, if it exists -fn find_by_id(entities: &[Entity], id: &String) -> bool { +/// Find an entity matching a tag +fn find_by_id<'a>(entities: &'a [Entity], id: &'a String) -> Option<&'a Entity> { entities .iter() - .any(|e| e.id.hyphenated().to_string().starts_with(id)) + .find(|e| e.id.hyphenated().to_string().starts_with(id)) +} + +/// Find an entity matching a name +fn find_by_name<'a>(entities: &'a [Entity], id: &'a String) -> Option<&'a Entity> { + entities.iter().find(|e| e.name.starts_with(id)) } diff --git a/lib_efo/src/entity/implementations.rs b/lib_efo/src/entity/implementations.rs index afaa235..acded70 100644 --- a/lib_efo/src/entity/implementations.rs +++ b/lib_efo/src/entity/implementations.rs @@ -153,6 +153,11 @@ impl Entity { pub fn change_stat(&mut self, pair: StatPair) { overwrite_stat(&mut self.stats, pair); } + + /// Applies a delta to all the stats + pub fn delta_stat(&mut self, pair: StatPair) { + add_to_stat(&mut self.stats, pair); + } } // TODO: Redoc /// Overwrite a stat in a table @@ -162,7 +167,6 @@ fn overwrite_stat(stats: &mut OverallStats, pair: StatPair) { } /// Add to a stat in a table -#[allow(dead_code)] fn add_to_stat(stats: &mut OverallStats, pair: StatPair) { let affected_stat = stats.get_field_mut(&pair.stat); let current_value = affected_stat.clone();