From f426715a4b2d4094ddf57c72b0954cbd42bf1297 Mon Sep 17 00:00:00 2001 From: ahmedmgamal94 <98055904+ahmedmgamal94@users.noreply.github.com> Date: Sun, 25 Aug 2024 00:23:20 -0700 Subject: [PATCH 1/2] dynamic fragment slots based on aspect --- .../subclass/AbilitiesModification.tsx | 67 ++++++++++++++----- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/src/features/subclass/AbilitiesModification.tsx b/src/features/subclass/AbilitiesModification.tsx index ebea61f..50d4e42 100644 --- a/src/features/subclass/AbilitiesModification.tsx +++ b/src/features/subclass/AbilitiesModification.tsx @@ -209,7 +209,6 @@ const fetchMods = async (subclass: ManifestSubclass) => { return modsData; } }; - const AbilitiesModification: React.FC = ({ subclass }) => { const [mods, setMods] = useState<{ [key: string]: (ManifestPlug | ManifestAspect | ManifestStatPlug)[]; @@ -237,6 +236,10 @@ const AbilitiesModification: React.FC = ({ subclass } }, [subclass]); + const calculateAvailableFragmentSlots = useCallback(() => { + return loadout.aspects.reduce((total, aspect) => total + (aspect.energyCapacity || 0), 0); + }, [loadout.aspects]); + const handleModSelect = ( category: string, mod: ManifestPlug | ManifestAspect | ManifestStatPlug, @@ -246,23 +249,22 @@ const AbilitiesModification: React.FC = ({ subclass switch (category) { case 'ASPECTS': - updatedMods = [...loadout.aspects]; + updatedMods = [...loadout.aspects] as ManifestAspect[]; break; case 'FRAGMENTS': - updatedMods = [...loadout.fragments]; + updatedMods = [...loadout.fragments] as ManifestStatPlug[]; break; case 'SUPERS': case 'CLASS_ABILITIES': case 'MOVEMENT_ABILITIES': case 'MELEE_ABILITIES': case 'GRENADES': - updatedMods = [mod]; + updatedMods = [mod] as ManifestPlug[]; break; default: return; } - // Check if the mod already exists in another slot and replace it with an empty mod if (category === 'ASPECTS' || category === 'FRAGMENTS') { const modIndex = updatedMods.findIndex( (existingMod) => existingMod.itemHash === mod.itemHash @@ -272,13 +274,39 @@ const AbilitiesModification: React.FC = ({ subclass updatedMods[modIndex] = category === 'ASPECTS' ? EMPTY_ASPECT : EMPTY_FRAGMENT; } - // Assign the mod to the selected slot - updatedMods[index!] = mod; - } else { - // Directly assign the mod for SUPERS and abilities - updatedMods[0] = mod; + updatedMods[index!] = mod as (typeof updatedMods)[number]; + + // If updating aspects, we need to check if we need to empty any fragment slots + if (category === 'ASPECTS') { + const newAspects = updatedMods as ManifestAspect[]; + const newAvailableSlots = newAspects.reduce( + (total, aspect) => total + (aspect.energyCapacity || 0), + 0 + ); + + // Update fragments, emptying any that exceed the new available slots + const updatedFragments = loadout.fragments.map((fragment, idx) => + idx < newAvailableSlots ? fragment : EMPTY_FRAGMENT + ); + + // Dispatch updates for both aspects and fragments + dispatch( + updateSubclassMods({ + category: 'ASPECTS', + mods: newAspects, + }) + ); + dispatch( + updateSubclassMods({ + category: 'FRAGMENTS', + mods: updatedFragments, + }) + ); + return; // Exit the function early as we've already dispatched the updates + } } + // For all other cases, dispatch the update as before dispatch( updateSubclassMods({ category, @@ -312,21 +340,26 @@ const AbilitiesModification: React.FC = ({ subclass const SlotComponent = category === 'SUPERS' ? SuperModSlot : ModSlot; + const availableFragmentSlots = calculateAvailableFragmentSlots(); + const isDisabled = category === 'FRAGMENTS' && index! >= availableFragmentSlots; + return (
handleMouseEnter(e, slotId)} onMouseLeave={handleMouseLeave}> {isEmptyMod && ( - Empty + {isDisabled ? 'Locked' : 'Empty'} )} - {isHovered && ( + {isHovered && !isDisabled && ( = ({ subclass ); }, - [mods, handleModSelect, hoveredSlot, submenuPosition] + [mods, handleModSelect, hoveredSlot, submenuPosition, calculateAvailableFragmentSlots] ); if (loading) { @@ -413,9 +446,13 @@ const AbilitiesModification: React.FC = ({ subclass FRAGMENTS - {loadout.fragments.map((fragment, index) => ( + {Array.from({ length: 5 }).map((_, index) => ( - {renderModCategory('FRAGMENTS', fragment, index)} + {renderModCategory( + 'FRAGMENTS', + loadout.fragments[index] || EMPTY_FRAGMENT, + index + )} ))} From 81787b095ff9310701fbbdb501e5e3f58f40e938 Mon Sep 17 00:00:00 2001 From: ahmedmgamal94 <98055904+ahmedmgamal94@users.noreply.github.com> Date: Sun, 25 Aug 2024 00:24:46 -0700 Subject: [PATCH 2/2] dynamic fragment slots depending on aspect --- src/features/subclass/AbilitiesModification.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/features/subclass/AbilitiesModification.tsx b/src/features/subclass/AbilitiesModification.tsx index 50d4e42..9d8d15a 100644 --- a/src/features/subclass/AbilitiesModification.tsx +++ b/src/features/subclass/AbilitiesModification.tsx @@ -276,7 +276,6 @@ const AbilitiesModification: React.FC = ({ subclass updatedMods[index!] = mod as (typeof updatedMods)[number]; - // If updating aspects, we need to check if we need to empty any fragment slots if (category === 'ASPECTS') { const newAspects = updatedMods as ManifestAspect[]; const newAvailableSlots = newAspects.reduce( @@ -284,12 +283,10 @@ const AbilitiesModification: React.FC = ({ subclass 0 ); - // Update fragments, emptying any that exceed the new available slots const updatedFragments = loadout.fragments.map((fragment, idx) => idx < newAvailableSlots ? fragment : EMPTY_FRAGMENT ); - // Dispatch updates for both aspects and fragments dispatch( updateSubclassMods({ category: 'ASPECTS', @@ -302,11 +299,10 @@ const AbilitiesModification: React.FC = ({ subclass mods: updatedFragments, }) ); - return; // Exit the function early as we've already dispatched the updates + return; } } - // For all other cases, dispatch the update as before dispatch( updateSubclassMods({ category,