Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | import styles from './Presenter.module.css'; import { useNavigate, useParams, Navigate } from 'react-router-dom'; import { useWebSocket } from '../../contexts/WebSocketContext'; import { useEffect, useState } from 'react'; import { QRCodeSVG } from 'qrcode.react'; import { HIPPO_COLORS } from '../../config/hippoColors'; /** * Presenter - React component that displays the session ID to the host after creating a new game. * * The Presenter is the host interface shown after creating a new game session. * * ## Purpose: * - Displays the session code as text and QR code. * - Allows the host to copy the session ID. * - Lets the host choose a game mode (Easy, Medium, Hard). * - Shows a lobby with currently connected users (hippo players and AAC users). * - Allows the host to start the game when all required participants are present. * * ## Features: * - Session ID is shown and copyable. * - QR code directs users to the role selection screen. * - Shows up to 4 hippo slots and 1 AAC device. * - Hosts can cycle between game modes (with visual + audio feedback). * - Prevents game start until at least 1 AAC user and 1 Hippo player are connected. * - Sends `PLAYER_JOIN`, `START_GAME`, and `START_TIMER` messages via WebSocket. * * ## Hooks: * - `useParams`: Gets `sessionId` from the route. * - `useWebSocket`: Sends messages, receives user list. * - `useEffect`: Joins the session as "Presenter", plays audio on mode change. * - `useNavigate`: Redirects user back or forwards to Spectator screen. * * @component * @returns {JSX.Element} The rendered Presenter game lobby interface. * * @example * ```tsx * <Route path="/presenter/:sessionId" element={<Presenter />} /> * ``` */ const presenterBg = '/assets/presenterBg.webp'; /** * Mapping of game modes to their label, icon path, and number of icons. * Used for rendering game mode selector UI. */ const modeDetails = { Easy: { label: 'Easy', iconPath: '/assets/fruits/strawberry.png', count: 1 }, Medium: { label: 'Medium', iconPath: '/assets/fruits/strawberry.png', count: 2 }, Hard: { label: 'Hard', iconPath: '/assets/fruits/strawberry.png', count: 3 }, }; function Presenter() { /** * State to control the visibility of the "How to Play" help GIF overlay. * * @type {boolean} * @default false * * @description * - `true`: The help GIF overlay is visible on screen. * - `false`: The overlay is hidden. * * Used to toggle the instructional animation when a user clicks the "?" icon next to the game mode selector. */ const [showHelpGif, setShowHelpGif] = useState(false); const navigate = useNavigate(); /** * Session ID extracted from the route. * Used to join the WebSocket session and render QR/game code. */ const { sessionId } = useParams<{ sessionId: string }>(); /** * Whether the session ID was copied to clipboard recently. */ const [copied, setCopied] = useState(false); /** * Current selected game mode. * Cycles between Easy, Medium, and Hard. */ const [mode, setMode] = useState<'Easy' | 'Medium' | 'Hard'>('Easy'); if (!sessionId || sessionId.length < 5) { console.error('Invalid sessionId:', sessionId); return <Navigate to="/" replace />; } /** * Hardcoded presenter ID to register as "Presenter" over WebSocket. */ const presenterId = 'presenter'; /** * WebSocket context values: * - `sendMessage`: function to emit WebSocket messages. * - `connectedUsers`: list of all users in the session. * - `isConnected`: whether WebSocket is open. */ const { sendMessage, connectedUsers, isConnected, lastMessage, clearLastMessage } = useWebSocket(); /** * List of available game modes in the order they should cycle through. * * Used for navigating between modes via the `cycleMode` function. */ const modes: Array<'Easy' | 'Medium' | 'Hard'> = ['Easy', 'Medium', 'Hard']; /** * Plays the audio for the selected game mode. * * @param selectedMode - The mode for which to play the audio. */ const playModeAudio = (selectedMode: 'Easy' | 'Medium' | 'Hard') => { const audio = new Audio(`/audio/modes/${selectedMode.toLowerCase()}.mp3`); audio.play().catch((e) => { console.warn('Audio playback failed:', e); }); }; /** * Cycles through the available game modes either to the left (previous) or right (next). * * @param direction - The direction to cycle: `'left'` for previous, `'right'` for next. */ const cycleMode = (direction: 'left' | 'right') => { const currentIndex = modes.indexOf(mode); const newIndex = direction === 'left' ? (currentIndex + modes.length - 1) % modes.length : (currentIndex + 1) % modes.length; setMode(modes[newIndex]); }; // --- ERROR HANDLING --- useEffect(() => { if (lastMessage?.type === 'ERROR_MESSAGE' && lastMessage?.payload?.code === 'SESSION_NOT_FOUND') { alert(`An error occurred: ${lastMessage.payload.message}`); clearLastMessage?.(); navigate('/'); } }, [lastMessage, navigate, clearLastMessage]); // Play audio for the initial mode when the component mounts useEffect(() => { playModeAudio(mode); }, [mode]); // Join session as "Presenter" to receive updates useEffect(() => { if (sessionId && isConnected) { sendMessage({ type: 'PLAYER_JOIN', payload: { sessionId, userId: presenterId, role: 'Presenter' } }); } }, [sessionId, isConnected, sendMessage]); /** * Handler for clicking the close button. * Navigates the user back to the landing page. */ const handleCancel = () => { navigate('/'); }; /** * ID to use for the Spectator client that gets opened by the presenter. */ const spectatorId = "PresenterSpectator"; // Or use presenterId, or generate unique /** * Number of connected users with AAC User role. */ const aacCount = connectedUsers.filter(u => u.role === 'AAC User').length; /** * List of all connected users who selected the "Hippo Player" role. */ const hippoPlayers = connectedUsers.filter(u => u.role === 'Hippo Player'); // Function to handle start game const handleStartGame = () => { console.log('Start Game button clicked, sending START_GAME message'); if (!sessionId) { console.error('No sessionId available'); return; } sendMessage({ type: 'PLAYER_JOIN', payload: { sessionId, userId: spectatorId, role: 'Spectator' } }); sendMessage({ type: 'START_GAME', payload: { sessionId, mode }, }); console.log('Sending START_TIMER for session ', sessionId, ' with mode ', mode); sendMessage({ type: 'START_TIMER', payload: { sessionId, mode }, }); navigate(`/spectator/${sessionId}/${spectatorId}`, { state: { userId: spectatorId, role: 'Spectator' } }); }; const handleCopy = () => { navigator.clipboard.writeText(sessionId); setCopied(true); setTimeout(() => setCopied(false), 1500); }; /** * List of slot indices for Hippo player UI display. * Each slot corresponds to a position around the pond. */ const lobbyHippoSlots = [0, 1, 2, 3]; function renderHippoSlot(player: any, index: number) { const isActive = !!player; // Use the HIPPO_COLORS array to get the color for the slot const hippoColor = isActive ? HIPPO_COLORS.find(h => h.color === player.color) : null; console.log('Rendering hippo slot:', index, 'Player:', player, 'Color:', hippoColor); return ( <div key={index} className={styles.hippoSlot}> <div className={styles.hippoImageWrapper}> <img src="/assets/hippos/outlineHippo.png" alt="Image of Hippo" className={styles.hippoImage} /> {isActive && hippoColor && ( <img src={hippoColor.imgSrc} alt={`${hippoColor.color} Hippo`} className={`${styles.hippoImage} ${styles.fadeIn}`} /> )} </div> <span className={styles.userId}>{isActive && player.color ? player.color.charAt(0).toUpperCase() + player.color.slice(1) : ''}</span> </div> ); } return ( <div className={styles.containerImg}> <div className={styles.roleWrapper}> <div className={styles.contentRow}> {/* Left Column: QR Code and Game Code */} <div className={styles.leftColumn}> <h1 className={styles.scanQrCodeText}> QR Code{' '} <img src="/assets/cameraIcon.png" alt="Camera icon" className={styles.cameraIcon} /> </h1> <div className={styles.qrWrapper}> <QRCodeSVG className={styles.QrCode} value={`${window.location.origin}/roleselect/${sessionId}`} size={128} /> </div> <div className={styles.joinRoomDivider}> <span>or</span> </div> <h1 className={styles.gameCodeText}> Game Code:{' '} <span className={styles.copyWrapper} onClick={handleCopy}> <span className={styles.sessionBox}> <span className={styles.sessionId}>{sessionId}</span> <span className={styles.copyIcon} aria-label="Copy icon" role="img"> ⎘ </span> </span> <span className={styles.tooltip}> {copied ? 'Code copied!' : 'Click to copy'} </span> </span> </h1> <p className={styles.limitNote}>(Up to 4 Hippos)</p> </div> {/* Right Column: Hippo Slots and AAC Device */} <div className={styles.rightColumn}> <button className={styles.closeButton} onClick={handleCancel} aria-label="Cancel New Game" > ✖ </button> <div className={styles.mapWrapper}> <div className={styles.pondArea}> <img src={presenterBg} alt="Pond background" className={styles.pondImage} /> <div className={styles.hippoGrid}> {lobbyHippoSlots.map((slotIndex) => renderHippoSlot(hippoPlayers[slotIndex], slotIndex) )} </div> <div className={styles.aacCenter}> <div className={styles.aacImageWrapper}> <img src="/assets/aacDeviceOutline.png" alt="AAC Outline" className={styles.aacImage} /> <img src="/assets/aacDevice.png" alt="AAC Device" className={`${styles.aacImage} ${aacCount >= 1 ? styles.fadeIn : ''}`} /> </div> {aacCount >= 1 && <span className={styles.userId}>AAC User</span>} </div> </div> </div> {/* Game Mode Selection */} <div className={styles.modeSelectorWrapper}> <button className={styles.arrowButton} onClick={() => cycleMode('left')} aria-label="Previous mode" > ◀ </button> <div className={styles.modeDisplay} style={{ color: 'black', backgroundColor: mode === 'Easy' ? '#4CAF50' : mode === 'Medium' ? '#e2d733ff' : '#fe1c1cff', fontWeight: '550', fontFamily: 'Fredoka, sans-serif', }} > <div className={styles.flexRowWrapper}> {/* <span className={styles.modeLabel}>{modeDetails[mode].label}</span> */} <div className={styles.modeIconContainer}> {Array.from({ length: modeDetails[mode].count }).map((_, i) => ( <img key={i} src={modeDetails[mode].iconPath} alt={mode} className={styles.modeIcon} /> ))} </div> </div> </div> <button className={styles.arrowButton} onClick={() => cycleMode('right')} aria-label="Next mode" > ▶ </button> {/* Question mark help button */} <button className={styles.helpButton} onClick={() => setShowHelpGif(true)} aria-label="Show help" > ? </button> </div> {showHelpGif && ( <div className={styles.helpGifOverlay}> <img src={ mode === 'Easy' ? '/assets/mode/easyCompress.gif' : mode === 'Medium' ? '/assets/mode/medCompress.gif' : '/assets/mode/hardCompress.gif' } alt={`How to play: ${mode}`} className={styles.overlayGif} /> <button className={styles.closeButton} onClick={() => setShowHelpGif(false)} aria-label="Close help overlay" > ✖ </button> </div> )} {/* Start Game Button */} <div className={styles.startButtonWrapper}> <button className={styles.startButton} onClick={handleStartGame} disabled={hippoPlayers.length < 1 || aacCount < 1} > <div className={styles.buttonContent}> <div className={styles.iconRow}> <img src="/assets/hippos/brownHippo.png" alt="Hippo" className={`${styles.requirementIcon} ${ hippoPlayers.length >= 1 ? styles.iconReady : '' }`} /> <img src="/assets/aacDevice.png" alt="AAC" className={`${styles.requirementIcon} ${ aacCount >= 1 ? styles.iconReady : '' }`} /> <span className={styles.buttonLabel}>Start Game</span> </div> </div> </button> </div> </div> </div> </div> </div> ); } export default Presenter; |