All files / Hungry-Hippo-Game/src/pages/RoleSelection RoleSelect.tsx

70.18% Statements 193/275
63.15% Branches 36/57
75% Functions 9/12
70.18% Lines 193/275

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 4101x 1x 1x 1x 1x 1x 1x                                                                                               11x 11x 11x   11x 11x   13x 13x 13x 13x   13x         13x 13x 13x 13x 13x 13x     13x     13x 13x 13x 13x 13x 13x     13x 6x                 6x       6x   13x     13x 6x       13x     13x 6x 13x     13x 6x         6x   6x 6x 6x 13x         13x 13x   13x                                 13x   13x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 13x     13x 6x         13x       13x 13x     13x                             13x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x 1x     13x 4x 3x 3x 3x 3x 3x 4x 1x 1x     4x           4x 4x   4x 4x 4x 4x   13x         13x 2x 2x 2x   2x 2x 2x 2x   2x 2x 2x 2x       13x 1x                                   1x 1x 1x 1x   13x   13x 13x   13x                       13x 13x 13x 13x 13x 13x   13x     13x 1x 1x 1x 1x 1x 1x 1x   1x             1x 1x 1x 1x 1x 1x 1x   1x 1x     12x   12x 12x   12x 12x 12x 12x   12x 12x 12x     12x 12x 12x 12x   12x 12x 12x 12x     12x 5x 5x 5x 5x 40x 40x 40x 40x 40x   40x 40x 5x 5x 5x   12x   12x 12x   13x 13x   13x   1x  
import { useEffect, useState } from 'react';
import styles from './RoleSelect.module.css';
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import { useWebSocket } from '../../contexts/WebSocketContext';
import { HIPPO_COLORS } from '../../config/hippoColors';  
import { EventBus } from '../../game/EventBus';
import { updatePlayerInSessionStorage } from '../../components/Storage/Storage';
 
/**
 * RoleSelect - React component for selecting a player's role in the game.
 *
 * <p>Purpose:
 * - Allows the user to choose between two roles: "Hippo Player" or "AAC User".
 * - Prevents navigation to the next game screen until a role is selected.
 * - Highlights the selection dropdown in red if the user attempts to proceed without selecting a role.
 * - Navigates to the game page with the session ID from the URL when valid input is provided.
 *
 * <p>Data Fields:
 * - role: string
 *   - The selected role by the user.
 *   - Empty by default until the user makes a choice.
 *
 * - error: boolean
 *   - True when no role is selected and the user attempts to proceed.
 *   - Used to trigger UI feedback like red border styling.
 *
 * - sessionId: string | undefined
 *   - Retrieved from the route parameters using `useParams()`.
 *   - Represents the unique session identifier needed for routing to the game page.
 *
 * <p>Methods:
 * - handleStart(): void
 *   - Purpose: Validates the role selection and navigates to the game page using the session ID.
 *   - Pre-condition: A role must be selected and sessionId must be defined.
 *   - Post-condition: Navigates to `/gamepage/{sessionId}` if role is valid.
 *   - Parameters: None
 *   - Return value: void
 *   - Exceptions: None explicitly thrown, but `sessionId` may be undefined.
 *     Recovery: Navigation is blocked until valid data is provided.
 *
 * - handleRoleChange(e: React.ChangeEvent<HTMLSelectElement>): void
 *   - Purpose: Updates role state when the user changes their selection.
 *   - Pre-condition: User selects a value from the dropdown.
 *   - Post-condition: Updates internal `role` state; clears error if one was previously set.
 *   - Parameters:
 *     - e: React.ChangeEvent<HTMLSelectElement> — the DOM event triggered by changing the selection.
 *   - Return value: void
 *   - Exceptions: None
 *
 * <p>UI Behavior:
 * - If `role` is not selected, the dropdown will receive a red border and an alert will be shown.
 * - ButtonClick component triggers `handleStart` on click.
 */
 
function playAudio(src: string) {
  const audio = new Audio(src);
  audio.play().catch(error => {
    console.error('Error playing audio:', error);
  });
}
 
function RoleSelect() {
  const navigate = useNavigate();
  const { sessionId } = useParams<{ sessionId: string }>();
  const location = useLocation();
 
  const generateUsername = () => {
    const num = Math.floor(Math.random() * 1000);
    return `User${String(num).padStart(3, '0')}`;
  };
 
  const [role, setRole] = useState<string>(''); 
  const [selectedColor, setSelectedColor] = useState<string | null>(null);
  const [username] = useState(location.state?.userId || generateUsername());
  const [waiting, setWaiting] = useState(false);
  const [showClosedSession, setShowClosedSession] = useState(false);
  const { connectedUsers, gameStarted, sendMessage, isConnected, lastMessage, clearLastMessage } = useWebSocket();
 
  // State to track colors already taken by connected users
  const [takenColors, setTakenColors] = useState<string[]>([]);
 
  // Count connected users by role
  const HIPPO_PLAYER_LIMIT = 4;
  const AAC_USER_LIMIT = 1;
  const hippoPlayersCount = connectedUsers.filter(user => user.role === 'Hippo Player').length;
  const aacUsersCount = connectedUsers.filter(user => user.role === 'AAC User').length;
  const isHippoRoleFull = hippoPlayersCount >= HIPPO_PLAYER_LIMIT;
  const isAacRoleFull = aacUsersCount >= AAC_USER_LIMIT;
 
  // --- ERROR HANDLING ---
  useEffect(() => {
    if (lastMessage?.type !== 'ERROR_MESSAGE') return;
    const errorCode = lastMessage.payload.code;
    const errorMessage = lastMessage.payload.message;
    // Handle specific error codes
    switch (errorCode) {
      case 'SESSION_NOT_FOUND':
        alert(`An error occurred: ${errorMessage}`);
        navigate('/');
        break;
      default :
        alert(`An unexpected error occurred: ${errorMessage}`);
        navigate('/');
        break;
    }
    clearLastMessage?.();
  }, [lastMessage, navigate, clearLastMessage]);
 
  // If presenter closes the session before game starts, go back to main page
  useEffect(() => {
    if (lastMessage?.type === 'SESSION_CLOSED') {
        setShowClosedSession(true);
        clearLastMessage?.();
    }
  }, [lastMessage, clearLastMessage]);
 
  // Play audio when the component mounts
  useEffect(() => {
    playAudio('/audio/role-select/select-your-role.mp3');
  }, []);
 
  // Effect to handle WebSocket connection and game resetting
  useEffect(() => {
    const handleResetGame = () => {
      console.log('[RoleSelect] RESET_GAME received - entering waiting state');
      setWaiting(true);
    };
 
    EventBus.on('RESET_GAME', handleResetGame);
 
    return () => {
      EventBus.off('RESET_GAME', handleResetGame);
    };
  }, []);
 
  // Navigate to the next page depending on the selected role.
  // and pass ALL player info in the state for the next page to use.
  // Effect to navigate when gameStarted becomes true
  useEffect(() => {
    if (!waiting) return; // Only navigate if waiting is true (after Next clicked)
 
    if (gameStarted) {
      console.log('[RoleSelect] Game started, navigating...');
      if (role === 'AAC User') {
        navigate(`/aac/${sessionId}/${username}/${role}`, {
          state: { userId: username, role },
        });
      } else if (role === 'Hippo Player') {
        navigate(`/hippo/${sessionId}/${username}/${role}`, {
          state: { userId: username, role, color: selectedColor },
        });
      }
      else if (role === 'Spectator') {
        navigate(`/spectator/${sessionId}/${username}`, {
          state: { userId: username, role },
        });
      }
    }
  }, [gameStarted, waiting, role, selectedColor, sessionId, username, navigate]);
 
  useEffect(() => {
    if (sessionId && username && isConnected) {
      sendMessage({
        type: 'PLAYER_JOIN',
        payload: {
          sessionId,
          userId: username,
          role: 'pending',
        },
      });
      updatePlayerInSessionStorage(sessionId, { userId: username, role: 'pending' });
    }
  }, [sessionId, username, isConnected, sendMessage]);
 
  // Listen for updates on which colors are taken by other players
  useEffect(() => {
    if (lastMessage?.type === 'COLOR_UPDATE') {
      const newTakenColors = lastMessage.payload.takenColors || [];
      setTakenColors(newTakenColors);
      clearLastMessage?.();
    }
  }, [lastMessage, clearLastMessage]);
 
 
  // Reset role if AAC User is selected and the role is full
  useEffect(() => {
    if (!waiting && role === 'AAC User' && isAacRoleFull) {
      setRole('');
    }
  }, [connectedUsers, role, isAacRoleFull]);
  /**
   * Handles the logic for starting the game after a role is selected.
   *
   * <p>This function validates that a role has been selected. It then sends a POST request
   * to the server to update the user's role for the current session. Upon a successful
   * update, the user is navigated to the appropriate game page based on their role.
   *
   * <p>If no role is selected, an error state is triggered and the function exits early.
   * If the server fails to update the role, an alert is shown to the user.
   *
   * @async
   * @function handleStart
   * @returns {Promise<void>} Resolves after role is updated and user is navigated to next page.
   */
  const handleStart = () => {
    if (!role && !selectedColor) {
        return;
    }
 
    playAudio('/audio/role-select/waiting-for-game-to-start.mp3');
    sendMessage({
        type: 'PLAYER_JOIN',
        payload: {
          sessionId,
          userId: username,
          role,
          color: selectedColor,
        },
    });
 
    if (sessionId) {
      updatePlayerInSessionStorage(sessionId, { userId: username, role, color: selectedColor });
    }
 
    setWaiting(true);
  };
 
  // Handle role selection
  const handleRoleSelect = (selectedRole: string) => {
    if (selectedRole === 'Hippo Player') {
      playAudio('/audio/role-select/hippo-player.mp3');
      sendMessage({
        type: 'REQUEST_COLOR_UPDATE',
        payload: { sessionId }
      });
    } else if (selectedRole === 'AAC User') {
      playAudio('/audio/role-select/aac-user.mp3');
    }
 
    // If switching from Hippo Player to AAC User, release the color
    if (role === 'Hippo Player' && selectedColor) {
      sendMessage({
        type: 'SELECT_COLOR',
        payload: { sessionId, userId: username, color: null }
      });
    }
    setRole(selectedRole);
    setSelectedColor(null);
 
    if (sessionId) {
      updatePlayerInSessionStorage(sessionId, { userId: username, role: selectedRole, color: null });
    }
  };
 
  const handleCancel = () => {
    navigate('/');
  };
 
  // Handle color selection for Hippo Player
  const handleColorSelect = (hippo: { color: string, audioSrc: string }) => {
    setSelectedColor(hippo.color);
    const audio = new Audio(hippo.audioSrc);
    audio.play();
 
    sendMessage({
        type: 'SELECT_COLOR',
        payload: { sessionId, userId: username, color: hippo.color }
    });
 
    if (sessionId) {
      updatePlayerInSessionStorage(sessionId, { userId: username, role, color: hippo.color });
    }
  };
 
  // Handle going back or canceling
  // If we're on the "Waiting..." screen, go back to role selection.
  const handleGoBackOrCancel = () => {
    if (waiting) {
      setWaiting(false);
      setRole('');
      setSelectedColor(null);
      sendMessage({
        type: 'PLAYER_JOIN',
        payload: {
          sessionId,
          userId: username,
          role: 'pending',
          color: null
        },
      });
 
      sendMessage({
        type: 'REQUEST_COLOR_UPDATE',
        payload: { sessionId }
      });
    } else {
      navigate('/');
    }
};
 
  const isNextDisabled = !role || (role === 'Hippo Player' && !selectedColor)
 
  return (
    <div className={styles.containerImg}>
      {/* Popup for session closed by host */}
      {showClosedSession && (
        <div className={styles.closedSessionOverlay}>
          <div className={styles.closedSessionContent}>
            <button onClick={handleCancel} className={styles.closedSessionCloseButton}>✖</button>
            <h3>Session Closed</h3>
            <p>The host has left the lobby.</p>
            <button onClick={handleCancel} className={styles.closedSessionOkButton}>
                OK
            </button>
        </div>
      </div>
      )}
      <div className={styles.roleContainer}>
        <button
          className={styles.closeButton}
          onClick={handleGoBackOrCancel}
          aria-label="Close"
        >
          ✖
        </button>
 
        {/* Waiting Screen */}
        {waiting ? (
          <div className={styles.waitingContainer}>
            {role === 'Hippo Player' && (
              <img
                src="/assets/mode/easyCompress.gif"
                alt="How to play Hippo Player"
                className={styles.instructionGif}
              />
            )}
            {role === 'AAC User' && (
              <img
                src="/assets/mode/aacCompress.gif"
                alt="How to play AAC User"
                className={styles.instructionGif}
              />
            )}
            <div className={styles.loadingSection}>
              <img
                src="/assets/load.gif"
                alt="Loading..."
                className={styles.loadingSpinner}
              />
              <h2 className={styles.waitingText}>Waiting . . .</h2>
 
            </div>
          </div>
 
        ) : (
          <>
            {/* Role Selection Header */}
            <h3 className={styles.selectRoleTitle}>Select Your Role</h3>
            <div className={styles.roleChoiceContainer}>
              {/* Hippo Player Choice Button */}
              <button 
                className={`${styles.roleChoiceButton} ${role === 'Hippo Player' ? styles.selected : ''}`}
                onClick={() => handleRoleSelect('Hippo Player')}
                disabled={isHippoRoleFull}
              >
                <img src="/assets/hippos/outlineHippo.png" alt="Hippo Player" className={styles.roleIcon} />
                <span>Hippo Player {isHippoRoleFull ? '(Full)' : ''}</span>
              </button>
 
              {/* AAC User Choice Button */}
              <button 
                className={`${styles.roleChoiceButton} ${role === 'AAC User' ? styles.selected : ''}`}
                onClick={() => handleRoleSelect('AAC User')}
                disabled={isAacRoleFull}
              >
                <img src="/assets/aacDevice.png" alt="AAC User" className={styles.roleIcon} />
                <span>AAC User {isAacRoleFull ? '(Full)' : ''}</span>
              </button>
            </div>
 
            {/* Color Selection for Hippo Player */}
            {role === 'Hippo Player' && (
              <div className={styles.colorSelectionContainer}>
                  <h3 className={styles.selectColorTitle}>Choose your Hippo</h3>
                  <div className={styles.hippoGrid}>
                      {HIPPO_COLORS.map((hippo) => (
                          <button
                              key={hippo.color}
                              className={`${styles.colorButton} ${selectedColor === hippo.color ? styles.selected : ''}`}
                              disabled={takenColors.includes(hippo.color) && hippo.color !== selectedColor}
                              onClick={() => handleColorSelect(hippo)}
                          >
                              <img src={hippo.imgSrc} alt={hippo.color} className={styles.hippoImage} />
                          </button>
                      ))}
                  </div>
              </div>
            )}
            <button className={styles.nextButton} onClick={handleStart} disabled={isNextDisabled}>
              Next
            </button>
          </>
        )}
      </div>
    </div>
  );
}
 
export default RoleSelect;