All files / web/src/arcade-games/memory-quiz/components DisplayPhase.tsx

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { AbacusReact, useAbacusConfig } from '@soroban/abacus-react'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useMemoryQuiz } from '../Provider'
import type { QuizCard } from '../types'

// Calculate maximum columns needed for a set of numbers
function calculateMaxColumns(numbers: number[]): number {
  if (numbers.length === 0) return 1
  const maxNumber = Math.max(...numbers)
  if (maxNumber === 0) return 1
  return Math.floor(Math.log10(maxNumber)) + 1
}

export function DisplayPhase() {
  const { state, nextCard, showInputPhase, resetGame, isRoomCreator } = useMemoryQuiz()
  const [currentCard, setCurrentCard] = useState<QuizCard | null>(null)
  const [isTransitioning, setIsTransitioning] = useState(false)
  const isDisplayPhaseActive = state.currentCardIndex < state.quizCards.length
  const isProcessingRef = useRef(false)
  const lastProcessedIndexRef = useRef(-1)
  const appConfig = useAbacusConfig()

  // In multiplayer room mode, only the room creator controls card timing
  // In local mode (isRoomCreator === undefined), allow timing control
  const shouldControlTiming = isRoomCreator === undefined || isRoomCreator === true

  // Calculate maximum columns needed for this quiz set
  const maxColumns = useMemo(() => {
    const allNumbers = state.quizCards.map((card) => card.number)
    return calculateMaxColumns(allNumbers)
  }, [state.quizCards])

  // Calculate adaptive animation duration
  const flashDuration = useMemo(() => {
    const displayTimeMs = state.displayTime * 1000
    return Math.min(Math.max(displayTimeMs * 0.3, 150), 600) / 1000 // Convert to seconds for CSS
  }, [state.displayTime])

  const progressPercentage = (state.currentCardIndex / state.quizCards.length) * 100

  useEffect(() => {
    // Prevent processing the same card index multiple times
    // This prevents race conditions from optimistic updates
    if (state.currentCardIndex === lastProcessedIndexRef.current) {
      console.log(
        `DisplayPhase: Skipping duplicate processing of index ${state.currentCardIndex} (lastProcessed: ${lastProcessedIndexRef.current})`
      )
      return
    }

    if (state.currentCardIndex >= state.quizCards.length) {
      // Only the room creator (or local mode) triggers phase transitions
      if (shouldControlTiming) {
        console.log(
          `DisplayPhase: All cards shown (${state.quizCards.length}), transitioning to input phase`
        )
        showInputPhase?.()
      }
      return
    }

    // Prevent multiple concurrent executions
    if (isProcessingRef.current) {
      console.log(
        `DisplayPhase: Already processing, skipping (index: ${state.currentCardIndex}, lastProcessed: ${lastProcessedIndexRef.current})`
      )
      return
    }

    // Mark this index as being processed
    lastProcessedIndexRef.current = state.currentCardIndex

    const showNextCard = async () => {
      isProcessingRef.current = true
      const card = state.quizCards[state.currentCardIndex]
      console.log(
        `DisplayPhase: Showing card ${state.currentCardIndex + 1}/${state.quizCards.length}, number: ${card.number} (isRoomCreator: ${isRoomCreator}, shouldControlTiming: ${shouldControlTiming})`
      )

      // Calculate adaptive timing based on display speed
      const displayTimeMs = state.displayTime * 1000
      const flashDuration = Math.min(Math.max(displayTimeMs * 0.3, 150), 600) // 30% of display time, between 150ms-600ms
      const transitionPause = Math.min(Math.max(displayTimeMs * 0.1, 50), 200) // 10% of display time, between 50ms-200ms

      // Trigger adaptive transition effect
      setIsTransitioning(true)
      setCurrentCard(card)

      // Reset transition effect with adaptive duration
      setTimeout(() => setIsTransitioning(false), flashDuration)

      console.log(
        `DisplayPhase: Card ${state.currentCardIndex + 1} now visible (flash: ${flashDuration}ms, pause: ${transitionPause}ms)`
      )

      // Only the room creator (or local mode) controls the timing
      if (shouldControlTiming) {
        // Display card for specified time with adaptive transition pause
        await new Promise((resolve) => setTimeout(resolve, displayTimeMs - transitionPause))

        // Don't hide the abacus - just advance to next card for smooth transition
        console.log(
          `DisplayPhase: Card ${state.currentCardIndex + 1} transitioning to next (controlled by ${isRoomCreator === undefined ? 'local mode' : 'room creator'})`
        )
        await new Promise((resolve) => setTimeout(resolve, transitionPause)) // Adaptive pause for visual transition

        isProcessingRef.current = false
        nextCard?.()
      } else {
        // Non-creator players just display the card, don't control timing
        console.log(
          `DisplayPhase: Non-creator player displaying card ${state.currentCardIndex + 1}, waiting for creator to advance`
        )
        isProcessingRef.current = false
      }
    }

    showNextCard()
  }, [
    state.currentCardIndex,
    state.displayTime,
    state.quizCards.length,
    nextCard,
    showInputPhase,
    shouldControlTiming,
    isRoomCreator,
  ])

  return (
    <div
      style={{
        textAlign: 'center',
        padding: '12px',
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        boxSizing: 'border-box',
        height: '100%',
        animation: isTransitioning ? `subtlePageFlash ${flashDuration}s ease-out` : undefined,
      }}
    >
      <div
        style={{
          position: 'relative',
          width: '100%',
          maxWidth: '800px',
          marginBottom: '12px',
          display: 'flex',
          flexDirection: 'column',
          gap: '12px',
        }}
      >
        <div>
          <div
            style={{
              width: '100%',
              height: '8px',
              background: '#e5e7eb',
              borderRadius: '4px',
              overflow: 'hidden',
              marginBottom: '8px',
            }}
          >
            <div
              style={{
                height: '100%',
                background: 'linear-gradient(90deg, #28a745, #20c997)',
                borderRadius: '4px',
                width: `${progressPercentage}%`,
                transition: 'width 0.5s ease',
              }}
            />
          </div>
          <span
            style={{
              fontSize: '14px',
              fontWeight: 'bold',
              color: '#374151',
            }}
          >
            Card {state.currentCardIndex + 1} of {state.quizCards.length}
          </span>
        </div>
        <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
          <button
            style={{
              background: '#ef4444',
              color: 'white',
              border: 'none',
              borderRadius: '6px',
              padding: '6px 12px',
              fontSize: '12px',
              cursor: 'pointer',
              transition: 'background 0.2s ease',
            }}
            onClick={() => resetGame?.()}
          >
            End Quiz
          </button>
        </div>
      </div>

      {/* Persistent abacus container - stays mounted during entire memorize phase */}
      <div
        style={{
          width: 'min(90vw, 800px)',
          height: 'min(70vh, 500px)',
          display: isDisplayPhaseActive ? 'flex' : 'none',
          alignItems: 'center',
          justifyContent: 'center',
          margin: '0 auto',
          transition: 'opacity 0.3s ease',
          overflow: 'visible',
          padding: '20px 12px',
        }}
      >
        <div
          style={{
            width: '100%',
            height: '100%',
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            justifyContent: 'center',
            gap: '20px',
          }}
        >
          {/* Persistent abacus with smooth bead animations and dynamically calculated columns */}
          <AbacusReact
            value={currentCard?.number || 0}
            columns={maxColumns}
            beadShape={appConfig.beadShape}
            colorScheme={appConfig.colorScheme}
            hideInactiveBeads={appConfig.hideInactiveBeads}
            scaleFactor={5.5}
            interactive={false}
            showNumbers={false}
            animated={true}
          />
        </div>
      </div>
    </div>
  )
}