Browse Source

load mag feeding tables

pbs
jake 4 years ago
parent
commit
19dd5309cd
  1. 1
      Cargo.toml
  2. 92
      src/entity/item/mag.rs

1
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"

92
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<MagType, MagStats> = {
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<String, MagStats> = toml::from_str(&s).unwrap();
mag_stats.into_iter()
.map(|(name, stats)| {
(name.parse().unwrap(), stats)
})
.collect::<HashMap<MagType, MagStats>>()
};
static ref MAG_FEEDING_TABLES: Vec<HashMap<ToolType, MagFeedTable>> = {
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<String, Vec<HashMap<String, MagFeedTable>>> = 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::<Vec<HashMap<ToolType, MagFeedTable>>>()
};
}
#[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<String, MagStats> = toml::from_str(&s).unwrap();
let _mags = mags.into_iter()
.map(|(name, stats)| {
(name.parse().unwrap(), stats)
})
.collect::<HashMap<MagType, MagStats>>();
}
#[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<String, Vec<HashMap<String, MagFeedTable>>> = 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::<Vec<HashMap<ToolType, MagFeedTable>>>();
}
}
Loading…
Cancel
Save