PkmnLib_rs/src/dynamic_data/libraries/dynamic_library.rs

172 lines
7.1 KiB
Rust
Raw Normal View History

2023-04-15 12:34:42 +00:00
use anyhow::Result;
2022-12-24 11:00:50 +00:00
use std::fmt::Debug;
use std::sync::Arc;
2022-06-06 11:54:59 +00:00
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;
2022-06-06 11:54:59 +00:00
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;
2023-04-15 12:34:42 +00:00
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
2022-12-24 11:00:50 +00:00
/// 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 + ValueIdentifiable {
/// The static data is the immutable storage data for this library.
fn static_data(&self) -> &Box<dyn StaticData>;
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
2023-04-02 12:17:30 +00:00
fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator>;
2022-12-24 11:00:50 +00:00
/// The damage calculator deals with the calculation of things relating to damage.
2023-04-02 12:17:30 +00:00
fn damage_calculator(&self) -> &Box<dyn DamageLibrary>;
2022-12-24 11:00:50 +00:00
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
2023-04-02 12:17:30 +00:00
fn misc_library(&self) -> &Box<dyn MiscLibrary>;
2022-12-24 11:00:50 +00:00
/// 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,
2023-04-15 12:34:42 +00:00
) -> Result<Option<Arc<dyn Script>>>;
2022-12-24 11:00:50 +00:00
/// 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.
2023-04-15 12:34:42 +00:00
fn load_item_script(&self, _key: &Arc<dyn Item>) -> Result<Option<Arc<dyn ItemScript>>>;
2022-12-24 11:00:50 +00:00
}
/// 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.
2022-06-06 11:54:59 +00:00
#[derive(Debug)]
2022-12-24 11:00:50 +00:00
pub struct DynamicLibraryImpl {
2022-10-14 08:33:19 +00:00
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The static data is the immutable storage data for this library.
2022-12-24 11:00:50 +00:00
static_data: Box<dyn StaticData>,
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
stat_calculator: Box<dyn BattleStatCalculator>,
/// The damage calculator deals with the calculation of things relating to damage.
damage_calculator: Box<dyn DamageLibrary>,
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
misc_library: Box<dyn MiscLibrary>,
/// The script resolver deals with how to resolve the scripts from specific unique key combinations.
script_resolver: Box<dyn ScriptResolver>,
2022-06-06 11:54:59 +00:00
}
2022-12-24 11:00:50 +00:00
impl DynamicLibraryImpl {
/// Instantiates a new DynamicLibrary with given parameters.
pub fn new(
2022-12-24 11:00:50 +00:00
static_data: Box<dyn StaticData>,
stat_calculator: Box<dyn BattleStatCalculator>,
damage_calculator: Box<dyn DamageLibrary>,
misc_library: Box<dyn MiscLibrary>,
script_resolver: Box<dyn ScriptResolver>,
) -> Self {
Self {
2022-10-14 08:33:19 +00:00
identifier: Default::default(),
static_data,
stat_calculator,
damage_calculator,
misc_library,
script_resolver,
}
}
2022-12-24 11:00:50 +00:00
}
2022-12-24 11:00:50 +00:00
impl DynamicLibrary for DynamicLibraryImpl {
/// The static data is the immutable storage data for this library.
2022-12-24 11:00:50 +00:00
fn static_data(&self) -> &Box<dyn StaticData> {
2022-06-06 11:54:59 +00:00
&self.static_data
}
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
2023-04-02 12:17:30 +00:00
fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator> {
&self.stat_calculator
2022-06-06 11:54:59 +00:00
}
/// The damage calculator deals with the calculation of things relating to damage.
2023-04-02 12:17:30 +00:00
fn damage_calculator(&self) -> &Box<dyn DamageLibrary> {
&self.damage_calculator
}
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
2023-04-02 12:17:30 +00:00
fn misc_library(&self) -> &Box<dyn MiscLibrary> {
&self.misc_library
}
2022-06-06 11:54:59 +00:00
/// Loads a standard script with a given unique combination of category and key. If no script
/// can be created with this combination, returns None.
2022-12-24 11:00:50 +00:00
fn load_script(
&self,
owner: ScriptOwnerData,
_category: ScriptCategory,
_key: &StringKey,
2023-04-15 12:34:42 +00:00
) -> 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.
2023-04-15 12:34:42 +00:00
fn load_item_script(&self, _key: &Arc<dyn Item>) -> Result<Option<Arc<dyn ItemScript>>> {
2022-06-06 11:54:59 +00:00
todo!()
}
}
2022-12-24 11:00:50 +00:00
impl ValueIdentifiable for DynamicLibraryImpl {
2022-10-14 08:33:19 +00:00
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub mod test {
2022-12-24 11:00:50 +00:00
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;
2022-12-24 11:00:50 +00:00
mockall::mock! {
#[derive(Debug)]
pub DynamicLibrary{}
impl DynamicLibrary for DynamicLibrary {
fn static_data(&self) -> &Box<dyn StaticData>;
2023-04-02 12:17:30 +00:00
fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator>;
fn damage_calculator(&self) -> &Box<dyn DamageLibrary>;
fn misc_library(&self) -> &Box<dyn MiscLibrary>;
2022-12-24 11:00:50 +00:00
fn load_script(
&self,
owner: ScriptOwnerData,
_category: ScriptCategory,
_key: &StringKey,
2023-04-15 12:34:42 +00:00
) -> Result<Option<Arc<dyn Script>>>;
fn load_item_script(&self, _key: &Arc<dyn Item>) -> Result<Option<Arc<dyn ItemScript>>>;
2022-12-24 11:00:50 +00:00
}
impl ValueIdentifiable for DynamicLibrary{
fn value_identifier(&self) -> ValueIdentifier{
ValueIdentifier::new(0)
}
}
}
pub fn build() -> DynamicLibraryImpl {
DynamicLibraryImpl {
2022-10-14 08:33:19 +00:00
identifier: Default::default(),
2022-12-24 11:00:50 +00:00
static_data: Box::new(crate::static_data::libraries::static_data::test::build()),
2022-10-14 08:33:19 +00:00
stat_calculator: Box::new(Gen7BattleStatCalculator::new()),
damage_calculator: Box::new(Gen7DamageLibrary::new(false)),
misc_library: Box::new(Gen7MiscLibrary::new()),
2022-10-14 08:33:19 +00:00
script_resolver: Box::new(EmptyScriptResolver {
identifier: Default::default(),
}),
}
}
}