All files / components/screens/endscreen/components PerformanceBreakdownOverlayHtml.tsx

100% Statements 27/27
88.37% Branches 38/43
100% Functions 7/7
100% Lines 26/26

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                              478x                           2x               80x 80x   80x                                                                                                                 20x           20x 20x     20x             20x         20x   20x             2x         20x 20x 20x   20x 20x       20x 20x         20x 20x 20x 1x   19x     20x                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
import React, { useMemo } from "react";
import { MatchStatistics } from "../../../../systems/combat";
import { PlayerMatchStats } from "../../../../systems/player";
import { FONT_FAMILY, KOREAN_COLORS } from "../../../../types/constants";
import { hexToRgbaString } from "../../../../utils/colorUtils";
 
export interface PerformanceBreakdownProps {
  readonly matchStats: MatchStatistics;
  readonly isMobile: boolean;
  readonly isTablet: boolean;
}
 
/**
 * Helper to convert hex color to CSS string
 */
const toCssColor = (hex: number): string => hexToRgbaString(hex, 1);
 
/**
 * Category rating component
 */
interface CategoryRatingProps {
  readonly category: string;
  readonly korean: string;
  readonly value: number;
  readonly maxValue: number;
  readonly color: number;
  readonly fontSize: number;
}
 
const CategoryRating: React.FC<CategoryRatingProps> = ({
  category,
  korean,
  value,
  maxValue,
  color,
  fontSize,
}) => {
  const percentage = Math.round((value / maxValue) * 100);
  const grade = percentage >= 90 ? "S" : percentage >= 75 ? "A" : percentage >= 60 ? "B" : "C";
 
  return (
    <div
      style={{
        display: "flex",
        flexDirection: "column",
        gap: "6px",
      }}
      data-testid={`category-${category.toLowerCase()}`}
    >
      <div
        style={{
          display: "flex",
          justifyContent: "space-between",
          alignItems: "center",
          fontSize: fontSize - 2,
          color: toCssColor(KOREAN_COLORS.TEXT_SECONDARY),
        }}
      >
        <span>{korean} | {category}</span>
        <span
          style={{
            fontWeight: "bold",
            color: toCssColor(color),
            fontSize: fontSize + 2,
          }}
        >
          {grade}
        </span>
      </div>
      <div
        style={{
          width: "100%",
          height: "8px",
          background: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_MEDIUM, 0.5),
          borderRadius: "4px",
          overflow: "hidden",
        }}
      >
        <div
          style={{
            width: `${percentage}%`,
            height: "100%",
            background: toCssColor(color),
            transition: "width 0.5s ease-out",
          }}
          data-testid={`progress-${category.toLowerCase()}`}
        />
      </div>
    </div>
  );
};
 
/**
 * Calculate category scores from match statistics
 */
function calculateCategoryScores(stats: PlayerMatchStats) {
  // Offense: Based on damage dealt and hits landed
  const offense = Math.min(
    (stats.totalDamageDealt / 100) * 50 + (stats.hitsLanded / 10) * 50,
    100
  );
 
  // Defense: Based on damage received (inverted) and hits taken (inverted)
  const defenseRaw = Math.max(0, 100 - stats.totalDamageReceived / 2);
  const defense = Math.min(defenseRaw, 100);
 
  // Technique: Based on perfect strikes and vital point hits
  const technique = Math.min(
    stats.perfectStrikes * 15 + stats.vitalPointHits * 10,
    100
  );
 
  // Efficiency: Based on damage ratio
  const damageRatio =
    stats.totalDamageReceived > 0
      ? stats.totalDamageDealt / stats.totalDamageReceived
      : stats.totalDamageDealt > 0
      ? 2
      : 0;
  const efficiency = Math.min(damageRatio * 40, 100);
 
  return { offense, defense, technique, efficiency };
}
 
/**
 * Performance Breakdown Component
 * Provides detailed analysis of combat performance by category
 */
export const PerformanceBreakdown: React.FC<PerformanceBreakdownProps> = ({
  matchStats,
  isMobile,
  isTablet,
}) => {
  const fontSize = isMobile ? 12 : isTablet ? 13 : 14;
  const labelFontSize = isMobile ? 14 : isTablet ? 16 : 18;
  const padding = isMobile ? 12 : isTablet ? 15 : 18;
 
  const winnerStats = useMemo(
    () => (matchStats.winner === 0 ? matchStats.player1 : matchStats.player2),
    [matchStats]
  );
 
  const scores = useMemo(
    () => calculateCategoryScores(winnerStats),
    [winnerStats]
  );
 
  // Technique breakdown
  const techniqueCount = winnerStats.techniques?.length ?? 0;
  const uniqueTechniques = useMemo(() => {
    if (!winnerStats.techniques || winnerStats.techniques.length === 0) {
      return 0;
    }
    return new Set(winnerStats.techniques).size;
  }, [winnerStats.techniques]);
 
  return (
    <div
      data-testid="performance-breakdown"
      style={{
        width: isMobile ? "95%" : isTablet ? "80%" : "70%",
        maxWidth: "900px",
        background: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_DARK, 0.9),
        border: `2px solid ${hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.6)}`,
        borderRadius: "12px",
        padding: padding * 1.5,
        marginBottom: padding,
        fontFamily: FONT_FAMILY.KOREAN,
      }}
    >
      {/* Title */}
      <div
        style={{
          fontSize: labelFontSize + 2,
          fontWeight: "bold",
          color: toCssColor(KOREAN_COLORS.PRIMARY_CYAN),
          textAlign: "center",
          marginBottom: padding * 1.5,
        }}
        data-testid="breakdown-title"
      >
        전투 분석 | Performance Breakdown
      </div>
 
      {/* Category Ratings */}
      <div
        style={{
          display: "flex",
          flexDirection: "column",
          gap: padding,
          marginBottom: padding * 1.5,
        }}
        data-testid="category-ratings"
      >
        <CategoryRating
          category="Offense"
          korean="공격"
          value={scores.offense}
          maxValue={100}
          color={KOREAN_COLORS.ACCENT_RED}
          fontSize={fontSize}
        />
        <CategoryRating
          category="Defense"
          korean="방어"
          value={scores.defense}
          maxValue={100}
          color={KOREAN_COLORS.ACCENT_BLUE}
          fontSize={fontSize}
        />
        <CategoryRating
          category="Technique"
          korean="기술"
          value={scores.technique}
          maxValue={100}
          color={KOREAN_COLORS.ACCENT_GOLD}
          fontSize={fontSize}
        />
        <CategoryRating
          category="Efficiency"
          korean="효율"
          value={scores.efficiency}
          maxValue={100}
          color={KOREAN_COLORS.PRIMARY_CYAN}
          fontSize={fontSize}
        />
      </div>
 
      {/* Technique Analysis */}
      <div
        style={{
          padding,
          background: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_MEDIUM, 0.4),
          borderRadius: "8px",
        }}
        data-testid="technique-analysis"
      >
        <div
          style={{
            fontSize: labelFontSize,
            fontWeight: "bold",
            color: toCssColor(KOREAN_COLORS.ACCENT_GOLD),
            marginBottom: padding / 2,
            textAlign: "center",
          }}
        >
          기술 사용 분석 | Technique Analysis
        </div>
 
        <div
          style={{
            display: "grid",
            gridTemplateColumns: isMobile ? "1fr" : "1fr 1fr 1fr",
            gap: padding,
            marginTop: padding,
          }}
        >
          <div style={{ textAlign: "center" }}>
            <div
              style={{
                fontSize: fontSize + 6,
                fontWeight: "bold",
                color: toCssColor(KOREAN_COLORS.PRIMARY_CYAN),
              }}
            >
              {techniqueCount}
            </div>
            <div
              style={{
                fontSize: fontSize - 2,
                color: toCssColor(KOREAN_COLORS.TEXT_TERTIARY),
              }}
            >
              총 기술 | Total Uses
            </div>
          </div>
 
          <div style={{ textAlign: "center" }}>
            <div
              style={{
                fontSize: fontSize + 6,
                fontWeight: "bold",
                color: toCssColor(KOREAN_COLORS.ACCENT_GOLD),
              }}
            >
              {uniqueTechniques}
            </div>
            <div
              style={{
                fontSize: fontSize - 2,
                color: toCssColor(KOREAN_COLORS.TEXT_TERTIARY),
              }}
            >
              고유 기술 | Unique
            </div>
          </div>
 
          <div style={{ textAlign: "center" }}>
            <div
              style={{
                fontSize: fontSize + 6,
                fontWeight: "bold",
                color: toCssColor(KOREAN_COLORS.VITAL_POINT_HIT),
              }}
            >
              {winnerStats.vitalPointHits ?? 0}
            </div>
            <div
              style={{
                fontSize: fontSize - 2,
                color: toCssColor(KOREAN_COLORS.TEXT_TERTIARY),
              }}
            >
              급소 타격 | Vital Hits
            </div>
          </div>
        </div>
 
        {/* Most Used Techniques */}
        {winnerStats.techniques && winnerStats.techniques.length > 0 && (
          <div
            style={{
              marginTop: padding,
              paddingTop: padding,
              borderTop: `1px solid ${hexToRgbaString(
                KOREAN_COLORS.UI_BORDER,
                0.3
              )}`,
            }}
          >
            <div
              style={{
                fontSize: fontSize - 2,
                color: toCssColor(KOREAN_COLORS.TEXT_TERTIARY),
                marginBottom: padding / 2,
              }}
            >
              주요 기술 | Primary Techniques:
            </div>
            <div
              style={{
                fontSize: fontSize,
                color: toCssColor(KOREAN_COLORS.TEXT_SECONDARY),
                lineHeight: 1.6,
              }}
            >
              {winnerStats.techniques.slice(0, 5).join(", ")}
              {winnerStats.techniques.length > 5 && "..."}
            </div>
          </div>
        )}
      </div>
 
      {/* Combat Effectiveness Summary */}
      <div
        style={{
          marginTop: padding,
          padding,
          background: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_MEDIUM, 0.4),
          borderRadius: "8px",
          textAlign: "center",
        }}
        data-testid="effectiveness-summary"
      >
        <div
          style={{
            fontSize: fontSize - 2,
            color: toCssColor(KOREAN_COLORS.TEXT_TERTIARY),
            marginBottom: padding / 4,
          }}
        >
          전투 효율성 | Combat Effectiveness
        </div>
        <div
          style={{
            fontSize: labelFontSize + 2,
            fontWeight: "bold",
            color: toCssColor(KOREAN_COLORS.ACCENT_GOLD),
          }}
        >
          {Math.round((scores.offense + scores.defense + scores.technique + scores.efficiency) / 4)}%
        </div>
      </div>
    </div>
  );
};