Compare commits

...

3 Commits

Author SHA1 Message Date
iceyrazor 962d407b3f removed dependencies 2025-02-12 18:19:15 -06:00
iceyrazor a715ebf60c fin for now 2025-02-12 18:14:29 -06:00
iceyrazor bcbabc4902 a new beggining 2025-02-11 23:22:01 -06:00
3 changed files with 88 additions and 359 deletions

79
Cargo.lock generated
View File

@ -2,22 +2,10 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "cc"
version = "1.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "755717a7de9ec452bf7f3f1a3099085deabd7f2962b861dae91ecd7a365903d2"
dependencies = [
"shlex",
]
[[package]]
name = "dnd-calc"
version = "0.1.0"
dependencies = [
"ncurses",
"serde",
"struct_iterable",
"toml",
]
@ -27,15 +15,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "erased-serde"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c138974f9d5e7fe373eb04df7cae98833802ae4b11c24ac7039a21d5af4b26c"
dependencies = [
"serde",
]
[[package]]
name = "hashbrown"
version = "0.15.2"
@ -52,35 +31,12 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "libc"
version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "ncurses"
version = "6.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbd71afa95710e841d173521e8483e764004eb332bdf47bd01d00f568688027f"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]]
name = "pkg-config"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]]
name = "proc-macro2"
version = "1.0.93"
@ -128,41 +84,6 @@ dependencies = [
"serde",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "struct_iterable"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "849a064c6470a650b72e41fa6c057879b68f804d113af92900f27574828e7712"
dependencies = [
"struct_iterable_derive",
"struct_iterable_internal",
]
[[package]]
name = "struct_iterable_derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bb939ce88a43ea4e9d012f2f6b4cc789deb2db9d47bad697952a85d6978662c"
dependencies = [
"erased-serde",
"proc-macro2",
"quote",
"struct_iterable_internal",
"syn",
]
[[package]]
name = "struct_iterable_internal"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9426b2a0c03e6cc2ea8dbc0168dbbf943f88755e409fb91bcb8f6a268305f4a"
[[package]]
name = "syn"
version = "2.0.98"

View File

@ -1,10 +1,7 @@
[package]
name = "dnd-calc"
version = "0.1.0"
version = "1.0.0"
edition = "2021"
[dependencies]
ncurses = "6.0.1"
serde = { version = "1.0.217", features = ["derive"] }
struct_iterable = "0.1.1"
toml = "0.8.20"

View File

@ -1,75 +1,8 @@
//#![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<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>,
}
use toml::{Table, Value};
use std::collections::HashMap;
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>());
@ -77,222 +10,100 @@ fn print_type_of<T>(_: &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 toml_data = toml_str.parse::<Table>().unwrap();
let mut throws = HashMap::new();
let mut skills = HashMap::new();
let binding = toml_data.clone();
//for whatever reason. if i dont have this as a var, and instead put toml_data.clone in the
//loop below, the throws.insert makes a borrow error. i dont get it
for (key,val) in &binding{
match key.as_str(){
"base_bonuses" => {
if let Some(base_bonuses) = val.as_table() {
for (base_key, base_val) in base_bonuses {
//toml_data["str"] = toml::Value::Integer(base_val);
if let (Some(existing_num), Some(base_num)) = (toml_data[base_key].as_integer(), base_val.as_integer()) {
toml_data[base_key] = Value::Integer(existing_num + base_num);
}
}
}
// for (base_key, base_val) in val.as_table_mut().unwrap_or(&Default::default()) {
// toml_data["str"] = Value::Integer(0);
// }
//print_type_of(&val.get("base_bonuses"));
},
"base_modifiers" =>{
if let Some(base_modifiers) = val.as_table(){
for(base_key, base_val) in base_modifiers{
if let (Some(profiency), Some(base_num)) = (toml_data["profiency"].as_integer(), base_val.as_integer()) {
if toml_data["prof_throws"]
.as_array()
.map_or(false, |arr| arr.iter().any(|val2| val2.as_str().map_or(false, |s| s.contains(base_key))))
{
throws.insert(base_key,profiency + base_num);
} else {
throws.insert(base_key,base_num);
}
}
}
}
},
_ => {},
}
}
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;
}
"list" =>{
if let Some(list) = val.as_table(){
if let Some(base_skills) = list["skills"].as_array(){
for skill in base_skills{
//make this cleaner
if let (Some(skill_name),Some(skill_mod),Some(base_modifiers),Some(profiency),Some(skill_bonus)) = (skill[0].as_str(),skill[1].as_str(),toml_data["base_modifiers"].as_table(),toml_data["profiency"].as_integer(),toml_data["skill_prof_bonuses"].as_array()){
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,
_ => {},
let mut add_number=0;
if toml_data["prof_skills"]
.as_array()
.map_or(false, |arr| arr.iter().any(|val2| val2.as_str().map_or(false, |s| s.contains(&String::from(skill_name)))))
{
add_number = add_number + base_modifiers[skill_mod].as_integer().expect("none") + profiency;
} else {
add_number = add_number + base_modifiers[skill_mod].as_integer().expect("none");
}
for bone in skill_bonus{
if skill_mod == bone[1].as_str().expect("none"){
add_number = add_number + base_modifiers[bone[0].as_str().expect("none")].as_integer().expect("none");
}
}
skills.insert(skill_name,add_number);
}
},
_ => {},
}
/*
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
}
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,
//fin
for (key,val) in toml_data{
match key.as_str(){
"str" => println!("{}:{}",key,val),
"dex" => println!("{}:{}",key,val),
"consti" => println!("{}:{}",key,val),
"intell" => println!("{}:{}",key,val),
"wis" => println!("{}:{}",key,val),
"charis" => println!("{}:{}",key,val),
"profiency" => println!("{}:{}",key,val),
_ => {},
}
}
println!("-------");
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 (key,val) in throws.iter(){
println!("throws: {}::{}",key,val);
}
}
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;
println!("-------");
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);
}
for (key,val) in skills.iter(){
println!("skills: {}::{}",key,val);
}
}
// 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");
}
*/