2023-04-15 12:34:42 +00:00
|
|
|
use anyhow::Result;
|
2022-12-24 11:00:50 +00:00
|
|
|
use std::fmt::Debug;
|
2022-06-19 19:34:08 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-06-06 11:54:59 +00:00
|
|
|
use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator;
|
2022-06-16 15:59:33 +00:00
|
|
|
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;
|
2022-06-30 15:34:57 +00:00
|
|
|
use crate::dynamic_data::{ItemScript, ScriptResolver};
|
2022-09-07 16:01:26 +00:00
|
|
|
use crate::dynamic_data::{Script, ScriptOwnerData};
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::static_data::Item;
|
|
|
|
use crate::static_data::StaticData;
|
2023-04-15 12:34:42 +00:00
|
|
|
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
2021-01-31 16:31:22 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +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,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The static data is the immutable storage data for this library.
|
2022-12-24 11:00:50 +00:00
|
|
|
static_data: Box<dyn StaticData>,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
|
|
|
|
/// Pokemons attributes.
|
2022-06-19 19:34:08 +00:00
|
|
|
stat_calculator: Box<dyn BattleStatCalculator>,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The damage calculator deals with the calculation of things relating to damage.
|
2022-06-16 15:59:33 +00:00
|
|
|
damage_calculator: Box<dyn DamageLibrary>,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
|
|
|
|
/// calculators.
|
2022-08-20 11:17:20 +00:00
|
|
|
misc_library: Box<dyn MiscLibrary>,
|
2022-06-30 15:34:57 +00:00
|
|
|
|
|
|
|
/// 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 {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Instantiates a new DynamicLibrary with given parameters.
|
2022-06-17 17:53:33 +00:00
|
|
|
pub fn new(
|
2022-12-24 11:00:50 +00:00
|
|
|
static_data: Box<dyn StaticData>,
|
2022-06-19 19:34:08 +00:00
|
|
|
stat_calculator: Box<dyn BattleStatCalculator>,
|
2022-06-17 17:53:33 +00:00
|
|
|
damage_calculator: Box<dyn DamageLibrary>,
|
2022-08-20 11:17:20 +00:00
|
|
|
misc_library: Box<dyn MiscLibrary>,
|
2022-06-30 15:34:57 +00:00
|
|
|
script_resolver: Box<dyn ScriptResolver>,
|
2022-06-17 17:53:33 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2022-10-14 08:33:19 +00:00
|
|
|
identifier: Default::default(),
|
2022-06-17 17:53:33 +00:00
|
|
|
static_data,
|
|
|
|
stat_calculator,
|
|
|
|
damage_calculator,
|
|
|
|
misc_library,
|
2022-06-30 15:34:57 +00:00
|
|
|
script_resolver,
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-24 11:00:50 +00:00
|
|
|
}
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2022-12-24 11:00:50 +00:00
|
|
|
impl DynamicLibrary for DynamicLibraryImpl {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// 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
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// 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
|
|
|
}
|
2022-06-30 15:34:57 +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
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
2022-06-30 15:34:57 +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> {
|
|
|
|
&self.misc_library
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
|
2022-06-30 15:34:57 +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(
|
2022-06-30 15:34:57 +00:00
|
|
|
&self,
|
2022-09-07 16:01:26 +00:00
|
|
|
owner: ScriptOwnerData,
|
2022-06-30 15:34:57 +00:00
|
|
|
_category: ScriptCategory,
|
|
|
|
_key: &StringKey,
|
2023-04-15 12:34:42 +00:00
|
|
|
) -> Result<Option<Arc<dyn Script>>> {
|
2022-06-30 15:34:57 +00:00
|
|
|
self.script_resolver.load_script(owner, _category, _key)
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
2022-06-30 15:34:57 +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-06-06 11:54:59 +00:00
|
|
|
todo!()
|
|
|
|
}
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:31:22 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub mod test {
|
2022-12-24 11:00:50 +00:00
|
|
|
use super::*;
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::dynamic_data::libraries::battle_stat_calculator::Gen7BattleStatCalculator;
|
2022-06-16 15:59:33 +00:00
|
|
|
use crate::dynamic_data::libraries::damage_library::Gen7DamageLibrary;
|
|
|
|
use crate::dynamic_data::libraries::misc_library::Gen7MiscLibrary;
|
2022-06-30 15:34:57 +00:00
|
|
|
use crate::dynamic_data::EmptyScriptResolver;
|
2021-01-31 16:31:22 +00:00
|
|
|
|
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()),
|
2022-06-16 15:59:33 +00:00
|
|
|
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(),
|
|
|
|
}),
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|