All files / components/combat/components MatchCountdown.tsx

74.07% Statements 40/54
83.92% Branches 47/56
64.28% Functions 9/14
75% Lines 39/52

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                                                                                            10x   8x   1x   1x                                           3x             3x           10x     10x       10x     10x 10x 10x 10x         10x 10x   10x   1x 9x   1x         10x           10x     10x 10x 1x   9x     10x 10x 1x   9x     10x 10x     10x 10x 10x     10x 10x         10x 1x     9x                                                                                                                                                                                                                                                                                                                                    
/**
 * MatchCountdown Component - Displays match start countdown sequence
 *
 * Korean: 매치 시작 카운트다운 (Match Start Countdown)
 *
 * Shows "Ready?" → "3... 2... 1..." → "Fight!" sequence with animations.
 * Implements Korean cyberpunk aesthetic with bilingual text support.
 * Plays audio cues for countdown and fight announcement.
 *
 * @module components/combat/MatchCountdown
 * @category Combat UI
 */
 
import React, { useEffect, useMemo, useRef } from "react";
import { useAudio } from "../../../audio/AudioProvider";
import {
  MatchCountdownState,
  useMatchCountdown,
} from "../../../hooks/useMatchCountdown";
import { FONT_FAMILY, KOREAN_COLORS } from "../../../types/constants";
import { hexColorToCSS } from "../../../utils/colorUtils";
 
/**
 * Props for the MatchCountdown component
 */
export interface MatchCountdownProps {
  /** Callback when countdown completes */
  readonly onComplete: () => void;
  /** Whether layout should adapt for mobile screens */
  readonly isMobile: boolean;
  /** Optional callback for skip button */
  readonly onSkip?: () => void;
  /** Whether skip button should be shown */
  readonly showSkip?: boolean;
}
 
/**
 * Get display text for current countdown state
 */
function getDisplayText(
  state: MatchCountdownState,
  currentNumber: number
): {
  ko: string;
  en: string;
} | null {
  switch (state) {
    case "ready":
      return { ko: "준비?", en: "Ready?" };
    case "counting":
      return { ko: String(currentNumber), en: String(currentNumber) };
    case "fight":
      return { ko: "전투!", en: "Fight!" };
    default:
      return null;
  }
}
 
/**
 * MatchCountdown Component
 *
 * Displays match start countdown with:
 * - "Ready?" announcement (1 second)
 * - Countdown from 3 to 1 (1 second intervals)
 * - "Fight!" announcement (1 second)
 * - Pulse/scale animations for emphasis
 * - Bilingual Korean-English text
 * - Audio cues for countdown and fight
 * - Optional skip button
 * - Responsive sizing for mobile/tablet/desktop
 *
 * Korean: 매치 시작 카운트다운 컴포넌트
 */
// Static config outside component to prevent re-creation on each render
const COUNTDOWN_CONFIG = {
  readyDuration: 1,
  countdownInterval: 1,
  fightDuration: 1,
  startNumber: 3,
} as const;
 
export const MatchCountdown: React.FC<MatchCountdownProps> = ({
  onComplete,
  isMobile,
  onSkip,
  showSkip = false,
}) => {
  const audio = useAudio();
 
  // Use match countdown hook with stable config reference
  const { state, currentNumber, startCountdown, skipCountdown, isActive } =
    useMatchCountdown(COUNTDOWN_CONFIG, onComplete);
 
  // Track if we've started to avoid double-start in Strict Mode
  const hasStartedRef = useRef(false);
 
  // Auto-start countdown on mount (only once)
  useEffect(() => {
    Eif (!hasStartedRef.current) {
      hasStartedRef.current = true;
      startCountdown();
    }
  }, [startCountdown]);
 
  // Play audio cues based on state transitions
  useEffect(() => {
    Iif (!audio.isAudioReady) return;
 
    if (state === "counting" && currentNumber > 0) {
      // Play beep for each countdown number
      audio.playSFX("attack_light"); // Using placeholder - will be countdown_beep
    } else if (state === "fight") {
      // Play fight announcement
      audio.playSFX("attack_heavy"); // Using placeholder - will be fight_start
    }
  }, [state, currentNumber, audio]);
 
  // Handle skip
  const handleSkip = () => {
    skipCountdown();
    onSkip?.();
  };
 
  // Get display text
  const displayText = getDisplayText(state, currentNumber);
 
  // Calculate responsive font sizes - extracted to avoid nested ternaries
  const getMainFontSize = (): string => {
    if (isMobile) {
      return state === "fight" ? "72px" : "64px";
    }
    return state === "fight" ? "120px" : "96px";
  };
 
  const getSubFontSize = (): string => {
    if (isMobile) {
      return state === "fight" ? "48px" : "40px";
    }
    return state === "fight" ? "72px" : "56px";
  };
 
  const mainFontSize = getMainFontSize();
  const subFontSize = getSubFontSize();
 
  // Convert hex colors to CSS - memoized for performance
  const goldColor = useMemo(() => hexColorToCSS(KOREAN_COLORS.ACCENT_GOLD), []);
  const cyanColor = useMemo(
    () => hexColorToCSS(KOREAN_COLORS.PRIMARY_CYAN),
    []
  );
  const darkBg = useMemo(
    () => hexColorToCSS(KOREAN_COLORS.UI_BACKGROUND_DARK),
    []
  );
 
  // Don't render if countdown not active or complete
  if (!isActive || !displayText) {
    return null;
  }
 
  return (
    <>
      <div
        data-testid="match-countdown"
        role="dialog"
        aria-modal="true"
        aria-label="Match countdown in progress"
        aria-live="assertive"
        style={{
          position: "fixed",
          top: 0,
          left: 0,
          width: "100%",
          height: "100%",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          backgroundColor: state === "fight" ? `${darkBg}cc` : `${darkBg}ee`,
          zIndex: 1000,
          animation: state === "ready" ? "fadeIn 0.3s ease-in" : "none",
        }}
      >
        <div
          style={{
            fontSize: mainFontSize,
            fontWeight: "bold",
            color: state === "fight" ? goldColor : cyanColor,
            fontFamily: FONT_FAMILY.KOREAN,
            textShadow: `0 0 ${state === "fight" ? "40px" : "30px"} ${
              state === "fight" ? goldColor : cyanColor
            }`,
            animation:
              state === "ready"
                ? "pulse 0.8s ease-out"
                : state === "counting"
                ? "countdownPulse 0.8s ease-out"
                : state === "fight"
                ? "flash 0.3s ease-out"
                : "none",
            textAlign: "center",
            userSelect: "none",
          }}
          data-testid="countdown-text"
        >
          {displayText.ko}
          <br />
          <span
            style={{
              fontSize: subFontSize,
            }}
          >
            {displayText.en}
          </span>
        </div>
 
        {/* Optional Skip Button */}
        {showSkip && state !== "fight" && (
          <button
            onClick={handleSkip}
            data-testid="skip-countdown-button"
            aria-label="Skip countdown"
            style={{
              position: "absolute",
              bottom: isMobile ? "40px" : "60px",
              padding: isMobile ? "10px 24px" : "12px 32px",
              fontSize: isMobile ? "14px" : "16px",
              backgroundColor: cyanColor,
              color: darkBg,
              border: "none",
              borderRadius: "6px",
              fontFamily: FONT_FAMILY.KOREAN,
              fontWeight: "bold",
              cursor: "pointer",
              transition: "all 0.2s ease",
            }}
            onMouseEnter={(e) => {
              e.currentTarget.style.transform = "scale(1.05)";
              e.currentTarget.style.boxShadow = `0 0 20px ${cyanColor}`;
            }}
            onMouseLeave={(e) => {
              e.currentTarget.style.transform = "scale(1)";
              e.currentTarget.style.boxShadow = "none";
            }}
            onFocus={(e) => {
              e.currentTarget.style.transform = "scale(1.05)";
              e.currentTarget.style.boxShadow = `0 0 20px ${cyanColor}`;
              e.currentTarget.style.outline = `2px solid ${cyanColor}`;
            }}
            onBlur={(e) => {
              e.currentTarget.style.transform = "scale(1)";
              e.currentTarget.style.boxShadow = "none";
              e.currentTarget.style.outline = "none";
            }}
          >
            건너뛰기 | Skip
          </button>
        )}
      </div>
 
      {/* CSS Animations */}
      <style>
        {`
          @keyframes fadeIn {
            from {
              opacity: 0;
            }
            to {
              opacity: 1;
            }
          }
 
          @keyframes pulse {
            0% {
              opacity: 0;
              transform: scale(0.9);
            }
            50% {
              opacity: 1;
              transform: scale(1.05);
            }
            100% {
              opacity: 1;
              transform: scale(1);
            }
          }
 
          @keyframes countdownPulse {
            0% {
              opacity: 0;
              transform: scale(1.2);
            }
            50% {
              opacity: 1;
              transform: scale(1.1);
            }
            100% {
              opacity: 1;
              transform: scale(1);
            }
          }
 
          @keyframes flash {
            0% {
              opacity: 0;
              transform: scale(1.5);
            }
            30% {
              opacity: 1;
              transform: scale(1.3);
            }
            100% {
              opacity: 1;
              transform: scale(1);
            }
          }
        `}
      </style>
    </>
  );
};
 
export default MatchCountdown;