diff options
author | MikunoNaka <bokuwakanojogahoshii@yahoo.com> | 2021-08-01 01:52:33 +0530 |
---|---|---|
committer | MikunoNaka <bokuwakanojogahoshii@yahoo.com> | 2021-08-01 01:52:33 +0530 |
commit | 4766a5d0caac22453ab99f386d80c6c132407904 (patch) | |
tree | 7cdaa1f012d574936b8a1d4d90c96186b267b6a9 /src/components | |
parent | d43fa3768c5e9eee06c0af3b093c84aa38aedbf8 (diff) |
Added protection against duplicate room codes
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/MultiplayerGrid.js | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/components/MultiplayerGrid.js b/src/components/MultiplayerGrid.js index 3db9dc6..5954afa 100644 --- a/src/components/MultiplayerGrid.js +++ b/src/components/MultiplayerGrid.js @@ -26,17 +26,31 @@ const socket = io("http://localhost:5000"); const MultiplayerGrid = (props) => { // 0 is O, 1 is X, 2 is blank const [board, setBoard] = useState([2,2,2,2,2,2,2,2,2]); - const turn = props.turn; + const turn = props.turn; const isHost = props.isHost; + const setMessage = props.setMessage; + const setShowMessage = props.setShowMessage; + useEffect(() => { if (isHost) { socket.emit("host"); - socket.on("set-host-id", (id) => alert(id)); + + socket.on("broadcast code", (code) => { + setMessage(`Game Code: ${code}`); + setShowMessage(true); + }); + + socket.on('user joined', () => { + setMessage("Opponent Joined") + setShowMessage(true) + setTimeout(() => setShowMessage(false), 3000) + }) } else { socket.emit("join", prompt("Enter ID")); + socket.on("join fail", () => alert("join fail")); } - }, [isHost]); + }, [isHost, setMessage, setShowMessage]); const getBoard = (index) => { // if it's not your turn you can't play @@ -52,10 +66,10 @@ const MultiplayerGrid = (props) => { } const endGame = (data) => { - props.setMessage( + setMessage( `${data.winner === "Data" ? "" : "WINNER: "}${data.winner}` ); - props.setShowMessage(true); + setShowMessage(true); props.setScoreX(data.scoreX); props.setScoreO(data.scoreO); |