diff --git a/src/stat_io.rs b/src/stat_io.rs index efc7b14..29baeb6 100644 --- a/src/stat_io.rs +++ b/src/stat_io.rs @@ -3,35 +3,72 @@ #![allow(clippy::missing_docs_in_private_items)] use std::{collections::HashMap, fs::File, path::Path}; -use anyhow::Result; +use anyhow::{Context, Result}; +use csv::StringRecord; use serde::{Deserialize, Serialize}; /// How to represent mathematically the affinity for a stat type Affinity = f64; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug)] /// A row representing the creature affinity for a stat struct CreatureCharacteristicsAffinity { - /// Strength - f: Affinity, - /// Agility - ag: Affinity, - /// Perception - per: Affinity, - /// Psychic power - psy: Affinity, - /// Intellect - int: Affinity, - /// Willpower - vol: Affinity, - /// Luck - chn: Affinity, - /// Charisma - chr: Affinity, + affinity: Vec, +} + +impl CreatureCharacteristicsAffinity { + fn get_affinity(&self, affinity: AffName) -> Affinity { + self.affinity[affinity as usize] + } +} + +#[derive(Debug)] +struct Creature { + name: String, + // id : usize, + affinities: CreatureCharacteristicsAffinity, + actives_affinities: Vec, + selected_affinities: Vec, +} + +impl TryFrom for Creature { + type Error = anyhow::Error; + + fn try_from(value: StringRecord) -> std::prelude::v1::Result { + let mut iterable = value.into_iter(); + let creature = iterable.next().context("Getting name")?; + let aff = iterable + .map(|m| m.parse::().context("Parsing string").unwrap()) + .collect(); + Ok(Self { + name: creature.to_owned(), + affinities: CreatureCharacteristicsAffinity { affinity: aff }, + actives_affinities: Vec::new(), + selected_affinities: Vec::new(), + }) + } +} + +impl Creature { + // Get most desired affinity + // Get list of selected affinities +} + +impl TryFrom for CreatureCharacteristicsAffinity { + type Error = anyhow::Error; + + fn try_from(value: StringRecord) -> Result { + let aff = value + .into_iter() + .skip(1) + .map(|m| m.parse::().context("Parsing string").unwrap()) + .collect(); + Ok(Self { affinity: aff }) + } } -#[repr(u8)] /// The names of all affinities (essentialy a list of characteristics) +#[derive(Deserialize, Serialize, Debug)] enum AffName { F, Ag, @@ -43,6 +80,18 @@ enum AffName { Chr, } +#[inline] +fn get_aff_index(affinity: AffName) -> usize { + affinity as usize +} + +#[derive(Deserialize, Serialize)] +struct Solution { + creature: String, + affinities: CreatureCharacteristicsAffinity, + picks: Vec, +} + impl CreatureCharacteristicsAffinity { // Create an affinity from a record } @@ -53,21 +102,40 @@ struct CreaturesAffinity { matrix: CharacCharMatrix, } +#[derive(Debug)] /// A matrix representing: List of characteristics and associated list of creatures /// All math is done at this level to avoid cost /// Right now the index of every creature is used in the matrix, but a references could be used instead struct CharacCharMatrix { + /// The inner vector structure, used to access rapidly the underlying affinities inner: Vec>, -} -struct Creature { - affinities: CreatureCharacteristicsAffinity, - actives_affinities: Vec, - selected_affinities: Vec, + /// A translation vector to remember what is what + creatures: Vec, } -impl Creature { - // Get most desired affinity - // Get list of selected affinities +impl From> for CharacCharMatrix { + fn from(creatures: Vec) -> Self { + let size = creatures.len() + 1; + let mut inner = vec![ + Vec::<(usize, Affinity)>::with_capacity(size), + Vec::<(usize, Affinity)>::with_capacity(size), + Vec::<(usize, Affinity)>::with_capacity(size), + Vec::<(usize, Affinity)>::with_capacity(size), + Vec::<(usize, Affinity)>::with_capacity(size), + Vec::<(usize, Affinity)>::with_capacity(size), + Vec::<(usize, Affinity)>::with_capacity(size), + Vec::<(usize, Affinity)>::with_capacity(size), + ]; + + println!("I: {:?}", creatures); + for (creature_id, creature) in creatures.iter().enumerate() { + for (index, value) in creature.affinities.affinity.iter().enumerate() { + inner[index].push((creature_id, *value)); + } + } + + Self { inner, creatures } + } } /// Load all the records in the csv @@ -86,8 +154,22 @@ where .from_reader(file); println!("{:?}", rdr); println!("{:?}", rdr.headers().cloned()); - for rec in rdr.into_records() { - println!("{:?}", rec); - } + // for rec in rdr.into_records() { + // println!("{:?}", rec); + // println!("{:?}", CreatureCharacteristicsAffinity::try_from(rec?)); + // } + // let creatures_aff: Vec = rdr + // .into_records() + // .flat_map(|c| CreatureCharacteristicsAffinity::try_from(c.unwrap())) + // .collect(); + + // println!("{:#?}", creatures_aff); + + let creatures: Vec = rdr + .into_records() + .flat_map(|c| Creature::try_from(c.unwrap())) + .collect(); + let creature_matrix = CharacCharMatrix::from(creatures); + println!("{:#?}", creature_matrix); Ok(()) } diff --git a/src/stats.rs b/src/stats.rs index fff5506..8b14c68 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -5,7 +5,7 @@ use anyhow::Result; use clap::Args; -use crate::{stat_io::load_stat_records, Config, ParserCLIArgs}; +use crate::{stat_io::load_stat_records, Config}; #[derive(Args, Debug)] pub(crate) struct StatConfig { @@ -15,7 +15,9 @@ pub(crate) struct StatConfig { /// Main solver function, in charge of selecting algorithms and handling all program flow pub(crate) fn stat_solver(args: &StatConfig, config: &Config) -> Result<()> { - load_stat_records(&"data/Book1.csv"); + let _ = config; + let _ = args; + let _ = load_stat_records("data/statw.csv"); Ok(()) }