From 27c4c056327a8298e4920d08b0d50683ef1ee31a Mon Sep 17 00:00:00 2001 From: Chris Vickery Date: Mon, 22 Jan 2024 22:20:48 -0800 Subject: [PATCH] basic enchantment filtering in crafting menus --- src/game/crafting/destroy-items.tsx | 83 +++++++++++++++++++++----- src/game/crafting/disenchant-items.tsx | 81 ++++++++++++++++++++----- src/helpers.ts | 12 +++- 3 files changed, 143 insertions(+), 33 deletions(-) diff --git a/src/game/crafting/destroy-items.tsx b/src/game/crafting/destroy-items.tsx index c4f1841..8ee0869 100644 --- a/src/game/crafting/destroy-items.tsx +++ b/src/game/crafting/destroy-items.tsx @@ -1,5 +1,6 @@ import React, { useState, useEffect } from "react"; +import Grid from "@mui/material/Grid"; import FormControl from "@mui/material/FormControl"; import InputLabel from "@mui/material/InputLabel"; import Select from "@mui/material/Select"; @@ -18,6 +19,8 @@ import { addSpaces, isItemEquipped, itemSorter, + itemImprovesCrafting, + EnchantmentNames, } from "src/helpers"; export function DestroyItems({ @@ -28,26 +31,43 @@ export function DestroyItems({ disabled: boolean; }): JSX.Element { let [value, setValue] = useState(""); + let [selectedEnchantment, setSelectedEnchantment] = useState(""); + const hasImprovedCrafting = !!hero.inventory.find((item) => + itemImprovesCrafting(item.baseItem), + ); const [destroyItemMutation, { loading }] = useDestroyItemMutation(); - const destroyableItems = hero.inventory - .filter((item) => { - if (item.type === InventoryItemType.Quest) { - return false; - } - if (isItemEquipped(hero, item)) { - return false; - } + const destroyableItems = hero.inventory.filter((item) => { + if (item.type === InventoryItemType.Quest) { + return false; + } + if (isItemEquipped(hero, item)) { + return false; + } - return !!item.enchantment; - }) + return !!item.enchantment; + }); + const filteredItems = destroyableItems + .filter( + (item) => + selectedEnchantment === "" || item.enchantment === selectedEnchantment, + ) .sort(itemSorter); + + const destroyableEnchantments: EnchantmentType[] = [ + ...new Set( + destroyableItems + .filter((item) => !isItemEquipped(hero, item)) + .map((item) => item.enchantment), + ), + ]; + const label = "Select item to destroy"; - let selectedItem = destroyableItems.find((item) => item.id === value); + let selectedItem = filteredItems.find((item) => item.id === value); if (!selectedItem && value.length) { - if (destroyableItems.length) { - selectedItem = destroyableItems[0]; + if (filteredItems.length) { + selectedItem = filteredItems[0]; value = selectedItem.id; } else { value = ""; @@ -81,7 +101,7 @@ export function DestroyItems({ onChange={(e) => { const itemId = e.target.value; const inventoryItem = hero.inventory.find( - (item) => item.id === itemId + (item) => item.id === itemId, ); if (!inventoryItem) { return; @@ -89,7 +109,7 @@ export function DestroyItems({ setValue(itemId); }} > - {destroyableItems.map((item) => { + {filteredItems.map((item) => { return ( +
+
+ {hasImprovedCrafting && ( + + + Filter by enchantment + + + + )} ); } diff --git a/src/game/crafting/disenchant-items.tsx b/src/game/crafting/disenchant-items.tsx index 944eb04..208d768 100644 --- a/src/game/crafting/disenchant-items.tsx +++ b/src/game/crafting/disenchant-items.tsx @@ -19,6 +19,8 @@ import { addSpaces, isItemEquipped, itemSorter, + itemImprovesCrafting, + EnchantmentNames, } from "src/helpers"; export function DisenchantItems({ @@ -28,26 +30,42 @@ export function DisenchantItems({ hero: Hero; disabled: boolean; }): JSX.Element { + let [selectedEnchantment, setSelectedEnchantment] = useState(""); let [value, setValue] = useState(""); + const hasImprovedCrafting = !!hero.inventory.find((item) => + itemImprovesCrafting(item.baseItem), + ); const [disenchantItemMutation, { loading }] = useDisenchantItemMutation(); - const disenchantableItems = hero.inventory - .filter((item) => { - if (item.type === InventoryItemType.Quest) { - return false; - } - if (isItemEquipped(hero, item)) { - return false; - } + const disenchantableItems = hero.inventory.filter((item) => { + if (item.type === InventoryItemType.Quest) { + return false; + } + if (isItemEquipped(hero, item)) { + return false; + } - return !!item.enchantment; - }) + return !!item.enchantment; + }); + const filteredItems = disenchantableItems + .filter( + (item) => + selectedEnchantment === "" || item.enchantment === selectedEnchantment, + ) .sort(itemSorter); - let selectedItem = disenchantableItems.find((item) => item.id === value); + const destroyableEnchantments: EnchantmentType[] = [ + ...new Set( + disenchantableItems + .filter((item) => !isItemEquipped(hero, item)) + .map((item) => item.enchantment), + ), + ]; + + let selectedItem = filteredItems.find((item) => item.id === value); if (!selectedItem && value.length) { - if (disenchantableItems.length) { - selectedItem = disenchantableItems[0]; + if (filteredItems.length) { + selectedItem = filteredItems[0]; value = selectedItem.id; } else { value = ""; @@ -84,7 +102,7 @@ export function DisenchantItems({ onChange={(e) => { const itemId = e.target.value; const inventoryItem = hero.inventory.find( - (item) => item.id === itemId + (item) => item.id === itemId, ); if (!inventoryItem) { return; @@ -92,7 +110,7 @@ export function DisenchantItems({ setValue(itemId); }} > - {disenchantableItems.map((item) => { + {filteredItems.map((item) => { return ( +
+
+ {hasImprovedCrafting && ( + + + Filter by enchantment + + + + )} ); } diff --git a/src/helpers.ts b/src/helpers.ts index 1be0c9c..5fa2fd1 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -70,14 +70,14 @@ export const EnchantmentNames: { [x in EnchantmentType]?: string } = { }; export function pureEnchantmentDisplayName( - enchantment: EnchantmentType + enchantment: EnchantmentType, ): string { return EnchantmentNames[enchantment] ?? addSpaces(enchantment); } export function enchantmentDisplayName( itemName: string, - enchantment: EnchantmentType + enchantment: EnchantmentType, ): string { return `${itemName} of ${EnchantmentNames[enchantment] ?? "The Unknown"}`; } @@ -96,7 +96,7 @@ type DistanceableLocation = { export function distance2d( a: DistanceableLocation, - b: DistanceableLocation + b: DistanceableLocation, ): number { return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2)); } @@ -147,6 +147,12 @@ export function itemAllowsCrafting(item: string): boolean { } return false; } +export function itemImprovesCrafting(item: string): boolean { + if (item === "crafting-goggles") { + return true; + } + return false; +} export function itemImprovesAutoBattle(item: string): boolean { if (item === "orb-of-forbidden-power") {