diff options
| author | iceyrazor <iceyfoxservers@gmail.com> | 2025-02-09 14:52:59 -0600 |
|---|---|---|
| committer | iceyrazor <iceyfoxservers@gmail.com> | 2025-02-09 14:52:59 -0600 |
| commit | f52ac5b5e10fc36c87a7bb03846e8766671ab55d (patch) | |
| tree | 9079afd156de3006d7893bfd2582574749a5ed17 /src | |
the long way
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 304 |
1 files changed, 304 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..4ef6502 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,304 @@ +#![allow(unused)] +use toml::{value::Array, Table}; +//use std::io; +use std::fs; +use std::option; +use std::any::{Any, TypeId}; +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<String>, + prof_skills: Vec<String>, + skill_prof_bonuses: Vec<Vec<String>>, + base_bonuses: BaseBonuses, + base_modifiers: BaseModifiers, + list: List, +} + +#[derive(Deserialize,Iterable)] +struct BaseBonuses { + str: Option<i64>, + dex: Option<i64>, + consti: Option<i64>, + intell: Option<i64>, + wis: Option<i64>, + charis: Option<i64>, +} + +#[derive(Deserialize,Iterable)] +struct BaseModifiers{ + str: Option<i64>, + dex: Option<i64>, + consti: Option<i64>, + intell: Option<i64>, + wis: Option<i64>, + charis: Option<i64>, +} + +#[derive(Deserialize,Iterable)] +struct List{ + skills: Vec<Vec<String>>, +} + + +#[derive(Clone)] +struct SkillArr{ + name: String, + modi: i64, +} + +struct DndData{ + main_stats: Vec<u8>, + profiency: u8, + prof_throws: Vec<String>, + prof_throws_vec: Vec<i64>, + base_bonuses: Vec<i64>, + base_modifiers: Vec<i64>, + prof_skills: Vec<String>, + skill_prof_bonuses: Vec<Vec<String>>, + skills_names: Vec<Vec<String>>, + skills: Vec<SkillArr>, + throws: Vec<i64>, +} + +fn print_type_of<T>(_: &T) { + println!("{}", std::any::type_name::<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::<u8>() { + 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<i64> + match val.downcast_ref::<Option<i64>>() { + 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::<Option<i64>>() { + match option_val { + Some(x) => println!("{}, val (Option<i64>): {}", name, x), + None => println!("none in Option<i64>"), + } + } + */ + } + for (name, val) in toml_data.base_modifiers.iter() { + match val.downcast_ref::<Option<i64>>() { + 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 + } + i=0; + 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; + modi=0; + } + + 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!("---------"); + i=0; + for val in &dnd_data.skills{ + if val.name!=""{ + println!("{} : {}",val.name,val.modi); + } + } + i=0; +} + + +// need to convert to hash map +/* + // Deserialize the entire TOML data into a HashMap<String, toml::Value> + let toml_data: HashMap<String, Value> = 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<i64> + match val.downcast_ref::<Option<i64>>() { + Some(Some(x)) => { + // Handle the case where it's an Option<i64> with a value + println!("{}: {}", name, x); + }, + Some(None) => { + // Handle the case where it's an Option<i64> but the value is None + println!("{}: None", name); + }, + None => { + // Handle the case where it's not an Option<i64> + println!("{}: Not an Option<i64>", name); + } + } + } + } else { + println!("base_bonuses not found or not a table"); + } +*/ |
