From 7615c17b60679197e5404f5cd6b48f1b20970264 Mon Sep 17 00:00:00 2001 From: endernon Date: Fri, 3 Jan 2025 20:14:12 +0000 Subject: [PATCH] CraftedGear and CraftedConsu RequirementData (2/2) --- src/encode.rs | 19 +++++++++++--- src/errorfr.rs | 7 ++++- src/jsonstruct.rs | 65 +++++++++++++++++++++++++++++++++++++++++------ src/main.rs | 9 +++++-- 4 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/encode.rs b/src/encode.rs index edf2f0f..e47d9a2 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -1,6 +1,6 @@ -use crate::jsonstruct::{CraftedTypesFr, Durability, FuncParams, Identificationer, ItemTypeDeser, Powder, RequirementsDeser, Shinyjson, Shinystruct}; -use idmangler_lib::types::{Element, ItemType, RollType, Stat}; -use idmangler_lib::{CustomGearTypeData, CustomConsumableTypeData, DataEncoder, EndData, IdentificationData, NameData, PowderData, RerollData, ShinyData, StartData, TypeData, DurabilityData}; +use crate::jsonstruct::{ClassDeser, CraftedTypesFr, Durability, FuncParams, Identificationer, ItemTypeDeser, Powder, RequirementsDeser, Shinyjson, Shinystruct, SkillPointDeser}; +use idmangler_lib::types::{ClassType, Element, ItemType, RollType, SkillType, Stat}; +use idmangler_lib::{CustomGearTypeData, CustomConsumableTypeData, DataEncoder, EndData, IdentificationData, NameData, PowderData, RequirementsData, RerollData, ShinyData, StartData, TypeData, DurabilityData}; use std::collections::HashMap; use crate::errorfr::Errorfr; @@ -78,7 +78,18 @@ pub fn encode_duradata(general_params: &mut FuncParams, real_dura: Durability) - Ok(()) } pub fn encode_reqdata(general_params: &mut FuncParams, real_reqdata: RequirementsDeser) { - + let mut fr_class: Option = None; + if let Some(actualclass) = real_reqdata.class { + fr_class = Some(ClassType::from(actualclass)) + } + let spvec: Vec<(SkillType, i32)> = Vec::<(SkillType, i32)>::from(real_reqdata.sp); + RequirementsData { + level: real_reqdata.level, + class: fr_class, + skills: spvec + } + .encode(general_params.fr_ver, general_params.fr_out) + .unwrap() } pub fn encode_namedata(general_params: &mut FuncParams, real_name: &str) { // ENCODE: NameData diff --git a/src/errorfr.rs b/src/errorfr.rs index 5c39ff4..02b165f 100644 --- a/src/errorfr.rs +++ b/src/errorfr.rs @@ -61,6 +61,11 @@ pub enum Errorfr { #[error("Error 4.4: Durability percentage is out of range (Should be between 0 and 100)")] JsonDuraOutOfRange, + /// Durability was not found but is necessary #[error("Error 4.5: \"Durability\" was not found (necessary for Crafted Gear item type)")] - JsonNotFoundDura + JsonNotFoundDura, + + /// Durability Bad + #[error("Error 4.6: \"Requirements\" was not found (necessary for Crafted Gear / Consumable items)")] + JsonNotFoundReqs } diff --git a/src/jsonstruct.rs b/src/jsonstruct.rs index 0b252bf..70e9071 100644 --- a/src/jsonstruct.rs +++ b/src/jsonstruct.rs @@ -3,6 +3,7 @@ use idmangler_lib::types::{ItemType, TransformVersion, ConsumableType, GearType} use serde::Deserialize; use std::fs; use idmangler_lib::types::{ConsumableType::*,GearType::*, ClassType, SkillType}; +use idmangler_lib::types::Element::Earth; use crate::jsonstruct::CraftedTypesFr::{Consu, Gear}; // structs for the json parsing @@ -31,9 +32,9 @@ pub struct Jsonconfig { // This avoids confusing end user. #[derive(Deserialize)] pub struct RequirementsDeser { - level: u8, - class: Option, - sp: SkillPointDeser + pub level: u8, + pub class: Option, + pub sp: SkillPointDeser } #[derive(Deserialize)] pub enum ClassDeser { @@ -43,13 +44,61 @@ pub enum ClassDeser { Mage, Shaman } +impl From for ClassType { + fn from(value: ClassDeser) -> Self { + match value { + ClassDeser::Archer => ClassType::Archer, + ClassDeser::Warrior => ClassType::Warrior, + ClassDeser::Assassin => ClassType::Assasin, + ClassDeser::Mage => ClassType::Mage, + ClassDeser::Shaman => ClassType::Shaman + } + } +} #[derive(Deserialize)] pub struct SkillPointDeser { - Earth: Option, - Thunder: Option, - Water: Option, - Fire: Option, - Air: Option + #[serde(alias = "Str")] + #[serde(alias = "str")] + #[serde(alias = "strength")] + pub strength: Option, + #[serde(alias = "Dex")] + #[serde(alias = "dex")] + #[serde(alias = "dexterity")] + pub dexterity: Option, + #[serde(alias = "Def")] + #[serde(alias = "def")] + #[serde(alias = "defense")] + pub defense: Option, + #[serde(alias = "Int")] + #[serde(alias = "int")] + #[serde(alias = "intelligence")] + pub intelligence: Option, + #[serde(alias = "Agi")] + #[serde(alias = "agi")] + #[serde(alias = "agility")] + pub agility: Option +} + +impl From for Vec<(SkillType, i32)> { + fn from(value: SkillPointDeser) -> Self { + let mut returnedvec: Vec<(SkillType, i32)> = Vec::new(); + if let Some(fr_str) = value.strength { + returnedvec.push((SkillType::Strength, fr_str)) + } + if let Some(fr_dex) = value.dexterity { + returnedvec.push((SkillType::Dexterity, fr_dex)) + } + if let Some(fr_int) = value.intelligence { + returnedvec.push((SkillType::Intelligence, fr_int)) + } + if let Some(fr_def) = value.defense { + returnedvec.push((SkillType::Defence, fr_def)) + } + if let Some(fr_agi) = value.agility { + returnedvec.push((SkillType::Agility, fr_agi)) + } + returnedvec + } } pub enum CraftedTypesFr { Gear(GearType), diff --git a/src/main.rs b/src/main.rs index 54cb2e9..43826f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -160,7 +160,7 @@ fn cook( _ => {} } - // ENCODE: DurabilityData (OPTIONAL) (REQUIRED for CraftedGear) + // ENCODE: DurabilityData (REQUIRED for CraftedGear) match json_config.item_type { ItemTypeDeser::CraftedGear => { if let Some(real_dura) = json_config.durability { @@ -176,7 +176,12 @@ fn cook( // ENCODE: RequirementsData if ItemType is CraftedGear, CraftedConsu match json_config.item_type { ItemTypeDeser::CraftedGear | ItemTypeDeser::CraftedConsu => { - + if let Some(real_reqs) = json_config.requirements { + encode_reqdata(&mut fr_params, real_reqs) + } + else { + return Err(Errorfr::JsonNotFoundReqs) + } }, _ => {} }