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

100% Statements 31/31
78.57% Branches 33/42
100% Functions 7/7
100% Lines 27/27

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                                      250x             23x       23x         23x         23x       23x 23x       23x     23x   23x                 23x 22x 13x 1x             2x         25x 25x 25x 25x   25x 23x       25x 23x       25x     25x 23x       25x                                                                                                                                                                                                                                                                                                                                                              
import React, { useMemo } from "react";
import { MatchStatistics } from "../../../../systems/combat";
import {
  FONT_FAMILY,
  KOREAN_COLORS,
  PERFORMANCE_RATING_THRESHOLDS,
} from "../../../../types/constants";
import { hexToRgbaString } from "../../../../utils/colorUtils";
import { pulseAnimation } from "./animations";
 
export interface PerformanceRatingProps {
  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);
 
/**
 * Calculate performance score based on match statistics
 * Score ranges from 0-100 based on combat effectiveness
 */
function calculatePerformanceScore(stats: MatchStatistics): number {
  const winnerStats = stats.winner === 0 ? stats.player1 : stats.player2;
 
  // Calculate accuracy based on offensive performance only
  // Use a normalized scale: higher hits landed = better accuracy
  const accuracy = Math.min((winnerStats.hitsLanded / 10) * 100, 100);
 
  // Calculate damage efficiency (damage dealt / damage taken ratio)
  // Perfect defense (no damage taken) gets bonus ratio
  const damageRatio =
    winnerStats.totalDamageReceived > 0
      ? winnerStats.totalDamageDealt / winnerStats.totalDamageReceived
      : winnerStats.totalDamageDealt > 0
      ? 2
      : 0;
  const damageScore = Math.min(damageRatio * 30, 30); // Max 30 points
 
  // Perfect strikes and vital point hits bonus
  const precisionBonus =
    winnerStats.perfectStrikes * 5 + winnerStats.vitalPointHits * 3;
  const precisionScore = Math.min(precisionBonus, 25); // Max 25 points
 
  // Speed bonus (shorter match duration is better)
  const speedScore =
    stats.matchDuration < 60 ? 15 : stats.matchDuration < 120 ? 10 : 5;
 
  // Combine scores
  const totalScore = accuracy * 0.3 + damageScore + precisionScore + speedScore;
 
  return Math.min(Math.round(totalScore), 100);
}
 
/**
 * Get performance rating based on score
 */
function getPerformanceRating(
  score: number
): keyof typeof PERFORMANCE_RATING_THRESHOLDS {
  if (score >= PERFORMANCE_RATING_THRESHOLDS.S.minScore) return "S";
  if (score >= PERFORMANCE_RATING_THRESHOLDS.A.minScore) return "A";
  if (score >= PERFORMANCE_RATING_THRESHOLDS.B.minScore) return "B";
  return "C";
}
 
/**
 * Performance Rating Component
 * Displays S/A/B/C ranking based on combat performance
 */
export const PerformanceRating: React.FC<PerformanceRatingProps> = ({
  matchStats,
  isMobile,
  isTablet,
}) => {
  const ratingFontSize = isMobile ? 36 : isTablet ? 48 : 56;
  const labelFontSize = isMobile ? 12 : isTablet ? 14 : 15;
  const scoreFontSize = isMobile ? 18 : isTablet ? 20 : 24;
  const padding = isMobile ? 10 : isTablet ? 12 : 15;
 
  const performanceScore = useMemo(
    () => calculatePerformanceScore(matchStats),
    [matchStats]
  );
 
  const rating = useMemo(
    () => getPerformanceRating(performanceScore),
    [performanceScore]
  );
 
  const ratingInfo = PERFORMANCE_RATING_THRESHOLDS[rating];
 
  // Extract winner stats for clarity
  const winnerStats = useMemo(
    () => (matchStats.winner === 0 ? matchStats.player1 : matchStats.player2),
    [matchStats]
  );
 
  return (
    <div
      data-testid="performance-rating"
      style={{
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
        background: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_DARK, 0.9),
        border: `3px solid ${hexToRgbaString(ratingInfo.color, 0.8)}`,
        borderRadius: "16px",
        padding: padding,
        marginBottom: padding / 2,
        minWidth: isMobile ? "260px" : "300px",
        boxShadow: `0 0 30px ${hexToRgbaString(ratingInfo.color, 0.3)}`,
        animation: "ratingPulse 2s ease-in-out infinite",
      }}
    >
      {/* Title */}
      <div
        style={{
          fontSize: labelFontSize,
          color: toCssColor(KOREAN_COLORS.TEXT_SECONDARY),
          fontFamily: FONT_FAMILY.KOREAN,
          marginBottom: padding / 2,
          textTransform: "uppercase",
          letterSpacing: "0.1em",
        }}
        data-testid="rating-label"
      >
        전투 등급 | Performance Rating
      </div>
 
      {/* Rating Letter */}
      <div
        style={{
          fontSize: ratingFontSize,
          fontWeight: "bold",
          color: toCssColor(ratingInfo.color),
          fontFamily: FONT_FAMILY.KOREAN,
          textShadow: `0 0 20px ${hexToRgbaString(ratingInfo.color, 0.6)}`,
          marginBottom: padding / 2,
          animation: "ratingGlow 1.5s ease-in-out infinite",
        }}
        data-testid="rating-letter"
      >
        {rating}
      </div>
 
      {/* Rating Description */}
      <div
        style={{
          fontSize: labelFontSize,
          color: toCssColor(KOREAN_COLORS.PRIMARY_CYAN),
          fontFamily: FONT_FAMILY.KOREAN,
          marginBottom: padding,
          textAlign: "center",
        }}
        data-testid="rating-description"
      >
        {ratingInfo.description.korean} | {ratingInfo.description.english}
      </div>
 
      {/* Performance Score */}
      <div
        style={{
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          width: "100%",
          padding: padding,
          background: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_MEDIUM, 0.4),
          borderRadius: "8px",
        }}
      >
        <div
          style={{
            fontSize: scoreFontSize,
            fontWeight: "bold",
            color: toCssColor(ratingInfo.color),
            fontFamily: FONT_FAMILY.KOREAN,
            marginBottom: padding / 4,
          }}
          data-testid="performance-score"
        >
          {performanceScore}
        </div>
        <div
          style={{
            fontSize: labelFontSize - 2,
            color: toCssColor(KOREAN_COLORS.TEXT_TERTIARY),
            fontFamily: FONT_FAMILY.KOREAN,
          }}
        >
          전투 점수 | Combat Score
        </div>
 
        {/* Score Breakdown */}
        <div
          style={{
            display: "grid",
            gridTemplateColumns: "1fr 1fr",
            gap: padding / 2,
            marginTop: padding,
            width: "100%",
            fontSize: labelFontSize - 4,
            color: toCssColor(KOREAN_COLORS.TEXT_SECONDARY),
          }}
          data-testid="score-breakdown"
        >
          <div style={{ textAlign: "center" }}>
            <div
              style={{
                color: toCssColor(KOREAN_COLORS.ACCENT_GOLD),
                fontWeight: "bold",
              }}
            >
              {winnerStats.perfectStrikes ?? 0}
            </div>
            <div>완벽 | Perfect</div>
          </div>
          <div style={{ textAlign: "center" }}>
            <div
              style={{
                color: toCssColor(KOREAN_COLORS.VITAL_POINT_HIT),
                fontWeight: "bold",
              }}
            >
              {winnerStats.vitalPointHits ?? 0}
            </div>
            <div>급소 | Vital Hits</div>
          </div>
          <div style={{ textAlign: "center" }}>
            <div
              style={{
                color: toCssColor(KOREAN_COLORS.PRIMARY_CYAN),
                fontWeight: "bold",
              }}
            >
              {winnerStats.techniques?.length ?? 0}
            </div>
            <div>기술 | Techniques</div>
          </div>
          <div style={{ textAlign: "center" }}>
            <div
              style={{
                color: toCssColor(KOREAN_COLORS.CRITICAL_HIT),
                fontWeight: "bold",
              }}
            >
              {matchStats.criticalHits}
            </div>
            <div>치명타 | Criticals</div>
          </div>
        </div>
      </div>
 
      {/* CSS Animations */}
      <style>{`
        ${pulseAnimation}
 
        @keyframes ratingGlow {
          0%, 100% {
            text-shadow: 0 0 20px ${hexToRgbaString(ratingInfo.color, 0.6)};
          }
          50% {
            text-shadow: 0 0 30px ${hexToRgbaString(
              ratingInfo.color,
              0.9
            )}, 0 0 50px ${hexToRgbaString(ratingInfo.color, 0.5)};
          }
        }
      `}</style>
    </div>
  );
};