Gen7ScriptsRs/pkmn_lib_interface/src/app_interface/dynamic_data/statistic_set.rs

263 lines
7.3 KiB
Rust
Executable File

use crate::app_interface::Statistic;
#[cfg(not(feature = "mock_data"))]
use crate::handling::wasm_result::WasmResult;
use crate::{ExternRef, ExternalReferenceType, PkmnResult};
use alloc::rc::Rc;
use core::convert::{TryFrom, TryInto};
use core::fmt::Debug;
use core::marker::PhantomData;
pub trait StatisticSetTrait<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
fn hp(&self) -> PkmnResult<T> {
self.get_stat(Statistic::Attack)
}
fn attack(&self) -> PkmnResult<T> {
self.get_stat(Statistic::Attack)
}
fn defense(&self) -> PkmnResult<T> {
self.get_stat(Statistic::Defense)
}
fn special_attack(&self) -> PkmnResult<T> {
self.get_stat(Statistic::SpecialAttack)
}
fn special_defense(&self) -> PkmnResult<T> {
self.get_stat(Statistic::SpecialDefense)
}
fn speed(&self) -> PkmnResult<T> {
self.get_stat(Statistic::Speed)
}
fn get_stat(&self, stat: Statistic) -> PkmnResult<T>;
fn set_stat(&self, stat: Statistic, value: T) -> PkmnResult<()>;
fn increase_stat(&self, stat: Statistic, value: T) -> PkmnResult<()>;
fn decrease_stat(&self, stat: Statistic, value: T) -> PkmnResult<()>;
}
pub type StatisticSet<T> = Rc<dyn StatisticSetTrait<T>>;
#[derive(Clone)]
pub struct StatisticSetImpl<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
reference: ExternRef<Self>,
_p: PhantomData<T>,
}
#[cfg(not(feature = "mock_data"))]
impl<T> StatisticSetImpl<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self {
reference,
_p: Default::default(),
}
}
}
#[cfg(not(feature = "mock_data"))]
impl<T> StatisticSetTrait<T> for StatisticSetImpl<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
fn get_stat(&self, stat: Statistic) -> PkmnResult<T> {
unsafe {
Ok(statistic_set_get(self.reference.cast(), stat)
.as_res()?
.try_into()
.unwrap())
}
}
fn set_stat(&self, stat: Statistic, value: T) -> PkmnResult<()> {
unsafe {
statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()).as_res()
}
}
fn increase_stat(&self, stat: Statistic, value: T) -> PkmnResult<()> {
unsafe {
statistic_set_increase_stat(self.reference.cast(), stat, value.try_into().unwrap())
.as_res()
}
}
fn decrease_stat(&self, stat: Statistic, value: T) -> PkmnResult<()> {
unsafe {
statistic_set_decrease_stat(self.reference.cast(), stat, value.try_into().unwrap())
.as_res()
}
}
}
#[cfg(not(feature = "mock_data"))]
impl<T> ExternalReferenceType for StatisticSetImpl<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
fn from_extern_value(reference: ExternRef<Self>) -> Self {
StatisticSetImpl::<T>::new(reference)
}
}
#[derive(Clone)]
pub struct ClampedStatisticSet<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
reference: ExternRef<Self>,
_p: PhantomData<T>,
}
impl<T> ClampedStatisticSet<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self {
reference,
_p: Default::default(),
}
}
#[cfg(not(feature = "mock_data"))]
pub fn hp(&self) -> PkmnResult<T> {
self.get_stat(Statistic::HP)
}
#[cfg(not(feature = "mock_data"))]
pub fn attack(&self) -> PkmnResult<T> {
self.get_stat(Statistic::Attack)
}
#[cfg(not(feature = "mock_data"))]
pub fn defense(&self) -> PkmnResult<T> {
self.get_stat(Statistic::Defense)
}
#[cfg(not(feature = "mock_data"))]
pub fn special_attack(&self) -> PkmnResult<T> {
self.get_stat(Statistic::SpecialAttack)
}
#[cfg(not(feature = "mock_data"))]
pub fn special_defense(&self) -> PkmnResult<T> {
self.get_stat(Statistic::SpecialDefense)
}
#[cfg(not(feature = "mock_data"))]
pub fn speed(&self) -> PkmnResult<T> {
self.get_stat(Statistic::Speed)
}
#[cfg(not(feature = "mock_data"))]
pub fn get_stat(&self, stat: Statistic) -> PkmnResult<T> {
unsafe {
Ok(clamped_statistic_set_get(self.reference.cast(), stat)
.as_res()?
.try_into()
.unwrap())
}
}
#[cfg(not(feature = "mock_data"))]
pub fn set_stat(&self, stat: Statistic, value: T) -> PkmnResult<()> {
unsafe {
clamped_statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap())
.as_res()
}
}
#[cfg(not(feature = "mock_data"))]
pub fn increase_stat(&self, stat: Statistic, value: T) -> PkmnResult<bool> {
unsafe {
clamped_statistic_set_increase_stat(
self.reference.cast(),
stat,
value.try_into().unwrap(),
)
.as_res()
}
}
#[cfg(not(feature = "mock_data"))]
pub fn decrease_stat(&self, stat: Statistic, value: T) -> PkmnResult<bool> {
unsafe {
clamped_statistic_set_decrease_stat(
self.reference.cast(),
stat,
value.try_into().unwrap(),
)
.as_res()
}
}
}
impl<T> ExternalReferenceType for ClampedStatisticSet<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
fn from_extern_value(reference: ExternRef<Self>) -> Self {
ClampedStatisticSet::<T>::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn statistic_set_get(r: ExternRef<StatisticSetImpl<i64>>, stat: Statistic) -> WasmResult<i64>;
fn statistic_set_set(
r: ExternRef<StatisticSetImpl<i64>>,
stat: Statistic,
value: i64,
) -> WasmResult<()>;
fn statistic_set_increase_stat(
r: ExternRef<StatisticSetImpl<i64>>,
stat: Statistic,
value: i64,
) -> WasmResult<()>;
fn statistic_set_decrease_stat(
r: ExternRef<StatisticSetImpl<i64>>,
stat: Statistic,
value: i64,
) -> WasmResult<()>;
fn clamped_statistic_set_get(
r: ExternRef<ClampedStatisticSet<i64>>,
stat: Statistic,
) -> WasmResult<i64>;
fn clamped_statistic_set_set(
r: ExternRef<ClampedStatisticSet<i64>>,
stat: Statistic,
value: i64,
) -> WasmResult<()>;
fn clamped_statistic_set_increase_stat(
r: ExternRef<ClampedStatisticSet<i64>>,
stat: Statistic,
value: i64,
) -> WasmResult<bool>;
fn clamped_statistic_set_decrease_stat(
r: ExternRef<ClampedStatisticSet<i64>>,
stat: Statistic,
value: i64,
) -> WasmResult<bool>;
}