Skip to content

Commit 9c82b2d

Browse files
fix: render errors + add maia to tournament games + access errors
1 parent b373209 commit 9c82b2d

File tree

5 files changed

+62
-65
lines changed

5 files changed

+62
-65
lines changed

src/components/Analysis/Highlight.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export const Highlight: React.FC<Props> = ({
3131
<p className="text-xl font-semibold">Current Position</p>
3232
<p className="text-sm text-secondary">
3333
Maia predicts that Black will play{' '}
34-
{moveEvaluation.maia
35-
? colorSanMapping[Object.keys(moveEvaluation.maia.policy)[0]].san
34+
{moveEvaluation?.maia
35+
? (colorSanMapping[Object.keys(moveEvaluation.maia.policy)[0]]
36+
?.san ?? Object.keys(moveEvaluation.maia.policy)[0])
3637
: '...'}{' '}
3738
next. This is a blunder.
3839
</p>
@@ -41,20 +42,20 @@ export const Highlight: React.FC<Props> = ({
4142
<div className="flex flex-col items-center justify-center bg-human-3/5 py-4">
4243
<p className="text-sm text-human-2">Maia White Win %</p>
4344
<p className="text-2xl font-bold text-human-1">
44-
{moveEvaluation.maia
45+
{moveEvaluation?.maia
4546
? `${Math.round(moveEvaluation.maia?.value * 1000) / 10}%`
4647
: '...'}
4748
</p>
4849
</div>
4950
<div className="flex flex-col items-center justify-center bg-engine-3/5 py-4">
5051
<p className="text-sm text-engine-2">
5152
SF Eval
52-
{moveEvaluation.stockfish?.depth
53+
{moveEvaluation?.stockfish?.depth
5354
? ` (D${moveEvaluation.stockfish?.depth})`
5455
: ''}
5556
</p>
5657
<p className="text-2xl font-bold text-engine-1">
57-
{moveEvaluation.stockfish
58+
{moveEvaluation?.stockfish
5859
? `${moveEvaluation.stockfish.model_optimal_cp / 100 > 0 ? '+' : ''}${moveEvaluation.stockfish.model_optimal_cp / 100}`
5960
: '...'}
6061
</p>

src/components/Analysis/MoveMap.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ export const MoveMap: React.FC<Props> = ({
173173
fill={colorSanMapping[entry.move].color || '#fff'}
174174
onMouseEnter={() => onMouseEnter(entry.move)}
175175
onMouseOutCapture={() => {
176-
console.log('beep')
177176
setHoverArrow(null)
178177
}}
179178
/>

src/components/Analysis/MoveRecommendations.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,28 @@ export const MoveRecommendations: React.FC<Props> = ({
4545
%
4646
</p>
4747
</div>
48-
{recommendations.maia?.map(({ move, prob }, index) => (
49-
<div
50-
key={index}
51-
className="flex items-center justify-between"
52-
style={{
53-
color: colorSanMapping[move].color,
54-
}}
55-
>
56-
<p
57-
className="cursor-default font-mono hover:underline"
58-
onMouseEnter={() => onMouseEnter(move)}
59-
onMouseOutCapture={() => setHoverArrow(null)}
48+
{recommendations.maia?.map(({ move, prob }, index) => {
49+
return (
50+
<div
51+
key={index}
52+
className="flex items-center justify-between"
53+
style={{
54+
color: colorSanMapping[move].color,
55+
}}
6056
>
61-
{colorSanMapping[move].san}
62-
</p>
63-
<p className="font-mono text-sm">
64-
{Math.round(prob * 1000) / 10}%
65-
</p>
66-
</div>
67-
))}
57+
<p
58+
className="cursor-default font-mono hover:underline"
59+
onMouseEnter={() => onMouseEnter(move)}
60+
onMouseOutCapture={() => setHoverArrow(null)}
61+
>
62+
{colorSanMapping[move]?.san ?? move}
63+
</p>
64+
<p className="font-mono text-sm">
65+
{Math.round(prob * 1000) / 10}%
66+
</p>
67+
</div>
68+
)
69+
})}
6870
</div>
6971
</div>
7072
<div className="flex flex-col gap-2 bg-background-1/60 p-5">
@@ -83,7 +85,7 @@ export const MoveRecommendations: React.FC<Props> = ({
8385
key={index}
8486
className="flex items-center justify-between"
8587
style={{
86-
color: colorSanMapping[move].color,
88+
color: colorSanMapping[move].color ?? '#FFFFFF',
8789
}}
8890
>
8991
<p

src/hooks/useAnalysisController/useAnalysisController.ts

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const useAnalysisController = (
5656
const [currentMaiaModel, setCurrentMaiaModel] = useState(MAIA_MODELS[0])
5757

5858
useEffect(() => {
59-
if (game.type === 'tournament') return
6059
const board = new Chess(game.moves[controller.currentIndex].board)
6160

6261
;(async () => {
@@ -176,22 +175,28 @@ export const useAnalysisController = (
176175
const lastMove = game.moves[controller.currentIndex].lastMove?.join('')
177176
if (!lastMove) return null
178177

179-
const cp_vec = game.stockfishEvaluations[
180-
controller.currentIndex
181-
] as MoveMap
182-
const model_optimal_cp = Math.max(...Object.values(cp_vec))
183-
const cp_relative_vec = Object.fromEntries(
184-
Object.entries(cp_vec).map(([move, evaluation]) => [
185-
move,
186-
model_optimal_cp - evaluation,
187-
]),
178+
const cp_vec = Object.fromEntries(
179+
Object.entries(
180+
game.stockfishEvaluations[controller.currentIndex] as MoveMap,
181+
).sort(([, a], [, b]) => b - a),
188182
)
189183

190-
stockfish = {
191-
cp_vec,
192-
cp_relative_vec,
193-
model_optimal_cp,
194-
} as StockfishEvaluation
184+
if (cp_vec) {
185+
const model_optimal_cp = Math.max(...Object.values(cp_vec))
186+
187+
const cp_relative_vec = Object.fromEntries(
188+
Object.entries(cp_vec).map(([move, evaluation]) => [
189+
move,
190+
model_optimal_cp - evaluation,
191+
]),
192+
)
193+
194+
stockfish = {
195+
cp_vec,
196+
cp_relative_vec,
197+
model_optimal_cp,
198+
} as StockfishEvaluation
199+
}
195200
}
196201

197202
let maia
@@ -325,27 +330,20 @@ export const useAnalysisController = (
325330
stockfish?: { move: string; cp: number }[]
326331
} = {}
327332

328-
if (maiaEvaluations[controller.currentIndex]) {
329-
const policy =
330-
maiaEvaluations[controller.currentIndex][currentMaiaModel].policy
331-
333+
if (moveEvaluation?.maia) {
334+
const policy = moveEvaluation.maia.policy
332335
const maia = Object.entries(policy)
333336
.slice(0, 5)
334-
.map(([move, prob]) => {
335-
return { move, prob } // Replace this with SAN notation
336-
})
337+
.map(([move, prob]) => ({ move, prob }))
337338

338339
recommendations.maia = maia
339340
}
340341

341-
if (stockfishEvaluations[controller.currentIndex]) {
342-
const cp_vec = stockfishEvaluations[controller.currentIndex].cp_vec
343-
342+
if (moveEvaluation?.stockfish) {
343+
const cp_vec = moveEvaluation.stockfish.cp_vec
344344
const stockfish = Object.entries(cp_vec)
345345
.slice(0, 5)
346-
.map(([move, cp]) => {
347-
return { move, cp }
348-
})
346+
.map(([move, cp]) => ({ move, cp }))
349347

350348
recommendations.stockfish = stockfish
351349
}
@@ -354,18 +352,15 @@ export const useAnalysisController = (
354352
}, [controller.currentIndex, maiaEvaluations, stockfishEvaluations])
355353

356354
const moveMap = useMemo(() => {
357-
const maiaRaw = maiaEvaluations[controller.currentIndex]
358-
const stockfishRaw = stockfishEvaluations[controller.currentIndex]
359-
360-
if (!maiaRaw || !stockfishRaw) {
355+
if (!moveEvaluation?.maia || !moveEvaluation?.stockfish) {
361356
return
362357
}
363358

364359
const maia = Object.fromEntries(
365-
Object.entries(maiaRaw[currentMaiaModel].policy).slice(0, 3),
360+
Object.entries(moveEvaluation.maia.policy).slice(0, 3),
366361
)
367362
const stockfish = Object.fromEntries(
368-
Object.entries(stockfishRaw.cp_vec).slice(0, 3),
363+
Object.entries(moveEvaluation.stockfish.cp_vec).slice(0, 3),
369364
)
370365

371366
const moves = Array.from(
@@ -375,8 +370,8 @@ export const useAnalysisController = (
375370
const data = []
376371

377372
for (const move of moves) {
378-
const cp = Math.max(-4, stockfishRaw.cp_relative_vec[move])
379-
const prob = maiaRaw[currentMaiaModel].policy[move] * 100
373+
const cp = Math.max(-4, moveEvaluation.stockfish.cp_relative_vec[move])
374+
const prob = moveEvaluation?.maia.policy[move] * 100
380375

381376
data.push({
382377
move,
@@ -386,7 +381,7 @@ export const useAnalysisController = (
386381
}
387382

388383
return data
389-
}, [controller.currentIndex, maiaEvaluations, stockfishEvaluations])
384+
}, [controller.currentIndex, moveEvaluation])
390385

391386
const move = useMemo(() => {
392387
if (

src/pages/analysis/[...id].tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ const Analysis: React.FC<Props> = ({
379379
min={0}
380380
max={800}
381381
value={
382-
moveEvaluation
383-
? 400 + moveEvaluation?.stockfish?.model_optimal_cp
382+
moveEvaluation?.stockfish
383+
? 400 + moveEvaluation.stockfish.model_optimal_cp
384384
: void 0
385385
}
386386
label="Stockfish Evaluation"

0 commit comments

Comments
 (0)