@@ -9,7 +9,7 @@ import { colyseusService } from '../../services/colyseusService';
99import greenLayer from '../../assets/game/greenLayer.png' ;
1010import purpleLayer from '../../assets/game/purpleLayer.png' ;
1111
12- const GameRoom = ( { roomId, onQuit, currentUser } ) => {
12+ const GameRoom = ( { roomId, betAmount , currency , onQuit, currentUser } ) => {
1313 const [ room , setRoom ] = useState ( null ) ;
1414 // Authoritative state from the server
1515 const [ gameState , setGameState ] = useState ( {
@@ -64,13 +64,18 @@ const GameRoom = ({ roomId, onQuit, currentUser }) => {
6464 roomInstance . onMessage ( "error" , ( message ) => console . error ( "Server error:" , message ) ) ;
6565
6666 roomInstance . onMessage ( "opponent_left" , ( message ) => {
67- setModalMessage ( message . message || 'Opponent left.' ) ;
68- setShowOpponentLeftModal ( true ) ;
67+ setModalMessage (
68+ < >
69+ < p style = { { color : 'white' , fontSize : '1.5em' , margin : 0 } } > Opponent left</ p >
70+ < p style = { { color : 'white' , fontSize : '1em' , margin : '10px 0' } } > Your bet is returned</ p >
71+ </ >
72+ ) ;
73+ setShowEndGameModal ( true ) ;
6974 } ) ;
7075
7176 roomInstance . onMessage ( "game_over" , ( message ) => {
72- setModalMessage ( message . message || 'Game is over.' ) ;
73- setShowOpponentLeftModal ( true ) ; // Reuse the same modal
77+ // This is handled by the useEffect watching gameState.winner
78+ // No explicit message setting here to avoid conflicts.
7479 } ) ;
7580
7681 // Handle reconnection events
@@ -179,18 +184,42 @@ const GameRoom = ({ roomId, onQuit, currentUser }) => {
179184 // Modal state
180185 const [ showQuitConfirmModal , setShowQuitConfirmModal ] = useState ( false ) ;
181186 const [ showBearOffButton , setShowBearOffButton ] = useState ( false ) ;
182- const [ showOpponentLeftModal , setShowOpponentLeftModal ] = useState ( false ) ;
183- const [ modalMessage , setModalMessage ] = useState ( '' ) ;
187+ const [ showEndGameModal , setShowEndGameModal ] = useState ( false ) ;
188+ const [ modalMessage , setModalMessage ] = useState ( null ) ; // Changed to null for JSX
184189
185190 useEffect ( ( ) => {
186- if ( showOpponentLeftModal ) {
191+ if ( gameState . winner && gameState . winner !== '' ) {
192+ const prizeAmount = betAmount * 2 ;
193+ const prizeText = `${ prizeAmount } ${ currency } ` ;
194+
195+ if ( gameState . winner === playerColor ) {
196+ setModalMessage (
197+ < >
198+ < p style = { { color : '#2E8B57' , fontSize : '2em' , margin : 0 , fontWeight : 'bold' } } > WIN</ p >
199+ < p style = { { color : 'white' , fontSize : '1.5em' , margin : '10px 0' } } > { prizeText } </ p >
200+ </ >
201+ ) ;
202+ } else {
203+ setModalMessage (
204+ < >
205+ < p style = { { color : '#DC143C' , fontSize : '2em' , margin : 0 , fontWeight : 'bold' } } > LOSE</ p >
206+ < p style = { { color : 'white' , fontSize : '1.5em' , margin : '10px 0' } } > { `-${ betAmount } ${ currency } ` } </ p >
207+ </ >
208+ ) ;
209+ }
210+ setShowEndGameModal ( true ) ;
211+ }
212+ } , [ gameState . winner , playerColor , betAmount , currency ] ) ;
213+
214+ useEffect ( ( ) => {
215+ if ( showEndGameModal ) {
187216 const timer = setTimeout ( ( ) => {
188217 onQuit ( ) ;
189218 } , 3000 ) ; // 3-second delay before quitting
190219
191220 return ( ) => clearTimeout ( timer ) ; // Cleanup the timer
192221 }
193- } , [ showOpponentLeftModal , onQuit ] ) ;
222+ } , [ showEndGameModal , onQuit ] ) ;
194223
195224 // Effect to control the visibility of the bear-off button
196225 useEffect ( ( ) => {
@@ -292,12 +321,10 @@ const GameRoom = ({ roomId, onQuit, currentUser }) => {
292321 </ div >
293322 </ div >
294323 ) }
295- { showOpponentLeftModal && (
324+ { showEndGameModal && (
296325 < div className = "modal-overlay" >
297- < div className = "create-room-modal" style = { { padding : '24px' } } >
298- < h2 > Game Over</ h2 >
299- < p > { modalMessage } </ p >
300- < p > Returning to lobby...</ p >
326+ < div className = "create-room-modal" style = { { padding : '24px' , textAlign : 'center' } } >
327+ { modalMessage }
301328 </ div >
302329 </ div >
303330 ) }
@@ -327,6 +354,8 @@ const GameRoom = ({ roomId, onQuit, currentUser }) => {
327354
328355GameRoom . propTypes = {
329356 roomId : PropTypes . string . isRequired ,
357+ betAmount : PropTypes . number . isRequired ,
358+ currency : PropTypes . string . isRequired ,
330359 onQuit : PropTypes . func . isRequired ,
331360 currentUser : PropTypes . shape ( {
332361 username : PropTypes . string ,
0 commit comments