Skip to content

Commit

Permalink
✨ Add CalculatorConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
luoshuijs committed Oct 25, 2023
1 parent 5f7033d commit e4283ad
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
40 changes: 40 additions & 0 deletions python_genshin_artifact_core/src/applications/input/calculator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use mona::artifacts::Artifact;
use mona::artifacts::effect_config::ArtifactConfigInterface;
use mona_wasm::applications::common::{BuffInterface, CharacterInterface, EnemyInterface, SkillInterface, WeaponInterface};
use pyo3::prelude::*;

#[derive(Debug, Clone)]
#[pyclass]
pub struct CalculatorConfig {
pub character: CharacterInterface,
// pub weapon: WeaponInterface,
// pub buffs: Vec<BuffInterface>,
// pub artifacts: Vec<Artifact>,
// pub artifact_config: Option<ArtifactConfigInterface>,
// pub skill: SkillInterface,
// pub enemy: Option<EnemyInterface>,
}

#[pymethods]
impl CalculatorConfig {
#[new]
// #[args(buffs="Vec::new()", artifacts="Vec::new()", artifact_config="None", enemy="None")]
fn new(character: CharacterInterface,
// weapon: WeaponInterface,
// buffs: Vec<BuffInterface>,
// artifacts: Vec<Artifact>,
// artifact_config: Option<ArtifactConfigInterface>,
// skill: SkillInterface,
// enemy: Option<EnemyInterface>,
) -> Self {
CalculatorConfig {
character,
// weapon,
// buffs,
// artifacts,
// artifact_config,
// skill,
// enemy,
}
}
}
74 changes: 74 additions & 0 deletions python_genshin_artifact_core/src/applications/input/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::str::FromStr;
use mona::character::{CharacterConfig, CharacterName};
use mona_wasm::applications::common::CharacterInterface;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyDict;

#[derive(Clone)]
#[pyclass(name = "CharacterInterface")]
pub struct PyCharacterInterface {
pub name: String,
pub level: usize,
pub ascend: bool,
pub constellation: i32,
pub skill1: usize,
pub skill2: usize,
pub skill3: usize,
pub params: Option<PyDict>,
}

#[pymethods]
impl PyCharacterInterface {
#[new]
fn new(name: String,
level: usize,
ascend: bool,
constellation: i32,
skill1: usize,
skill2: usize,
skill3: usize,
params: Option<PyDict>,
) -> Self {
PyCharacterInterface {
name,
level,
ascend,
constellation,
skill1,
skill2,
skill3,
params,
}
}


}

impl PyCharacterInterface {

fn py_dict_to_character_config(py_dict: &PyDict) -> PyResult<CharacterConfig> {
let json_str = py_dict.to_string()?;
let json_value = serde_json::from_str(&json_str)?;
let character_config: CharacterConfig = serde_json::from_value(json_value)?;
Ok(character_config)
}

pub fn to_rust(&self) -> CharacterInterface {
let name = CharacterName::from_str(&self.name).map_err(|e| PyValueError::new_err((e.to_string(), &self.name, 0)))?;
let params = self.params.as_ref().map_or(Ok(CharacterConfig::NoConfig), |py_dict| {
self.py_dict_to_character_config(py_dict).map_err(|e| PyValueError::new_err((e.to_string(), &self.params, 0)))?
})?;
CharacterInterface {
name,
level: self.level.clone(),
ascend: self.ascend.clone(),
constellation: self.constellation.clone(),
skill1: self.skill1.clone(),
skill2: self.skill2.clone(),
skill3: self.skill3.clone(),
params,
}
}
}

3 changes: 3 additions & 0 deletions python_genshin_artifact_core/src/applications/input/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod calculator;
pub mod common;
pub mod character;
1 change: 1 addition & 0 deletions python_genshin_artifact_core/src/applications/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod generate;
pub mod wasm;
pub mod input;
2 changes: 2 additions & 0 deletions python_genshin_artifact_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use applications::generate::weapon::gen_weapon_meta_as_json;
use applications::wasm::{get_damage_analysis, get_transformative_damage};
use pyo3::import_exception;
use pyo3::prelude::*;
use crate::applications::input::calculator::CalculatorConfig;

import_exception!(json, JSONDecodeError);

Expand All @@ -20,5 +21,6 @@ fn genshin_artifact_core(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(gen_weapon_meta_as_json, m)?)?;
m.add_function(wrap_pyfunction!(gen_artifact_meta_as_json, m)?)?;
m.add_function(wrap_pyfunction!(gen_generate_locale_as_json, m)?)?;
m.add_class::<CalculatorConfig>()?;
Ok(())
}

0 comments on commit e4283ad

Please sign in to comment.