142 lines
5.1 KiB
Rust
142 lines
5.1 KiB
Rust
use hashbrown::HashSet;
|
|
use pkmn_lib::static_data::{DataLibrary, EffectParameter, MoveData, MoveLibrary, SecondaryEffect, TypeLibrary};
|
|
use pkmn_lib::StringKey;
|
|
use project_root::get_project_root;
|
|
use serde_json::Value;
|
|
use std::fs::File;
|
|
use std::io::Read;
|
|
|
|
pub fn load_types(path: &String) -> TypeLibrary {
|
|
let mut type_library = TypeLibrary::new(18);
|
|
|
|
let mut reader = csv::ReaderBuilder::new()
|
|
.delimiter(b'|')
|
|
.from_path(path.to_string() + "Types.csv")
|
|
.unwrap();
|
|
let headers = reader.headers().unwrap();
|
|
for header in headers.iter().skip(1) {
|
|
type_library.register_type(&StringKey::new(header.clone()));
|
|
}
|
|
|
|
for record in reader.records() {
|
|
let record = record.unwrap();
|
|
let offensive_type = record.get(0).unwrap();
|
|
let offensive_type_id = type_library.get_type_id(&StringKey::new(offensive_type.clone()));
|
|
|
|
for (i, v) in record.iter().skip(1).enumerate() {
|
|
let effectiveness = v.parse::<f32>().unwrap();
|
|
type_library.set_effectiveness(offensive_type_id, i as u8, effectiveness);
|
|
}
|
|
}
|
|
|
|
type_library
|
|
}
|
|
|
|
pub fn load_moves(path: &String, type_library: &TypeLibrary) -> MoveLibrary {
|
|
let mut file = File::open(path.to_string() + "Moves.json").unwrap();
|
|
let mut data = String::new();
|
|
file.read_to_string(&mut data).unwrap();
|
|
println!("1");
|
|
let json: Value = serde_json::from_str(&data).unwrap();
|
|
println!("a");
|
|
let data = json.as_object().unwrap().get("data").unwrap().as_array().unwrap();
|
|
println!("this");
|
|
let mut move_library = MoveLibrary::new(data.len());
|
|
println!("Heeere");
|
|
for move_data in data {
|
|
let move_data = move_data.as_object().unwrap();
|
|
let move_name = StringKey::new(move_data["name"].as_str().unwrap().clone());
|
|
println!("Loaded move {:?}", move_name);
|
|
let move_type = StringKey::new(move_data["type"].as_str().unwrap());
|
|
let move_type_id = type_library.get_type_id(&move_type);
|
|
let move_category = serde_json::from_value(move_data["category"].clone()).unwrap();
|
|
let base_power = move_data["power"].as_i64().unwrap() as u8;
|
|
let accuracy = move_data["accuracy"].as_i64().unwrap() as u8;
|
|
let pp = move_data["pp"].as_i64().unwrap() as u8;
|
|
let target = serde_json::from_value(move_data["target"].clone()).unwrap();
|
|
let priority = move_data["priority"].as_i64().unwrap() as i8;
|
|
let secondary_effect = if let Some(v) = move_data.get("effect") {
|
|
let mut chance = -1.0;
|
|
if let Some(chance_value) = v.get("chance") {
|
|
chance = chance_value.as_f64().unwrap() as f32;
|
|
}
|
|
let mut parameters = Vec::new();
|
|
if let Some(pars) = v.get("parameters") {
|
|
let pars = pars.as_array().unwrap();
|
|
for par in pars {
|
|
match par {
|
|
Value::Null => {
|
|
panic!("Unexpected type")
|
|
}
|
|
Value::Bool(b) => {
|
|
parameters.push(EffectParameter::Bool(*b));
|
|
}
|
|
Value::Number(n) => {
|
|
if n.is_f64() {
|
|
parameters.push(EffectParameter::Float(n.as_f64().unwrap() as f32));
|
|
} else {
|
|
parameters.push(EffectParameter::Int(n.as_i64().unwrap()));
|
|
}
|
|
}
|
|
Value::String(s) => {
|
|
parameters.push(EffectParameter::String(s.clone()));
|
|
}
|
|
Value::Array(_) => {
|
|
panic!("Unexpected type")
|
|
}
|
|
Value::Object(_) => {
|
|
panic!("Unexpected type")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
SecondaryEffect::new(chance, StringKey::new(v["name"].as_str().unwrap().clone()), parameters)
|
|
} else {
|
|
SecondaryEffect::empty()
|
|
};
|
|
|
|
let mut flags = HashSet::new();
|
|
if let Some(f) = move_data.get("flags") {
|
|
let f = f.as_array().unwrap();
|
|
for flag in f {
|
|
flags.insert(StringKey::new(flag.as_str().unwrap()));
|
|
}
|
|
}
|
|
|
|
move_library.add(
|
|
&move_name,
|
|
MoveData::new(
|
|
&move_name.clone(),
|
|
move_type_id,
|
|
move_category,
|
|
base_power,
|
|
accuracy,
|
|
pp,
|
|
target,
|
|
priority,
|
|
secondary_effect,
|
|
flags,
|
|
),
|
|
);
|
|
}
|
|
|
|
move_library
|
|
}
|
|
|
|
#[test]
|
|
#[cfg_attr(miri, ignore)]
|
|
fn test_type_library_loaded() {
|
|
let mut path = get_project_root().unwrap();
|
|
path.push("tests/data/");
|
|
let lib = load_types(&path.to_str().unwrap().to_string());
|
|
|
|
assert_eq!(
|
|
lib.get_effectiveness(
|
|
lib.get_type_id(&StringKey::new("fire")),
|
|
&vec![lib.get_type_id(&StringKey::new("grass")),]
|
|
),
|
|
2.0
|
|
);
|
|
}
|