-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathuseBoardDescription.ts
More file actions
62 lines (55 loc) · 1.62 KB
/
useBoardDescription.ts
File metadata and controls
62 lines (55 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { useMemo } from 'react'
import {
BlunderMeterResult,
GameNode,
MaiaEvaluation,
StockfishEvaluation,
} from 'src/types'
import { MAIA_MODELS } from './constants'
import { describePosition } from './useDescriptionGenerator'
type ColorSanMapping = {
[move: string]: {
san: string
color: string
}
}
export const useBoardDescription = (
currentNode: GameNode | null,
moveEvaluation: {
maia?: MaiaEvaluation
stockfish?: StockfishEvaluation
} | null,
blunderMeter: BlunderMeterResult,
colorSanMapping: ColorSanMapping,
) => {
return useMemo(() => {
if (
!currentNode ||
!moveEvaluation?.stockfish ||
!moveEvaluation?.maia ||
moveEvaluation.stockfish.depth < 12
) {
return ''
}
const fen = currentNode.fen
const whiteToMove = currentNode.turn === 'w'
const stockfishEvals = moveEvaluation.stockfish.cp_vec
const maiaEvals: Record<string, number[]> = {}
const allMaiaAnalysis = currentNode.analysis.maia || {}
Object.keys(moveEvaluation.maia.policy).forEach((move) => {
maiaEvals[move] = new Array(MAIA_MODELS.length).fill(0)
})
MAIA_MODELS.forEach((model, index) => {
const modelAnalysis = allMaiaAnalysis[model]
if (modelAnalysis?.policy) {
Object.entries(modelAnalysis.policy).forEach(([move, probability]) => {
if (!maiaEvals[move]) {
maiaEvals[move] = new Array(MAIA_MODELS.length).fill(0)
}
maiaEvals[move][index] = probability
})
}
})
return describePosition(fen, stockfishEvals, maiaEvals, whiteToMove)
}, [currentNode, moveEvaluation])
}