Skip to content

Commit

Permalink
feat: add trait filters to bestiary listing
Browse files Browse the repository at this point in the history
  • Loading branch information
RakuJa committed Dec 8, 2024
1 parent 703ee5f commit fcc8daf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/db/bestiary_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<CreatureCoreData>> {
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<Creature> {
if let Ok(creatures) = get_all_creatures_from_db(app_state).await {
return match variant {
Expand Down
16 changes: 16 additions & 0 deletions src/models/creature/creature_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
})
}
}
2 changes: 2 additions & 0 deletions src/models/routers_validator_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct CreatureFieldFilters {
pub rarity_filter: Option<Vec<RarityEnum>>,
pub size_filter: Option<Vec<SizeEnum>>,
pub alignment_filter: Option<Vec<AlignmentEnum>>,
pub trait_whitelist_filter: Option<Vec<String>>,
pub trait_blacklist_filter: Option<Vec<String>>,
pub role_filter: Option<Vec<CreatureRoleEnum>>,
pub type_filter: Option<Vec<CreatureTypeEnum>>,
#[schema(minimum = 0, maximum = 100, example = 50)]
Expand Down

0 comments on commit fcc8daf

Please sign in to comment.