@@ -21,8 +21,8 @@ import gameOverSound from '../assets/sounds/game-over-sound.mp3'
21
21
//DRAW 4 WILD - 600
22
22
23
23
let socket
24
- // const ENDPOINT = 'http://localhost:5000'
25
- const ENDPOINT = 'https://uno-online-multiplayer.herokuapp.com/'
24
+ const ENDPOINT = 'http://localhost:5000'
25
+ // const ENDPOINT = 'https://uno-online-multiplayer.herokuapp.com/'
26
26
27
27
const Game = ( props ) => {
28
28
const data = queryString . parse ( props . location . search )
@@ -32,6 +32,8 @@ const Game = (props) => {
32
32
const [ roomFull , setRoomFull ] = useState ( false )
33
33
const [ users , setUsers ] = useState ( [ ] )
34
34
const [ currentUser , setCurrentUser ] = useState ( '' )
35
+ const [ message , setMessage ] = useState ( '' )
36
+ const [ messages , setMessages ] = useState ( [ ] )
35
37
36
38
useEffect ( ( ) => {
37
39
const connectionOptions = {
@@ -156,6 +158,13 @@ const Game = (props) => {
156
158
socket . on ( 'currentUserData' , ( { name } ) => {
157
159
setCurrentUser ( name )
158
160
} )
161
+
162
+ socket . on ( 'message' , message => {
163
+ setMessages ( messages => [ ...messages , message ] )
164
+
165
+ const chatBody = document . querySelector ( '.chat-body' )
166
+ chatBody . scrollTop = chatBody . scrollHeight
167
+ } )
159
168
} , [ ] )
160
169
161
170
//some util functions
@@ -167,6 +176,23 @@ const Game = (props) => {
167
176
return arr . length === 1 ? player : ''
168
177
}
169
178
179
+ const toggleChatBox = ( ) => {
180
+ const chatBody = document . querySelector ( '.chat-body' )
181
+ if ( chatBody . style . display === 'none' )
182
+ chatBody . style . display = 'block'
183
+ else
184
+ chatBody . style . display = 'none'
185
+ }
186
+
187
+ const sendMessage = ( event ) => {
188
+ event . preventDefault ( )
189
+ if ( message ) {
190
+ socket . emit ( 'sendMessage' , { message : message } , ( ) => {
191
+ setMessage ( '' )
192
+ } )
193
+ }
194
+ }
195
+
170
196
//driver functions
171
197
const onCardPlayedHandler = ( played_card ) => {
172
198
//extract player who played the card
@@ -1196,14 +1222,15 @@ const Game = (props) => {
1196
1222
</ span >
1197
1223
</ div >
1198
1224
1225
+ { /* PLAYER LEFT MESSAGES */ }
1199
1226
{ users . length === 1 && currentUser === 'Player 2' && < h1 className = 'topInfoText' > Player 1 has left the game.</ h1 > }
1200
-
1201
1227
{ users . length === 1 && currentUser === 'Player 1' && < h1 className = 'topInfoText' > Waiting for Player 2 to join the game.</ h1 > }
1202
1228
1203
1229
{ users . length === 2 && < >
1204
1230
1205
1231
{ gameOver ? < div > { winner !== '' && < > < h1 > GAME OVER</ h1 > < h2 > { winner } wins!</ h2 > </ > } </ div > :
1206
1232
< div >
1233
+ { /* PLAYER 1 VIEW */ }
1207
1234
{ currentUser === 'Player 1' && < >
1208
1235
< div className = 'player2Deck' style = { { pointerEvents : 'none' } } >
1209
1236
< p className = 'playerDeckText' > Player 2</ p >
@@ -1241,8 +1268,35 @@ const Game = (props) => {
1241
1268
src = { require ( `../assets/cards-front/${ item } .png` ) . default }
1242
1269
/>
1243
1270
) ) }
1271
+ </ div >
1272
+ { /* <Chat Box Div */ }
1273
+ < div className = "chatBoxWrapper" >
1274
+ < div className = "chat-box chat-box-player1" >
1275
+ < div className = "chat-head" >
1276
+ < h2 > Chat Box</ h2 >
1277
+ < img
1278
+ onClick = { toggleChatBox }
1279
+ src = "https://maxcdn.icons8.com/windows10/PNG/16/Arrows/angle_down-16.png"
1280
+ title = "Expand Arrow"
1281
+ width = "16" />
1282
+ </ div >
1283
+ < div className = "chat-body" >
1284
+ < div className = "msg-insert" >
1285
+ { messages . map ( msg => {
1286
+ if ( msg . user === 'Player 2' )
1287
+ return < div className = "msg-receive" > { msg . text } </ div >
1288
+ if ( msg . user === 'Player 1' )
1289
+ return < div className = "msg-send" > { msg . text } </ div >
1290
+ } ) }
1291
+ </ div >
1292
+ < div className = "chat-text" >
1293
+ < input type = 'text' placeholder = 'Type a message...' value = { message } onChange = { event => setMessage ( event . target . value ) } onKeyPress = { event => event . key === 'Enter' && sendMessage ( event ) } />
1294
+ </ div >
1295
+ </ div >
1296
+ </ div >
1244
1297
</ div > </ > }
1245
1298
1299
+ { /* PLAYER 2 VIEW */ }
1246
1300
{ currentUser === 'Player 2' && < >
1247
1301
< div className = 'player1Deck' style = { { pointerEvents : 'none' } } >
1248
1302
< p className = 'playerDeckText' > Player 1</ p >
@@ -1280,6 +1334,32 @@ const Game = (props) => {
1280
1334
src = { require ( `../assets/cards-front/${ item } .png` ) . default }
1281
1335
/>
1282
1336
) ) }
1337
+ </ div >
1338
+ { /* <Chat Box Div */ }
1339
+ < div className = "chatBoxWrapper" >
1340
+ < div className = "chat-box chat-box-player2" >
1341
+ < div className = "chat-head" >
1342
+ < h2 > Chat Box</ h2 >
1343
+ < img
1344
+ onClick = { toggleChatBox }
1345
+ src = "https://maxcdn.icons8.com/windows10/PNG/16/Arrows/angle_down-16.png"
1346
+ title = "Expand Arrow"
1347
+ width = "16" />
1348
+ </ div >
1349
+ < div className = "chat-body" >
1350
+ < div className = "msg-insert" >
1351
+ { messages . map ( msg => {
1352
+ if ( msg . user === 'Player 1' )
1353
+ return < div className = "msg-receive" > { msg . text } </ div >
1354
+ if ( msg . user === 'Player 2' )
1355
+ return < div className = "msg-send" > { msg . text } </ div >
1356
+ } ) }
1357
+ </ div >
1358
+ < div className = "chat-text" >
1359
+ < input type = 'text' placeholder = 'Type a message...' value = { message } onChange = { event => setMessage ( event . target . value ) } onKeyPress = { event => event . key === 'Enter' && sendMessage ( event ) } />
1360
+ </ div >
1361
+ </ div >
1362
+ </ div >
1283
1363
</ div > </ > }
1284
1364
</ div > }
1285
1365
</ > }
0 commit comments