From 13697be13ee22aa400ae198d612387b1c5ebf7ac Mon Sep 17 00:00:00 2001 From: rakuja Date: Sun, 26 May 2024 14:15:01 +0200 Subject: [PATCH] feat: introduce possibility to filter with pathfinder versions --- src/db/data_providers/raw_query_builder.rs | 1 + src/db/proxy.rs | 3 +-- src/models/creature.rs | 8 +++++++ .../creature_component/filter_struct.rs | 2 ++ src/models/creature_filter_enum.rs | 6 +++++- src/models/encounter_structs.rs | 2 ++ src/models/mod.rs | 1 + src/models/pf_version_enum.rs | 10 +++++++++ src/models/routers_validator_structs.rs | 2 ++ src/routes/bestiary.rs | 21 ++++++++++--------- src/services/encounter_service.rs | 14 +++++++++++++ 11 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 src/models/pf_version_enum.rs diff --git a/src/db/data_providers/raw_query_builder.rs b/src/db/data_providers/raw_query_builder.rs index d5b6b10..e2415a3 100644 --- a/src/db/data_providers/raw_query_builder.rs +++ b/src/db/data_providers/raw_query_builder.rs @@ -11,6 +11,7 @@ pub fn prepare_filtered_get_creatures_core( for (key, value) in key_value_filters { match key { CreatureFilter::Level + | CreatureFilter::PathfinderVersion | CreatureFilter::Melee | CreatureFilter::Ranged | CreatureFilter::SpellCaster => { diff --git a/src/db/proxy.rs b/src/db/proxy.rs index 35689a8..221566a 100644 --- a/src/db/proxy.rs +++ b/src/db/proxy.rs @@ -75,10 +75,9 @@ pub async fn get_creatures_passing_all_filters( fetch_elite: bool, ) -> Result> { let mut creature_vec = Vec::new(); - let empty_set = HashSet::new(); let level_vec = key_value_filters .get(&CreatureFilter::Level) - .unwrap_or(&empty_set) + .unwrap_or(&HashSet::new()) .clone(); let modified_filters = prepare_filters_for_db_communication(key_value_filters, fetch_weak, fetch_elite); diff --git a/src/models/creature.rs b/src/models/creature.rs index 4e64e1f..b3291b4 100644 --- a/src/models/creature.rs +++ b/src/models/creature.rs @@ -5,6 +5,7 @@ use crate::models::creature_component::creature_spell_caster::CreatureSpellCaste use crate::models::creature_component::creature_variant::CreatureVariantData; use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::variant_enum::CreatureVariant; +use crate::models::pf_version_enum::PathfinderVersionEnum; use crate::models::routers_validator_structs::FieldFilters; use serde::{Deserialize, Serialize}; @@ -142,6 +143,12 @@ impl Creature { } }); + let version_pass = match filters.pathfinder_version.clone().unwrap_or_default() { + PathfinderVersionEnum::Legacy => !self.core_data.essential.remaster, + PathfinderVersionEnum::Remaster => self.core_data.essential.remaster, + PathfinderVersionEnum::Any => true, + }; + rarity_pass && size_pass && alignment_pass @@ -150,6 +157,7 @@ impl Creature { && is_spell_caster_pass && type_pass && role_pass + && version_pass } fn check_creature_pass_string_filters(&self, filters: &FieldFilters) -> bool { diff --git a/src/models/creature_component/filter_struct.rs b/src/models/creature_component/filter_struct.rs index 9a9e8b5..1855f53 100644 --- a/src/models/creature_component/filter_struct.rs +++ b/src/models/creature_component/filter_struct.rs @@ -3,6 +3,7 @@ use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::rarity_enum::RarityEnum; use crate::models::creature_metadata::size_enum::SizeEnum; use crate::models::creature_metadata::type_enum::CreatureTypeEnum; +use crate::models::pf_version_enum::PathfinderVersionEnum; use std::collections::HashSet; pub struct FilterStruct { @@ -14,4 +15,5 @@ pub struct FilterStruct { pub creature_types: Option>, pub creature_roles: Option>, pub lvl_combinations: HashSet, + pub pathfinder_version: PathfinderVersionEnum, } diff --git a/src/models/creature_filter_enum.rs b/src/models/creature_filter_enum.rs index aa44d54..8627843 100644 --- a/src/models/creature_filter_enum.rs +++ b/src/models/creature_filter_enum.rs @@ -15,6 +15,7 @@ pub enum CreatureFilter { CreatureTypes, #[serde(alias = "creature_roles")] CreatureRoles, + PathfinderVersion, } impl fmt::Display for CreatureFilter { @@ -45,7 +46,7 @@ impl fmt::Display for CreatureFilter { write!(f, "traits") } CreatureFilter::CreatureTypes => { - write!(f, "creature_types") + write!(f, "cr_type") } CreatureFilter::CreatureRoles => { write!(f, "creature_roles") @@ -53,6 +54,9 @@ impl fmt::Display for CreatureFilter { CreatureFilter::Alignment => { write!(f, "alignment") } + CreatureFilter::PathfinderVersion => { + write!(f, "remaster") + } } } } diff --git a/src/models/encounter_structs.rs b/src/models/encounter_structs.rs index 7bcb0ba..c34b4ee 100644 --- a/src/models/encounter_structs.rs +++ b/src/models/encounter_structs.rs @@ -3,6 +3,7 @@ use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::rarity_enum::RarityEnum; use crate::models::creature_metadata::size_enum::SizeEnum; use crate::models::creature_metadata::type_enum::CreatureTypeEnum; +use crate::models::pf_version_enum::PathfinderVersionEnum; use rand::distributions::{Distribution, Standard}; use rand::Rng; use serde::{Deserialize, Serialize}; @@ -36,6 +37,7 @@ pub struct RandomEncounterData { pub allow_elite_variants: Option, pub allow_weak_variants: Option, pub is_pwl_on: bool, + pub pathfinder_versions: Option, } #[derive( diff --git a/src/models/mod.rs b/src/models/mod.rs index d14456b..efb2622 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -6,6 +6,7 @@ pub mod creature_metadata; pub mod db; pub mod encounter_structs; pub mod items; +pub mod pf_version_enum; pub mod response_data; pub mod routers_validator_structs; pub mod scales_struct; diff --git a/src/models/pf_version_enum.rs b/src/models/pf_version_enum.rs new file mode 100644 index 0000000..5dcbba6 --- /dev/null +++ b/src/models/pf_version_enum.rs @@ -0,0 +1,10 @@ +use serde::{Deserialize, Serialize}; +use utoipa::ToSchema; + +#[derive(Serialize, Deserialize, Eq, PartialEq, Hash, Default, ToSchema, Clone)] +pub enum PathfinderVersionEnum { + Legacy, + Remaster, + #[default] + Any, +} diff --git a/src/models/routers_validator_structs.rs b/src/models/routers_validator_structs.rs index c89712d..66e660e 100644 --- a/src/models/routers_validator_structs.rs +++ b/src/models/routers_validator_structs.rs @@ -3,6 +3,7 @@ use crate::models::creature_metadata::creature_role::CreatureRoleEnum; use crate::models::creature_metadata::rarity_enum::RarityEnum; use crate::models::creature_metadata::size_enum::SizeEnum; use crate::models::creature_metadata::type_enum::CreatureTypeEnum; +use crate::models::pf_version_enum::PathfinderVersionEnum; use serde::{Deserialize, Serialize}; use utoipa::IntoParams; use validator::Validate; @@ -24,6 +25,7 @@ pub struct FieldFilters { pub is_melee_filter: Option, pub is_ranged_filter: Option, pub is_spell_caster_filter: Option, + pub pathfinder_version: Option, } #[derive(Serialize, Deserialize, IntoParams, Validate, Eq, PartialEq, Hash)] diff --git a/src/routes/bestiary.rs b/src/routes/bestiary.rs index bf40d19..e84fef8 100644 --- a/src/routes/bestiary.rs +++ b/src/routes/bestiary.rs @@ -1,12 +1,3 @@ -use crate::models::creature_metadata::alignment_enum::AlignmentEnum; -use crate::models::creature_metadata::creature_role::CreatureRoleEnum; -use crate::models::creature_metadata::rarity_enum::RarityEnum; -use crate::models::creature_metadata::size_enum::SizeEnum; -use crate::models::creature_metadata::type_enum::CreatureTypeEnum; -use crate::models::creature_metadata::variant_enum::CreatureVariant; -use crate::models::response_data::OptionalData; -use crate::models::response_data::ResponseCreature; - use crate::models::creature_component::creature_combat::CreatureCombatData; use crate::models::creature_component::creature_combat::SavingThrows; use crate::models::creature_component::creature_core::CreatureCoreData; @@ -16,6 +7,15 @@ use crate::models::creature_component::creature_extra::AbilityScores; use crate::models::creature_component::creature_extra::CreatureExtraData; use crate::models::creature_component::creature_spell_caster::CreatureSpellCasterData; use crate::models::creature_component::creature_variant::CreatureVariantData; +use crate::models::creature_metadata::alignment_enum::AlignmentEnum; +use crate::models::creature_metadata::creature_role::CreatureRoleEnum; +use crate::models::creature_metadata::rarity_enum::RarityEnum; +use crate::models::creature_metadata::size_enum::SizeEnum; +use crate::models::creature_metadata::type_enum::CreatureTypeEnum; +use crate::models::creature_metadata::variant_enum::CreatureVariant; +use crate::models::pf_version_enum::PathfinderVersionEnum; +use crate::models::response_data::OptionalData; +use crate::models::response_data::ResponseCreature; use crate::models::items::action::Action; use crate::models::items::skill::Skill; @@ -88,7 +88,8 @@ pub fn init_docs(doc: &mut utoipa::openapi::OpenApi) { Action, Skill, CreatureRoleEnum, - SpellCasterEntry + SpellCasterEntry, + PathfinderVersionEnum )) )] struct ApiDoc; diff --git a/src/services/encounter_service.rs b/src/services/encounter_service.rs index 0a5ec25..7c3d3b9 100644 --- a/src/services/encounter_service.rs +++ b/src/services/encounter_service.rs @@ -5,6 +5,7 @@ use crate::models::creature_filter_enum::CreatureFilter; use crate::models::encounter_structs::{ EncounterChallengeEnum, EncounterParams, RandomEncounterData, }; +use crate::models::pf_version_enum::PathfinderVersionEnum; use crate::models::response_data::ResponseCreature; use crate::services::encounter_handler::encounter_calculator; use crate::services::encounter_handler::encounter_calculator::calculate_encounter_scaling_difficulty; @@ -110,6 +111,7 @@ async fn calculate_random_encounter( creature_types: enc_data.creature_types, creature_roles: enc_data.creature_roles, lvl_combinations: unique_levels, + pathfinder_version: enc_data.pathfinder_versions.unwrap_or_default(), }); let filtered_creatures = get_filtered_creatures( @@ -262,6 +264,18 @@ fn build_filter_map(filter_enum: FilterStruct) -> HashMap vec![String::from("FALSE")].into_iter(), + PathfinderVersionEnum::Remaster => vec![String::from("TRUE")].into_iter(), + PathfinderVersionEnum::Any => { + vec![String::from("TRUE"), String::from("FALSE")].into_iter() + } + }), + ); filter_map }