Add __dict__ for CalculatorConfig

This commit is contained in:
kotoriのねこ 2023-11-07 20:24:00 -05:00 committed by GitHub
parent 413be213be
commit 009124d4d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 33 deletions

View File

@ -20,14 +20,13 @@
## Build
- Navigate to core -> run cargo build
- (Optional) Run cargo build
```
$ cd python_genshin_artifact_core
$ cargo build
$ cargo build --no-default-features
...
Compiling pyo3-ffi v0.19.2
Compiling pyo3 v0.19.2
Compiling genshin_artifact_core v0.1.1 (/home/kotori/projects/python/python-genshin-artifact/python_genshin_artifact_core)
Compiling python_genshin_artifact v0.1.1 (/home/kotori/projects/python/python-genshin-artifact/python_genshin_artifact_core)
Finished dev [unoptimized + debuginfo] target(s) in 3.12s
```
- Use maturin to install the rust library into a python library
@ -35,20 +34,18 @@
$ maturin develop
...
📦 Built wheel for CPython 3.11 to /tmp/.tmpicGM3M/genshin_artifact_core-0.1.1-cp311-cp311-linux_x86_64.whl
🛠 Installed genshin_artifact_core-0.1.1
🛠 Installed python_genshin_artifact-0.1.1
```
- Navigate back to project root and build with poetry:
- Use maturin to build the wheel
```
$ poetry build
Building Python-Genshin-Artifact (0.1.1)
- Building sdist
- Built python_genshin_artifact-0.1.1.tar.gz
- Building wheel
- Built python_genshin_artifact-0.1.1-py3-none-any.whl
$ maturin build --out dist
...
Finished dev [unoptimized + debuginfo] target(s) in 3.88s
📦 Built wheel for CPython 3.11 to dist/python_genshin_artifact-0.1.4-cp311-cp311-manylinux_2_34_x86_64.whl
```
- Install the built wheel with pip:
```
$ pip install dist/python_genshin_artifact-0.1.1-py3-none-any.whl
$ pip install dist/python_genshin_artifact-xxx.whl
```

View File

@ -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
@ -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

View File

@ -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)?;

View File

@ -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]

View File

@ -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)]
@ -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())
}
}

View File

@ -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]
@ -36,7 +39,6 @@ impl PySkillInterface {
}
Ok(dict.into())
}
}
impl TryInto<MonaSkillInterface> for PySkillInterface {