Skip to content

Commit 3321de3

Browse files
committed
Made redis working
1 parent 9aa68aa commit 3321de3

File tree

3 files changed

+139
-8
lines changed

3 files changed

+139
-8
lines changed

frontend/chess/package-lock.json

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/chess/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@radix-ui/react-switch": "^1.1.1",
2020
"@radix-ui/react-tooltip": "^1.1.3",
2121
"@tanstack/react-table": "^8.20.5",
22+
"axios": "^1.7.7",
2223
"chess.js": "^0.13.4",
2324
"class-variance-authority": "^0.7.0",
2425
"clsx": "^2.1.1",

frontend/chess/src/app/chess/page.jsx

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22
import React, { useState, useMemo, useEffect, useRef } from "react";
3+
import axios from 'axios';
34
import { Chess } from "chess.js";
45
import { Chessboard } from "react-chessboard";
56
import { ThemeProvider } from "@/components/theme-provider";
@@ -14,7 +15,7 @@ import {
1415
export default function Home() {
1516
const [game, setGame] = useState(new Chess());
1617
const [position, setPosition] = useState(game.fen());
17-
const [lobbyId, setLobbyId] = useState();
18+
const [lobbyId, setLobbyId] = useState();
1819
const [lastMove, setLastMove] = useState(null);
1920
const [selectedSquare, setSelectedSquare] = useState(null);
2021

@@ -27,6 +28,30 @@ export default function Home() {
2728
const illegalSound = useRef(new Audio('/sounds/illegal.mp3'));
2829
const checkSound = useRef(new Audio('/sounds/move-check.mp3'));
2930

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+
3055
// Initialize WebSocket connection and join a lobby
3156
useEffect(() => {
3257
ws.current = new WebSocket(`ws://localhost:8000/ws/lobby/${lobbyId}`);
@@ -83,7 +108,7 @@ export default function Home() {
83108

84109
if (ws.current && ws.current.readyState === WebSocket.OPEN) {
85110
ws.current.send(
86-
JSON.stringify({ sourceSquare, targetSquare })
111+
JSON.stringify({ sourceSquare, targetSquare, position })
87112
);
88113
}
89114

@@ -97,12 +122,17 @@ export default function Home() {
97122
}
98123

99124
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.");
102135
}
103-
setLobbyId(newLobbyId);
104-
setGame(new Chess());
105-
setPosition(game.fen());
106136
}
107137

108138
const customPieces = useMemo(() => {
@@ -130,7 +160,7 @@ export default function Home() {
130160
<div className="lobby-controls">
131161
<InputOTP
132162
maxLength={6}
133-
onChange={(value) => handleLobbyChange(value)}
163+
onChange={(value) => handleLobbyChange(value)}
134164
>
135165
<InputOTPGroup>
136166
<InputOTPSlot index={0} />

0 commit comments

Comments
 (0)