Skip to content

Commit 012cea0

Browse files
feat: handle mate case + prevent keyboard move controls during full game analysis
1 parent bb3eea0 commit 012cea0

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/components/Analysis/AnalysisOverlay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const AnalysisOverlay: React.FC<Props> = ({ isActive }) => {
3838
exit={{ opacity: 0 }}
3939
transition={{ duration: 0.2 }}
4040
data-testid="analysis-overlay"
41-
style={{ pointerEvents: 'none' }}
41+
style={{ pointerEvents: 'auto' }}
4242
/>
4343
)
4444
}

src/components/Board/MovesContainer.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface AnalysisProps {
1414
type: 'analysis'
1515
showAnnotations?: boolean
1616
showVariations?: boolean
17+
disableKeyboardNavigation?: boolean
1718
}
1819

1920
interface TuringProps {
@@ -23,6 +24,7 @@ interface TuringProps {
2324
type: 'turing'
2425
showAnnotations?: boolean
2526
showVariations?: boolean
27+
disableKeyboardNavigation?: boolean
2628
}
2729

2830
interface PlayProps {
@@ -32,6 +34,7 @@ interface PlayProps {
3234
type: 'play'
3335
showAnnotations?: boolean
3436
showVariations?: boolean
37+
disableKeyboardNavigation?: boolean
3538
}
3639

3740
type Props = AnalysisProps | TuringProps | PlayProps
@@ -66,6 +69,7 @@ export const MovesContainer: React.FC<Props> = (props) => {
6669
type,
6770
showAnnotations = true,
6871
showVariations = true,
72+
disableKeyboardNavigation = false,
6973
} = props
7074
const { isMobile } = useContext(WindowSizeContext)
7175
const containerRef = useRef<HTMLDivElement>(null)
@@ -91,6 +95,8 @@ export const MovesContainer: React.FC<Props> = (props) => {
9195
}, [game, type, baseController.gameTree, baseController.currentNode])
9296

9397
useEffect(() => {
98+
if (disableKeyboardNavigation) return
99+
94100
const handleKeyDown = (event: KeyboardEvent) => {
95101
if (!baseController.currentNode) return
96102

@@ -114,7 +120,11 @@ export const MovesContainer: React.FC<Props> = (props) => {
114120

115121
window.addEventListener('keydown', handleKeyDown)
116122
return () => window.removeEventListener('keydown', handleKeyDown)
117-
}, [baseController.currentNode, baseController.goToNode])
123+
}, [
124+
baseController.currentNode,
125+
baseController.goToNode,
126+
disableKeyboardNavigation,
127+
])
118128

119129
// Auto-scroll to current move
120130
useEffect(() => {

src/hooks/useAnalysisController/useAnalysisController.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,26 @@ export const useAnalysisController = (
139139
currentMove: moveDisplay,
140140
}))
141141

142-
// Wait for analysis to reach target depth or complete (mate, etc.)
143-
let analysisRetries = 0
144-
const maxAnalysisRetries = 600 // 60 seconds max per position
145-
146-
while (
147-
analysisRetries < maxAnalysisRetries &&
148-
!gameAnalysisController.current.cancelled &&
149-
(!node.analysis.stockfish ||
150-
(node.analysis.stockfish.depth < targetDepth &&
151-
node.analysis.stockfish.model_optimal_cp !== 10000 &&
152-
node.analysis.stockfish.model_optimal_cp !== -10000))
153-
) {
154-
await new Promise((resolve) => setTimeout(resolve, 100))
155-
analysisRetries++
142+
// Skip analysis if this is a terminal position (checkmate/stalemate)
143+
const chess = new Chess(node.fen)
144+
const isTerminalPosition = chess.gameOver()
145+
146+
if (!isTerminalPosition) {
147+
// Wait for analysis to reach target depth or complete (mate, etc.)
148+
let analysisRetries = 0
149+
const maxAnalysisRetries = 600 // 60 seconds max per position
150+
151+
while (
152+
analysisRetries < maxAnalysisRetries &&
153+
!gameAnalysisController.current.cancelled &&
154+
(!node.analysis.stockfish ||
155+
(node.analysis.stockfish.depth < targetDepth &&
156+
node.analysis.stockfish.model_optimal_cp !== 10000 &&
157+
node.analysis.stockfish.model_optimal_cp !== -10000))
158+
) {
159+
await new Promise((resolve) => setTimeout(resolve, 100))
160+
analysisRetries++
161+
}
156162
}
157163
}
158164

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ const Analysis: React.FC<Props> = ({
701701
termination={analyzedGame.termination}
702702
type="analysis"
703703
showAnnotations={analysisEnabled}
704+
disableKeyboardNavigation={
705+
controller.gameAnalysis.progress.isAnalyzing
706+
}
704707
/>
705708
<BoardController
706709
gameTree={controller.gameTree}
@@ -1214,6 +1217,9 @@ const Analysis: React.FC<Props> = ({
12141217
termination={analyzedGame.termination}
12151218
type="analysis"
12161219
showAnnotations={analysisEnabled}
1220+
disableKeyboardNavigation={
1221+
controller.gameAnalysis.progress.isAnalyzing
1222+
}
12171223
/>
12181224
</div>
12191225
</div>

0 commit comments

Comments
 (0)