1
1
"use client" ;
2
2
import React , { useState , useMemo , useEffect , useRef } from "react" ;
3
+ import axios from 'axios' ;
3
4
import { Chess } from "chess.js" ;
4
5
import { Chessboard } from "react-chessboard" ;
5
6
import { ThemeProvider } from "@/components/theme-provider" ;
@@ -14,7 +15,7 @@ import {
14
15
export default function Home ( ) {
15
16
const [ game , setGame ] = useState ( new Chess ( ) ) ;
16
17
const [ position , setPosition ] = useState ( game . fen ( ) ) ;
17
- const [ lobbyId , setLobbyId ] = useState ( ) ;
18
+ const [ lobbyId , setLobbyId ] = useState ( ) ;
18
19
const [ lastMove , setLastMove ] = useState ( null ) ;
19
20
const [ selectedSquare , setSelectedSquare ] = useState ( null ) ;
20
21
@@ -27,6 +28,30 @@ export default function Home() {
27
28
const illegalSound = useRef ( new Audio ( '/sounds/illegal.mp3' ) ) ;
28
29
const checkSound = useRef ( new Audio ( '/sounds/move-check.mp3' ) ) ;
29
30
31
+ // Fetch the FEN position from the backend
32
+ useEffect ( ( ) => {
33
+ const fetchFenPosition = async ( ) => {
34
+ if ( ! lobbyId ) {
35
+ console . error ( "Lobby ID is not set." ) ;
36
+ return ;
37
+ }
38
+ try {
39
+ const response = await axios . get ( "http://127.0.0.1:8000/get-fen" ) ;
40
+ if ( response . data . fen_position ) {
41
+ setPosition ( response . data . fen_position ) ;
42
+ console . log ( position )
43
+ } else {
44
+ console . error ( response . data . error || "Unknown error" ) ;
45
+ }
46
+ } catch ( error ) {
47
+ console . error ( "Error fetching FEN position:" , error ) ;
48
+ }
49
+ } ;
50
+
51
+ fetchFenPosition ( ) ;
52
+ } , [ lobbyId ] ) ;
53
+
54
+
30
55
// Initialize WebSocket connection and join a lobby
31
56
useEffect ( ( ) => {
32
57
ws . current = new WebSocket ( `ws://localhost:8000/ws/lobby/${ lobbyId } ` ) ;
@@ -83,7 +108,7 @@ export default function Home() {
83
108
84
109
if ( ws . current && ws . current . readyState === WebSocket . OPEN ) {
85
110
ws . current . send (
86
- JSON . stringify ( { sourceSquare, targetSquare } )
111
+ JSON . stringify ( { sourceSquare, targetSquare, position } )
87
112
) ;
88
113
}
89
114
@@ -97,12 +122,17 @@ export default function Home() {
97
122
}
98
123
99
124
function handleLobbyChange ( newLobbyId ) {
100
- if ( ws . current ) {
101
- ws . current . close ( ) ;
125
+ if ( newLobbyId . length === 6 ) {
126
+ if ( ws . current ) {
127
+ ws . current . close ( ) ;
128
+ }
129
+ setLobbyId ( newLobbyId ) ;
130
+ setGame ( new Chess ( ) ) ;
131
+ setPosition ( game . fen ( ) ) ;
132
+
133
+ } else {
134
+ console . log ( "Lobby ID must be exactly 6 characters." ) ;
102
135
}
103
- setLobbyId ( newLobbyId ) ;
104
- setGame ( new Chess ( ) ) ;
105
- setPosition ( game . fen ( ) ) ;
106
136
}
107
137
108
138
const customPieces = useMemo ( ( ) => {
@@ -130,7 +160,7 @@ export default function Home() {
130
160
< div className = "lobby-controls" >
131
161
< InputOTP
132
162
maxLength = { 6 }
133
- onChange = { ( value ) => handleLobbyChange ( value ) }
163
+ onChange = { ( value ) => handleLobbyChange ( value ) }
134
164
>
135
165
< InputOTPGroup >
136
166
< InputOTPSlot index = { 0 } />
0 commit comments