Skip to content

Commit

Permalink
✨ Add __dict__ for CalculatorConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Billyzou0741326 committed Nov 7, 2023
1 parent 6847ba6 commit 18a4724
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
22 changes: 10 additions & 12 deletions src/applications/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ pub fn get_damage_analysis(calculator_config: PyCalculatorConfig) -> PyResult<Py
} else {
None
};
let artifact_config =
if let Some(artifact_config) = artifact_config_interface {
artifact_config.to_config()
} else {
ArtifactEffectConfig::default()
};
let artifact_config = if let Some(artifact_config) = artifact_config_interface {
artifact_config.to_config()
} else {
ArtifactEffectConfig::default()
};

let buffs = calculator_config
.buffs
Expand Down Expand Up @@ -124,12 +123,11 @@ pub fn get_transformative_damage(
} else {
None
};
let artifact_config =
if let Some(artifact_config) = artifact_config_interface {
artifact_config.to_config()
} else {
ArtifactEffectConfig::default()
};
let artifact_config = if let Some(artifact_config) = artifact_config_interface {
artifact_config.to_config()
} else {
ArtifactEffectConfig::default()
};

let buffs = calculator_config
.buffs
Expand Down
11 changes: 7 additions & 4 deletions src/applications/input/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ impl PyArtifact {
dict.set_item("slot", self.slot.as_ref(py))?;
dict.set_item("level", self.level)?;
dict.set_item("star", self.star)?;
let sub_stats_pylist = PyList::new(py, self.sub_stats.iter().map(|(s, v)| {
let stat_str = s.as_ref(py).to_str().unwrap();
(stat_str, *v)
}));
let sub_stats_pylist = PyList::new(
py,
self.sub_stats.iter().map(|(s, v)| {
let stat_str = s.as_ref(py).to_str().unwrap();
(stat_str, *v)
}),
);
dict.set_item("sub_stats", sub_stats_pylist)?;
let main_stat_tuple = (self.main_stat.0.as_ref(py), self.main_stat.1);
dict.set_item("main_stat", main_stat_tuple)?;
Expand Down
5 changes: 4 additions & 1 deletion src/applications/input/buff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ impl PyBuffInterface {
Some(config) => config.as_ref(py).repr()?.to_str()?.to_string(),
None => "None".to_string(),
};
Ok(format!("BuffInterface(name={}, config={})", name, config_repr))
Ok(format!(
"BuffInterface(name={}, config={})",
name, config_repr
))
}

#[getter]
Expand Down
33 changes: 32 additions & 1 deletion src/applications/input/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::applications::input::skill::PySkillInterface;
use crate::applications::input::weapon::PyWeaponInterface;

use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::types::{PyDict, PyList};

#[pyclass(name = "CalculatorConfig")]
#[derive(Clone)]
Expand Down Expand Up @@ -50,4 +50,35 @@ impl PyCalculatorConfig {
enemy,
})
}

#[getter]
pub fn __dict__(&self, py: Python) -> PyResult<PyObject> {
let dict = PyDict::new(py);
dict.set_item("character", self.character.__dict__(py)?)?;
dict.set_item("weapon", self.weapon.__dict__(py)?)?;
let buffs = self
.buffs
.iter()
.map(|b| b.__dict__(py))
.collect::<Result<Vec<PyObject>, PyErr>>()?;
dict.set_item("buffs", PyList::new(py, buffs))?;
let artifacts = self
.artifacts
.iter()
.map(|ar| ar.__dict__(py))
.collect::<Result<Vec<PyObject>, PyErr>>()?;
dict.set_item("artifacts", PyList::new(py, artifacts))?;
if let Some(artifact_config) = self.artifact_config.as_ref().map(|c| c.as_ref(py)) {
dict.set_item("artifact_config", artifact_config)?;
} else {
dict.set_item("artifact_config", py.None())?;
}
dict.set_item("skill", self.skill.__dict__(py)?)?;
if let Some(enemy) = self.enemy.as_ref().map(|e| e.__dict__(py)).transpose()? {
dict.set_item("enemy", enemy)?;
} else {
dict.set_item("enemy", py.None())?;
}
Ok(dict.into())
}
}
6 changes: 4 additions & 2 deletions src/applications/input/skill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ impl PySkillInterface {
Ok(Self { index, config })
}
pub fn __repr__(&self) -> PyResult<String> {
Ok(format!("SkillInterface(index: {}, config: {:?})", self.index, self.config))
Ok(format!(
"SkillInterface(index: {}, config: {:?})",
self.index, self.config
))
}

#[getter]
Expand All @@ -36,7 +39,6 @@ impl PySkillInterface {
}
Ok(dict.into())
}

}

impl TryInto<MonaSkillInterface> for PySkillInterface {
Expand Down

0 comments on commit 18a4724

Please sign in to comment.