Skip to content

Commit

Permalink
🎨 Add __dict__ and __repr__ for damage statistics struct
Browse files Browse the repository at this point in the history
  • Loading branch information
luoshuijs committed Nov 10, 2023
1 parent 29b6300 commit 6ace746
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/applications/output/damage_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -69,6 +70,75 @@ pub struct PyDamageAnalysis {
pub aggravate: Option<PyDamageResult>,
}


#[pymethods]
impl PyDamageAnalysis {

#[getter]
fn __dict__(&self, py: Python) -> PyResult<PyObject> { // skipcq: RS-R1000
let dict = PyDict::new(py);

fn insert_hashmap(dict: &PyDict, py: Python, key: &str, hashmap: &HashMap<String, f64>) -> 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<MonaDamageAnalysis> for PyDamageAnalysis {
fn from(damage_analysis: MonaDamageAnalysis) -> Self {
let element = damage_analysis.element.to_string();
Expand Down
25 changes: 25 additions & 0 deletions src/applications/output/damage_result.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use mona::damage::damage_result::DamageResult as MonaDamageResult;
use pyo3::prelude::*;
use pyo3::types::PyDict;

#[pyclass(name = "DamageResult")]
#[derive(Clone)]
Expand Down Expand Up @@ -34,6 +35,30 @@ impl PyDamageResult {
is_shield,
})
}

pub fn __repr__(&self) -> PyResult<String> {
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<PyObject> {
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<MonaDamageResult> for PyDamageResult {
Expand Down
20 changes: 20 additions & 0 deletions src/applications/output/transformative_damage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use mona::damage::transformative_damage::TransformativeDamage as MonaTransformativeDamage;
use pyo3::prelude::*;
use pyo3::types::PyDict;

#[pyclass(name = "TransformativeDamage")]
#[derive(Clone)]
Expand Down Expand Up @@ -66,6 +67,25 @@ impl PyTransformativeDamage {
crystallize,
})
}

#[getter]
pub fn __dict__(&self, py: Python) -> PyResult<PyObject> {
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<MonaTransformativeDamage> for PyTransformativeDamage {
Expand Down

0 comments on commit 6ace746

Please sign in to comment.