All files / components/ui/combat ComboCounter.tsx

97.22% Statements 35/36
100% Branches 37/37
90% Functions 9/10
100% Lines 32/32

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                                                                                              1x       24x   24x 20x 4x 2x   2x               1x 24x   18x   2x   4x                                                                                                 1x               48x   48x 26x 24x     48x 24x       48x 26x 24x       48x 26x 24x 24x 24x       48x 24x       48x 2x     46x                                                                                                                                                                                                                                                                                                                                
/**
 * ComboCounter - Combo count and timing window display
 *
 * Shows current combo count and visual timing indicator for combo continuation.
 * Korean/English bilingual with Korean cyberpunk theming. Displays optimal
 * timing window for chaining techniques.
 *
 * Features:
 * - Bilingual labels: "연속 공격 | Combo"
 * - Combo count display with multiplier
 * - Visual timing window indicator
 * - Color-coded timing feedback (perfect/good/miss)
 * - Korean cyberpunk aesthetic
 * - WCAG AA contrast compliance
 * - Mobile-optimized sizing
 *
 * @module components/ui/combat/ComboCounter
 * @category Combat UI
 * @korean 콤보카운터 - 연속 공격 카운터 및 타이밍 표시기
 */
 
import React, { useMemo, useEffect, useState } from "react";
import { KOREAN_COLORS } from "../../../types/constants"; // eslint-disable-line no-restricted-imports -- This UI component directly uses color constants
import { hexToRgbaString } from "../../../utils/colorUtils";
 
/**
 * Props for ComboCounter component
 */
export interface ComboCounterProps {
  /** Current combo count */
  readonly comboCount: number;
  /** Timing window for next technique (milliseconds) */
  readonly comboWindow?: number;
  /** Time elapsed since last technique (milliseconds) */
  readonly timeSinceLastHit?: number;
  /** Whether combo is currently active */
  readonly isActive: boolean;
  /** Whether to show on mobile devices */
  readonly showOnMobile?: boolean;
  /** Additional CSS classes */
  readonly className?: string;
}
 
/**
 * Get timing quality based on elapsed time and window
 * 타이밍 품질 판정
 */
const getTimingQuality = (
  elapsed: number,
  window: number
): "perfect" | "good" | "miss" => {
  const ratio = elapsed / window;
  
  if (ratio <= 0.5) {
    return "perfect"; // First half of window
  } else if (ratio <= 1.0) {
    return "good"; // Second half of window
  } else {
    return "miss"; // Window expired
  }
};
 
/**
 * Get timing color based on quality
 * 타이밍에 따른 색상 결정
 */
const getTimingColor = (quality: "perfect" | "good" | "miss"): string => {
  switch (quality) {
    case "perfect":
      return `#${KOREAN_COLORS.ACCENT_GREEN.toString(16).padStart(6, "0")}`;
    case "good":
      return `#${KOREAN_COLORS.ACCENT_YELLOW.toString(16).padStart(6, "0")}`;
    case "miss":
      return `#${KOREAN_COLORS.ACCENT_RED.toString(16).padStart(6, "0")}`;
  }
};
 
/**
 * ComboCounter Component
 *
 * Displays combo count and timing window for Son (Wind) stance combo chains.
 * Provides visual feedback for optimal technique timing within combo flow.
 *
 * @example
 * ```tsx
 * const [comboState, setComboState] = useState({
 *   count: 0,
 *   lastHitTime: 0,
 *   window: 200,
 * });
 *
 * // On technique hit
 * const handleTechniqueHit = (technique: TrigramStanceTechnique) => {
 *   const now = Date.now();
 *   const elapsed = now - comboState.lastHitTime;
 *   
 *   if (elapsed <= comboState.window) {
 *     // Combo continues
 *     setComboState({
 *       count: comboState.count + 1,
 *       lastHitTime: now,
 *       window: technique.comboWindow || 200,
 *     });
 *   } else {
 *     // Combo resets
 *     setComboState({
 *       count: 1,
 *       lastHitTime: now,
 *       window: technique.comboWindow || 200,
 *     });
 *   }
 * };
 *
 * <ComboCounter
 *   comboCount={comboState.count}
 *   comboWindow={comboState.window}
 *   timeSinceLastHit={Date.now() - comboState.lastHitTime}
 *   isActive={comboState.count > 0}
 *   showOnMobile={true}
 * />
 * ```
 */
export const ComboCounter: React.FC<ComboCounterProps> = ({
  comboCount,
  comboWindow = 200,
  timeSinceLastHit = 0,
  isActive,
  showOnMobile = true,
  className = "",
}) => {
  const [animateCount, setAnimateCount] = useState(false);
 
  const timingQuality = useMemo(() => {
    if (!isActive || comboCount === 0) return "miss";
    return getTimingQuality(timeSinceLastHit, comboWindow);
  }, [isActive, comboCount, timeSinceLastHit, comboWindow]);
 
  const timingColor = useMemo(
    () => getTimingColor(timingQuality),
    [timingQuality]
  );
 
  const windowProgress = useMemo(() => {
    if (!isActive || comboCount === 0) return 0;
    return Math.min((timeSinceLastHit / comboWindow) * 100, 100);
  }, [isActive, comboCount, timeSinceLastHit, comboWindow]);
 
  // Animate count on change
  useEffect(() => {
    if (comboCount > 0) {
      setAnimateCount(true);
      const timer = setTimeout(() => setAnimateCount(false), 300);
      return () => clearTimeout(timer);
    }
  }, [comboCount]);
 
  const glowColor = useMemo(
    () => hexToRgbaString(parseInt(timingColor.slice(1), 16), 0.6),
    [timingColor]
  );
 
  if (!isActive && comboCount === 0) {
    return null; // Hide when not active
  }
 
  return (
    <div
      className={`combo-counter${className ? ` ${className}` : ""}${!showOnMobile ? " hidden-mobile" : ""}`}
      style={{
        display: "flex",
        flexDirection: "column",
        gap: "10px",
        padding: "14px 18px",
        backgroundColor: `#${KOREAN_COLORS.UI_BACKGROUND_MEDIUM.toString(16).padStart(6, "0")}`,
        border: `2px solid ${timingColor}`,
        borderRadius: "8px",
        minWidth: "220px",
        boxShadow: `0 0 16px ${glowColor}, inset 0 0 10px rgba(0, 0, 0, 0.5)`,
        transition: "border-color 0.2s ease, box-shadow 0.2s ease",
      }}
      role="status"
      aria-live="polite"
      aria-label={`Combo: ${comboCount} hits`}
    >
      {/* Bilingual Label and Count */}
      <div
        style={{
          display: "flex",
          justifyContent: "space-between",
          alignItems: "center",
        }}
      >
        <span
          style={{
            fontSize: "14px",
            fontWeight: 600,
            color: `#${KOREAN_COLORS.TEXT_PRIMARY.toString(16).padStart(6, "0")}`,
            fontFamily: "'Noto Sans KR', 'Nanum Gothic', sans-serif",
            letterSpacing: "0.5px",
          }}
        >
          연속 공격 | Combo
        </span>
        <span
          style={{
            fontSize: animateCount ? "28px" : "24px",
            fontWeight: 700,
            color: timingColor,
            fontFamily: "'Orbitron', 'Noto Sans KR', monospace",
            textShadow: `0 0 12px ${glowColor}`,
            transition: "font-size 0.15s ease, color 0.2s ease",
          }}
        >
          {comboCount}x
        </span>
      </div>
 
      {/* Timing Window Indicator */}
      <div
        style={{
          position: "relative",
          height: "28px",
          backgroundColor: `#${KOREAN_COLORS.UI_BACKGROUND_DARK.toString(16).padStart(6, "0")}`,
          borderRadius: "4px",
          overflow: "hidden",
          border: `1px solid #${KOREAN_COLORS.UI_BORDER.toString(16).padStart(6, "0")}`,
        }}
      >
        {/* Progress Fill */}
        <div
          style={{
            position: "absolute",
            left: 0,
            top: 0,
            height: "100%",
            width: `${windowProgress}%`,
            backgroundColor: timingColor,
            transition: "width 0.05s linear, background-color 0.2s ease",
            boxShadow: `inset 0 0 12px ${glowColor}`,
          }}
        />
 
        {/* Timing Label */}
        <div
          style={{
            position: "absolute",
            left: 0,
            top: 0,
            width: "100%",
            height: "100%",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            fontSize: "12px",
            fontWeight: 600,
            color: `#${KOREAN_COLORS.TEXT_PRIMARY.toString(16).padStart(6, "0")}`,
            fontFamily: "'Noto Sans KR', sans-serif",
            textShadow: "0 0 4px rgba(0, 0, 0, 0.8)",
            pointerEvents: "none",
          }}
        >
          {timingQuality === "perfect" && "완벽! | Perfect!"}
          {timingQuality === "good" && "좋음 | Good"}
          {timingQuality === "miss" && "놓침 | Missed"}
        </div>
 
        {/* Window Markers */}
        <div
          style={{
            position: "absolute",
            left: "50%",
            top: 0,
            width: "2px",
            height: "100%",
            backgroundColor: "rgba(255, 255, 255, 0.3)",
            transform: "translateX(-50%)",
          }}
        />
      </div>
 
      {/* Timing Window Info */}
      <div
        style={{
          fontSize: "11px",
          color: `#${KOREAN_COLORS.TEXT_SECONDARY.toString(16).padStart(6, "0")}`,
          fontFamily: "'Noto Sans KR', 'Nanum Gothic', sans-serif",
          textAlign: "center",
          opacity: 0.8,
        }}
      >
        타이밍 창: {comboWindow}ms | Window: {comboWindow}ms
      </div>
 
      <style>
        {`
          @media (max-width: 768px) {
            .combo-counter.hidden-mobile {
              display: none;
            }
            .combo-counter {
              min-width: 180px;
              padding: 12px 14px;
              gap: 8px;
            }
            .combo-counter > div:first-child span:first-child {
              font-size: 12px;
            }
            .combo-counter > div:first-child span:last-child {
              font-size: 20px;
            }
            .combo-counter > div:nth-child(2) {
              height: 24px;
            }
            .combo-counter > div:nth-child(2) > div:nth-child(2) {
              font-size: 11px;
            }
            .combo-counter > div:last-child {
              font-size: 10px;
            }
          }
        `}
      </style>
    </div>
  );
};