Skip to content

Commit

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

#[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>,
pub character: PyCharacterInterface,
}

#[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,
}
pub fn py_new( character: PyCharacterInterface) -> PyResult<Self> {
Ok(Self {character})
}
}
64 changes: 27 additions & 37 deletions python_genshin_artifact_core/src/applications/input/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::str::FromStr;
use mona::character::{CharacterConfig, CharacterName};
use mona_wasm::applications::common::CharacterInterface;
use pyo3::exceptions::PyValueError;
use mona_wasm::applications::common::CharacterInterface as MonaCharacterInterface;
use pyo3::prelude::*;
use pyo3::types::PyDict;

#[derive(Clone)]
#[pyclass(name = "CharacterInterface")]
#[derive(Clone)]
pub struct PyCharacterInterface {
pub name: String,
pub level: usize,
Expand All @@ -15,22 +14,23 @@ pub struct PyCharacterInterface {
pub skill1: usize,
pub skill2: usize,
pub skill3: usize,
pub params: Option<PyDict>,
pub params: Option<Py<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 {
pub fn py_new(
name: String,
level: usize,
ascend: bool,
constellation: i32,
skill1: usize,
skill2: usize,
skill3: usize,
params: Option<Py<PyDict>>,
) -> PyResult<Self> {
Ok(Self {
name,
level,
ascend,
Expand All @@ -39,36 +39,26 @@ impl PyCharacterInterface {
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 {
pub fn to_mona(&self) -> MonaCharacterInterface {
let name = CharacterName::from_str(&self.name).unwrap();
// todo : params 转换为 CharacterConfig::NoConfig
MonaCharacterInterface {
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,
level: self.level,
ascend: self.ascend,
constellation: self.constellation,
skill1: self.skill1,
skill2: self.skill2,
skill3: self.skill3,
params: CharacterConfig::NoConfig
}
}
}


3 changes: 1 addition & 2 deletions python_genshin_artifact_core/src/applications/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod calculator;
pub mod common;
pub mod character;
pub mod common;
2 changes: 1 addition & 1 deletion python_genshin_artifact_core/src/applications/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod generate;
pub mod wasm;
pub mod input;
pub mod input;

0 comments on commit 2ce6868

Please sign in to comment.