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

63 lines
1.9 KiB
Rust
Executable File

use crate::PkmnResult;
use alloc::rc::Rc;
#[cfg_attr(feature = "mock_data", mockall::automock)]
pub trait BattleRandomTrait {
fn get(&self) -> PkmnResult<i32>;
fn get_max(&self, max: i32) -> PkmnResult<i32>;
fn get_between(&self, min: i32, max: i32) -> PkmnResult<i32>;
}
pub type BattleRandom = Rc<dyn BattleRandomTrait>;
#[cfg(feature = "mock_data")]
pub type MockBattleRandom = MockBattleRandomTrait;
#[cfg(not(feature = "mock_data"))]
pub use implementation::*;
#[cfg(not(feature = "mock_data"))]
mod implementation {
use super::*;
use crate::handling::wasm_result::WasmResult;
use crate::{ExternRef, ExternalReferenceType};
#[derive(Clone)]
pub struct BattleRandomImpl {
reference: ExternRef<Self>,
}
impl BattleRandomTrait for BattleRandomImpl {
#[cfg(not(feature = "mock_data"))]
fn get(&self) -> PkmnResult<i32> {
unsafe { battle_random_get(self.reference).as_res() }
}
#[cfg(not(feature = "mock_data"))]
fn get_max(&self, max: i32) -> PkmnResult<i32> {
unsafe { battle_random_get_max(self.reference, max).as_res() }
}
#[cfg(not(feature = "mock_data"))]
fn get_between(&self, min: i32, max: i32) -> PkmnResult<i32> {
unsafe { battle_random_get_between(self.reference, min, max).as_res() }
}
// TODO: effect_chance()
}
impl ExternalReferenceType for BattleRandomImpl {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self { reference }
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn battle_random_get(r: ExternRef<BattleRandomImpl>) -> WasmResult<i32>;
fn battle_random_get_max(r: ExternRef<BattleRandomImpl>, max: i32) -> WasmResult<i32>;
fn battle_random_get_between(
r: ExternRef<BattleRandomImpl>,
min: i32,
max: i32,
) -> WasmResult<i32>;
}
}