PkmnLib_rs/src/dynamic_data/libraries/dynamic_library.rs

164 lines
6.8 KiB
Rust
Executable File

use anyhow::Result;
use std::fmt::Debug;
use std::sync::Arc;
use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator;
use crate::dynamic_data::libraries::damage_library::DamageLibrary;
use crate::dynamic_data::libraries::misc_library::MiscLibrary;
use crate::dynamic_data::libraries::script_resolver::ScriptCategory;
use crate::dynamic_data::{ItemScript, ScriptResolver};
use crate::dynamic_data::{Script, ScriptOwnerData};
use crate::static_data::Item;
use crate::static_data::StaticData;
use crate::StringKey;
/// The dynamic library stores a static data library, as well as holding different libraries and
/// calculators that might be customized between different generations and implementations.
pub trait DynamicLibrary: Debug + Send + Sync {
/// The static data is the immutable storage data for this library.
fn static_data(&self) -> &Arc<dyn StaticData>;
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
fn stat_calculator(&self) -> &Arc<dyn BattleStatCalculator>;
/// The damage calculator deals with the calculation of things relating to damage.
fn damage_calculator(&self) -> &Arc<dyn DamageLibrary>;
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
fn misc_library(&self) -> &Arc<dyn MiscLibrary>;
/// Loads a standard script with a given unique combination of category and key. If no script
/// can be created with this combination, returns None.
fn load_script(
&self,
owner: ScriptOwnerData,
_category: ScriptCategory,
_key: &StringKey,
) -> Result<Option<Arc<dyn Script>>>;
/// Loads an item script with the given unique key. If no script can be created with this
/// combinations, returns None. Note that ItemScripts are immutable, as their script should be
/// shared between all different usages.
fn load_item_script(&self, _key: &Arc<dyn Item>) -> Result<Option<Arc<dyn ItemScript>>>;
}
/// The dynamic library stores a static data library, as well as holding different libraries and
/// calculators that might be customized between different generations and implementations.
#[derive(Debug)]
pub struct DynamicLibraryImpl {
/// The static data is the immutable storage data for this library.
static_data: Arc<dyn StaticData>,
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
stat_calculator: Arc<dyn BattleStatCalculator>,
/// The damage calculator deals with the calculation of things relating to damage.
damage_calculator: Arc<dyn DamageLibrary>,
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
misc_library: Arc<dyn MiscLibrary>,
/// The script resolver deals with how to resolve the scripts from specific unique key combinations.
script_resolver: Arc<dyn ScriptResolver>,
}
unsafe impl Send for DynamicLibraryImpl {}
unsafe impl Sync for DynamicLibraryImpl {}
impl DynamicLibraryImpl {
/// Instantiates a new DynamicLibrary with given libraries.
pub fn new(
static_data: Arc<dyn StaticData>,
stat_calculator: Arc<dyn BattleStatCalculator>,
damage_calculator: Arc<dyn DamageLibrary>,
misc_library: Arc<dyn MiscLibrary>,
script_resolver: Arc<dyn ScriptResolver>,
) -> Self {
Self {
static_data,
stat_calculator,
damage_calculator,
misc_library,
script_resolver,
}
}
}
impl DynamicLibrary for DynamicLibraryImpl {
/// The static data is the immutable storage data for this library.
fn static_data(&self) -> &Arc<dyn StaticData> {
&self.static_data
}
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
fn stat_calculator(&self) -> &Arc<dyn BattleStatCalculator> {
&self.stat_calculator
}
/// The damage calculator deals with the calculation of things relating to damage.
fn damage_calculator(&self) -> &Arc<dyn DamageLibrary> {
&self.damage_calculator
}
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
fn misc_library(&self) -> &Arc<dyn MiscLibrary> {
&self.misc_library
}
/// Loads a standard script with a given unique combination of category and key. If no script
/// can be created with this combination, returns None.
fn load_script(
&self,
owner: ScriptOwnerData,
_category: ScriptCategory,
_key: &StringKey,
) -> Result<Option<Arc<dyn Script>>> {
self.script_resolver.load_script(owner, _category, _key)
}
/// Loads an item script with the given unique key. If no script can be created with this
/// combinations, returns None. Note that ItemScripts are immutable, as their script should be
/// shared between all different usages.
fn load_item_script(&self, _key: &Arc<dyn Item>) -> Result<Option<Arc<dyn ItemScript>>> {
// TODO
Err(anyhow::anyhow!("Not implemented yet"))
}
}
#[cfg(test)]
#[allow(clippy::indexing_slicing)]
pub mod test {
use super::*;
use crate::dynamic_data::libraries::battle_stat_calculator::Gen7BattleStatCalculator;
use crate::dynamic_data::libraries::damage_library::Gen7DamageLibrary;
use crate::dynamic_data::libraries::misc_library::Gen7MiscLibrary;
use crate::dynamic_data::EmptyScriptResolver;
mockall::mock! {
#[derive(Debug)]
pub DynamicLibrary{}
unsafe impl Send for DynamicLibrary{}
unsafe impl Sync for DynamicLibrary{}
impl DynamicLibrary for DynamicLibrary {
fn static_data(&self) -> &Arc<dyn StaticData>;
fn stat_calculator(&self) -> &Arc<dyn BattleStatCalculator>;
fn damage_calculator(&self) -> &Arc<dyn DamageLibrary>;
fn misc_library(&self) -> &Arc<dyn MiscLibrary>;
fn load_script(
&self,
owner: ScriptOwnerData,
_category: ScriptCategory,
_key: &StringKey,
) -> Result<Option<Arc<dyn Script>>>;
fn load_item_script(&self, _key: &Arc<dyn Item>) -> Result<Option<Arc<dyn ItemScript>>>;
}
}
pub fn build() -> DynamicLibraryImpl {
DynamicLibraryImpl {
static_data: Arc::new(crate::static_data::libraries::static_data::test::build()),
stat_calculator: Arc::new(Gen7BattleStatCalculator::new()),
damage_calculator: Arc::new(Gen7DamageLibrary::new(false)),
misc_library: Arc::new(Gen7MiscLibrary::new()),
script_resolver: Arc::new(EmptyScriptResolver {}),
}
}
}