Skip to content

Commit

Permalink
chore: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
RakuJa committed May 26, 2024
2 parents 584f659 + 13697be commit debb3b2
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/db/bestiary_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::models::creature::creature_metadata::alignment_enum::AlignmentEnum;
use crate::models::creature::creature_metadata::creature_role::CreatureRoleEnum;
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::creature::creature_metadata::variant_enum::CreatureVariant;
use crate::models::pf_version_enum::PathfinderVersionEnum;
use crate::models::response_data::OptionalData;
use crate::models::routers_validator_structs::{CreatureFieldFilters, PaginatedRequest};
use crate::AppState;
Expand Down Expand Up @@ -72,10 +73,9 @@ pub async fn get_creatures_passing_all_filters(
fetch_elite: bool,
) -> Result<Vec<Creature>> {
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);
Expand Down Expand Up @@ -123,6 +123,9 @@ pub async fn get_all_possible_values_of_filter(
CreatureFilter::Level => runtime_fields_values.list_of_levels,
CreatureFilter::CreatureTypes => CreatureTypeEnum::iter().map(|x| x.to_string()).collect(),
CreatureFilter::CreatureRoles => CreatureRoleEnum::iter().map(|x| x.to_string()).collect(),
CreatureFilter::PathfinderVersion => PathfinderVersionEnum::iter()
.map(|x| x.to_string())
.collect(),
};
x.sort();
x
Expand Down
1 change: 1 addition & 0 deletions src/db/data_providers/raw_query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions src/models/creature/creature_component/filter_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::models::creature::creature_metadata::creature_role::CreatureRoleEnum;
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::creature::creature_metadata::size_enum::SizeEnum;
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::pf_version_enum::PathfinderVersionEnum;
use std::collections::HashSet;

pub struct FilterStruct {
Expand All @@ -14,4 +15,5 @@ pub struct FilterStruct {
pub creature_types: Option<Vec<CreatureTypeEnum>>,
pub creature_roles: Option<Vec<CreatureRoleEnum>>,
pub lvl_combinations: HashSet<String>,
pub pathfinder_version: PathfinderVersionEnum,
}
6 changes: 5 additions & 1 deletion src/models/creature/creature_filter_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum CreatureFilter {
CreatureTypes,
#[serde(alias = "creature_roles", alias = "CREATURE_ROLE")]
CreatureRoles,
PathfinderVersion,
}

impl fmt::Display for CreatureFilter {
Expand Down Expand Up @@ -57,14 +58,17 @@ impl fmt::Display for CreatureFilter {
write!(f, "traits")
}
CreatureFilter::CreatureTypes => {
write!(f, "creature_types")
write!(f, "cr_type")
}
CreatureFilter::CreatureRoles => {
write!(f, "creature_roles")
}
CreatureFilter::Alignment => {
write!(f, "alignment")
}
CreatureFilter::PathfinderVersion => {
write!(f, "remaster")
}
CreatureFilter::Sources => {
write!(f, "sources")
}
Expand Down
7 changes: 6 additions & 1 deletion src/models/creature/creature_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::models::creature::creature_component::creature_spell_caster::Creature
use crate::models::creature::creature_component::creature_variant::CreatureVariantData;
use crate::models::creature::creature_metadata::creature_role::CreatureRoleEnum;
use crate::models::creature::creature_metadata::variant_enum::CreatureVariant;
use crate::models::pf_version_enum::PathfinderVersionEnum;
use crate::models::routers_validator_structs::CreatureFieldFilters;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -133,7 +134,11 @@ impl Creature {
self.core_data.derived.spell_caster_percentage >= t
}
}
})
}) && 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,
}
}

fn check_creature_pass_string_filters(&self, filters: &CreatureFieldFilters) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/models/encounter_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::models::creature::creature_metadata::creature_role::CreatureRoleEnum;
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::creature::creature_metadata::size_enum::SizeEnum;
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::pf_version_enum::PathfinderVersionEnum;
use rand::distributions::{Distribution, Standard};
use rand::Rng;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -36,6 +37,7 @@ pub struct RandomEncounterData {
pub allow_elite_variants: Option<bool>,
pub allow_weak_variants: Option<bool>,
pub is_pwl_on: bool,
pub pathfinder_version: Option<PathfinderVersionEnum>,
}

#[derive(
Expand Down
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod creature;
pub mod db;
pub mod encounter_structs;
pub mod item;
pub mod pf_version_enum;
pub mod response_data;
pub mod routers_validator_structs;
pub mod scales_struct;
Expand Down
13 changes: 13 additions & 0 deletions src/models/pf_version_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter};
use utoipa::ToSchema;

#[derive(
Serialize, Deserialize, Eq, PartialEq, Hash, Default, ToSchema, Clone, EnumIter, Display,
)]
pub enum PathfinderVersionEnum {
Legacy,
Remaster,
#[default]
Any,
}
3 changes: 3 additions & 0 deletions src/models/routers_validator_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::creature::creature_metadata::size_enum::SizeEnum;
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::item::item_metadata::type_enum::ItemTypeEnum;
use crate::models::pf_version_enum::PathfinderVersionEnum;
use serde::{Deserialize, Serialize};
use utoipa::IntoParams;
use validator::Validate;
Expand All @@ -30,6 +31,7 @@ pub struct CreatureFieldFilters {
pub is_melee_filter: Option<bool>,
pub is_ranged_filter: Option<bool>,
pub is_spell_caster_filter: Option<bool>,
pub pathfinder_version: Option<PathfinderVersionEnum>,
}

#[derive(Serialize, Deserialize, IntoParams, Validate)]
Expand Down Expand Up @@ -65,6 +67,7 @@ pub struct ItemFieldFilters {
pub type_filter: Option<ItemTypeEnum>,
pub rarity_filter: Option<RarityEnum>,
pub size_filter: Option<SizeEnum>,
pub pathfinder_version: Option<PathfinderVersionEnum>,
}

#[derive(Serialize, Deserialize, IntoParams, Validate, Eq, PartialEq, Hash)]
Expand Down
4 changes: 3 additions & 1 deletion src/routes/bestiary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::models::creature::creature_component::creature_extra::AbilityScores;
use crate::models::creature::creature_component::creature_extra::CreatureExtraData;
use crate::models::creature::creature_component::creature_spell_caster::CreatureSpellCasterData;
use crate::models::creature::creature_component::creature_variant::CreatureVariantData;
use crate::models::pf_version_enum::PathfinderVersionEnum;

use crate::models::creature::items::action::Action;
use crate::models::creature::items::skill::Skill;
Expand Down Expand Up @@ -88,7 +89,8 @@ pub fn init_docs(doc: &mut utoipa::openapi::OpenApi) {
Action,
Skill,
CreatureRoleEnum,
SpellCasterEntry
SpellCasterEntry,
PathfinderVersionEnum
))
)]
struct ApiDoc;
Expand Down
14 changes: 14 additions & 0 deletions src/services/encounter_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::models::creature::creature_struct::Creature;
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;
Expand Down Expand Up @@ -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_version.unwrap_or_default(),
});

let filtered_creatures = get_filtered_creatures(
Expand Down Expand Up @@ -262,6 +264,18 @@ fn build_filter_map(filter_enum: FilterStruct) -> HashMap<CreatureFilter, HashSe
);
};
filter_map.insert(CreatureFilter::Level, filter_enum.lvl_combinations);
filter_map.insert(
CreatureFilter::PathfinderVersion,
HashSet::from_iter(match filter_enum.pathfinder_version {
// The db column is a boolean called "remaster" so we translate the enum to
// FALSE if legacy, TRUE if remaster and TRUE, FALSE if both
PathfinderVersionEnum::Legacy => 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
}

Expand Down

0 comments on commit debb3b2

Please sign in to comment.