From 6ace746f5d17a10c8def7a4f16ac0f5ae7cd9723 Mon Sep 17 00:00:00 2001 From: luoshuijs Date: Fri, 10 Nov 2023 14:16:31 +0800 Subject: [PATCH] :art: Add `__dict__` and `__repr__` for damage statistics struct --- src/applications/output/damage_analysis.rs | 70 +++++++++++++++++++ src/applications/output/damage_result.rs | 25 +++++++ .../output/transformative_damage.rs | 20 ++++++ 3 files changed, 115 insertions(+) diff --git a/src/applications/output/damage_analysis.rs b/src/applications/output/damage_analysis.rs index 3e002e0..2e7835b 100644 --- a/src/applications/output/damage_analysis.rs +++ b/src/applications/output/damage_analysis.rs @@ -2,6 +2,7 @@ use crate::applications::output::damage_result::PyDamageResult; use mona::damage::DamageAnalysis as MonaDamageAnalysis; use pyo3::prelude::*; use std::collections::HashMap; +use pyo3::types::PyDict; #[pyclass(name = "DamageAnalysis")] #[derive(Clone)] @@ -69,6 +70,75 @@ pub struct PyDamageAnalysis { pub aggravate: Option, } + +#[pymethods] +impl PyDamageAnalysis { + + #[getter] + fn __dict__(&self, py: Python) -> PyResult { // skipcq: RS-R1000 + let dict = PyDict::new(py); + + fn insert_hashmap(dict: &PyDict, py: Python, key: &str, hashmap: &HashMap) -> PyResult<()> { + let hashmap_dict = PyDict::new(py); + for (k, &v) in hashmap.iter() { + hashmap_dict.set_item(k, v)?; + } + dict.set_item(key, hashmap_dict)?; + Ok(()) + } + + insert_hashmap(dict, py, "atk", &self.atk)?; + insert_hashmap(dict, py, "atk_ratio", &self.atk_ratio)?; + insert_hashmap(dict, py, "hp", &self.hp)?; + insert_hashmap(dict, py, "hp_ratio", &self.hp_ratio)?; + insert_hashmap(dict, py, "defense", &self.def)?; + insert_hashmap(dict, py, "def_ratio", &self.def_ratio)?; + insert_hashmap(dict, py, "em", &self.em)?; + insert_hashmap(dict, py, "em_ratio", &self.em_ratio)?; + insert_hashmap(dict, py, "extra_damage", &self.extra_damage)?; + insert_hashmap(dict, py, "bonus", &self.bonus)?; + insert_hashmap(dict, py, "critical", &self.critical)?; + insert_hashmap(dict, py, "critical_damage", &self.critical_damage)?; + insert_hashmap(dict, py, "melt_enhance", &self.melt_enhance)?; + insert_hashmap(dict, py, "vaporize_enhance", &self.vaporize_enhance)?; + insert_hashmap(dict, py, "healing_bonus", &self.healing_bonus)?; + insert_hashmap(dict, py, "shield_strength", &self.shield_strength)?; + insert_hashmap(dict, py, "spread_compose", &self.spread_compose)?; + insert_hashmap(dict, py, "aggravate_compose", &self.aggravate_compose)?; + insert_hashmap(dict, py, "def_minus", &self.def_minus)?; + insert_hashmap(dict, py, "def_penetration", &self.def_penetration)?; + insert_hashmap(dict, py, "res_minus", &self.res_minus)?; + + dict.set_item("element", &self.element)?; + dict.set_item("is_heal", self.is_heal)?; + dict.set_item("is_shield", self.is_shield)?; + + dict.set_item("normal", self.normal.__dict__(py)?)?; + if let Some(melt) = self.melt.as_ref().map(|e| e.__dict__(py)).transpose()? { + dict.set_item("melt", melt)?; + } else { + dict.set_item("melt", py.None())?; + } + if let Some(vaporize) = self.vaporize.as_ref().map(|e| e.__dict__(py)).transpose()? { + dict.set_item("vaporize", vaporize)?; + } else { + dict.set_item("vaporize", py.None())?; + } + if let Some(spread) = self.spread.as_ref().map(|e| e.__dict__(py)).transpose()? { + dict.set_item("spread", spread)?; + } else { + dict.set_item("spread", py.None())?; + } + if let Some(aggravate) = self.aggravate.as_ref().map(|e| e.__dict__(py)).transpose()? { + dict.set_item("aggravate", aggravate)?; + } else { + dict.set_item("aggravate", py.None())?; + } + + Ok(dict.into()) + } +} + impl From for PyDamageAnalysis { fn from(damage_analysis: MonaDamageAnalysis) -> Self { let element = damage_analysis.element.to_string(); diff --git a/src/applications/output/damage_result.rs b/src/applications/output/damage_result.rs index c4fe14d..abb7631 100644 --- a/src/applications/output/damage_result.rs +++ b/src/applications/output/damage_result.rs @@ -1,5 +1,6 @@ use mona::damage::damage_result::DamageResult as MonaDamageResult; use pyo3::prelude::*; +use pyo3::types::PyDict; #[pyclass(name = "DamageResult")] #[derive(Clone)] @@ -34,6 +35,30 @@ impl PyDamageResult { is_shield, }) } + + pub fn __repr__(&self) -> PyResult { + Ok(format!( + "DamageResult(critical={}, non_critical={}, expectation={}, is_heal={}, is_shield={})", + self.critical, + self.non_critical, + self.expectation, + self.is_heal, + self.is_shield + )) + } + + #[getter] + pub fn __dict__(&self, py: Python) -> PyResult { + let dict = PyDict::new(py); + dict.set_item("critical", self.critical)?; + dict.set_item("non_critical", self.non_critical)?; + dict.set_item("expectation", self.expectation)?; + dict.set_item("is_heal", self.is_heal)?; + dict.set_item("is_shield", self.is_shield)?; + Ok(dict.into()) + } + + } impl From for PyDamageResult { diff --git a/src/applications/output/transformative_damage.rs b/src/applications/output/transformative_damage.rs index b388394..11c7e01 100644 --- a/src/applications/output/transformative_damage.rs +++ b/src/applications/output/transformative_damage.rs @@ -1,5 +1,6 @@ use mona::damage::transformative_damage::TransformativeDamage as MonaTransformativeDamage; use pyo3::prelude::*; +use pyo3::types::PyDict; #[pyclass(name = "TransformativeDamage")] #[derive(Clone)] @@ -66,6 +67,25 @@ impl PyTransformativeDamage { crystallize, }) } + + #[getter] + pub fn __dict__(&self, py: Python) -> PyResult { + let dict = PyDict::new(py); + dict.set_item("swirl_cryo", self.swirl_cryo)?; + dict.set_item("swirl_hydro", self.swirl_hydro)?; + dict.set_item("swirl_pyro", self.swirl_pyro)?; + dict.set_item("swirl_electro", self.swirl_electro)?; + dict.set_item("overload", self.overload)?; + dict.set_item("electro_charged", self.electro_charged)?; + dict.set_item("shatter", self.shatter)?; + dict.set_item("super_conduct", self.super_conduct)?; + dict.set_item("bloom", self.bloom)?; + dict.set_item("hyper_bloom", self.hyper_bloom)?; + dict.set_item("burgeon", self.burgeon)?; + dict.set_item("burning", self.burning)?; + dict.set_item("crystallize", self.crystallize)?; + Ok(dict.into()) + } } impl From for PyTransformativeDamage {