diff options
-rw-r--r-- | src/components/App.js | 3 | ||||
-rw-r--r-- | src/components/GamemodeChooser.js | 1 | ||||
-rw-r--r-- | src/components/MultiplayerGrid.js | 5 | ||||
-rw-r--r-- | src/components/MultiplayerMenu.js | 45 | ||||
-rw-r--r-- | src/server/index.js | 8 |
5 files changed, 46 insertions, 16 deletions
diff --git a/src/components/App.js b/src/components/App.js index db5af1b..5b13230 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -33,6 +33,7 @@ const App = () => { const [showMessageBox, setShowMessageBox] = useState(false); const [message, setMessage] = useState(""); const [isHost, setIsHost] = useState(false); + const [joinCode, setJoinCode] = useState(); return gameStarted ? ( <> @@ -61,6 +62,7 @@ const App = () => { setScoreO={setScoreO} setMessage={setMessage} setShowMessage={setShowMessageBox} + joinCode={joinCode} /> : <Grid turn={turn} setTurn={setTurn} @@ -81,6 +83,7 @@ const App = () => { setMultiplayer={setMultiplayer} setGameStarted={setGameStarted} setIsHost={setIsHost} + setJoinCode={setJoinCode} /> <Footer/> </> diff --git a/src/components/GamemodeChooser.js b/src/components/GamemodeChooser.js index 91010e4..6b2e985 100644 --- a/src/components/GamemodeChooser.js +++ b/src/components/GamemodeChooser.js @@ -47,6 +47,7 @@ const GamemodeChooser = (props) => { <MultiplayerMenu setIsHost={props.setIsHost} setGameStarted={props.setGameStarted} + setJoinCode={props.setJoinCode} /> <div className={"GamemodeButton"} onClick={() => { diff --git a/src/components/MultiplayerGrid.js b/src/components/MultiplayerGrid.js index 7c00f17..d7eae36 100644 --- a/src/components/MultiplayerGrid.js +++ b/src/components/MultiplayerGrid.js @@ -29,6 +29,7 @@ const MultiplayerGrid = (props) => { const turn = props.turn; const isHost = props.isHost; + const joinCode = props.joinCode; const setMessage = props.setMessage; const setShowMessage = props.setShowMessage; @@ -48,7 +49,7 @@ const MultiplayerGrid = (props) => { setTimeout(() => setShowMessage(false), 3000) }) } else { - socket.emit("join", prompt("Enter ID")); + socket.emit("join", joinCode); // error if room doesn't exist socket.on("join failed", () => { @@ -57,7 +58,7 @@ const MultiplayerGrid = (props) => { setTimeout(() => window.location.reload(), 3000) }); } - }, [isHost, setMessage, setShowMessage]); + }, [isHost, setMessage, setShowMessage, joinCode]); const getBoard = (index) => { // if it's not your turn you can't play diff --git a/src/components/MultiplayerMenu.js b/src/components/MultiplayerMenu.js index 1dfe9dd..aa77507 100644 --- a/src/components/MultiplayerMenu.js +++ b/src/components/MultiplayerMenu.js @@ -15,24 +15,45 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import React from 'react'; +import React, { useState } from 'react'; import './style.css'; const MultiplayerMenu = (props) => { + const [showCodeInput, setShowCodeInput] = useState(false) + const [joinCode, setJoinCode] = useState(""); + return ( <div className={"MultiplayerMenu"}> - <div className={"GamemodeButton"} onClick={() => { - props.setIsHost(true); - props.setGameStarted(true); - }}> - HOST GAME - </div> + {showCodeInput + ? <> + <input type="text" placeholder={"ENTER CODE"} + className={"GamemodeButton"} onChange={ + (event) => setJoinCode(event.target.value) + } + /> + + <div className={"GamemodeButton"} onClick={() => { + props.setGameStarted(true); + props.setJoinCode(joinCode); + }}> + START + </div> + </> + : <> + <div className={"GamemodeButton"} onClick={() => { + props.setIsHost(true); + props.setGameStarted(true); + }}> + HOST GAME + </div> - <div className={"GamemodeButton"} onClick={() => { - props.setGameStarted(true); - }}> - JOIN GAME - </div> + <div className={"GamemodeButton"} onClick={() => { + setShowCodeInput(true); + }}> + JOIN GAME + </div> + </> + } </div> ); } diff --git a/src/server/index.js b/src/server/index.js index 5eeff99..a5aa97f 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -3,10 +3,14 @@ const app = express(); const http = require('http').Server(app); const path = require('path'); -// const io = require('socket.io')(http); const io = require("socket.io")(http, { cors: { - origin: ["http://localhost:5000", "http://localhost:3000"], + origin: [ + "http://localhost:5000", + "http://localhost:3000", + "https://mikunonaka-tic-tac-toe.herokuapp.com", + "http://mikunonaka-tic-tac-toe.herokuapp.com" + ], "Access-Control-Allow-Origin": "*", methods: ["GET", "POST"] } |