@@ -22,12 +22,10 @@ const statsLoader = async () => {
2222
2323export const useTuringController = ( ) => {
2424 const router = useRouter ( )
25- const [ turingGames , setTuringGames ] = useState < { [ id : string ] : TuringGame } > (
26- { } ,
27- )
25+ const [ turingGames , setTuringGames ] = useState < TuringGame [ ] > ( [ ] )
2826
2927 const [ loading , setLoading ] = useState ( false )
30- const [ currentGameId , setCurrentGameId ] = useState < string > ( '' )
28+ const [ currentIndex , setCurrentIndex ] = useState ( 0 )
3129 const [ stats , incrementStats , updateRating ] = useStats ( statsLoader )
3230
3331 const getNewGame = useCallback ( async ( ) => {
@@ -41,20 +39,20 @@ export const useTuringController = () => {
4139 }
4240
4341 setLoading ( false )
44- setTuringGames ( { ... turingGames , [ game . id ] : game } )
45- setCurrentGameId ( game . id )
42+ setCurrentIndex ( turingGames . length )
43+ setTuringGames ( turingGames . concat ( [ game ] ) )
4644 } , [ turingGames , router ] )
4745
4846 useEffect ( ( ) => {
49- if ( Object . keys ( turingGames ) . length === 0 ) getNewGame ( )
50- } , [ getNewGame , turingGames ] )
47+ if ( turingGames . length === 0 && ! loading ) getNewGame ( )
48+ } , [ getNewGame , turingGames . length , loading ] )
5149
5250 const game = useMemo (
53- ( ) => turingGames [ currentGameId ?? '' ] ,
54- [ turingGames , currentGameId ] ,
51+ ( ) => turingGames [ currentIndex ] ,
52+ [ turingGames , currentIndex ] ,
5553 )
5654
57- const controller = useTreeController ( game ? .tree , 'white' )
55+ const controller = useTreeController ( game . tree , 'white' )
5856
5957 useEffect ( ( ) => {
6058 if ( controller . tree && game ) {
@@ -63,26 +61,32 @@ export const useTuringController = () => {
6361 }
6462 } , [ game ] )
6563
66- const gameIds = useMemo ( ( ) => Object . keys ( turingGames ) , [ turingGames ] )
64+ const gameIds = useMemo ( ( ) => turingGames . map ( ( g ) => g . id ) , [ turingGames ] )
6765
6866 const submitGuess = useCallback (
6967 async ( guess : Color , comment = '' , rating ?: number ) => {
7068 if ( game && ! game . result ) {
7169 const result = await submitTuringGuess ( game . id , guess , comment )
72- setTuringGames ( {
73- ...turingGames ,
74- [ game . id ] : {
75- ...game ,
76- result : { ...result , ratingDiff : result . turingElo - ( rating || 0 ) } ,
77- } ,
78- } )
70+ setTuringGames (
71+ turingGames . map ( ( g , index ) =>
72+ index === currentIndex
73+ ? {
74+ ...g ,
75+ result : {
76+ ...result ,
77+ ratingDiff : result . turingElo - ( rating || 0 ) ,
78+ } ,
79+ }
80+ : g ,
81+ ) ,
82+ )
7983 commentController [ 1 ] ( '' )
8084
8185 updateRating ( result . turingElo )
8286 incrementStats ( 1 , result . correct ? 1 : 0 )
8387 }
8488 } ,
85- [ game , incrementStats , turingGames , updateRating ] ,
89+ [ game , incrementStats , turingGames , updateRating , currentIndex ] ,
8690 )
8791
8892 const commentController = useState ( '' )
@@ -104,8 +108,8 @@ export const useTuringController = () => {
104108 getNewGame,
105109 loading,
106110 gameIds,
107- currentGameId ,
108- setCurrentGameId ,
111+ currentIndex ,
112+ setCurrentIndex ,
109113 submitGuess,
110114 commentController,
111115 stats,
0 commit comments