Moves a bunch of libraries to traits
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-12-24 12:00:50 +01:00
parent bce636b97e
commit 47df85e8d3
47 changed files with 730 additions and 358 deletions

View File

@@ -137,3 +137,23 @@ impl ValueIdentifiable for Gen7BattleStatCalculator {
self.identifier
}
}
#[cfg(test)]
pub mod tests {
use super::*;
mockall::mock! {
#[derive(Debug)]
pub BattleStatCalculator{}
impl BattleStatCalculator for BattleStatCalculator {
fn calculate_flat_stats(&self, pokemon: &Pokemon, stats: &StatisticSet<u32>);
fn calculate_flat_stat(&self, pokemon: &Pokemon, stat: Statistic) -> u32;
fn calculate_boosted_stats(&self, pokemon: &Pokemon, stats: &StatisticSet<u32>);
fn calculate_boosted_stat(&self, pokemon: &Pokemon, stat: Statistic) -> u32;
}
impl ValueIdentifiable for BattleStatCalculator {
fn value_identifier(&self) -> ValueIdentifier{
ValueIdentifier::new(0)
}
}
}
}

View File

@@ -1,3 +1,4 @@
use std::fmt::Debug;
use std::ops::Deref;
use std::sync::Arc;
@@ -11,14 +12,42 @@ use crate::static_data::Item;
use crate::static_data::StaticData;
use crate::{PkmnResult, StringKey, ValueIdentifiable, ValueIdentifier};
/// 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.
fn stat_calculator(&self) -> &dyn BattleStatCalculator;
/// The damage calculator deals with the calculation of things relating to damage.
fn damage_calculator(&self) -> &dyn DamageLibrary;
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
fn misc_library(&self) -> &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,
) -> PkmnResult<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>) -> PkmnResult<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 DynamicLibrary {
pub struct DynamicLibraryImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The static data is the immutable storage data for this library.
static_data: StaticData,
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>,
@@ -32,13 +61,10 @@ pub struct DynamicLibrary {
script_resolver: Box<dyn ScriptResolver>,
}
unsafe impl Sync for DynamicLibrary {}
unsafe impl Send for DynamicLibrary {}
impl DynamicLibrary {
impl DynamicLibraryImpl {
/// Instantiates a new DynamicLibrary with given parameters.
pub fn new(
static_data: StaticData,
static_data: Box<dyn StaticData>,
stat_calculator: Box<dyn BattleStatCalculator>,
damage_calculator: Box<dyn DamageLibrary>,
misc_library: Box<dyn MiscLibrary>,
@@ -53,29 +79,31 @@ impl DynamicLibrary {
script_resolver,
}
}
}
impl DynamicLibrary for DynamicLibraryImpl {
/// The static data is the immutable storage data for this library.
pub fn static_data(&self) -> &StaticData {
fn static_data(&self) -> &Box<dyn StaticData> {
&self.static_data
}
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
pub fn stat_calculator(&self) -> &dyn BattleStatCalculator {
fn stat_calculator(&self) -> &dyn BattleStatCalculator {
self.stat_calculator.deref()
}
/// The damage calculator deals with the calculation of things relating to damage.
pub fn damage_calculator(&self) -> &dyn DamageLibrary {
fn damage_calculator(&self) -> &dyn DamageLibrary {
self.damage_calculator.deref()
}
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
pub fn misc_library(&self) -> &dyn MiscLibrary {
fn misc_library(&self) -> &dyn MiscLibrary {
self.misc_library.deref()
}
/// Loads a standard script with a given unique combination of category and key. If no script
/// can be created with this combination, returns None.
pub fn load_script(
fn load_script(
&self,
owner: ScriptOwnerData,
_category: ScriptCategory,
@@ -86,12 +114,12 @@ impl DynamicLibrary {
/// 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.
pub fn load_item_script(&self, _key: &Arc<dyn Item>) -> PkmnResult<Option<Arc<dyn ItemScript>>> {
fn load_item_script(&self, _key: &Arc<dyn Item>) -> PkmnResult<Option<Arc<dyn ItemScript>>> {
todo!()
}
}
impl ValueIdentifiable for DynamicLibrary {
impl ValueIdentifiable for DynamicLibraryImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
@@ -99,16 +127,39 @@ impl ValueIdentifiable for DynamicLibrary {
#[cfg(test)]
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::dynamic_library::DynamicLibrary;
use crate::dynamic_data::libraries::misc_library::Gen7MiscLibrary;
use crate::dynamic_data::EmptyScriptResolver;
pub fn build() -> DynamicLibrary {
DynamicLibrary {
mockall::mock! {
#[derive(Debug)]
pub DynamicLibrary{}
impl DynamicLibrary for DynamicLibrary {
fn static_data(&self) -> &Box<dyn StaticData>;
fn stat_calculator(&self) -> &dyn BattleStatCalculator;
fn damage_calculator(&self) -> &dyn DamageLibrary;
fn misc_library(&self) -> &dyn MiscLibrary;
fn load_script(
&self,
owner: ScriptOwnerData,
_category: ScriptCategory,
_key: &StringKey,
) -> PkmnResult<Option<Arc<dyn Script>>>;
fn load_item_script(&self, _key: &Arc<dyn Item>) -> PkmnResult<Option<Arc<dyn ItemScript>>>;
}
impl ValueIdentifiable for DynamicLibrary{
fn value_identifier(&self) -> ValueIdentifier{
ValueIdentifier::new(0)
}
}
}
pub fn build() -> DynamicLibraryImpl {
DynamicLibraryImpl {
identifier: Default::default(),
static_data: crate::static_data::libraries::static_data::test::build(),
static_data: Box::new(crate::static_data::libraries::static_data::test::build()),
stat_calculator: Box::new(Gen7BattleStatCalculator::new()),
damage_calculator: Box::new(Gen7DamageLibrary::new(false)),
misc_library: Box::new(Gen7MiscLibrary::new()),

View File

@@ -47,7 +47,7 @@ impl Gen7MiscLibrary {
))),
HashSet::new(),
));
let struggle_learned_move = Arc::new(LearnedMove::new(&struggle_data, MoveLearnMethod::Unknown));
let struggle_learned_move = Arc::new(LearnedMove::new(struggle_data, MoveLearnMethod::Unknown));
Self {
identifier: Default::default(),
struggle_learned_move,