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 where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { fn hp(&self) -> PkmnResult { self.get_stat(Statistic::Attack) } fn attack(&self) -> PkmnResult { self.get_stat(Statistic::Attack) } fn defense(&self) -> PkmnResult { self.get_stat(Statistic::Defense) } fn special_attack(&self) -> PkmnResult { self.get_stat(Statistic::SpecialAttack) } fn special_defense(&self) -> PkmnResult { self.get_stat(Statistic::SpecialDefense) } fn speed(&self) -> PkmnResult { self.get_stat(Statistic::Speed) } fn get_stat(&self, stat: Statistic) -> PkmnResult; 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 = Rc>; #[derive(Clone)] pub struct StatisticSetImpl where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { reference: ExternRef, _p: PhantomData, } #[cfg(not(feature = "mock_data"))] impl StatisticSetImpl where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { pub(crate) fn new(reference: ExternRef) -> Self { Self { reference, _p: Default::default(), } } } #[cfg(not(feature = "mock_data"))] impl StatisticSetTrait for StatisticSetImpl where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { fn get_stat(&self, stat: Statistic) -> PkmnResult { 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 ExternalReferenceType for StatisticSetImpl where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { fn from_extern_value(reference: ExternRef) -> Self { StatisticSetImpl::::new(reference) } } #[derive(Clone)] pub struct ClampedStatisticSet where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { reference: ExternRef, _p: PhantomData, } impl ClampedStatisticSet where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { pub(crate) fn new(reference: ExternRef) -> Self { Self { reference, _p: Default::default(), } } #[cfg(not(feature = "mock_data"))] pub fn hp(&self) -> PkmnResult { self.get_stat(Statistic::HP) } #[cfg(not(feature = "mock_data"))] pub fn attack(&self) -> PkmnResult { self.get_stat(Statistic::Attack) } #[cfg(not(feature = "mock_data"))] pub fn defense(&self) -> PkmnResult { self.get_stat(Statistic::Defense) } #[cfg(not(feature = "mock_data"))] pub fn special_attack(&self) -> PkmnResult { self.get_stat(Statistic::SpecialAttack) } #[cfg(not(feature = "mock_data"))] pub fn special_defense(&self) -> PkmnResult { self.get_stat(Statistic::SpecialDefense) } #[cfg(not(feature = "mock_data"))] pub fn speed(&self) -> PkmnResult { self.get_stat(Statistic::Speed) } #[cfg(not(feature = "mock_data"))] pub fn get_stat(&self, stat: Statistic) -> PkmnResult { 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 { 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 { unsafe { clamped_statistic_set_decrease_stat( self.reference.cast(), stat, value.try_into().unwrap(), ) .as_res() } } } impl ExternalReferenceType for ClampedStatisticSet where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { fn from_extern_value(reference: ExternRef) -> Self { ClampedStatisticSet::::new(reference) } } #[cfg(not(feature = "mock_data"))] extern "wasm" { fn statistic_set_get(r: ExternRef>, stat: Statistic) -> WasmResult; fn statistic_set_set( r: ExternRef>, stat: Statistic, value: i64, ) -> WasmResult<()>; fn statistic_set_increase_stat( r: ExternRef>, stat: Statistic, value: i64, ) -> WasmResult<()>; fn statistic_set_decrease_stat( r: ExternRef>, stat: Statistic, value: i64, ) -> WasmResult<()>; fn clamped_statistic_set_get( r: ExternRef>, stat: Statistic, ) -> WasmResult; fn clamped_statistic_set_set( r: ExternRef>, stat: Statistic, value: i64, ) -> WasmResult<()>; fn clamped_statistic_set_increase_stat( r: ExternRef>, stat: Statistic, value: i64, ) -> WasmResult; fn clamped_statistic_set_decrease_stat( r: ExternRef>, stat: Statistic, value: i64, ) -> WasmResult; }