Skip to content

Commit

Permalink
fix(fribbels#170):update build scores when saved build modal opens
Browse files Browse the repository at this point in the history
  • Loading branch information
kaixin-hc committed Apr 14, 2024
1 parent 15f07a5 commit 461580d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/components/BuildsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SaveState } from 'lib/saveState'
import { Message } from 'lib/message'
import { Character, SavedBuild } from 'types/Character'
import { CharacterPreview } from 'components/CharacterPreview.jsx'
import { RelicScorer } from 'lib/relicScorer'

interface BuildsModalProps {
open: boolean
Expand All @@ -26,10 +27,11 @@ const BuildsModal: React.FC<BuildsModalProps> = ({
const characterMetadata = DB.getMetadata().characters[selectedCharacter?.id || 0]
const characterName = characterMetadata?.displayName

// Pick the first build if there are any
// When opening, pick the first build if there are any + update build scores
useEffect(() => {
if (open && selectedCharacter?.builds?.length) {
setSelectedBuild(0)
updateBuildsScoringAlgo(selectedCharacter.builds)
}
}, [open, selectedCharacter])

Expand Down Expand Up @@ -79,6 +81,16 @@ const BuildsModal: React.FC<BuildsModalProps> = ({
})
}

// Updates all saved builds with the latest scoring algorithm
function updateBuildsScoringAlgo(builds: SavedBuild[]) {
for (let b of builds) {
const relicsById = window.store.getState().relicsById
const relics = Object.values(b.build).map((x) => relicsById[x])
let score = RelicScorer.scoreCharacterWithRelics(selectedCharacter, relics)
b.score = { score: Math.round(score.totalScore ?? 0), rating: score.totalRating ?? 'N/A' }
}
}

function onModalOk() {
setOpen(false)
}
Expand Down

0 comments on commit 461580d

Please sign in to comment.