Working base import

This commit is contained in:
Dandaedre 2024-05-08 23:58:53 +02:00
parent 589047baef
commit 621e82e8b6
2 changed files with 116 additions and 32 deletions

View File

@ -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<Affinity>,
}
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<Affinity>,
selected_affinities: Vec<AffName>,
}
impl TryFrom<StringRecord> for Creature {
type Error = anyhow::Error;
fn try_from(value: StringRecord) -> std::prelude::v1::Result<Self, Self::Error> {
let mut iterable = value.into_iter();
let creature = iterable.next().context("Getting name")?;
let aff = iterable
.map(|m| m.parse::<f64>().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<StringRecord> for CreatureCharacteristicsAffinity {
type Error = anyhow::Error;
fn try_from(value: StringRecord) -> Result<Self> {
let aff = value
.into_iter()
.skip(1)
.map(|m| m.parse::<f64>().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<AffName>,
}
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<Vec<(usize, Affinity)>>,
}
struct Creature {
affinities: CreatureCharacteristicsAffinity,
actives_affinities: Vec<Affinity>,
selected_affinities: Vec<AffName>,
/// A translation vector to remember what is what
creatures: Vec<Creature>,
}
impl Creature {
// Get most desired affinity
// Get list of selected affinities
impl From<Vec<Creature>> for CharacCharMatrix {
fn from(creatures: Vec<Creature>) -> 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<CreatureCharacteristicsAffinity> = rdr
// .into_records()
// .flat_map(|c| CreatureCharacteristicsAffinity::try_from(c.unwrap()))
// .collect();
// println!("{:#?}", creatures_aff);
let creatures: Vec<Creature> = rdr
.into_records()
.flat_map(|c| Creature::try_from(c.unwrap()))
.collect();
let creature_matrix = CharacCharMatrix::from(creatures);
println!("{:#?}", creature_matrix);
Ok(())
}

View File

@ -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(())
}