Skip to content

Commit

Permalink
Move string search to game_data crate & Ignore HQ/CL icons
Browse files Browse the repository at this point in the history
  • Loading branch information
KonaeAkira committed Aug 23, 2024
1 parent 8025bd6 commit 62a5d7f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 66 deletions.
6 changes: 6 additions & 0 deletions game_data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ pub use config::*;
mod locales;
pub use locales::*;

mod search;
pub use search::*;

use serde::{Deserialize, Serialize};
use simulator::{Action, ActionMask, Settings};

pub const HQ_ICON_CHAR: char = '\u{e03c}';
pub const CL_ICON_CHAR: char = '\u{e03d}';

#[derive(Debug, Clone, Copy)]
pub struct Item {
pub item_level: u16,
Expand Down
62 changes: 62 additions & 0 deletions game_data/src/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::{
get_item_name, Consumable, Locale, CL_ICON_CHAR, HQ_ICON_CHAR, MEALS, POTIONS, RECIPES,
};

fn contains_noncontiguous(string: &str, pattern: &str) -> bool {
let mut it = string.split_whitespace();
for c in pattern.split_whitespace() {
loop {
let Some(c2) = it.next() else {
return false;
};
if c2.contains(c) {
break;
}
}
}
true
}

fn preprocess_pattern(pattern: &str) -> String {
pattern
.to_lowercase()
.replace([HQ_ICON_CHAR, CL_ICON_CHAR], "")
}

pub fn find_recipes(search_string: &str, locale: Locale) -> Vec<usize> {
let pattern = preprocess_pattern(search_string);
RECIPES
.iter()
.enumerate()
.filter_map(|(index, recipe)| {
let item_name = get_item_name(recipe.item_id, false, locale);
match contains_noncontiguous(&item_name.to_lowercase(), &pattern) {
true => Some(index),
false => None,
}
})
.collect()
}

fn find_consumables(search_string: &str, locale: Locale, consumables: &[Consumable]) -> Vec<usize> {
let pattern = preprocess_pattern(search_string);
consumables
.iter()
.enumerate()
.filter_map(|(index, consumable)| {
let item_name = get_item_name(consumable.item_id, false, locale);
match contains_noncontiguous(&item_name.to_lowercase(), &pattern) {
true => Some(index),
false => None,
}
})
.collect()
}

pub fn find_meals(search_string: &str, locale: Locale) -> Vec<usize> {
find_consumables(search_string, locale, MEALS)
}

pub fn find_potions(search_string: &str, locale: Locale) -> Vec<usize> {
find_consumables(search_string, locale, POTIONS)
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ pub use app::MacroSolverApp;
pub use worker::Worker;

mod config;
mod utils;
mod widgets;
mod worker;
14 changes: 0 additions & 14 deletions src/utils.rs

This file was deleted.

20 changes: 4 additions & 16 deletions src/widgets/food_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@ use egui::{
Align, Id, Layout, Widget,
};
use egui_extras::Column;
use game_data::{get_item_name, Consumable, CrafterStats, Locale};

use crate::utils::contains_noncontiguous;
use game_data::{find_meals, get_item_name, Consumable, CrafterStats, Locale};

#[derive(Default)]
struct FoodFinder {}

impl ComputerMut<(&str, Locale), Vec<usize>> for FoodFinder {
fn compute(&mut self, (text, locale): (&str, Locale)) -> Vec<usize> {
game_data::MEALS
.iter()
.enumerate()
.filter_map(|(index, item)| {
let item_name = get_item_name(item.item_id, item.hq, locale);
match contains_noncontiguous(&item_name.to_lowercase(), text) {
true => Some(index),
false => None,
}
})
.collect()
find_meals(text, locale)
}
}

Expand Down Expand Up @@ -84,15 +72,15 @@ impl<'a> Widget for FoodSelect<'a> {
ui.horizontal(|ui| {
ui.label("Search:");
if ui.text_edit_singleline(&mut search_text).changed() {
search_text = search_text.replace("\0", "").replace("(HQ)", "\u{e03c}");
search_text = search_text.replace("\0", "");
}
});
ui.separator();

let mut search_result = Vec::new();
ui.ctx().memory_mut(|mem| {
let search_cache = mem.caches.cache::<FoodSearchCache<'_>>();
search_result = search_cache.get((&search_text.to_lowercase(), self.locale));
search_result = search_cache.get((&search_text, self.locale));
});

ui.ctx().data_mut(|data| {
Expand Down
20 changes: 4 additions & 16 deletions src/widgets/potion_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@ use egui::{
Align, Id, Layout, Widget,
};
use egui_extras::Column;
use game_data::{get_item_name, Consumable, CrafterStats, Locale};

use crate::utils::contains_noncontiguous;
use game_data::{find_potions, get_item_name, Consumable, CrafterStats, Locale};

#[derive(Default)]
struct PotionFinder {}

impl ComputerMut<(&str, Locale), Vec<usize>> for PotionFinder {
fn compute(&mut self, (text, locale): (&str, Locale)) -> Vec<usize> {
game_data::POTIONS
.iter()
.enumerate()
.filter_map(|(index, item)| {
let item_name = get_item_name(item.item_id, item.hq, locale);
match contains_noncontiguous(&item_name.to_lowercase(), text) {
true => Some(index),
false => None,
}
})
.collect()
find_potions(text, locale)
}
}

Expand Down Expand Up @@ -84,15 +72,15 @@ impl<'a> Widget for PotionSelect<'a> {
ui.horizontal(|ui| {
ui.label("Search:");
if ui.text_edit_singleline(&mut search_text).changed() {
search_text = search_text.replace("\0", "").replace("(HQ)", "\u{e03c}");
search_text = search_text.replace("\0", "");
}
});
ui.separator();

let mut search_result = Vec::new();
ui.ctx().memory_mut(|mem| {
let search_cache = mem.caches.cache::<PotionSearchCache<'_>>();
search_result = search_cache.get((&search_text.to_lowercase(), self.locale));
search_result = search_cache.get((&search_text, self.locale));
});

ui.ctx().data_mut(|data| {
Expand Down
25 changes: 6 additions & 19 deletions src/widgets/recipe_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@ use egui::{
};
use egui_extras::Column;
use game_data::{
get_game_settings, get_item_name, get_job_name, Consumable, Ingredient, Locale, RLVLS,
find_recipes, get_game_settings, get_item_name, get_job_name, Consumable, Ingredient, Locale,
RLVLS,
};

use crate::{
config::{CrafterConfig, QualitySource, RecipeConfiguration},
utils::contains_noncontiguous,
};
use crate::config::{CrafterConfig, QualitySource, RecipeConfiguration};

#[derive(Default)]
struct RecipeFinder {}

impl ComputerMut<(&str, Locale), Vec<usize>> for RecipeFinder {
fn compute(&mut self, (text, locale): (&str, Locale)) -> Vec<usize> {
game_data::RECIPES
.iter()
.enumerate()
.filter_map(|(index, recipe)| {
let item_name = get_item_name(recipe.item_id, false, locale);
match contains_noncontiguous(&item_name.to_lowercase(), text) {
true => Some(index),
false => None,
}
})
.collect()
find_recipes(text, locale)
}
}

Expand Down Expand Up @@ -69,16 +57,15 @@ impl<'a> RecipeSelect<'a> {
ui.horizontal(|ui| {
ui.label("Search:");
if ui.text_edit_singleline(&mut search_text).changed() {
search_text = search_text.replace("\0", "").replace("(CL)", "\u{e03d}");
search_text = search_text.replace("\0", "");
}
});
ui.separator();

let mut search_result = Vec::new();
ui.ctx().memory_mut(|mem| {
let search_cache = mem.caches.cache::<SearchCache<'_>>();
let cache_search_string = search_text.trim_end_matches('\u{e03c}').to_lowercase();
search_result = search_cache.get((&cache_search_string, self.locale));
search_result = search_cache.get((&search_text, self.locale));
});

ui.ctx().data_mut(|data| {
Expand Down

0 comments on commit 62a5d7f

Please sign in to comment.