diff --git a/src/SounddittoGame.js b/src/SounddittoGame.js index b12543f..8463d8d 100644 --- a/src/SounddittoGame.js +++ b/src/SounddittoGame.js @@ -1,34 +1,37 @@ -import React, { useState, useEffect } from "react"; -import { Button } from "@/components/ui/button"; -import { Card, CardContent, CardHeader } from "@/components/ui/card"; -import { Switch } from "@/components/ui/switch"; -import { Info } from "lucide-react"; -import englishWords from "./lib/englishWords"; -import spanishWords from "./lib/spanishWords"; -import Footer from "./components/ui/Footer"; -import InstructionsModal from "./components/modals/InstructionsModal"; +import React, { useState, useEffect } from 'react'; +import { Button } from "@/components/ui/button" +import { Card, CardContent, CardHeader } from "@/components/ui/card" +import { Switch } from "@/components/ui/switch" +import { Info } from "lucide-react" +import englishWords from './lib/englishWords'; +import spanishWords from './lib/spanishWords'; +import localization from './lib/localization'; +import Footer from './components/ui/Footer'; +import InstructionsModal from './components/modals/InstructionsModal'; const GAME_DURATION = 60; // Game duration in seconds const SKIP_PENALTY = 5; // Time penalty for skipping in seconds const SounddittoGame = () => { - const [gameState, setGameState] = useState("waiting"); - const [currentWords, setCurrentWords] = useState({ easy: "", hard: "" }); + const [gameState, setGameState] = useState('waiting'); + const [currentWords, setCurrentWords] = useState({ easy: '', hard: '' }); const [score, setScore] = useState(0); const [timeLeft, setTimeLeft] = useState(GAME_DURATION); - const [language, setLanguage] = useState("english"); + const [language, setLanguage] = useState('en'); const [availableWords, setAvailableWords] = useState([]); const [isInstructionsOpen, setIsInstructionsOpen] = useState(false); const [isTimerEnabled, setIsTimerEnabled] = useState(true); + const t = (key) => localization[language][key]; + useEffect(() => { let timer; - if (gameState === "playing" && isTimerEnabled) { + if (gameState === 'playing' && isTimerEnabled) { timer = setInterval(() => { setTimeLeft((prevTime) => { if (prevTime <= 1) { clearInterval(timer); - setGameState("finished"); + setGameState('finished'); return 0; } return prevTime - 1; @@ -39,10 +42,9 @@ const SounddittoGame = () => { }, [gameState, isTimerEnabled]); const startGame = () => { - const words = - language === "english" ? [...englishWords] : [...spanishWords]; + const words = language === 'en' ? [...englishWords] : [...spanishWords]; setAvailableWords(words); - setGameState("playing"); + setGameState('playing'); setScore(0); if (isTimerEnabled) { setTimeLeft(GAME_DURATION); @@ -51,12 +53,12 @@ const SounddittoGame = () => { }; const endGame = () => { - setGameState("finished"); + setGameState('finished'); }; const nextWord = (words = availableWords) => { if (words.length <= 1) { - setGameState("finished"); + setGameState('finished'); return; } const randomIndex = Math.floor(Math.random() * words.length); @@ -72,48 +74,38 @@ const SounddittoGame = () => { }; const handleGotIt = (difficulty) => { - setScore((prevScore) => prevScore + (difficulty === "easy" ? 1 : 2)); + setScore((prevScore) => prevScore + (difficulty === 'easy' ? 1 : 2)); nextWord(); }; const toggleLanguage = () => { - setLanguage((prevLang) => (prevLang === "english" ? "spanish" : "english")); + setLanguage(prevLang => prevLang === 'en' ? 'es' : 'en'); }; const toggleTimer = () => { - setIsTimerEnabled((prevState) => !prevState); + setIsTimerEnabled(prevState => !prevState); }; const renderLanguageToggle = () => (
- - EN - + EN - - ES - + ES
); const renderTimerToggle = () => (
- - {language === "english" ? "Timer" : "Temporizador"} - + {t('timer')}
@@ -139,7 +131,7 @@ const SounddittoGame = () => { - {gameState === "waiting" && ( + {gameState === 'waiting' && ( <> {renderLanguageToggle()} {renderTimerToggle()} @@ -147,98 +139,61 @@ const SounddittoGame = () => { )} - {gameState === "waiting" && ( - )} - {gameState === "playing" && ( + {gameState === 'playing' && (

- {language === "english" - ? "Choose a word:" - : "Elige una palabra:"} + {t('chooseWord')}

- -
-

- {language === "english" ? "Score:" : "Puntos:"} - {score} + {t('score')} {score}

{isTimerEnabled ? (

- {language === "english" ? "Time:" : "Tiempo:"} - {timeLeft}s + {t('time')} {timeLeft}s

) : ( - )}
)} - {gameState === "finished" && ( + {gameState === 'finished' && (

- {language === "english" ? "Game Over!" : "¡Juego Terminado!"} + {t('gameOver')}

- {language === "english" - ? "Final Score:" - : "Puntuación Final:"}{" "} - {score} + {t('finalScore')} {score}

-
)} @@ -246,14 +201,9 @@ const SounddittoGame = () => {