diff --git a/client/src/components/Game.js b/client/src/components/Game.js index 2a7f75b..ef28a05 100644 --- a/client/src/components/Game.js +++ b/client/src/components/Game.js @@ -37,16 +37,19 @@ const Game = (props) => { const [messages, setMessages] = useState([]) const [playerName, setPlayerName] = useState(data.playerName) + const [player1Name, setPlayer1Name] = useState('Player 1') + const [player2Name, setPlayer2Name] = useState('Player 2') + useEffect(() => { const connectionOptions = { "forceNew" : true, - "reconnectionAttempts": "Infinity", - "timeout" : 10000, + "reconnectionAttempts": "Infinity", + "timeout" : 10000, "transports" : ["websocket"] } socket = io.connect(ENDPOINT, connectionOptions) - socket.emit('join', {room: room}, (error) => { + socket.emit('join', {room: room, playerName: playerName}, (error) => { if(error) setRoomFull(true) }) @@ -157,12 +160,22 @@ const Game = (props) => { socket.on("roomData", ({ users }) => { setUsers(users) + + // For each user, update the name + users.forEach(function(user){ + if(user.name === 'Player 1'){ + setPlayer1Name(user.playerName) + }else if(user.name === 'Player 2'){ + setPlayer2Name(user.playerName) + } + }) }) socket.on('currentUserData', ({ name }) => { setCurrentUser(name) }) + socket.on('message', message => { setMessages(messages => [ ...messages, message ]) @@ -175,7 +188,7 @@ const Game = (props) => { const checkGameOver = (arr) => { return arr.length === 1 } - + const checkWinner = (arr, player) => { return arr.length === 1 ? player : '' } @@ -689,7 +702,7 @@ const Game = (props) => { } } //check for number match - if draw 2 card played on draw 2 card - else if(currentNumber === 252) { + else if(currentNumber === 252) { console.log('number matched!') //check who played the card and return new state accordingly if(cardPlayedBy === 'Player 1') { @@ -1013,7 +1026,7 @@ const Game = (props) => { break; } } - + const onCardDrawnHandler = () => { //extract player who drew the card const cardDrawnBy = turn @@ -1215,7 +1228,7 @@ const Game = (props) => { } } } - + return (
Player 2
+{player2Name}
{player2Deck.map((item, i) => ( {{playerName}
+{player1Name}
{player1Deck.map((item, i) => ( { {/* PLAYER 2 VIEW */} {currentUser === 'Player 2' && <>Player 1
+{player1Name}
{player1Deck.map((item, i) => ( {{playerName}
+{player2Name}
{player2Deck.map((item, i) => ( {