Addressed further review.

This commit is contained in:
Sam 2021-12-06 23:33:11 -05:00
parent 94c19735ca
commit bf348b7f43
5 changed files with 33 additions and 29 deletions

View File

@ -219,7 +219,9 @@ impl TagExampleInfo for Material {
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum ItemTag { pub enum ItemTag {
/// Used to indicate that an item is composed of this material
Material(Material), Material(Material),
/// Used to indicate that an item is composed of this material kind
MaterialKind(MaterialKind), MaterialKind(MaterialKind),
Cultist, Cultist,
Potion, Potion,

View File

@ -369,7 +369,7 @@ pub fn modify_name<'a>(item_name: &'a str, item: &'a Item) -> Cow<'a, str> {
/// manifests in voxygen (Main component, material, hands) /// manifests in voxygen (Main component, material, hands)
pub type ModularWeaponKey = (String, String, Hands); pub type ModularWeaponKey = (String, String, Hands);
pub fn weapon_to_key(mod_weap: &dyn ItemDesc) -> ModularWeaponKey { pub fn weapon_to_key(mod_weap: impl ItemDesc) -> ModularWeaponKey {
let hands = if let ItemKind::Tool(tool) = &*mod_weap.kind() { let hands = if let ItemKind::Tool(tool) = &*mod_weap.kind() {
tool.hands tool.hands
} else { } else {

View File

@ -258,10 +258,12 @@ pub fn convert_waypoint_from_database_json(
} }
// Used to handle cases of modular items that are composed of components. // Used to handle cases of modular items that are composed of components.
// Returns a mutable reference to the parent of an item that is a component. If // When called with the index of a component's parent item, it can get a mutable
// parent item is itself a component, recursively goes through inventory until // reference to that parent item so that the component can be added to the
// it grabs component. // parent item. If the item corresponding to the index this is called on is
fn get_mutable_parent_item<'a, 'b, T>( // itself a component, recursively goes through inventory until it grabs
// component.
fn get_mutable_item<'a, 'b, T>(
index: usize, index: usize,
inventory_items: &'a [Item], inventory_items: &'a [Item],
item_indices: &'a HashMap<i64, usize>, item_indices: &'a HashMap<i64, usize>,
@ -271,14 +273,14 @@ fn get_mutable_parent_item<'a, 'b, T>(
where where
'b: 'a, 'b: 'a,
{ {
// First checks if parent item is itself also a component, if it is, tries to // First checks if item is a component, if it is, tries to get a mutable
// get a mutable reference to itself by getting a mutable reference to the item // reference to itself by getting a mutable reference to the item that is its
// that is its own parent // parent
if inventory_items[index].position.contains("component_") { if inventory_items[index].position.contains("component_") {
if let Some(parent) = item_indices if let Some(parent) = item_indices
.get(&inventory_items[index].parent_container_item_id) .get(&inventory_items[index].parent_container_item_id)
.and_then(move |i| { .and_then(move |i| {
get_mutable_parent_item( get_mutable_item(
*i, *i,
inventory_items, inventory_items,
item_indices, item_indices,
@ -294,9 +296,8 @@ where
.split('_') .split('_')
.nth(1) .nth(1)
.and_then(|s| s.parse::<usize>().ok()); .and_then(|s| s.parse::<usize>().ok());
// Returns mutable reference to parent item of original item, by grabbing // Returns mutable reference to component item by accessing the component
// the component representing the parent item from the parent item's parent // through its parent item item
// item
component_index.and_then(move |i| parent.persistence_access_mutable_component(i)) component_index.and_then(move |i| parent.persistence_access_mutable_component(i))
} else { } else {
None None
@ -394,7 +395,7 @@ pub fn convert_inventory_from_database_items(
)); ));
} }
} else if let Some(&j) = item_indices.get(&db_item.parent_container_item_id) { } else if let Some(&j) = item_indices.get(&db_item.parent_container_item_id) {
if let Some(parent) = get_mutable_parent_item( if let Some(parent) = get_mutable_item(
j, j,
inventory_items, inventory_items,
&item_indices, &item_indices,
@ -460,7 +461,7 @@ pub fn convert_loadout_from_database_items(
.map_err(convert_error)?; .map_err(convert_error)?;
} else if let Some(&j) = item_indices.get(&db_item.parent_container_item_id) { } else if let Some(&j) = item_indices.get(&db_item.parent_container_item_id) {
if let Some(parent) = if let Some(parent) =
get_mutable_parent_item(j, database_items, &item_indices, &mut loadout, &|l, s| { get_mutable_item(j, database_items, &item_indices, &mut loadout, &|l, s| {
l.get_mut_item_at_slot_using_persistence_key(s).ok() l.get_mut_item_at_slot_using_persistence_key(s).ok()
}) })
{ {

View File

@ -28,7 +28,7 @@ use conrod_core::{
widget_ids, Color, Colorable, Labelable, Positionable, Sizeable, Widget, WidgetCommon, widget_ids, Color, Colorable, Labelable, Positionable, Sizeable, Widget, WidgetCommon,
}; };
use i18n::Localization; use i18n::Localization;
use std::{borrow::Cow, sync::Arc}; use std::sync::Arc;
use strum::{EnumIter, IntoEnumIterator}; use strum::{EnumIter, IntoEnumIterator};
@ -464,22 +464,23 @@ impl<'a> Widget for Crafting<'a> {
.all(|&substring| output_name.contains(substring)) .all(|&substring| output_name.contains(substring))
}, },
SearchFilter::Input => recipe.inputs().any(|(input, _, _)| { SearchFilter::Input => recipe.inputs().any(|(input, _, _)| {
let input_name = match input { let search = |input_name: &str| {
RecipeInput::Item(def) => def.name(), let input_name = input_name.to_lowercase();
RecipeInput::Tag(tag) => Cow::Borrowed(tag.name()), search_keys
RecipeInput::TagSameItem(tag) => Cow::Borrowed(tag.name()), .iter()
// Works, but probably will have some...interesting false positives .all(|&substring| input_name.contains(substring))
// Code reviewers probably required to do magic to make not hacky };
match input {
RecipeInput::Item(def) => search(&def.name()),
RecipeInput::Tag(tag) => search(tag.name()),
RecipeInput::TagSameItem(tag) => search(tag.name()),
RecipeInput::ListSameItem(defs) => { RecipeInput::ListSameItem(defs) => {
Cow::Owned(defs.iter().map(|def| def.name()).collect()) defs.iter().any(|def| search(&def.name()))
}, },
} }
.to_lowercase();
search_keys
.iter()
.all(|&substring| input_name.contains(substring))
}), }),
_ => false, SearchFilter::Nonexistant => false,
}) })
.map(|(name, recipe)| { .map(|(name, recipe)| {
let has_materials = self.client.available_recipes().get(name.as_str()).is_some(); let has_materials = self.client.available_recipes().get(name.as_str()).is_some();

View File

@ -471,7 +471,7 @@ impl<'a> Widget for ItemTooltip<'a> {
let item_kind = util::kind_text(item, i18n).to_string(); let item_kind = util::kind_text(item, i18n).to_string();
let material = item.tags().iter().find_map(|t| match t { let material = item.tags().iter().find_map(|t| match t {
ItemTag::Material(material) => Some(material), ItemTag::MaterialKind(material) => Some(material),
_ => None, _ => None,
}); });
@ -479,7 +479,7 @@ impl<'a> Widget for ItemTooltip<'a> {
format!( format!(
"{} ({})", "{} ({})",
item_kind, item_kind,
util::material_kind_text(&material.material_kind(), i18n) util::material_kind_text(material, i18n)
) )
} else { } else {
item_kind item_kind