From bcbabc4902c1116ffcaf10388ef237bc1dd43b39 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Tue, 11 Feb 2025 23:22:01 -0600 Subject: [PATCH] a new beggining --- src/main.rs | 290 +--------------------------------------------------- 1 file changed, 3 insertions(+), 287 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1fc6284..b6e8387 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,75 +1,7 @@ //#![allow(unused)] //use std::io; use std::fs; -use serde::Deserialize; -use struct_iterable::Iterable; - -// https://crates.io/crates/toml -// https://docs.rs/toml/0.8.20/toml/ -// https://crates.io/crates/ncurses - -#[derive(Deserialize,Iterable)] -struct Config { - str: u8, - dex: u8, - consti: u8, - intell: u8, - wis: u8, - charis: u8, - profiency: u8, - prof_throws: Vec, - prof_skills: Vec, - skill_prof_bonuses: Vec>, - base_bonuses: BaseBonuses, - base_modifiers: BaseModifiers, - list: List, -} - -#[derive(Deserialize,Iterable)] -struct BaseBonuses { - str: Option, - dex: Option, - consti: Option, - intell: Option, - wis: Option, - charis: Option, -} - -#[derive(Deserialize,Iterable)] -struct BaseModifiers{ - str: Option, - dex: Option, - consti: Option, - intell: Option, - wis: Option, - charis: Option, -} - -#[derive(Deserialize,Iterable)] -struct List{ - skills: Vec>, -} - - -#[derive(Clone)] -struct SkillArr{ - name: String, - modi: i64, -} - -struct DndData{ - main_stats: Vec, - profiency: u8, - prof_throws: Vec, - prof_throws_vec: Vec, - base_bonuses: Vec, - base_modifiers: Vec, - prof_skills: Vec, - skill_prof_bonuses: Vec>, - skills_names: Vec>, - skills: Vec, - throws: Vec, -} +use toml::Table; fn print_type_of(_: &T) { println!("{}", std::any::type_name::()); @@ -77,222 +9,6 @@ fn print_type_of(_: &T) { fn main() { let toml_str: String = fs::read_to_string("data.toml").expect("error"); - let toml_data: Config = toml::from_str(&toml_str).unwrap(); - let mut dnd_data = DndData{ - main_stats: vec![0,0,0,0,0,0], - profiency: toml_data.profiency, - prof_throws: vec![String::new(); 100], - prof_throws_vec: vec![0,0,0,0,0,0], - prof_skills: vec![String::new(); 100], - base_bonuses: vec![0,0,0,0,0,0], - base_modifiers: vec![0,0,0,0,0,0], - skill_prof_bonuses: vec![vec![String::new(); 2]; 100], - skills_names: vec![vec![String::new(); 2]; 100], - throws: vec![0,0,0,0,0,0], - skills: vec![SkillArr{ name: String::new(), modi: 0}; 100], - }; - for (name, val) in toml_data.iter() { - match val.downcast_ref::() { - Some(x) => { - match name{ - "str" => dnd_data.main_stats[0]=*x, - "dex" => dnd_data.main_stats[1]=*x, - "consti" => dnd_data.main_stats[2]=*x, - "intell" => dnd_data.main_stats[3]=*x, - "wis" => dnd_data.main_stats[4]=*x, - "charis" => dnd_data.main_stats[5]=*x, - _ => {}, - } - }, - _ => {}, - } - } - let mut i=0; - for name in &toml_data.prof_throws { - dnd_data.prof_throws[i]=name.to_string(); - i=i+1; - } - i=0; - for name in &toml_data.skill_prof_bonuses{ - dnd_data.skill_prof_bonuses[i][0]=name[0].to_string(); - dnd_data.skill_prof_bonuses[i][1]=name[1].to_string(); - i=i+1; - } - i=0; - for name in &toml_data.prof_throws { - match name.as_str(){ - "str" => dnd_data.prof_throws_vec[0]=dnd_data.profiency as i64, - "dex" => dnd_data.prof_throws_vec[1]=dnd_data.profiency as i64, - "consti" => dnd_data.prof_throws_vec[2]=dnd_data.profiency as i64, - "intell" => dnd_data.prof_throws_vec[3]=dnd_data.profiency as i64, - "wis" => dnd_data.prof_throws_vec[4]=dnd_data.profiency as i64, - "charis" => dnd_data.prof_throws_vec[5]=dnd_data.profiency as i64, - _ => {}, - } - i=i+1; - } - i=0; - for name in &toml_data.prof_skills { - dnd_data.prof_skills[i]=name.to_string(); - i=i+1; - } - i=0; - for name in &toml_data.list.skills { - dnd_data.skills_names[i][0]=name[0].to_string(); - dnd_data.skills_names[i][1]=name[1].to_string(); - i=i+1; - } - - for (name, val) in toml_data.base_bonuses.iter() { - // Try to downcast to Option - match val.downcast_ref::>() { - Some(Some(x)) => { - match name{ - "str" => dnd_data.base_bonuses[0]=*x, - "dex" => dnd_data.base_bonuses[1]=*x, - "consti" => dnd_data.base_bonuses[2]=*x, - "intell" => dnd_data.base_bonuses[3]=*x, - "wis" => dnd_data.base_bonuses[4]=*x, - "charis" => dnd_data.base_bonuses[5]=*x, - _ => {}, - } - }, - _ => {}, - } - - /* - if let Some(option_val) = val.downcast_ref::>() { - match option_val { - Some(x) => println!("{}, val (Option): {}", name, x), - None => println!("none in Option"), - } - } - */ - } - for (name, val) in toml_data.base_modifiers.iter() { - match val.downcast_ref::>() { - Some(Some(x)) => { - match name{ - "str" => dnd_data.base_modifiers[0]=*x, - "dex" => dnd_data.base_modifiers[1]=*x, - "consti" => dnd_data.base_modifiers[2]=*x, - "intell" => dnd_data.base_modifiers[3]=*x, - "wis" => dnd_data.base_modifiers[4]=*x, - "charis" => dnd_data.base_modifiers[5]=*x, - _ => {}, - } - }, - _ => {}, - } - } - - i=0; - for val in &dnd_data.base_bonuses{ - dnd_data.main_stats[i]=dnd_data.main_stats[i]+*val as u8; - i=i+1; - } - i=0; - for val in &dnd_data.base_modifiers{ - dnd_data.throws[i]=dnd_data.prof_throws_vec[i] + val; - i=i+1 - } - fn base_to_num(str: String) -> usize{ - match str.as_str(){ - "str" => return 0, - "dex" => return 1, - "consti" => return 2, - "intell" => return 3, - "wis" => return 4, - "charis" => return 5, - _ => return 0, - } - } - - i=0; - for name in &dnd_data.skills_names{ - let mut modi=0; - for prof_skills in &dnd_data.prof_skills{ - if name[0]==*prof_skills && name[0]!="" { - modi=dnd_data.profiency; - } - } - for skill_prof_bonuses in &dnd_data.skill_prof_bonuses{ - if name[1]==skill_prof_bonuses[1] && name[0]!="" { - modi=modi+dnd_data.base_modifiers[base_to_num(String::from(skill_prof_bonuses[0].as_str()))] as u8; - } - } - modi=modi+dnd_data.base_modifiers[base_to_num(String::from(name[1].as_str()))] as u8; - dnd_data.skills[i].name=String::from(name[0].as_str()); - dnd_data.skills[i].modi=modi as i64; - - i=i+1; - } - - println!("profiency: {}",dnd_data.profiency); - fn num_to_base(num: usize) -> String{ - match num{ - 0 => return String::from("str"), - 1 => return String::from("dex"), - 2 => return String::from("consti"), - 3 => return String::from("intell"), - 4 => return String::from("wis"), - 5 => return String::from("charis"), - _ => return String::from("Null"), - } - } - println!("---------"); - i=0; - for val in &dnd_data.main_stats{ - println!("{}: {}",num_to_base(i),val); - i=i+1 - } - println!("---------"); - i=0; - for val in &dnd_data.base_modifiers{ - println!("mod {}: {}",num_to_base(i),val); - i=i+1 - } - println!("---------"); - i=0; - for val in &dnd_data.throws{ - println!("throws {}: {}",num_to_base(i),val); - i=i+1 - } - println!("---------"); - for val in &dnd_data.skills{ - if val.name!=""{ - println!("{} : {}",val.name,val.modi); - } - } + let toml_data = toml_str.parse::().unwrap(); + println!("debug: {}",toml_data["base_bonuses"]["beans"]); } - - -// need to convert to hash map -/* -// Deserialize the entire TOML data into a HashMap -let toml_data: HashMap = from_str(toml_str).unwrap(); - -// Accessing base_bonuses as a Table (nested structure) -if let Some(base_bonuses) = toml_data.get("base_bonuses").and_then(Value::as_table) { -// Iterate over the keys and values in the base_bonuses table -for (name, val) in base_bonuses.iter() { -// Try to downcast to Option -match val.downcast_ref::>() { -Some(Some(x)) => { -// Handle the case where it's an Option with a value -println!("{}: {}", name, x); -}, -Some(None) => { -// Handle the case where it's an Option but the value is None -println!("{}: None", name); -}, -None => { -// Handle the case where it's not an Option -println!("{}: Not an Option", name); -} -} -} -} else { -println!("base_bonuses not found or not a table"); -} -*/