Update RingSelect

- Changes rings from being an object to an array.
- Being an array means the index is 0-based not 1-based
- Ensured logic is correct, including what happens when to ring 4 when you unset ring 3
This commit is contained in:
Justin Edmund 2025-02-09 18:44:10 -08:00
parent 0430b5b23c
commit a188143422

View file

@ -25,21 +25,21 @@ interface Props {
const RingSelect = ({ gridCharacter, sendValues }: Props) => {
// Ring value states
const [rings, setRings] = useState<CharacterOverMastery>({
1: { ...emptyRing, modifier: 1 },
2: { ...emptyRing, modifier: 2 },
3: emptyRing,
4: emptyRing,
})
const [rings, setRings] = useState<CharacterOverMastery>([
{ ...emptyRing, modifier: 1 },
{ ...emptyRing, modifier: 2 },
emptyRing,
emptyRing,
])
useEffect(() => {
if (gridCharacter.over_mastery) {
setRings({
1: gridCharacter.over_mastery[1],
2: gridCharacter.over_mastery[2],
3: gridCharacter.over_mastery[3],
4: gridCharacter.over_mastery[4],
})
setRings([
gridCharacter.over_mastery[0],
gridCharacter.over_mastery[1],
gridCharacter.over_mastery[2],
gridCharacter.over_mastery[3],
])
}
}, [gridCharacter])
@ -64,13 +64,13 @@ const RingSelect = ({ gridCharacter, sendValues }: Props) => {
}
switch (index) {
case 1:
case 0:
return overMastery.a ? [overMastery.a[0]] : []
case 2:
case 1:
return overMastery.a ? [overMastery.a[1]] : []
case 3:
case 2:
return overMastery.b ? [noValue, ...overMastery.b] : []
case 4:
case 3:
return overMastery.c ? [noValue, ...overMastery.c] : []
default:
return []
@ -78,72 +78,74 @@ const RingSelect = ({ gridCharacter, sendValues }: Props) => {
}
function receiveRingValues(index: number, left: number, right: number) {
// console.log(`Receiving values from ${index}: ${left} ${right}`)
if (index == 1 || index == 2) {
setSyncedRingValues(index, right)
} else if (index == 3 && left == 0) {
setRings({
...rings,
3: {
modifier: 0,
strength: 0,
},
4: {
modifier: 0,
strength: 0,
},
if (index === 0 || index === 1) {
// For rings 1 and 2 (indices 0 and 1), update using the synced function.
setSyncedRingValues(index as 0 | 1, right)
} else if (index === 2 && left === 0) {
// If ring 3 (index 2) is being unset (left is 0), then also unset ring 4.
setRings((prev) => {
const newRings = [...prev]
newRings[2] = { modifier: 0, strength: 0 }
newRings[3] = { modifier: 0, strength: 0 }
return newRings
})
} else {
setRings({
...rings,
[index]: {
modifier: left,
strength: right,
},
// For any other case (including ring 4 being unset), update only that ring.
setRings((prev) => {
const newRings = [...prev]
newRings[index] = { modifier: left, strength: right }
return newRings
})
}
}
function setSyncedRingValues(index: 1 | 2, value: number) {
// console.log(`Setting synced value for ${index} with value ${value}`)
const atkValues = (dataSet(1)[0] as ItemSkill).values ?? []
const hpValues = (dataSet(2)[0] as ItemSkill).values ?? []
function setSyncedRingValues(changedIndex: 0 | 1, newStrength: number) {
// Assume dataSet(0) holds the attack-related data and dataSet(1) holds the HP-related data.
// (Adjust these calls if your datasets are in different positions.)
const attackItem = dataSet(0)[0] as ItemSkill
const hpItem = dataSet(1)[0] as ItemSkill
const found =
index === 1 ? atkValues.indexOf(value) : hpValues.indexOf(value)
const atkValue = atkValues[found] ?? 0
const hpValue = hpValues[found] ?? 0
const attackValues: number[] = attackItem.values ?? []
const hpValues: number[] = hpItem.values ?? []
setRings({
...rings,
1: {
modifier: 1,
strength: atkValue,
},
2: {
modifier: 2,
strength: hpValue,
},
// Determine the index based on which ring changed:
const selectedIndex =
changedIndex === 0
? attackValues.indexOf(newStrength)
: hpValues.indexOf(newStrength)
// If the new strength value isnt found, do nothing.
if (selectedIndex === -1) {
return
}
// Get the corresponding values for both rings.
const newAttackValue = attackValues[selectedIndex] ?? 0
const newHpValue = hpValues[selectedIndex] ?? 0
// Update both ring values simultaneously.
setRings((prev) => {
const newRings = [...prev]
newRings[0] = { modifier: 1, strength: newAttackValue }
newRings[1] = { modifier: 2, strength: newHpValue }
return newRings
})
}
return (
<div className={styles.rings}>
{[...Array(4)].map((e, i) => {
const index = i + 1
const ringStat = rings[index]
{rings.map((ringStat, i) => {
return (
<ExtendedMasterySelect
name={`ring-${index}`}
name={`ring-${i}`}
object="ring"
key={`ring-${index}`}
dataSet={dataSet(index)}
leftSelectDisabled={index === 1 || index === 2}
leftSelectValue={ringStat.modifier ? ringStat.modifier : 0}
rightSelectValue={ringStat.strength ? ringStat.strength : 0}
key={`ring-${i}`}
dataSet={dataSet(i)}
leftSelectDisabled={i === 0 || i === 1}
leftSelectValue={ringStat?.modifier ?? 0}
rightSelectValue={ringStat?.strength ?? 0}
sendValues={(left: number, right: number) => {
receiveRingValues(index, left, right)
receiveRingValues(i, left, right)
}}
/>
)