diff --git a/Cargo.toml b/Cargo.toml index da84f18..1537cfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,4 +28,5 @@ derive_more = { version = "0.99.3", features = ["display"]} thiserror = "1.0.15" ages-prs = "0.1" async-trait = "0.1.31" +lazy_static = "1.4.0" diff --git a/src/entity/item/mag.rs b/src/entity/item/mag.rs index 84bfc81..cd3d002 100644 --- a/src/entity/item/mag.rs +++ b/src/entity/item/mag.rs @@ -1,5 +1,57 @@ +use std::collections::HashMap; use serde::{Serialize, Deserialize}; use crate::entity::item::tool::ToolType; +use std::io::Read; + +#[derive(Debug, Deserialize)] +struct MagStats { + feed_table: u32, +} + +#[derive(Debug, Deserialize)] +struct MagFeedTable { + def: i32, + pow: i32, + dex: i32, + mnd: i32, + iq: i32, + syn: i32, +} + +lazy_static::lazy_static! { + static ref MAG_STATS: HashMap = { + let mut f = std::fs::File::open("data/item_stats/mag_stats.toml").unwrap(); + let mut s = String::new(); + f.read_to_string(&mut s).unwrap(); + + let mag_stats: HashMap = toml::from_str(&s).unwrap(); + mag_stats.into_iter() + .map(|(name, stats)| { + (name.parse().unwrap(), stats) + }) + .collect::>() + }; + + static ref MAG_FEEDING_TABLES: Vec> = { + let mut f = std::fs::File::open("data/item_stats/mag_feed_table.toml").unwrap(); + let mut s = String::new(); + f.read_to_string(&mut s).unwrap(); + + let mut feed: HashMap>> = toml::from_str(&s).unwrap(); + let feed = feed.remove("feedtable".into()).unwrap(); + feed.into_iter() + .map(|table| { + table.into_iter() + .map(|(tool, stats)| { + (tool.parse().unwrap(), stats) + }) + .collect() + }) + .collect::>>() + }; +} + + #[derive(Debug, Copy, Clone)] pub enum ItemParseError { @@ -389,4 +441,44 @@ impl Mag { Err(ItemParseError::InvalidMagBytes) // TODO: error handling if wrong bytes are given } } + + +#[cfg(test)] +mod test { + use super::*; + use std::io::Read; + + #[test] + fn test_load_mag_stats() { + let mut f = std::fs::File::open("data/item_stats/mag_stats.toml").unwrap(); + let mut s = String::new(); + f.read_to_string(&mut s).unwrap(); + + let mags: HashMap = toml::from_str(&s).unwrap(); + let _mags = mags.into_iter() + .map(|(name, stats)| { + (name.parse().unwrap(), stats) + }) + .collect::>(); + } + + #[test] + fn test_load_mag_feed_table() { + let mut f = std::fs::File::open("data/item_stats/mag_feed_table.toml").unwrap(); + let mut s = String::new(); + f.read_to_string(&mut s).unwrap(); + + let mut feed: HashMap>> = toml::from_str(&s).unwrap(); + let feed = feed.remove("feedtable".into()).unwrap(); + let _feed = feed.into_iter() + .map(|table| { + table.into_iter() + .map(|(tool, stats)| { + (tool.parse().unwrap(), stats) + }) + .collect() + }) + .collect::>>(); + } + }