From d12945803911131cff17ba6ffde2c349c291e378 Mon Sep 17 00:00:00 2001 From: Vishesh 'ironeagle' Bangotra Date: Fri, 28 Nov 2025 16:56:34 +0530 Subject: [PATCH] - Added clear turn messaging for both players (Your Turn / Opponent's Turn). - Fixed initial board state to avoid showing incorrect spectator/opponent status. - Introduced players[] tracking in React state to correctly reflect match readiness. - Updated Board component to derive turn text based on player index and session user ID. - Ensured proper handling of initial match state broadcast from backend. - Improved overall clarity of gameplay state for both clients on match start. --- src/tictactoe/Board.tsx | 89 ++++++++++++++++++++++++++++--------- src/tictactoe/TicTacToe.tsx | 23 +++++++--- 2 files changed, 85 insertions(+), 27 deletions(-) diff --git a/src/tictactoe/Board.tsx b/src/tictactoe/Board.tsx index 92520b0..c8dd082 100644 --- a/src/tictactoe/Board.tsx +++ b/src/tictactoe/Board.tsx @@ -4,16 +4,52 @@ interface BoardProps { board: string[][]; turn: number; winner: string | null; + players: string[]; + myUserId: string | null; onCellClick: (row: number, col: number) => void; } -export default function Board({ board, turn, winner, onCellClick }: BoardProps) { +export default function Board({ + board, + turn, + winner, + players, + myUserId, + onCellClick, +}: BoardProps) { + const myIndex = players.indexOf(myUserId ?? ""); + const gameReady = players.length === 2; + + const mySymbol = + myIndex === 0 ? "X" : myIndex === 1 ? "O" : null; + + const opponentSymbol = + mySymbol === "X" ? "O" : mySymbol === "O" ? "X" : null; + + const isMyTurn = gameReady && myIndex !== -1 && turn === myIndex; + + // ------------------------------- + // 🟦 HEADER STATUS FIXED + // ------------------------------- + let status; + if (!gameReady) { + status = "Waiting for opponent..."; + } else if (winner) { + status = `Winner: ${winner}`; + } else if (myIndex === -1) { + status = "Spectating"; + } else { + status = isMyTurn ? "Your turn" : "Opponent's turn"; + } + return (
- {winner ? ( -

Winner: {winner}

- ) : ( -

Turn: Player {turn === 0 ? "X" : "O"}

+

{status}

+ + {gameReady && mySymbol && ( +
+ You: {mySymbol} — Opponent: {opponentSymbol} +
)}
{board.map((row, rIdx) => - row.map((cell, cIdx) => ( - - )) + row.map((cell, cIdx) => { + const disabled = + !!cell || + !!winner || + !gameReady || + myIndex === -1 || + !isMyTurn; + + return ( + + ); + }) )}
diff --git a/src/tictactoe/TicTacToe.tsx b/src/tictactoe/TicTacToe.tsx index c3174f2..353a6ce 100644 --- a/src/tictactoe/TicTacToe.tsx +++ b/src/tictactoe/TicTacToe.tsx @@ -2,7 +2,7 @@ import { useState, useEffect } from "react"; import { useNakama } from "./providers/NakamaProvider"; import { Match } from "@heroiclabs/nakama-js"; import Board from "./Board"; -import MatchList from "./MatchList"; +// import MatchList from "./MatchList"; export default function TicTacToe() { const [username, setUsername] = useState(""); @@ -14,7 +14,8 @@ export default function TicTacToe() { ]); const [turn, setTurn] = useState(0); const [winner, setWinner] = useState(null); - const [openMatches, setOpenMatches] = useState([]); + // const [openMatches, setOpenMatches] = useState([]); + const [players, setPlayers] = useState([]); const { loginOrRegister, @@ -31,9 +32,15 @@ export default function TicTacToe() { console.log("[Match Data]", msg); if (msg.opCode === 2) { - setBoard(msg.data.board); - setTurn(msg.data.turn); - setWinner(msg.data.winner || null); + const state = msg.data; + console.log("Match state:", state); + + setBoard(state.board); + setTurn(state.turn); + setWinner(state.winner || null); + + // new: + setPlayers(state.players || []); } }); }, [onMatchData]); @@ -69,10 +76,14 @@ export default function TicTacToe() { if (msg.opCode === 2) { const state = msg.data; + console.log("Match state:", state); setBoard(state.board); setTurn(state.turn); setWinner(state.winner || null); + + // new: + setPlayers(state.players || []); } }); } @@ -140,6 +151,8 @@ export default function TicTacToe() { board={board} turn={turn} winner={winner} + players={players} + myUserId={session?.user_id ?? null} onCellClick={handleCellClick} /> )}