Skip to content

Commit

Permalink
feat: implement min max number of creatures in encounter generator
Browse files Browse the repository at this point in the history
  • Loading branch information
RakuJa committed Nov 15, 2023
1 parent 73dcd85 commit b9a04d6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

# You can pin the exact commit or the version.
# uses: SonarSource/sonarcloud-github-action@de2e56b42aa84d0b1c5b622644ac17e505c9a049
uses: SonarSource/sonarcloud-github-action@de2e56b42aa84d0b1c5b622644ac17e505c9a049
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} # Generate a token on Sonarcloud.io, add it to the secrets of this repo with the name SONAR_TOKEN (Settings > Secrets > Actions > add new repository secret)
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 @@ -25,6 +25,8 @@ pub struct RandomEncounterData {
pub alignments: Option<Vec<AlignmentEnum>>,
pub creature_types: Option<Vec<CreatureTypeEnum>>,
pub challenge: Option<EncounterChallengeEnum>,
pub min_creatures: Option<u8>,
pub max_creatures: Option<u8>,
#[validate(length(min = 1))]
pub party_levels: Vec<i16>,
}
Expand Down
4 changes: 3 additions & 1 deletion src/routes/encounter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ pub async fn get_encounter_info(
tag = "encounter",
request_body(
content = RandomEncounterData,
description = "Party levels as a vector of integers",
description = "Party levels as a vector of integers,\
if min and max are not set they will not be considered. If only one of them is set, \
the other one will be set at the same value.",
content_type = "application/json",
),
params(
Expand Down
24 changes: 24 additions & 0 deletions src/services/encounter_handler/encounter_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet};
use std::ops::Neg;
// Used to explicitly tell about the iter trait
use strum::IntoEnumIterator;
use validator::HasLen;

static MAX_LVL_DIFF: i16 = -4;
lazy_static! {
Expand Down Expand Up @@ -102,6 +103,29 @@ pub fn calculate_lvl_combination_for_encounter(
)
}

pub fn filter_combinations_outside_range(
combinations: HashSet<Vec<i16>>,
lower_bound: Option<u8>,
upper_bound: Option<u8>,
) -> HashSet<Vec<i16>> {
let mut lower = lower_bound.unwrap_or(0);
let mut upper = upper_bound.unwrap_or(0);
if lower != 0 && upper == 0 {
upper = lower;
} else if lower == 0 && upper != 0 {
lower = upper;
} else if lower == 0 && upper == 0 {
return combinations;
}
let mut filtered_combo = HashSet::new();
combinations.iter().for_each(|curr_combo| {
if curr_combo.length() >= lower as u64 && curr_combo.length() <= upper as u64 {
filtered_combo.insert(curr_combo.clone());
}
});
filtered_combo
}

fn convert_lvl_diff_into_exp(lvl_diff: f32, party_size: usize) -> i16 {
let lvl_diff_rounded_down = lvl_diff.floor() as i16;
LVL_AND_EXP_MAP
Expand Down
9 changes: 7 additions & 2 deletions src/services/encounter_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ async fn calculate_random_encounter(

let (exp, lvl_combinations) =
encounter_calculator::calculate_lvl_combination_for_encounter(&enc_diff, &party_levels);
let filtered_lvl_combinations = encounter_calculator::filter_combinations_outside_range(
lvl_combinations,
enc_data.min_creatures,
enc_data.max_creatures,
);
let unique_levels = HashSet::from_iter(
lvl_combinations
filtered_lvl_combinations
.clone()
.into_iter()
.flatten()
Expand All @@ -111,7 +116,7 @@ async fn calculate_random_encounter(
"No creatures have been fetched"
);
let chosen_encounter =
choose_random_creatures_combination(filtered_creatures, lvl_combinations)?;
choose_random_creatures_combination(filtered_creatures, filtered_lvl_combinations)?;

let scaled_exp_levels = calculate_encounter_scaling_difficulty(party_levels.len());

Expand Down

0 comments on commit b9a04d6

Please sign in to comment.