Skip to content

Commit 3b91505

Browse files
fix: stockfish promotion logic + blunder meter cases
1 parent e562873 commit 3b91505

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/hooks/useAnalysisController/utils.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const generateColorSanMapping = (
2626
if (!stockfish) return mapping
2727

2828
moves.forEach((m) => {
29-
const moveKey = `${m.from}${m.to}`
29+
const moveKey = `${m.from}${m.to}${m.promotion || ''}`
3030
// Use winrate_loss_vec if available, otherwise fall back to cp_relative_vec
3131
const winrateLoss = stockfish?.winrate_loss_vec?.[moveKey]
3232
const relativeEval = stockfish?.cp_relative_vec[moveKey]
@@ -226,31 +226,41 @@ export const calculateBlunderMeter = (
226226
}))
227227

228228
const totalFloored = flooredPercentages.reduce((sum, p) => sum + p.floored, 0)
229-
const remainingPoints = 100 - totalFloored
229+
const remainingPoints = Math.max(0, Math.min(100 - totalFloored, 100))
230230

231231
const sortedByFractional = [...flooredPercentages].sort(
232232
(a, b) => b.fractional - a.fractional,
233233
)
234234

235-
for (let i = 0; i < remainingPoints; i++) {
236-
sortedByFractional[i].floored += 1
235+
for (let i = 0; i < remainingPoints && i < sortedByFractional.length; i++) {
236+
if (sortedByFractional[i]) {
237+
sortedByFractional[i].floored += 1
238+
}
237239
}
238240

239-
const adjustedGood = sortedByFractional.find((p) => p.key === 'good')
240-
const adjustedOk = sortedByFractional.find((p) => p.key === 'ok')
241-
const adjustedBlunder = sortedByFractional.find((p) => p.key === 'blunder')
241+
const adjustedGood = sortedByFractional.find(
242+
(p) => p && p.key === 'good',
243+
) || {
244+
floored: 0,
245+
}
246+
const adjustedOk = sortedByFractional.find((p) => p && p.key === 'ok') || {
247+
floored: 0,
248+
}
249+
const adjustedBlunder = sortedByFractional.find(
250+
(p) => p && p.key === 'blunder',
251+
) || { floored: 0 }
242252

243253
return {
244254
blunderMoves: {
245-
probability: adjustedBlunder?.floored || 0,
255+
probability: adjustedBlunder.floored,
246256
moves: blunderMoveChanceInfo,
247257
},
248258
okMoves: {
249-
probability: adjustedOk?.floored || 0,
259+
probability: adjustedOk.floored,
250260
moves: okMoveChanceInfo,
251261
},
252262
goodMoves: {
253-
probability: adjustedGood?.floored || 0,
263+
probability: adjustedGood.floored,
254264
moves: goodMoveChanceInfo,
255265
},
256266
}

src/hooks/useStockfishEngine/engine.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class Engine {
5555
this.store = {}
5656
this.legalMoveCount = legalMoveCount
5757
const board = new Chess(fen)
58-
this.moves = board.moves({ verbose: true }).map((x) => x.from + x.to)
58+
this.moves = board
59+
.moves({ verbose: true })
60+
.map((x) => x.from + x.to + (x.promotion || ''))
5961
this.fen = fen
6062
this.isEvaluating = true
6163
this.evaluationGenerator = this.createEvaluationGenerator()

0 commit comments

Comments
 (0)