From fcc8daf30aee0530a7043efe35e3164e7ccdf8ac Mon Sep 17 00:00:00 2001 From: rakuja Date: Sun, 8 Dec 2024 12:48:52 +0100 Subject: [PATCH] feat: add trait filters to bestiary listing --- src/db/bestiary_proxy.rs | 2 +- src/models/creature/creature_struct.rs | 16 ++++++++++++++++ src/models/routers_validator_structs.rs | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/db/bestiary_proxy.rs b/src/db/bestiary_proxy.rs index dfa07b2..78dd7a0 100644 --- a/src/db/bestiary_proxy.rs +++ b/src/db/bestiary_proxy.rs @@ -227,12 +227,12 @@ async fn get_all_keys(app_state: &AppState) -> FieldsUniqueValuesStruct { /// Gets all the creature core data from the DB. It will not fetch data outside of variant and core. /// It will cache the result. -#[once(sync_writes = true, result = true)] async fn get_all_creatures_from_db(app_state: &AppState) -> Result> { creature_fetcher::fetch_creatures_core_data(&app_state.conn, 0, -1).await } /// Infallible method, it will expose a vector representing the values fetched from db or empty vec +#[once(sync_writes = true)] async fn get_list(app_state: &AppState, variant: CreatureVariant) -> Vec { if let Ok(creatures) = get_all_creatures_from_db(app_state).await { return match variant { diff --git a/src/models/creature/creature_struct.rs b/src/models/creature/creature_struct.rs index 7d5c318..d2c091a 100644 --- a/src/models/creature/creature_struct.rs +++ b/src/models/creature/creature_struct.rs @@ -186,6 +186,22 @@ impl Creature { .to_lowercase() .contains(fam.to_lowercase().as_str()) }) + }) && filters.trait_whitelist_filter.as_ref().map_or(true, |x| { + x.iter().any(|filter_trait| { + self.core_data.traits.iter().any(|cr_trait| { + cr_trait + .to_lowercase() + .contains(filter_trait.to_lowercase().as_str()) + }) + }) + }) && !filters.trait_blacklist_filter.as_ref().map_or(false, |x| { + x.iter().any(|filter_trait| { + self.core_data.traits.iter().any(|cr_trait| { + cr_trait + .to_lowercase() + .eq(filter_trait.to_lowercase().as_str()) + }) + }) }) } } diff --git a/src/models/routers_validator_structs.rs b/src/models/routers_validator_structs.rs index 837e6a1..7832fd8 100644 --- a/src/models/routers_validator_structs.rs +++ b/src/models/routers_validator_structs.rs @@ -16,6 +16,8 @@ pub struct CreatureFieldFilters { pub rarity_filter: Option>, pub size_filter: Option>, pub alignment_filter: Option>, + pub trait_whitelist_filter: Option>, + pub trait_blacklist_filter: Option>, pub role_filter: Option>, pub type_filter: Option>, #[schema(minimum = 0, maximum = 100, example = 50)]