import React, { useState, useEffect, useCallback, useRef } from 'react'; import { Button } from '../components/ui/Button'; // Fix: Added missing Palette and X imports from lucide-react import { ArrowLeft, Play, RefreshCw, Trophy, Zap, AlertCircle, Sparkles, Timer, Palette, X } from 'lucide-react'; import { StorageService } from '../services/storageService'; interface GameProps { onExit: () => void; } const COLOR_POOL = [ { name: 'RED', hex: '#ef4444' }, { name: 'BLUE', hex: '#3b82f6' }, { name: 'GREEN', hex: '#22c55e' }, { name: 'YELLOW', hex: '#eab308' }, { name: 'PURPLE', hex: '#a855f7' }, { name: 'PINK', hex: '#ec4899' }, { name: 'CYAN', hex: '#06b6d4' } ]; interface RoundData { leftWord: typeof COLOR_POOL[0]; rightWordText: typeof COLOR_POOL[0]; rightWordInk: typeof COLOR_POOL[0]; isMatch: boolean; } export const FlexibilityColor: React.FC = ({ onExit }) => { const [isPlaying, setIsPlaying] = useState(false); const [gameOver, setGameOver] = useState(false); const [score, setScore] = useState(0); const [streak, setStreak] = useState(0); const [highScore, setHighScore] = useState(0); const [currentRound, setCurrentRound] = useState(null); const [timeLeft, setTimeLeft] = useState(2000); // 2 seconds in ms const timerRef = useRef(0); const startTimeRef = useRef(0); useEffect(() => { const loadHighScore = async () => { const profile = await StorageService.getProfile(); if (profile?.highScores?.truth) setHighScore(profile.highScores.truth); }; loadHighScore(); return () => cancelAnimationFrame(timerRef.current); }, []); const playTone = (freq: number, type: OscillatorType = 'sine') => { try { const ctx = new (window.AudioContext || (window as any).webkitAudioContext)(); const osc = ctx.createOscillator(); const gain = ctx.createGain(); osc.type = type; osc.frequency.setValueAtTime(freq, ctx.currentTime); gain.gain.setValueAtTime(0.1, ctx.currentTime); gain.gain.exponentialRampToValueAtTime(0.01, ctx.currentTime + 0.1); osc.connect(gain); gain.connect(ctx.destination); osc.start(); osc.stop(ctx.currentTime + 0.1); } catch (e) {} }; const generateRound = useCallback(() => { const leftColor = COLOR_POOL[Math.floor(Math.random() * COLOR_POOL.length)]; const rightText = COLOR_POOL[Math.floor(Math.random() * COLOR_POOL.length)]; // 50% chance of a match const shouldMatch = Math.random() > 0.5; let rightInk; if (shouldMatch) { rightInk = leftColor; } else { do { rightInk = COLOR_POOL[Math.floor(Math.random() * COLOR_POOL.length)]; } while (rightInk.hex === leftColor.hex); } setCurrentRound({ leftWord: leftColor, rightWordText: rightText, rightWordInk: rightInk, isMatch: shouldMatch }); setTimeLeft(2000); startTimeRef.current = Date.now(); }, []); const handleGameOver = useCallback(() => { setIsPlaying(false); setGameOver(true); playTone(150, 'sawtooth'); StorageService.addPoints(Math.floor(score / 5)); if (score > highScore) { setHighScore(score); StorageService.saveHighScore('truth', score); } }, [score, highScore]); const handleAnswer = (answer: boolean) => { if (!isPlaying || !currentRound) return; if (answer === currentRound.isMatch) { const points = 10 * (Math.floor(streak / 5) + 1); setScore(prev => prev + points); setStreak(prev => prev + 1); playTone(600 + streak * 20); generateRound(); } else { handleGameOver(); } }; const updateTimer = useCallback(() => { if (!isPlaying) return; const elapsed = Date.now() - startTimeRef.current; const remaining = Math.max(0, 2000 - elapsed); setTimeLeft(remaining); if (remaining === 0) { handleGameOver(); } else { timerRef.current = requestAnimationFrame(updateTimer); } }, [isPlaying, handleGameOver]); useEffect(() => { if (isPlaying) { timerRef.current = requestAnimationFrame(updateTimer); } else { cancelAnimationFrame(timerRef.current); } return () => cancelAnimationFrame(timerRef.current); }, [isPlaying, updateTimer]); const startGame = () => { setScore(0); setStreak(0); setGameOver(false); setIsPlaying(true); generateRound(); }; useEffect(() => { const handleKey = (e: KeyboardEvent) => { if (e.key === 'ArrowLeft') handleAnswer(false); if (e.key === 'ArrowRight') handleAnswer(true); }; window.addEventListener('keydown', handleKey); return () => window.removeEventListener('keydown', handleKey); }, [isPlaying, currentRound, handleAnswer]); return (
{/* Background Decor */}
{isPlaying && (
Score {score}
Streak x{streak}
)}
{!isPlaying && !gameOver && (
{/* Fix: Usage of Palette resolved by adding it to imports */}

Flexibility

Does the Meaning of the left word match the Ink Color of the right word?

Fast
2.0s
)} {isPlaying && currentRound && (
{/* Timer Bar */}
{/* Left Card: Meaning */}
Literal Meaning
{currentRound.leftWord.name}
?
{/* Right Card: Ink Color */}
Ink Presentation
{currentRound.rightWordText.name}
)} {gameOver && (

Mismatched

Neural link severed due to logic error.

Final Cognitive Score

{score}

)}
Stroop Protocol
Efficiency Rank
); }; // Helper Check icon (X is imported from Lucide) const Check = ({ size, className, strokeWidth }: any) => ( );