From e5455fddd88e14a57e04246ad4b3ca9b3e2f4613 Mon Sep 17 00:00:00 2001 From: endernon Date: Thu, 2 Jan 2025 20:25:59 +0000 Subject: [PATCH] implement DurabilityData --- src/encode.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++-- src/errorfr.rs | 7 +++++++ src/jsonstruct.rs | 9 ++++++++- src/main.rs | 18 ++++++++++++++++- 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/encode.rs b/src/encode.rs index 46f5d8f..1dd3ebc 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -1,6 +1,6 @@ -use crate::jsonstruct::{CraftedTypesFr, FuncParams, Identificationer, ItemTypeDeser, Powder, Shinyjson, Shinystruct}; +use crate::jsonstruct::{CraftedTypesFr, Durability, FuncParams, Identificationer, ItemTypeDeser, Powder, Shinyjson, Shinystruct}; use idmangler_lib::types::{Element, ItemType, RollType, Stat}; -use idmangler_lib::{CustomGearTypeData, CustomConsumableTypeData, DataEncoder, EndData, IdentificationData, NameData, PowderData, RerollData, ShinyData, StartData, TypeData}; +use idmangler_lib::{CustomGearTypeData, CustomConsumableTypeData, DataEncoder, EndData, IdentificationData, NameData, PowderData, RerollData, ShinyData, StartData, TypeData, DurabilityData}; use std::collections::HashMap; use crate::errorfr::Errorfr; @@ -32,6 +32,51 @@ pub fn encode_typedata_custom(general_params: &mut FuncParams, crafted_type: &st } Ok(()) } +pub fn encode_duradata(general_params: &mut FuncParams, real_dura: Durability) -> Result<(), Errorfr> { + let effect_strength_fr: u8; // but actually it should be 0 to 100, not 0 to 255. But i dunno how to use u7 data type. + if let Some(effstr) = real_dura.effect_strength { + effect_strength_fr = effstr + } + else { + let current_percentage = real_dura.dura_cur / real_dura.dura_max; // percentage of max durability + if current_percentage > 100 { + return Err(Errorfr::JsonDuraOutOfRange) + } + if current_percentage >= 50 { // for 100% dura to 50% dura, the effectiveness is 100% + effect_strength_fr = 100 + } + else if current_percentage >= 10 { // for 50% dura to 10% dura, the effectiveness is 100% to 50% + // see this answer from Stackoverflow for transcribing range + // https://stackoverflow.com/a/929107 + + // old range is 50-10 = 40 + let old_range = 40; + // new range is 100-50 = 50 + let new_range = 50; + // NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin + effect_strength_fr = ((((current_percentage - 10) * new_range) / old_range) + 50) as u8 + } + else if current_percentage >= 0 { // for 10% dura to 0% dura, the effectiveness is 50% to 10% + // old range is 10-0 = 10 + let old_range = 10; + // new range is 50-10 = 40 + let new_range = 40; + // NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin + effect_strength_fr = ((((current_percentage - 0) * new_range) / old_range) + 10) as u8 + } + else { + return Err(Errorfr::JsonDuraOutOfRange) + } + } + DurabilityData { + effect_strenght: effect_strength_fr, + current: real_dura.dura_cur, + max: real_dura.dura_max + } + .encode(general_params.fr_ver, general_params.fr_out) + .unwrap(); + Ok(()) +} pub fn encode_namedata(general_params: &mut FuncParams, real_name: &str) { // ENCODE: NameData NameData(real_name.trim().to_string()) diff --git a/src/errorfr.rs b/src/errorfr.rs index 8e817ff..5c39ff4 100644 --- a/src/errorfr.rs +++ b/src/errorfr.rs @@ -56,4 +56,11 @@ pub enum Errorfr { /// invalid crafted_type field #[error("Error 4.3: Invalid \"crafted_type\" value")] JsonInvalidCraftedType, + + /// Durability is out of allowed range (0 to 100) + #[error("Error 4.4: Durability percentage is out of range (Should be between 0 and 100)")] + JsonDuraOutOfRange, + + #[error("Error 4.5: \"Durability\" was not found (necessary for Crafted Gear item type)")] + JsonNotFoundDura } diff --git a/src/jsonstruct.rs b/src/jsonstruct.rs index abd335e..effbd70 100644 --- a/src/jsonstruct.rs +++ b/src/jsonstruct.rs @@ -15,6 +15,8 @@ pub struct Jsonconfig { pub crafted_type: Option, // name of item pub name: Option, + // durability data (Crafted Gear) + pub durability: Option, // shiny data pub shiny: Option, pub ids: Option>, @@ -57,7 +59,12 @@ impl TryFrom<&str> for CraftedTypesFr { } } } - +#[derive(Deserialize)] +pub struct Durability { + pub effect_strength: Option, + pub dura_cur: i32, + pub dura_max: i32 +} #[derive(Deserialize)] pub struct Shinystruct { pub id: u8, diff --git a/src/main.rs b/src/main.rs index b0feaa5..7055358 100644 --- a/src/main.rs +++ b/src/main.rs @@ -160,6 +160,22 @@ fn cook( _ => {} } + // ENCODE: DurabilityData (OPTIONAL) (REQUIRED for CraftedGear) + match json_config.item_type { + ItemTypeDeser::CraftedGear => { + if let Some(real_dura) = json_config.durability { + let resulted = encode_duradata(&mut fr_params, real_dura); + if let Err(e) = resulted { + return Err(e) + } + } + else { + return Err(Errorfr::JsonNotFoundDura) + } + }, + _ => {} + } + // ENCODE: PowderData if ItemType is Gear, CraftedGear match json_config.item_type { ItemTypeDeser::Gear | ItemTypeDeser::CraftedGear => { @@ -185,7 +201,7 @@ fn cook( match json_config.item_type { ItemTypeDeser::Gear => { if let Some(shiny) = json_config.shiny { - encode_shiny(&mut fr_params, shiny, json_shiny) + encode_shinydata(&mut fr_params, shiny, json_shiny) } } _ => {}