custom player names

This commit is contained in:
Dave Umrysh 2022-04-15 15:16:38 -06:00
parent 489d9e32ee
commit 9c84bb2efe
4 changed files with 33 additions and 17 deletions

View File

@ -37,16 +37,19 @@ const Game = (props) => {
const [messages, setMessages] = useState([]) const [messages, setMessages] = useState([])
const [playerName, setPlayerName] = useState(data.playerName) const [playerName, setPlayerName] = useState(data.playerName)
const [player1Name, setPlayer1Name] = useState('Player 1')
const [player2Name, setPlayer2Name] = useState('Player 2')
useEffect(() => { useEffect(() => {
const connectionOptions = { const connectionOptions = {
"forceNew" : true, "forceNew" : true,
"reconnectionAttempts": "Infinity", "reconnectionAttempts": "Infinity",
"timeout" : 10000, "timeout" : 10000,
"transports" : ["websocket"] "transports" : ["websocket"]
} }
socket = io.connect(ENDPOINT, connectionOptions) socket = io.connect(ENDPOINT, connectionOptions)
socket.emit('join', {room: room}, (error) => { socket.emit('join', {room: room, playerName: playerName}, (error) => {
if(error) if(error)
setRoomFull(true) setRoomFull(true)
}) })
@ -157,12 +160,22 @@ const Game = (props) => {
socket.on("roomData", ({ users }) => { socket.on("roomData", ({ users }) => {
setUsers(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 }) => { socket.on('currentUserData', ({ name }) => {
setCurrentUser(name) setCurrentUser(name)
}) })
socket.on('message', message => { socket.on('message', message => {
setMessages(messages => [ ...messages, message ]) setMessages(messages => [ ...messages, message ])
@ -175,7 +188,7 @@ const Game = (props) => {
const checkGameOver = (arr) => { const checkGameOver = (arr) => {
return arr.length === 1 return arr.length === 1
} }
const checkWinner = (arr, player) => { const checkWinner = (arr, player) => {
return arr.length === 1 ? 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 //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!') console.log('number matched!')
//check who played the card and return new state accordingly //check who played the card and return new state accordingly
if(cardPlayedBy === 'Player 1') { if(cardPlayedBy === 'Player 1') {
@ -1013,7 +1026,7 @@ const Game = (props) => {
break; break;
} }
} }
const onCardDrawnHandler = () => { const onCardDrawnHandler = () => {
//extract player who drew the card //extract player who drew the card
const cardDrawnBy = turn const cardDrawnBy = turn
@ -1215,7 +1228,7 @@ const Game = (props) => {
} }
} }
} }
return ( return (
<div className={`Game backgroundColorR backgroundColor${currentColor}`}> <div className={`Game backgroundColorR backgroundColor${currentColor}`}>
{(!roomFull) ? <> {(!roomFull) ? <>
@ -1245,9 +1258,9 @@ const Game = (props) => {
{gameOver ? <div>{winner !== '' && <><h1>GAME OVER</h1><h2>{winner} wins!</h2></>}</div> : {gameOver ? <div>{winner !== '' && <><h1>GAME OVER</h1><h2>{winner} wins!</h2></>}</div> :
<div> <div>
{/* PLAYER 1 VIEW */} {/* PLAYER 1 VIEW */}
{currentUser === 'Player 1' && <> {currentUser === 'Player 1' && <>
<div className='player2Deck' style={{pointerEvents: 'none'}}> <div className='player2Deck' style={{pointerEvents: 'none'}}>
<p className='playerDeckText'>Player 2</p> <p className='playerDeckText'>{player2Name}</p>
{player2Deck.map((item, i) => ( {player2Deck.map((item, i) => (
<img <img
key={i} key={i}
@ -1268,7 +1281,7 @@ const Game = (props) => {
</div> </div>
<br /> <br />
<div className='player1Deck' style={turn === 'Player 1' ? null : {pointerEvents: 'none'}}> <div className='player1Deck' style={turn === 'Player 1' ? null : {pointerEvents: 'none'}}>
<p className='playerDeckText'>{playerName}</p> <p className='playerDeckText'>{player1Name}</p>
{player1Deck.map((item, i) => ( {player1Deck.map((item, i) => (
<img <img
key={i} key={i}
@ -1314,7 +1327,7 @@ const Game = (props) => {
{/* PLAYER 2 VIEW */} {/* PLAYER 2 VIEW */}
{currentUser === 'Player 2' && <> {currentUser === 'Player 2' && <>
<div className='player1Deck' style={{pointerEvents: 'none'}}> <div className='player1Deck' style={{pointerEvents: 'none'}}>
<p className='playerDeckText'>Player 1</p> <p className='playerDeckText'>{player1Name}</p>
{player1Deck.map((item, i) => ( {player1Deck.map((item, i) => (
<img <img
key={i} key={i}
@ -1335,7 +1348,7 @@ const Game = (props) => {
</div> </div>
<br /> <br />
<div className='player2Deck' style={turn === 'Player 1' ? {pointerEvents: 'none'} : null}> <div className='player2Deck' style={turn === 'Player 1' ? {pointerEvents: 'none'} : null}>
<p className='playerDeckText'>{playerName}</p> <p className='playerDeckText'>{player2Name}</p>
{player2Deck.map((item, i) => ( {player2Deck.map((item, i) => (
<img <img
key={i} key={i}

View File

@ -10,7 +10,7 @@ const Homepage = () => {
<div className='Homepage'> <div className='Homepage'>
<div className='homepage-menu'> <div className='homepage-menu'>
<img src={require('../assets/logo.png').default} width='200px' /> <img src={require('../assets/logo.png').default} width='200px' />
<br> <br />
<input type='text' placeholder='Your name' className="your-name-input" onChange={(event) => setPlayerName(event.target.value)} /> <input type='text' placeholder='Your name' className="your-name-input" onChange={(event) => setPlayerName(event.target.value)} />
<div className='homepage-form'> <div className='homepage-form'>
<div className='homepage-join'> <div className='homepage-join'>

View File

@ -16,11 +16,14 @@ app.use(cors())
io.on('connection', socket => { io.on('connection', socket => {
socket.on('join', (payload, callback) => { socket.on('join', (payload, callback) => {
let numberOfUsersInRoom = getUsersInRoom(payload.room).length let numberOfUsersInRoom = getUsersInRoom(payload.room).length
let username = numberOfUsersInRoom===0 ? 'Player 1' : 'Player 2';
const { error, newUser} = addUser({ const { error, newUser} = addUser({
id: socket.id, id: socket.id,
name: numberOfUsersInRoom===0 ? 'Player 1' : 'Player 2', name: username,
room: payload.room room: payload.room,
playerName: payload.playerName
}) })
if(error) if(error)

View File

@ -1,11 +1,11 @@
const users = [] const users = []
const addUser = ({id, name, room}) => { const addUser = ({id, name, room, playerName}) => {
const numberOfUsersInRoom = users.filter(user => user.room === room).length const numberOfUsersInRoom = users.filter(user => user.room === room).length
if(numberOfUsersInRoom === 2) if(numberOfUsersInRoom === 2)
return { error: 'Room full' } return { error: 'Room full' }
const newUser = { id, name, room } const newUser = { id, name, room, playerName}
users.push(newUser) users.push(newUser)
return { newUser } return { newUser }
} }