Skip to content

Commit

Permalink
✨ Add Weapon and Buff
Browse files Browse the repository at this point in the history
  • Loading branch information
luoshuijs committed Oct 28, 2023
1 parent eb483e8 commit c34710f
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 8 deletions.
57 changes: 57 additions & 0 deletions python_genshin_artifact_core/src/applications/input/buff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use anyhow::anyhow;
use pyo3::prelude::*;
use pythonize::depythonize;
use mona::buffs::BuffConfig;
use mona::buffs::buff_name::BuffName;

use pyo3::types::{PyDict, PyString};

use mona_wasm::applications::common::BuffInterface as MonaBuffInterface;

#[pyclass(name = "BuffInterface")]
#[derive(Clone)]
pub struct PyBuffInterface {
pub name: Py<PyString>,
pub config: Option<Py<PyDict>>,
}

#[pymethods]
impl PyBuffInterface {
#[new]
pub fn py_new(
name: Py<PyString>,
config: Option<Py<PyDict>>,
) -> PyResult<Self> {
Ok(Self {
name,
config,
})
}
}

impl TryInto<MonaBuffInterface> for PyBuffInterface {
type Error = anyhow::Error;

fn try_into(self) -> Result<MonaBuffInterface, Self::Error> {
let name:BuffName = Python::with_gil(|py| {
let _string: &PyString = self.name.as_ref(py);
depythonize(_string).map_err(|err| anyhow!("Failed to deserialize name: {}", err))
})?;

let config: BuffConfig = if let Some(value) = self.config {
Python::with_gil(|py| {
let _dict: &PyDict = value.as_ref(py);
depythonize(_dict).map_err(|err| anyhow!("Failed to deserialize config: {}", err))
})?
} else {
BuffConfig::NoConfig
};

Ok(MonaBuffInterface {
name,
config,
})
}
}


14 changes: 11 additions & 3 deletions python_genshin_artifact_core/src/applications/input/calculator.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
use crate::applications::input::common::PyCharacterInterface;
use crate::applications::input::character::PyCharacterInterface;
use crate::applications::input::weapon::PyWeaponInterface;
use crate::applications::input::buff::PyBuffInterface;
use pyo3::prelude::*;

#[pyclass]
pub struct CalculatorConfig {
pub character: PyCharacterInterface,
pub weapon: PyWeaponInterface,
pub buffs: Vec<PyBuffInterface>,
}

#[pymethods]
impl CalculatorConfig {
#[new]
pub fn py_new(character: PyCharacterInterface) -> PyResult<Self> {
Ok(Self { character })
pub fn py_new(
character: PyCharacterInterface,
weapon: PyWeaponInterface,
buffs: Vec<PyBuffInterface>,
) -> PyResult<Self> {
Ok(Self { character, weapon, buffs })
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Context;
use mona::character::{CharacterConfig, CharacterName};
use mona_wasm::applications::common::CharacterInterface as MonaCharacterInterface;
use pyo3::prelude::*;
use pythonize::depythonize;
use std::str::FromStr;
use pyo3::types::PyDict;
use pyo3::prelude::*;

use mona::character::{CharacterConfig, CharacterName};
use mona_wasm::applications::common::CharacterInterface as MonaCharacterInterface;

#[pyclass(name = "CharacterInterface")]
#[derive(Clone)]
Expand Down Expand Up @@ -54,7 +55,8 @@ impl TryInto<MonaCharacterInterface> for PyCharacterInterface {
if let Some(value) = self.params {
Python::with_gil(|py| {
let _dict: &PyDict = value.as_ref(py);
params = depythonize(_dict).unwrap();;
params = depythonize(_dict).unwrap();
;
})
}
Ok(MonaCharacterInterface {
Expand Down
4 changes: 3 additions & 1 deletion python_genshin_artifact_core/src/applications/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod calculator;
pub mod common;
pub mod character;
pub mod weapon;
pub mod buff;
64 changes: 64 additions & 0 deletions python_genshin_artifact_core/src/applications/input/weapon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use anyhow::anyhow;
use mona_wasm::applications::common::WeaponInterface as MonaWeaponInterface;
use pyo3::prelude::*;
use pythonize::depythonize;
use mona::weapon::{WeaponConfig, WeaponName};
use pyo3::types::{PyDict, PyString};

#[pyclass(name = "WeaponInterface")]
#[derive(Clone)]
pub struct PyWeaponInterface {
pub name: Py<PyString>,
pub level: i32,
pub ascend: bool,
pub refine: i32,
pub params: Option<Py<PyDict>>,
}

#[pymethods]
impl PyWeaponInterface {
#[new]
pub fn py_new(
name: Py<PyString>,
level: i32,
ascend: bool,
refine: i32,
params: Option<Py<PyDict>>,
) -> PyResult<Self> {
Ok(Self {
name,
level,
ascend,
refine,
params,
})
}
}


impl TryInto<MonaWeaponInterface> for PyWeaponInterface {
type Error = anyhow::Error;

fn try_into(self) -> Result<MonaWeaponInterface, Self::Error> {
let name: WeaponName = Python::with_gil(|py| {
let _string: &PyString = self.name.as_ref(py);
depythonize(_string).map_err(|err| anyhow!("Failed to deserialize name: {}", err))
})?;

let params: WeaponConfig = if let Some(value) = self.params {
Python::with_gil(|py| {
let _dict: &PyDict = value.as_ref(py);
depythonize(_dict).map_err(|err| anyhow!("Failed to deserialize params: {}", err))
})?
} else {
WeaponConfig::NoConfig
};
Ok(MonaWeaponInterface {
name,
level: self.level,
ascend: self.ascend,
refine: self.refine,
params,
})
}
}

0 comments on commit c34710f

Please sign in to comment.