feat(tictactoe): migrate to multi-board state structure

- Replaced single `board` state with `boards` map in App.tsx
- Updated state parsing to use `state.boards` instead of `state.board.grid`
- Updated TicTacToeBoard props to accept `boards` instead of `board`
- Safely extracted TicTacToe board using `boards['tictactoe']?.grid ?? null`
- Added loading fallback when board is not yet available
- Updated rendering guards to prevent undefined map errors

This change fully aligns the frontend with the new multi-board MatchState
introduced on the server (supports TicTacToe, Battleship, and future games).
This commit is contained in:
2025-12-03 17:36:29 +05:30
parent 7b677653a7
commit 2b0af9fd1f
2 changed files with 11 additions and 8 deletions

View File

@@ -7,8 +7,8 @@ import { PlayerModel } from "./models/player";
import TicTacToeBoard from "./games/tictactoe/TicTacToeBoard";
export default function App() {
// setting up a 2D game board
const [board, setBoard] = useState<string[][]>([[]]);
// setting up a 2D game boards
const [boards, setBoards] = useState<Record<string, { grid: string[][] }>>({});
const [turn, setTurn] = useState<number>(0);
const [winner, setWinner] = useState<string | null>(null);
const [gameOver, setGameOver] = useState<boolean | null>(null);
@@ -26,7 +26,7 @@ export default function App() {
const state = msg.data;
console.log("Match state:", state);
setBoard(state.board.grid);
setBoards(state.boards);
setTurn(state.turn);
setGameOver(state.game_over);
if (state.winner >= 0) {
@@ -114,7 +114,7 @@ export default function App() {
}}
>
<TicTacToeBoard
board={board}
boards={boards}
turn={turn}
winner={winner}
gameOver={gameOver}

View File

@@ -5,7 +5,7 @@ import getHaiku from "../../utils/haikus";
import { PlayerModel } from "../../models/player";
interface BoardProps {
board: string[][];
boards: Record<string, { grid: string[][] }>;
turn: number;
winner: string | null;
gameOver: boolean | null;
@@ -15,7 +15,7 @@ interface BoardProps {
}
export default function TicTacToeBoard({
board,
boards,
turn,
winner,
gameOver,
@@ -62,6 +62,7 @@ export default function TicTacToeBoard({
const nextLineIn = 3600;
const allLinesStay = 2400;
const allLinesFade = 1200;
const board = boards['tictactoe']?.grid ?? null;
useEffect(() => {
const totalTime = haiku.length * nextLineIn + allLinesStay + allLinesFade;
@@ -107,7 +108,9 @@ export default function TicTacToeBoard({
{/* -------------------------
BOARD
-------------------------- */}
<motion.div
{!board && <div style={{ textAlign: "center", marginTop: "14px" }}>Loading...</div>}
{board && <motion.div
layout
style={{
display: "grid",
@@ -180,7 +183,7 @@ export default function TicTacToeBoard({
);
})
)}
</motion.div>
</motion.div>}
{!winner && (
<div