Spaces:
Sleeping
Sleeping
File size: 15,775 Bytes
100a6dd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | /// <reference path="../react-app-env.d.ts" />
import * as React from 'react';
const { useState, useEffect } = React;
import { getGameState } from '../services/api';
import { setSoundEnabled, isSoundEnabled, playSound } from '../services/soundService';
interface GameInfoProps {
boardTheme: 'brown' | 'grey';
onThemeChange: (theme: 'brown' | 'grey') => void;
}
const GameInfo: React.FC<GameInfoProps> = ({ boardTheme, onThemeChange }) => {
const [soundOn, setSoundOn] = useState(true);
const [gameState, setGameState] = useState<any>(null);
const [moveHistory, setMoveHistory] = useState<string[]>([]);
const [analysis, setAnalysis] = useState<any>(null);
const [whiteCaptured, setWhiteCaptured] = useState<string[]>([]);
const [blackCaptured, setBlackCaptured] = useState<string[]>([]);
useEffect(() => {
fetchGameInfo();
const interval = setInterval(fetchGameInfo, 2000); // Poll every 2 seconds
return () => clearInterval(interval);
}, []);
const fetchGameInfo = async () => {
try {
const response = await getGameState();
// Track captured pieces by comparing current board with previous board
if (gameState && response.board_state && response.board_state.fen) {
const previousPieces = parseFen(gameState.board_state.fen);
const currentPieces = parseFen(response.board_state.fen);
// Find pieces that were captured in the last move
const capturedWhitePieces = findCapturedPieces(previousPieces, currentPieces, 'white');
const capturedBlackPieces = findCapturedPieces(previousPieces, currentPieces, 'black');
if (capturedWhitePieces.length > 0) {
setWhiteCaptured(prev => [...prev, ...capturedWhitePieces]);
}
if (capturedBlackPieces.length > 0) {
setBlackCaptured(prev => [...prev, ...capturedBlackPieces]);
}
}
setGameState(response);
if (response.last_analysis) {
setAnalysis(response.last_analysis);
}
// Update move history if available
if (response.board_state && response.board_state.move_count > moveHistory.length) {
// In a real implementation, you would get the actual moves from the API
// For now, we'll create placeholder moves with algebraic notation
if (response.player_move && response.ai_move) {
setMoveHistory(prev => [
...prev,
response.player_move,
response.ai_move
]);
} else if (response.player_move) {
setMoveHistory(prev => [...prev, response.player_move]);
} else if (response.ai_move) {
setMoveHistory(prev => [...prev, response.ai_move]);
} else {
// Fallback if no specific moves are provided
setMoveHistory(prev => [...prev, `Move ${prev.length + 1}`]);
}
}
} catch (error) {
console.error('Error fetching game info:', error);
}
};
// Helper function to parse FEN string and get pieces
const parseFen = (fenString: string): {type: string, color: string}[] => {
const pieces: {type: string, color: string}[] = [];
const fenParts = fenString.split(' ');
const ranks = fenParts[0].split('/');
ranks.forEach((rank) => {
let fileIndex = 0;
for (let i = 0; i < rank.length; i++) {
const char = rank[i];
if (!isNaN(parseInt(char))) {
fileIndex += parseInt(char);
} else {
const color = char === char.toUpperCase() ? 'white' : 'black';
const type = char.toLowerCase();
pieces.push({
type,
color
});
fileIndex++;
}
}
});
return pieces;
};
// Helper function to find captured pieces
const findCapturedPieces = (
previousPieces: {type: string, color: string}[],
currentPieces: {type: string, color: string}[],
color: string
): string[] => {
const captured: string[] = [];
// Count pieces by type in previous and current board
const previousCount: Record<string, number> = {};
const currentCount: Record<string, number> = {};
previousPieces.forEach(piece => {
if (piece.color === color) {
const key = piece.type;
previousCount[key] = (previousCount[key] || 0) + 1;
}
});
currentPieces.forEach(piece => {
if (piece.color === color) {
const key = piece.type;
currentCount[key] = (currentCount[key] || 0) + 1;
}
});
// Find pieces that were captured (more in previous than current)
Object.keys(previousCount).forEach(type => {
const diff = previousCount[type] - (currentCount[type] || 0);
for (let i = 0; i < diff; i++) {
captured.push(type);
}
});
return captured;
};
const renderGameStatus = () => {
if (!gameState) return <p className="text-gradio-text-secondary text-lg">Loading game state...</p>;
const { status, board_state, player_color } = gameState;
let statusText = 'Game in progress';
let statusClass = 'text-gradio-blue';
if (status === 'game_over') {
statusText = gameState.result === 'draw'
? 'Game ended in a draw'
: `${gameState.winner} wins by ${gameState.reason}`;
statusClass = 'text-gradio-red';
} else if (board_state.game_state === 'check') {
statusText = `${board_state.turn} is in check`;
statusClass = 'text-gradio-orange';
}
return (
<div className="mb-5 p-4 bg-gradio-bg rounded-lg">
<h3 className="text-xl font-medium mb-3 text-gradio-green">Game Status</h3>
<p className={`font-medium text-lg ${statusClass} mb-2`}>{statusText}</p>
<div className="grid grid-cols-2 gap-3 text-lg">
<div>Turn: <span className="font-medium text-gradio-text">{board_state.turn}</span></div>
<div>Playing as: <span className="font-medium text-gradio-text">{player_color}</span></div>
{gameState.difficulty && (
<div>Difficulty: <span className="font-medium text-gradio-text">{gameState.difficulty}</span></div>
)}
</div>
</div>
);
};
const renderAnalysis = () => {
if (!analysis) return null;
return (
<div className="mb-4">
<h3 className="text-lg font-medium mb-2">Position Analysis</h3>
<div className="space-y-1">
<p>
Evaluation: <span className="font-medium">
{analysis.evaluation.total > 0 ? '+' : ''}{analysis.evaluation.total.toFixed(2)}
</span>
</p>
<div className="h-2 bg-gray-200 rounded overflow-hidden">
<div
className={`h-full ${analysis.evaluation.total > 0 ? 'bg-blue-600' : 'bg-black'}`}
style={{
width: `${Math.min(Math.abs(analysis.evaluation.total) * 10, 100)}%`,
marginLeft: analysis.evaluation.total > 0 ? '50%' : undefined,
marginRight: analysis.evaluation.total < 0 ? '50%' : undefined,
}}
></div>
</div>
<p className="text-sm text-gray-600">
Material: {analysis.evaluation.material.toFixed(2)} |
Position: {analysis.evaluation.positional.toFixed(2)} |
Safety: {analysis.evaluation.safety.toFixed(2)}
</p>
</div>
</div>
);
};
const renderMoveHistory = () => {
if (moveHistory.length === 0) return <p>No moves yet</p>;
// Group moves by pairs (white and black)
const moveGroups = [];
for (let i = 0; i < moveHistory.length; i += 2) {
moveGroups.push({
number: Math.floor(i / 2) + 1,
white: moveHistory[i],
black: i + 1 < moveHistory.length ? moveHistory[i + 1] : null
});
}
return (
<div className="overflow-y-auto max-h-48 border rounded">
<table className="w-full text-sm">
<thead className="bg-gray-100 sticky top-0">
<tr>
<th className="py-1 px-2 text-left">#</th>
<th className="py-1 px-2 text-left">White</th>
<th className="py-1 px-2 text-left">Black</th>
</tr>
</thead>
<tbody>
{moveGroups.map((group) => (
<tr key={group.number} className="hover:bg-gray-50">
<td className="py-1 px-2 font-medium">{group.number}.</td>
<td className="py-1 px-2">{group.white}</td>
<td className="py-1 px-2">{group.black}</td>
</tr>
))}
</tbody>
</table>
</div>
);
};
// Initialize sound settings
useEffect(() => {
setSoundEnabled(soundOn);
}, [soundOn]);
return (
<div className="bg-gradio-card rounded-lg shadow-lg p-5 text-gradio-text">
<h2 className="text-2xl font-bold mb-5 text-gradio-text">Game Information</h2>
{/* Sound Controls */}
<div className="mb-6 p-4 bg-gradio-bg rounded-lg">
<h3 className="text-xl font-medium mb-3 text-gradio-yellow">Settings</h3>
<div className="flex flex-col space-y-4">
{/* Theme selection removed but kept in code for future reference
<div className="flex items-center justify-between">
<span className="text-lg">Board Theme:</span>
<select
className="px-3 py-2 bg-gradio-card border border-gradio-border rounded text-gradio-text"
value={boardTheme}
onChange={(e) => {
const newTheme = e.target.value as 'brown' | 'grey';
console.log(`Theme changed to: ${newTheme}`);
onThemeChange(newTheme);
}}
>
<option value="brown">Brown</option>
<option value="grey">Grey</option>
</select>
</div>
*/}
<div className="flex items-center justify-between">
<span className="text-lg">Sound Effects:</span>
<button
className={`px-4 py-2 ${soundOn ? 'bg-gradio-green' : 'bg-gradio-border'} text-white rounded-lg transition-colors flex items-center gap-2`}
onClick={() => {
setSoundOn(!soundOn);
setSoundEnabled(!soundOn);
if (!soundOn) {
// Play a test sound when turning sound back on
playSound('move');
}
}}
>
<span className="text-xl">{soundOn ? '🔊' : '🔇'}</span>
<span>{soundOn ? 'On' : 'Off'}</span>
</button>
</div>
</div>
</div>
{renderGameStatus()}
<div className="mb-5">
<h3 className="text-xl font-medium mb-3 text-gradio-blue">Captured Pieces</h3>
<div className="flex justify-between p-3 bg-gradio-bg rounded-lg">
<div>
<p className="font-medium mb-1">White captured:</p>
<div className="flex gap-1">
{whiteCaptured.map((piece, index) => (
<span key={index} className="text-lg" title={piece}>
{piece === 'p' ? '♙' :
piece === 'r' ? '♖' :
piece === 'n' ? '♘' :
piece === 'b' ? '♗' :
piece === 'q' ? '♕' : '♔'}
</span>
))}
{whiteCaptured.length === 0 && <span className="text-gradio-text-secondary">None</span>}
</div>
</div>
<div>
<p className="font-medium mb-1">Black captured:</p>
<div className="flex gap-1">
{blackCaptured.map((piece, index) => (
<span key={index} className="text-lg" title={piece}>
{piece === 'p' ? '♟' :
piece === 'r' ? '♜' :
piece === 'n' ? '♞' :
piece === 'b' ? '♝' :
piece === 'q' ? '♛' : '♚'}
</span>
))}
{blackCaptured.length === 0 && <span className="text-gradio-text-secondary">None</span>}
</div>
</div>
</div>
</div>
<div className="mb-5">
<h3 className="text-xl font-medium mb-3 text-gradio-orange">Move History</h3>
<div className="overflow-y-auto max-h-48 border border-gradio-border rounded-lg">
<table className="w-full text-base">
<thead className="bg-gradio-bg sticky top-0">
<tr>
<th className="py-2 px-3 text-left">#</th>
<th className="py-2 px-3 text-left">White</th>
<th className="py-2 px-3 text-left">Black</th>
</tr>
</thead>
<tbody>
{moveHistory.length === 0 ? (
<tr>
<td colSpan={3} className="py-3 px-3 text-center text-gradio-text-secondary">No moves yet</td>
</tr>
) : (
Array.from({ length: Math.ceil(moveHistory.length / 2) }).map((_, i) => (
<tr key={i} className="hover:bg-gradio-bg transition-colors">
<td className="py-2 px-3 font-medium">{i + 1}.</td>
<td className="py-2 px-3">{moveHistory[i * 2] || ''}</td>
<td className="py-2 px-3">{moveHistory[i * 2 + 1] || ''}</td>
</tr>
))
)}
</tbody>
</table>
</div>
</div>
{analysis && (
<div className="mb-5">
<h3 className="text-xl font-medium mb-3 text-gradio-purple">Position Analysis</h3>
<div className="p-3 bg-gradio-bg rounded-lg">
<p className="mb-2">
Evaluation: <span className="font-medium text-gradio-text">
{analysis.evaluation.total > 0 ? '+' : ''}{analysis.evaluation.total.toFixed(2)}
</span>
</p>
<div className="h-3 bg-gradio-border rounded-full overflow-hidden mb-3">
<div
className={`h-full ${analysis.evaluation.total > 0 ? 'bg-gradio-blue' : 'bg-gradio-red'}`}
style={{
width: `${Math.min(Math.abs(analysis.evaluation.total) * 10, 100)}%`,
marginLeft: analysis.evaluation.total > 0 ? '50%' : undefined,
marginRight: analysis.evaluation.total < 0 ? '50%' : undefined,
}}
></div>
</div>
<div className="grid grid-cols-2 gap-2 text-sm">
<div>Material: <span className="font-medium">{analysis.evaluation.material.toFixed(2)}</span></div>
<div>Position: <span className="font-medium">{analysis.evaluation.positional.toFixed(2)}</span></div>
<div>Safety: <span className="font-medium">{analysis.evaluation.safety.toFixed(2)}</span></div>
<div>Mobility: <span className="font-medium">{analysis.evaluation.mobility.toFixed(2)}</span></div>
</div>
</div>
</div>
)}
</div>
);
};
export default GameInfo; |