Skip to content

Commit 2c6ca3f

Browse files
feat: cleanup analysis controllers + linter errors
1 parent a32281d commit 2c6ca3f

10 files changed

Lines changed: 73 additions & 89 deletions

File tree

src/components/Board/GameBoard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import Chessground from '@react-chess/chessground'
77
import type { DrawBrushes, DrawShape } from 'chessground/draw'
88

99
import { useChessSound } from 'src/hooks'
10-
import { BaseGame, Check, GameNode, ClientBaseGame, Color } from 'src/types'
10+
import { BaseGame, Check, GameNode, BaseGame, Color } from 'src/types'
1111

1212
interface Props {
13-
game?: BaseGame | ClientBaseGame
13+
game?: BaseGame | BaseGame
1414
currentNode: GameNode
1515
orientation?: Color
1616
moves?: Map<string, string[]>

src/components/Board/MovesContainer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import { Tooltip } from 'react-tooltip'
44
import React, { useContext, useMemo, Fragment, useEffect } from 'react'
55
import { WindowSizeContext } from 'src/contexts'
6-
import { GameNode, AnalyzedGame, Termination, ClientBaseGame } from 'src/types'
6+
import { GameNode, AnalyzedGame, Termination, BaseGame } from 'src/types'
77
import { TuringGame } from 'src/types/turing'
88
import { useBaseTreeController } from 'src/hooks/useBaseTreeController'
99

1010
interface AnalysisProps {
11-
game: ClientBaseGame | AnalyzedGame
11+
game: BaseGame | AnalyzedGame
1212
highlightIndices?: number[]
1313
termination?: Termination
1414
type: 'analysis'
@@ -22,7 +22,7 @@ interface TuringProps {
2222
}
2323

2424
interface PlayProps {
25-
game: ClientBaseGame
25+
game: BaseGame
2626
highlightIndices?: number[]
2727
termination?: Termination
2828
type: 'play'

src/contexts/TreeControllerContext/TreeControllerContext.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const defaultGameTree = new GameTree(new Chess().fen())
1414
export const TreeControllerContext =
1515
React.createContext<ITreeControllerContext>({
1616
gameTree: defaultGameTree,
17-
currentNode: undefined,
17+
currentNode: defaultGameTree.getRoot(),
1818
setCurrentNode: () => {
1919
throw new Error('TreeControllerContext not provided')
2020
},
@@ -35,8 +35,4 @@ export const TreeControllerContext =
3535
throw new Error('TreeControllerContext not provided')
3636
},
3737
plyCount: 0,
38-
currentIndex: 0,
39-
setCurrentIndex: (() => {
40-
throw new Error('TreeControllerContext not provided')
41-
}) as (indexOrUpdater: SetStateAction<number>) => void,
4238
})

src/hooks/useAnalysisController/useAnalysisController.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import { useMoveRecommendations } from './useMoveRecommendations'
1515
import { useBoardDescription } from './useBoardDescription'
1616

1717
export const useAnalysisController = (game: AnalyzedGame) => {
18-
const controller = useTreeController(
19-
game.tree as GameTree,
20-
game.tree.getRoot(),
21-
)
18+
const controller = useTreeController(game.tree)
2219

2320
const [analysisState, setAnalysisState] = useState(0)
2421
const inProgressAnalyses = useMemo(() => new Set<string>(), [])
@@ -130,8 +127,18 @@ export const useAnalysisController = (game: AnalyzedGame) => {
130127
}, [currentMove, controller.currentNode])
131128

132129
return {
130+
gameTree: controller.gameTree,
131+
currentNode: controller.currentNode,
132+
setCurrentNode: controller.setCurrentNode,
133+
goToNode: controller.goToNode,
134+
goToNextNode: controller.goToNextNode,
135+
goToPreviousNode: controller.goToPreviousNode,
136+
goToRootNode: controller.goToRootNode,
137+
plyCount: controller.plyCount,
138+
orientation: controller.orientation,
139+
setOrientation: controller.setOrientation,
140+
133141
maiaStatus,
134-
controller,
135142
downloadMaia,
136143
maiaProgress,
137144
move,

src/hooks/useTreeController/useTreeController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useState, useMemo, useCallback } from 'react'
33

44
export const useTreeController = (
55
gameTree: GameTree,
6-
initialNode?: GameNode,
76
initialOrientation: Color = 'white',
87
) => {
98
const [currentNode, setCurrentNode] = useState<GameNode>(gameTree.getRoot())
@@ -40,6 +39,7 @@ export const useTreeController = (
4039
}, [gameTree, setCurrentNode])
4140

4241
return {
42+
gameTree,
4343
currentNode,
4444
setCurrentNode,
4545
orientation,

src/hooks/useTuringController/useTuringController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const useTuringController = () => {
7676
return buildTuringGameTree(game)
7777
}, [game])
7878

79-
const controller = useTreeController(gameTree, undefined, 'white')
79+
const controller = useTreeController(gameTree, 'white')
8080

8181
useEffect(() => {
8282
if (gameTree && game) {

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

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -242,23 +242,7 @@ const Analysis: React.FC<Props> = ({
242242
const toastId = useRef<string>(null)
243243
const [currentSquare, setCurrentSquare] = useState<Key | null>(null)
244244

245-
const {
246-
maiaStatus,
247-
downloadMaia,
248-
maiaProgress,
249-
controller,
250-
move,
251-
moves,
252-
currentMaiaModel,
253-
setCurrentMaiaModel,
254-
colorSanMapping,
255-
moveEvaluation,
256-
movesByRating,
257-
moveRecommendations,
258-
moveMap,
259-
blunderMeter,
260-
boardDescription,
261-
} = useAnalysisController(analyzedGame)
245+
const controller = useAnalysisController(analyzedGame)
262246

263247
useEffect(() => {
264248
controller.setCurrentNode(analyzedGame.tree?.getRoot())
@@ -275,9 +259,9 @@ const Analysis: React.FC<Props> = ({
275259
}, [])
276260

277261
useEffect(() => {
278-
if (maiaStatus === 'loading' && !toastId.current) {
262+
if (controller.maiaStatus === 'loading' && !toastId.current) {
279263
toastId.current = toast.loading('Loading Maia Model...')
280-
} else if (maiaStatus === 'ready') {
264+
} else if (controller.maiaStatus === 'ready') {
281265
if (toastId.current) {
282266
toast.success('Loaded Maia! Analysis is ready', {
283267
id: toastId.current,
@@ -286,7 +270,7 @@ const Analysis: React.FC<Props> = ({
286270
toast.success('Loaded Maia! Analysis is ready')
287271
}
288272
}
289-
}, [maiaStatus])
273+
}, [controller.maiaStatus])
290274

291275
const launchContinue = useCallback(() => {
292276
const fen = controller.currentNode?.fen as string
@@ -298,8 +282,8 @@ const Analysis: React.FC<Props> = ({
298282
useEffect(() => {
299283
const arr = []
300284

301-
if (moveEvaluation?.maia) {
302-
const maia = Object.entries(moveEvaluation?.maia?.policy)[0]
285+
if (controller.moveEvaluation?.maia) {
286+
const maia = Object.entries(controller.moveEvaluation?.maia?.policy)[0]
303287
if (maia) {
304288
arr.push({
305289
brush: 'red',
@@ -309,8 +293,10 @@ const Analysis: React.FC<Props> = ({
309293
}
310294
}
311295

312-
if (moveEvaluation?.stockfish) {
313-
const stockfish = Object.entries(moveEvaluation?.stockfish.cp_vec)[0]
296+
if (controller.moveEvaluation?.stockfish) {
297+
const stockfish = Object.entries(
298+
controller.moveEvaluation?.stockfish.cp_vec,
299+
)[0]
314300
if (stockfish) {
315301
arr.push({
316302
brush: 'blue',
@@ -322,7 +308,11 @@ const Analysis: React.FC<Props> = ({
322308
}
323309

324310
setArrows(arr)
325-
}, [moveEvaluation, controller.currentNode, controller.orientation])
311+
}, [
312+
controller.moveEvaluation,
313+
controller.currentNode,
314+
controller.orientation,
315+
])
326316

327317
const hover = (move?: string) => {
328318
if (move) {
@@ -366,7 +356,7 @@ const Analysis: React.FC<Props> = ({
366356
newFen,
367357
moveString,
368358
san,
369-
currentMaiaModel,
359+
controller.currentMaiaModel,
370360
)
371361
controller.goToNode(newVariation)
372362
}
@@ -537,7 +527,7 @@ const Analysis: React.FC<Props> = ({
537527
<div className="relative flex aspect-square w-[45vh] 2xl:w-[55vh]">
538528
<GameBoard
539529
game={analyzedGame}
540-
moves={moves}
530+
moves={controller.moves}
541531
setCurrentSquare={setCurrentSquare}
542532
shapes={hoverArrow ? [...arrows, hoverArrow] : [...arrows]}
543533
currentNode={controller.currentNode as GameNode}
@@ -562,8 +552,8 @@ const Analysis: React.FC<Props> = ({
562552
/>
563553
</div>
564554
<ConfigurableScreens
565-
currentMaiaModel={currentMaiaModel}
566-
setCurrentMaiaModel={setCurrentMaiaModel}
555+
currentMaiaModel={controller.currentMaiaModel}
556+
setCurrentMaiaModel={controller.setCurrentMaiaModel}
567557
launchContinue={launchContinue}
568558
MAIA_MODELS={MAIA_MODELS}
569559
game={analyzedGame}
@@ -578,32 +568,32 @@ const Analysis: React.FC<Props> = ({
578568
<Highlight
579569
hover={hover}
580570
makeMove={makeMove}
581-
currentMaiaModel={currentMaiaModel}
582-
recommendations={moveRecommendations}
571+
currentMaiaModel={controller.currentMaiaModel}
572+
recommendations={controller.moveRecommendations}
583573
moveEvaluation={
584-
moveEvaluation as {
574+
controller.moveEvaluation as {
585575
maia?: MaiaEvaluation
586576
stockfish?: StockfishEvaluation
587577
}
588578
}
589-
movesByRating={movesByRating}
590-
colorSanMapping={colorSanMapping}
591-
boardDescription={boardDescription}
579+
movesByRating={controller.movesByRating}
580+
colorSanMapping={controller.colorSanMapping}
581+
boardDescription={controller.boardDescription}
592582
/>
593583
</div>
594584
<div className="flex h-[calc((55vh+4.5rem)/2)] flex-row gap-2">
595585
<div className="flex h-full w-full flex-col">
596586
<MoveMap
597-
moveMap={moveMap}
598-
colorSanMapping={colorSanMapping}
587+
moveMap={controller.moveMap}
588+
colorSanMapping={controller.colorSanMapping}
599589
setHoverArrow={setHoverArrow}
600590
/>
601591
</div>
602592
<BlunderMeter
603593
hover={hover}
604594
makeMove={makeMove}
605-
data={blunderMeter}
606-
colorSanMapping={colorSanMapping}
595+
data={controller.blunderMeter}
596+
colorSanMapping={controller.colorSanMapping}
607597
/>
608598
</div>
609599
</div>
@@ -667,8 +657,8 @@ const Analysis: React.FC<Props> = ({
667657
title="Analysis"
668658
icon="bar_chart"
669659
type="analysis"
670-
currentMaiaModel={currentMaiaModel}
671-
setCurrentMaiaModel={setCurrentMaiaModel}
660+
currentMaiaModel={controller.currentMaiaModel}
661+
setCurrentMaiaModel={controller.setCurrentMaiaModel}
672662
MAIA_MODELS={MAIA_MODELS}
673663
onGameListClick={() => setShowGameListMobile(true)}
674664
showGameListButton={true}
@@ -678,7 +668,7 @@ const Analysis: React.FC<Props> = ({
678668
<div className="relative flex h-[100vw] w-screen">
679669
<GameBoard
680670
game={analyzedGame}
681-
moves={moves}
671+
moves={controller.moves}
682672
setCurrentSquare={setCurrentSquare}
683673
shapes={hoverArrow ? [...arrows, hoverArrow] : [...arrows]}
684674
currentNode={controller.currentNode as GameNode}
@@ -712,33 +702,33 @@ const Analysis: React.FC<Props> = ({
712702
<BlunderMeter
713703
hover={hover}
714704
makeMove={makeMove}
715-
data={blunderMeter}
716-
colorSanMapping={colorSanMapping}
705+
data={controller.blunderMeter}
706+
colorSanMapping={controller.colorSanMapping}
717707
/>
718708
<Highlight
719709
hover={hover}
720710
makeMove={makeMove}
721-
currentMaiaModel={currentMaiaModel}
722-
recommendations={moveRecommendations}
711+
currentMaiaModel={controller.currentMaiaModel}
712+
recommendations={controller.moveRecommendations}
723713
moveEvaluation={
724-
moveEvaluation as {
714+
controller.moveEvaluation as {
725715
maia?: MaiaEvaluation
726716
stockfish?: StockfishEvaluation
727717
}
728718
}
729-
movesByRating={movesByRating}
730-
colorSanMapping={colorSanMapping}
731-
boardDescription={boardDescription}
719+
movesByRating={controller.movesByRating}
720+
colorSanMapping={controller.colorSanMapping}
721+
boardDescription={controller.boardDescription}
732722
/>
733723
<MoveMap
734-
moveMap={moveMap}
735-
colorSanMapping={colorSanMapping}
724+
moveMap={controller.moveMap}
725+
colorSanMapping={controller.colorSanMapping}
736726
setHoverArrow={setHoverArrow}
737727
/>
738728
</div>
739729
<ConfigurableScreens
740-
currentMaiaModel={currentMaiaModel}
741-
setCurrentMaiaModel={setCurrentMaiaModel}
730+
currentMaiaModel={controller.currentMaiaModel}
731+
setCurrentMaiaModel={controller.setCurrentMaiaModel}
742732
launchContinue={launchContinue}
743733
MAIA_MODELS={MAIA_MODELS}
744734
game={analyzedGame}
@@ -766,11 +756,15 @@ const Analysis: React.FC<Props> = ({
766756
/>
767757
</Head>
768758
<AnimatePresence>
769-
{maiaStatus === 'no-cache' || maiaStatus === 'downloading' ? (
770-
<DownloadModelModal progress={maiaProgress} download={downloadMaia} />
759+
{controller.maiaStatus === 'no-cache' ||
760+
controller.maiaStatus === 'downloading' ? (
761+
<DownloadModelModal
762+
progress={controller.maiaProgress}
763+
download={controller.downloadMaia}
764+
/>
771765
) : null}
772766
</AnimatePresence>
773-
<TreeControllerContext.Provider value={{ ...controller }}>
767+
<TreeControllerContext.Provider value={controller}>
774768
{analyzedGame && (isMobile ? mobileLayout : desktopLayout)}
775769
</TreeControllerContext.Provider>
776770
</>

src/types/analysis/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ClientGame, Game } from '../base'
1+
import { Game } from '../base'
22
import { AvailableMoves } from '../training'
33

44
type EvaluationType = 'tournament' | 'pgn' | 'play' | 'hand' | 'brain'
@@ -26,7 +26,7 @@ export interface AnalysisWebGame {
2626
pgn?: string
2727
}
2828

29-
export interface AnalyzedGame extends ClientGame {
29+
export interface AnalyzedGame extends Game {
3030
maiaEvaluations: { [rating: string]: MaiaEvaluation }[]
3131
stockfishEvaluations: StockfishEvaluations<EvaluationType>
3232
availableMoves: AvailableMoves[]

src/types/base/index.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ export interface Move {
1919
export interface BaseGame {
2020
id: string
2121
moves: Move[]
22-
}
23-
24-
export interface ClientBaseGame {
25-
id: string
26-
moves: Move[]
2722
tree: GameTree
2823
}
2924

@@ -34,13 +29,6 @@ export interface Game extends BaseGame {
3429
termination: Termination
3530
}
3631

37-
export interface ClientGame extends ClientBaseGame {
38-
gameType: string
39-
blackPlayer: Player
40-
whitePlayer: Player
41-
termination: Termination
42-
}
43-
4432
export interface DataNode {
4533
x: number
4634
y: number

0 commit comments

Comments
 (0)