diff --git a/.gitignore b/.gitignore index 7008a04..1cbb944 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,6 @@ Cargo.lock # already existing elements were commented out #/target -testepo +testepo* naming.json config.toml diff --git a/bin_efo/src/commands/chr.rs b/bin_efo/src/commands/chr.rs index e1a8167..0203db2 100644 --- a/bin_efo/src/commands/chr.rs +++ b/bin_efo/src/commands/chr.rs @@ -12,6 +12,7 @@ use lib_efo::{ error::EpochError, stats::{EntityType, StatPair}, }; +use log::info; use crate::error::AppError; use crate::success::display_success; @@ -43,11 +44,11 @@ pub enum EntitySubCommands { #[derive(Args, Debug)] pub struct EntityArgs { /// Either an identifying tag or an Id - #[arg(long, group = "id")] + #[arg(long, group = "identifier")] pub name: Option, /// An optional Uuid to either set or get - #[arg(long, group = "id")] + #[arg(long, group = "identifier")] pub id: Option, /// Non specified stat Repartition #[arg(value_enum, default_value_t = RngLevel::Zero)] @@ -205,11 +206,19 @@ fn handle_create_entity( let vec_entities: &mut Vec = vec_entities_mut(e_type, epoch_info); // TODO: Create real entity - let _ = entity_args; // TODO: Add name, add stats repartition to clap // TODO: Load stats to entity from clap - let e = Entity::create_rng(e_type, &RngLevel::Flawed)?; + let mut e = Entity::create_rng(e_type, &RngLevel::Flawed)?; + for stat in &entity_args.stats { + e.change_stat(stat.clone()); + } + 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( e.name.clone(), e.entity_type.to_shortest(), @@ -320,3 +329,10 @@ fn find_by_args(entity: &Entity, entity_args: &EntityArgs) -> bool { false } } + +/// Find a complete id in a list of entities, if it exists +fn find_by_id(entities: &[Entity], id: &String) -> bool { + entities + .iter() + .any(|e| e.id.hyphenated().to_string().starts_with(id)) +} diff --git a/lib_efo/src/entity/implementations.rs b/lib_efo/src/entity/implementations.rs index 655e22d..afaa235 100644 --- a/lib_efo/src/entity/implementations.rs +++ b/lib_efo/src/entity/implementations.rs @@ -8,7 +8,7 @@ use crate::Displayable; use crate::entity::Entity; use crate::epoch::epoch_traits::Diffable; use crate::error::EpochError; -use crate::stats::{EntityType, MetaStats, OverallStats, Position, Symbols}; +use crate::stats::{EntityType, GenericStat, MetaStats, OverallStats, Position, StatPair, Symbols}; use crate::utils::RngLevel; // ---------- Implementation ---------- @@ -147,6 +147,26 @@ impl Entity { ..Default::default() }) } + + // TODO: Redoc + /// Change a stat with a given statpair + pub fn change_stat(&mut self, pair: StatPair) { + overwrite_stat(&mut self.stats, pair); + } +} +// TODO: Redoc +/// Overwrite a stat in a table +fn overwrite_stat(stats: &mut OverallStats, pair: StatPair) { + let affected_stat = stats.get_field_mut(&pair.stat); + *affected_stat = pair.value; +} + +/// 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(); + *affected_stat = GenericStat(current_value.saturating_add(*pair.value)); } /// Compare a field on `self` with the same field on `other`. diff --git a/lib_efo/src/stats/implementations.rs b/lib_efo/src/stats/implementations.rs index c8881d4..837791a 100644 --- a/lib_efo/src/stats/implementations.rs +++ b/lib_efo/src/stats/implementations.rs @@ -91,7 +91,7 @@ macro_rules! impl_game_stat { macro_rules! field_accessor { ($name:ident { $($field:ident),* }) => { impl $name { - fn get_field(&self, key: &str) -> Option<&dyn GameStat> { + fn get_field_from_str(&self, key: &str) -> Option<&dyn GameStat> { match key { $( stringify!($field) => Some(&self.$field), @@ -100,7 +100,7 @@ macro_rules! field_accessor { } } - fn get_field_mut(&mut self, key: &str) -> Option<&mut dyn GameStat> { + fn get_field_mut_from_str(&mut self, key: &str) -> Option<&mut dyn GameStat> { match key { $( stringify!($field) => Some(&mut self.$field), @@ -209,6 +209,71 @@ impl From for &str { } } +impl OverallStats { + //TODO: Redoc + /// Get a field by a given liststat and return a reference to it + pub fn get_field(&self, field: &ListStats) -> &GenericStat { + match field { + ListStats::Demographics => &self.demographics, + ListStats::Population => &self.population, + ListStats::Growthrate => &self.growth_rate, + ListStats::Crime => &self.crime, + ListStats::Happiness => &self.happiness, + ListStats::Economy => &self.economy, + ListStats::Food => &self.food, + ListStats::Industry => &self.industry, + ListStats::Science => &self.science, + ListStats::Trade => &self.trade, + ListStats::Social => unimplemented!("This version doens't implement this stat"), //&self.social, + ListStats::Culture => unimplemented!("This version doens't implement this stat"), //&self.culture, + ListStats::Education => unimplemented!("This version doens't implement this stat"), //&self.education, + ListStats::Tourism => unimplemented!("This version doens't implement this stat"), //&self.tourism, + ListStats::Influence => unimplemented!("This version doens't implement this stat"), //&self.influence, + ListStats::Military => &self.military, + ListStats::Army => &self.army, + ListStats::Strength => &self.strength, + ListStats::Fortification => &self.fortification, + ListStats::Threat => &self.threat, + ListStats::Infrastructure => &self.infrastructure, + ListStats::Bureaucracy => &self.bureaucracy, + ListStats::Transportation => &self.transportation, + ListStats::Sprawl => &self.sprawl, + ListStats::Health => &self.health, + } + } + + /// Get a field by a given liststat and return a reference to it + pub fn get_field_mut(&mut self, field: &ListStats) -> &mut GenericStat { + match field { + ListStats::Demographics => &mut self.demographics, + ListStats::Population => &mut self.population, + ListStats::Growthrate => &mut self.growth_rate, + ListStats::Crime => &mut self.crime, + ListStats::Happiness => &mut self.happiness, + ListStats::Economy => &mut self.economy, + ListStats::Food => &mut self.food, + ListStats::Industry => &mut self.industry, + ListStats::Science => &mut self.science, + ListStats::Trade => &mut self.trade, + ListStats::Social => unimplemented!("This version doens't implement this stat"), //&mut self.social, + ListStats::Culture => unimplemented!("This version doens't implement this stat"), //&mut self.culture, + ListStats::Education => unimplemented!("This version doens't implement this stat"), //&mut self.education, + ListStats::Tourism => unimplemented!("This version doens't implement this stat"), //&mut self.tourism, + ListStats::Influence => unimplemented!("This version doens't implement this stat"), //&mut self.influence, + ListStats::Military => &mut self.military, + ListStats::Army => &mut self.army, + ListStats::Strength => &mut self.strength, + ListStats::Fortification => &mut self.fortification, + ListStats::Threat => &mut self.threat, + ListStats::Infrastructure => &mut self.infrastructure, + ListStats::Bureaucracy => &mut self.bureaucracy, + ListStats::Transportation => &mut self.transportation, + ListStats::Sprawl => &mut self.sprawl, + ListStats::Health => &mut self.health, + } + } +} + impl FromStr for StatPair { type Err = EpochError; diff --git a/lib_efo/src/stats/implementations/test.rs b/lib_efo/src/stats/implementations/test.rs index abc882e..ac75edd 100644 --- a/lib_efo/src/stats/implementations/test.rs +++ b/lib_efo/src/stats/implementations/test.rs @@ -25,9 +25,9 @@ fn test_field_accessors() { age: GenericStat(20), email: GenericStat(120), }; - let name = person.get_field("name").unwrap(); - let email = person.get_field("email").unwrap(); - let age = person.get_field("age").unwrap(); + let name = person.get_field_from_str("name").unwrap(); + let email = person.get_field_from_str("email").unwrap(); + let age = person.get_field_from_str("age").unwrap(); assert_eq!(name.to_100(), 0); assert_eq!(age.to_100(), 10); assert_eq!(email.to_100(), 100); @@ -61,149 +61,149 @@ fn test_list_stats_all_aliases() { assert_stat_ok("Demog", ListStats::Demographics); assert_stat_ok("DEMOGRAPHICS", ListStats::Demographics); assert_stat_ok("Demo", ListStats::Demographics); - stats.get_field(ListStats::Demographics.into()); + stats.get_field_from_str(ListStats::Demographics.into()); // Population assert_stat_ok("pop", ListStats::Population); assert_stat_ok("Popul", ListStats::Population); assert_stat_ok("POPULATIONS", ListStats::Population); - stats.get_field(ListStats::Population.into()); + stats.get_field_from_str((ListStats::Population).into()); // Growthrate assert_stat_ok("grow", ListStats::Growthrate); assert_stat_ok("GRW", ListStats::Growthrate); assert_stat_ok("Growth", ListStats::Growthrate); - stats.get_field(ListStats::Growthrate.into()); + stats.get_field_from_str(ListStats::Growthrate.into()); // Crime assert_stat_ok("cri", ListStats::Crime); assert_stat_ok("CRIM", ListStats::Crime); assert_stat_ok("Crimes", ListStats::Crime); - stats.get_field(ListStats::Crime.into()); + stats.get_field_from_str(ListStats::Crime.into()); // Happiness assert_stat_ok("hapn", ListStats::Happiness); assert_stat_ok("HAPPINES", ListStats::Happiness); assert_stat_ok("hap", ListStats::Happiness); - stats.get_field(ListStats::Happiness.into()); + stats.get_field_from_str(ListStats::Happiness.into()); // Economy assert_stat_ok("eco", ListStats::Economy); assert_stat_ok("Economy", ListStats::Economy); assert_stat_ok("econ", ListStats::Economy); - stats.get_field(ListStats::Economy.into()); + stats.get_field_from_str(ListStats::Economy.into()); // Food assert_stat_ok("food", ListStats::Food); assert_stat_ok("fo", ListStats::Food); assert_stat_ok("agr", ListStats::Food); assert_stat_ok("agri", ListStats::Food); - stats.get_field(ListStats::Food.into()); + stats.get_field_from_str(ListStats::Food.into()); // Industry assert_stat_ok("industry", ListStats::Industry); assert_stat_ok("Indus", ListStats::Industry); assert_stat_ok("ind", ListStats::Industry); - stats.get_field(ListStats::Industry.into()); + stats.get_field_from_str(ListStats::Industry.into()); // Science assert_stat_ok("science", ListStats::Science); assert_stat_ok("sci", ListStats::Science); assert_stat_ok("sc", ListStats::Science); - stats.get_field(ListStats::Science.into()); + stats.get_field_from_str(ListStats::Science.into()); // Trade assert_stat_ok("trade", ListStats::Trade); assert_stat_ok("trd", ListStats::Trade); assert_stat_ok("tr", ListStats::Trade); - stats.get_field(ListStats::Trade.into()); + stats.get_field_from_str(ListStats::Trade.into()); // Social assert_stat_ok("social", ListStats::Social); assert_stat_ok("soc", ListStats::Social); assert_stat_ok("sl", ListStats::Social); - stats.get_field(ListStats::Social.into()); + stats.get_field_from_str(ListStats::Social.into()); // Culture assert_stat_ok("culture", ListStats::Culture); assert_stat_ok("cul", ListStats::Culture); assert_stat_ok("cl", ListStats::Culture); - stats.get_field(ListStats::Culture.into()); + stats.get_field_from_str(ListStats::Culture.into()); // Education assert_stat_ok("education", ListStats::Education); assert_stat_ok("edu", ListStats::Education); assert_stat_ok("ed", ListStats::Education); - stats.get_field(ListStats::Education.into()); + stats.get_field_from_str(ListStats::Education.into()); // Tourism assert_stat_ok("tourism", ListStats::Tourism); assert_stat_ok("tour", ListStats::Tourism); assert_stat_ok("tm", ListStats::Tourism); - stats.get_field(ListStats::Tourism.into()); + stats.get_field_from_str(ListStats::Tourism.into()); // Influence assert_stat_ok("influence", ListStats::Influence); assert_stat_ok("infl", ListStats::Influence); assert_stat_ok("influ", ListStats::Influence); - stats.get_field(ListStats::Influence.into()); + stats.get_field_from_str(ListStats::Influence.into()); // Military assert_stat_ok("military", ListStats::Military); assert_stat_ok("mil", ListStats::Military); assert_stat_ok("mt", ListStats::Military); - stats.get_field(ListStats::Military.into()); + stats.get_field_from_str(ListStats::Military.into()); // Army assert_stat_ok("army", ListStats::Army); assert_stat_ok("arm", ListStats::Army); assert_stat_ok("ar", ListStats::Army); - stats.get_field(ListStats::Army.into()); + stats.get_field_from_str(ListStats::Army.into()); // Strength assert_stat_ok("strength", ListStats::Strength); assert_stat_ok("str", ListStats::Strength); assert_stat_ok("st", ListStats::Strength); - stats.get_field(ListStats::Strength.into()); + stats.get_field_from_str(ListStats::Strength.into()); // Fortification assert_stat_ok("fortification", ListStats::Fortification); assert_stat_ok("fort", ListStats::Fortification); assert_stat_ok("ft", ListStats::Fortification); - stats.get_field(ListStats::Fortification.into()); + stats.get_field_from_str(ListStats::Fortification.into()); // Threat assert_stat_ok("threat", ListStats::Threat); assert_stat_ok("thr", ListStats::Threat); assert_stat_ok("th", ListStats::Threat); - stats.get_field(ListStats::Threat.into()); + stats.get_field_from_str(ListStats::Threat.into()); // Infrastructure assert_stat_ok("infrastructure", ListStats::Infrastructure); assert_stat_ok("infra", ListStats::Infrastructure); assert_stat_ok("infr", ListStats::Infrastructure); - stats.get_field(ListStats::Infrastructure.into()); + stats.get_field_from_str(ListStats::Infrastructure.into()); // Bureaucracy assert_stat_ok("bureaucracy", ListStats::Bureaucracy); assert_stat_ok("bureau", ListStats::Bureaucracy); assert_stat_ok("bur", ListStats::Bureaucracy); - stats.get_field(ListStats::Bureaucracy.into()); + stats.get_field_from_str(ListStats::Bureaucracy.into()); // Transportation assert_stat_ok("transportation", ListStats::Transportation); assert_stat_ok("trans", ListStats::Transportation); assert_stat_ok("trns", ListStats::Transportation); - stats.get_field(ListStats::Transportation.into()); + stats.get_field_from_str(ListStats::Transportation.into()); // Sprawl assert_stat_ok("sprawl", ListStats::Sprawl); assert_stat_ok("spr", ListStats::Sprawl); assert_stat_ok("spra", ListStats::Sprawl); - stats.get_field(ListStats::Sprawl.into()); + stats.get_field_from_str(ListStats::Sprawl.into()); // Health assert_stat_ok("health", ListStats::Health); assert_stat_ok("hlth", ListStats::Health); assert_stat_ok("hlt", ListStats::Health); - stats.get_field(ListStats::Health.into()); + stats.get_field_from_str(ListStats::Health.into()); }