implement DurabilityData

This commit is contained in:
endernon 2025-01-02 20:25:59 +00:00
parent 5c17f1c21e
commit e5455fddd8
4 changed files with 79 additions and 4 deletions

View file

@ -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::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 std::collections::HashMap;
use crate::errorfr::Errorfr; use crate::errorfr::Errorfr;
@ -32,6 +32,51 @@ pub fn encode_typedata_custom(general_params: &mut FuncParams, crafted_type: &st
} }
Ok(()) 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) { pub fn encode_namedata(general_params: &mut FuncParams, real_name: &str) {
// ENCODE: NameData // ENCODE: NameData
NameData(real_name.trim().to_string()) NameData(real_name.trim().to_string())

View file

@ -56,4 +56,11 @@ pub enum Errorfr {
/// invalid crafted_type field /// invalid crafted_type field
#[error("Error 4.3: Invalid \"crafted_type\" value")] #[error("Error 4.3: Invalid \"crafted_type\" value")]
JsonInvalidCraftedType, 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
} }

View file

@ -15,6 +15,8 @@ pub struct Jsonconfig {
pub crafted_type: Option<String>, pub crafted_type: Option<String>,
// name of item // name of item
pub name: Option<String>, pub name: Option<String>,
// durability data (Crafted Gear)
pub durability: Option<Durability>,
// shiny data // shiny data
pub shiny: Option<Shinyjson>, pub shiny: Option<Shinyjson>,
pub ids: Option<Vec<Identificationer>>, pub ids: Option<Vec<Identificationer>>,
@ -57,7 +59,12 @@ impl TryFrom<&str> for CraftedTypesFr {
} }
} }
} }
#[derive(Deserialize)]
pub struct Durability {
pub effect_strength: Option<u8>,
pub dura_cur: i32,
pub dura_max: i32
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Shinystruct { pub struct Shinystruct {
pub id: u8, pub id: u8,

View file

@ -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 // ENCODE: PowderData if ItemType is Gear, CraftedGear
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::Gear | ItemTypeDeser::CraftedGear => { ItemTypeDeser::Gear | ItemTypeDeser::CraftedGear => {
@ -185,7 +201,7 @@ fn cook(
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::Gear => { ItemTypeDeser::Gear => {
if let Some(shiny) = json_config.shiny { if let Some(shiny) = json_config.shiny {
encode_shiny(&mut fr_params, shiny, json_shiny) encode_shinydata(&mut fr_params, shiny, json_shiny)
} }
} }
_ => {} _ => {}