Skip to content

Commit 6be92da

Browse files
committed
feat: Enhance game-end messages with win/loss and prize details
Refactor game-end modal in to display dynamic messages for winner and loser, including bet amount and currency. This involved: - Updating to pass and to . - Adjusting , , and to propagate room details correctly. - Implementing conditional rendering and styling for win/loss states in .
1 parent c53d6b9 commit 6be92da

5 files changed

Lines changed: 67 additions & 27 deletions

File tree

client/src/App.jsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function App() {
1717
const [clientId, setClientId] = useState(null);
1818
const [telegramData, setTelegramData] = useState(null);
1919
const [user, setUser] = useState(null);
20+
const [roomInfo, setRoomInfo] = useState(null); // NEW: State to hold room details
2021

2122
const [tonConnectUI] = useTonConnectUI();
2223
const firstProofLoading = useRef(true);
@@ -137,8 +138,8 @@ function App() {
137138
}
138139
};
139140

140-
const navigateToGame = useCallback(async (roomId) => {
141-
if (!roomId) {
141+
const navigateToGame = useCallback(async (info) => { // Changed parameter to 'info' object
142+
if (!info || !info.roomId) {
142143
console.error("navigateToGame called with no roomId");
143144
return;
144145
}
@@ -149,17 +150,18 @@ function App() {
149150
try {
150151
const currentRoom = colyseusService.getGameRoom();
151152
// Check if we are already in the correct room
152-
if (currentRoom && currentRoom.roomId === roomId) {
153+
if (currentRoom && currentRoom.roomId === info.roomId) {
153154
console.log('Already in the correct room. Navigating...');
154155
} else {
155-
console.log(`Joining room ${roomId}...`);
156-
await colyseusService.joinExistingRoom(roomId);
156+
console.log(`Joining room ${info.roomId}...`);
157+
await colyseusService.joinExistingRoom(info.roomId);
157158
}
158159

159-
setGameRoomId(roomId);
160+
setGameRoomId(info.roomId);
161+
setRoomInfo(info); // Store the full room info
160162
setCurrentScreen('game-room');
161163
} catch (e) {
162-
console.error(`Failed to navigate to game room ${roomId}:`, e);
164+
console.error(`Failed to navigate to game room ${info.roomId}:`, e);
163165
setError('Failed to join the game. Please try again.');
164166
// Go back to the main menu on failure
165167
setCurrentScreen('main-menu');
@@ -170,6 +172,7 @@ function App() {
170172

171173
const handleQuitGame = useCallback(async () => {
172174
setGameRoomId(null);
175+
setRoomInfo(null); // Clear room info on quit
173176
setCurrentScreen('main-menu');
174177

175178
// Reload user profile to get updated stats after game
@@ -218,7 +221,15 @@ function App() {
218221
/>
219222
)}
220223
{currentScreen === 'main-menu' && <MainMenu user={user} onNavigateToGame={navigateToGame} />}
221-
{currentScreen === 'game-room' && <GameRoom roomId={gameRoomId} onQuit={handleQuitGame} currentUser={user} />}
224+
{currentScreen === 'game-room' && (
225+
<GameRoom
226+
roomId={gameRoomId}
227+
betAmount={roomInfo?.betAmount} // Pass betAmount
228+
currency={roomInfo?.currency} // Pass currency
229+
onQuit={handleQuitGame}
230+
currentUser={user}
231+
/>
232+
)}
222233
</div>
223234
);
224235
}

client/src/screens/GameRoom/GameRoom.jsx

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { colyseusService } from '../../services/colyseusService';
99
import greenLayer from '../../assets/game/greenLayer.png';
1010
import 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

328355
GameRoom.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,

client/src/screens/MainMenu/components/CreateRoomModal.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const CreateRoomModal = ({ isOpen, onClose, balances, onNavigateToGame, user })
5252
onClose();
5353
setBetAmount('');
5454
if (onNavigateToGame) {
55-
onNavigateToGame(room.roomId);
55+
onNavigateToGame(room);
5656
}
5757
} catch (error) {
5858
console.error('Failed to create or join room:', error);

client/src/screens/MainMenu/components/RoomCard.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const RoomCard = ({ room, onEnter }) => {
88
const truncatedCreator = createdBy.length > 11 ? `${createdBy.substring(0, 11)}...` : createdBy;
99

1010
const handleEnterRoom = () => {
11-
onEnter(roomId);
11+
onEnter(room);
1212
};
1313

1414
return (

client/src/screens/MainMenu/components/RoomList.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ const RoomList = ({ onNavigateToGame }) => {
6060
}
6161
}, [])
6262

63-
const handleEnterRoom = (roomId) => {
64-
console.log(`Entering room ${roomId}`);
63+
const handleEnterRoom = (room) => {
64+
console.log(`Entering room ${room.roomId}`);
6565
if (onNavigateToGame) {
66-
onNavigateToGame(roomId);
66+
onNavigateToGame(room);
6767
}
6868
}
6969

0 commit comments

Comments
 (0)