All files / blacktrigram/src/components/ui RoundTimer.tsx

0% Statements 0/183
0% Branches 0/1
0% Functions 0/1
0% Lines 0/183

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import React, { useMemo } from "react";
import { KOREAN_COLORS } from "../../types/constants";
import {
  ResponsivePixiContainer,
  ResponsivePixiPanel,
} from "./base/ResponsivePixiComponents";
 
export interface RoundTimerProps {
  readonly timeRemaining: number; // in seconds
  readonly totalTime: number; // in seconds
  readonly currentRound: number;
  readonly maxRounds: number;
  readonly isPaused?: boolean;
  readonly x: number;
  readonly y: number;
  readonly width: number;
  readonly height: number;
  readonly screenWidth: number;
  readonly screenHeight: number;
  readonly showMilliseconds?: boolean;
}
 
export const RoundTimer: React.FC<RoundTimerProps> = ({
  timeRemaining,
  totalTime,
  currentRound,
  maxRounds,
  isPaused = false,
  x,
  y,
  width,
  height,
  screenWidth,
  screenHeight,
  showMilliseconds = false,
}) => {
  const isMobile = screenWidth < 768;
 
  const { minutes, seconds, milliseconds, timePercentage, urgencyLevel } =
    useMemo(() => {
      const mins = Math.floor(timeRemaining / 60);
      const secs = Math.floor(timeRemaining % 60);
      const ms = Math.floor((timeRemaining % 1) * 100);
      const percentage = totalTime > 0 ? timeRemaining / totalTime : 0;
 
      let urgency: "normal" | "warning" | "critical" = "normal";
      if (percentage < 0.1) urgency = "critical";
      else if (percentage < 0.3) urgency = "warning";
 
      return {
        minutes: mins,
        seconds: secs,
        milliseconds: ms,
        timePercentage: percentage,
        urgencyLevel: urgency,
      };
    }, [timeRemaining, totalTime]);
 
  const getTimerColor = () => {
    if (isPaused) return KOREAN_COLORS.TEXT_SECONDARY;
    switch (urgencyLevel) {
      case "critical":
        return KOREAN_COLORS.ACCENT_RED;
      case "warning":
        return KOREAN_COLORS.ACCENT_GOLD;
      default:
        return KOREAN_COLORS.ACCENT_GREEN;
    }
  };
 
  const formatTime = () => {
    const timeStr = `${minutes.toString().padStart(2, "0")}:${seconds
      .toString()
      .padStart(2, "0")}`;
    return showMilliseconds
      ? `${timeStr}.${milliseconds.toString().padStart(2, "0")}`
      : timeStr;
  };
 
  return (
    <ResponsivePixiContainer
      x={x}
      y={y}
      screenWidth={screenWidth}
      screenHeight={screenHeight}
      data-testid="round-timer"
    >
      <ResponsivePixiPanel
        title=""
        x={0}
        y={0}
        width={width}
        height={height}
        screenWidth={screenWidth}
        screenHeight={screenHeight}
      />
 
      {/* Round Information */}
      <pixiContainer x={width / 2} y={15} data-testid="round-info">
        <pixiText
          text={`라운드 ${currentRound} / ${maxRounds}`}
          style={{
            fontSize: isMobile ? 12 : 14,
            fill: KOREAN_COLORS.TEXT_PRIMARY as number,
            fontWeight: "bold",
            align: "center",
            fontFamily: "Noto Sans KR",
          }}
          anchor={0.5}
          data-testid="round-counter"
        />
      </pixiContainer>
 
      {/* Timer Display */}
      <pixiContainer x={width / 2} y={height / 2} data-testid="timer-display">
        {/* Timer background circle */}
        <pixiGraphics
          draw={(g) => {
            g.clear();
            const radius = isMobile ? 35 : 45;
 
            // Background circle
            g.fill({ color: KOREAN_COLORS.UI_BACKGROUND_DARK, alpha: 0.8 });
            g.circle(0, 0, radius);
            g.fill();
 
            // Progress ring
            g.stroke({ width: 4, color: getTimerColor(), alpha: 0.9 });
            const angle = timePercentage * Math.PI * 2;
            g.arc(0, 0, radius - 2, -Math.PI / 2, -Math.PI / 2 + angle);
            g.stroke();
 
            // Border
            g.stroke({
              width: 2,
              color: KOREAN_COLORS.ACCENT_GOLD,
              alpha: 0.6,
            });
            g.circle(0, 0, radius);
            g.stroke();
          }}
          data-testid="timer-circle"
        />
 
        {/* Time text */}
        <pixiText
          text={formatTime()}
          style={{
            fontSize: isMobile ? 14 : 18,
            fill: getTimerColor(),
            fontWeight: "bold",
            align: "center",
            fontFamily: "monospace",
          }}
          anchor={0.5}
          data-testid="timer-text"
        />
 
        {/* Pause indicator */}
        {isPaused && (
          <pixiText
            text="일시정지"
            style={{
              fontSize: isMobile ? 8 : 10,
              fill: KOREAN_COLORS.TEXT_SECONDARY,
              align: "center",
              fontFamily: "Noto Sans KR",
            }}
            x={0}
            y={isMobile ? 20 : 25}
            anchor={0.5}
            data-testid="pause-indicator"
          />
        )}
      </pixiContainer>
 
      {/* Time bar at bottom */}
      <pixiGraphics
        draw={(g) => {
          g.clear();
          const barWidth = width - 20;
          const barHeight = 4;
          const barY = height - 15;
 
          // Background bar
          g.fill({ color: KOREAN_COLORS.UI_BACKGROUND_DARK, alpha: 0.8 });
          g.rect(10, barY, barWidth, barHeight);
          g.fill();
 
          // Progress bar
          const progressWidth = barWidth * timePercentage;
          g.fill({ color: getTimerColor(), alpha: 0.9 });
          g.rect(10, barY, progressWidth, barHeight);
          g.fill();
 
          // Border
          g.stroke({ width: 1, color: KOREAN_COLORS.ACCENT_GOLD, alpha: 0.6 });
          g.rect(10, barY, barWidth, barHeight);
          g.stroke();
        }}
        data-testid="timer-progress-bar"
      />
 
      {/* Urgency effects */}
      {urgencyLevel === "critical" && !isPaused && (
        <pixiGraphics
          draw={(g) => {
            g.clear();
            const pulse = Math.sin(Date.now() * 0.01) * 0.3 + 0.7;
            g.stroke({
              width: 3,
              color: KOREAN_COLORS.ACCENT_RED,
              alpha: pulse,
            });
            g.rect(2, 2, width - 4, height - 4);
            g.stroke();
          }}
          data-testid="critical-timer-effect"
        />
      )}
 
      {/* Korean time label */}
      <pixiContainer x={width / 2} y={height - 30} data-testid="time-label">
        <pixiText
          text="남은 시간"
          style={{
            fontSize: isMobile ? 9 : 11,
            fill: KOREAN_COLORS.TEXT_TERTIARY,
            align: "center",
            fontFamily: "Noto Sans KR",
          }}
          anchor={0.5}
          data-testid="time-label-korean"
        />
      </pixiContainer>
    </ResponsivePixiContainer>
  );
};
 
export default RoundTimer;