1- "use client"
2-
3- import { useState , useEffect } from "react"
4- import { useParams } from "react-router-dom"
1+ import { useState } from "react"
2+ import { useParams } from "react-router-dom"
53import F1StartingLights from "../../layout/game/components/F1StartingLights"
64import QuizTimer from "../../layout/game/components/QuizTimer"
75import QuizQuestion from "../../layout/game/components/QuizQuestion"
86import QuizCompleted from "../../layout/game/components/QuizCompleted"
9- import WinnerModal from "../../layout/game/components/WinnerModal"
107import QuizResultsModal from "../../layout/game/components/QuizResultsModal"
11-
12- const sampleQuestions = [
13- {
14- question : "Which driver won the 2024 Monaco Grand Prix?" ,
15- answer : "Charles Leclerc" ,
16- } ,
17- {
18- question : "What is the maximum number of power units a driver can use in a season?" ,
19- answer : "4" ,
20- } ,
21- {
22- question : 'Which circuit is known as "The Temple of Speed"?' ,
23- answer : "Monza" ,
24- } ,
25- ]
8+ import { useRecoilValue } from "recoil" ;
9+ import {
10+ questionResultAtom ,
11+ questionsAtom ,
12+ questionStartAtom
13+ } from "../../state/atoms" ;
2614
2715const finalResults = [
2816 { rank : 1 , name : "Lewis Hamilton" , correctAnswers : 12 , totalQuestions : 25 , score : 120 , color : "text-red-600" } ,
@@ -32,74 +20,39 @@ const finalResults = [
3220]
3321
3422function GamePlay ( ) {
35- const { id : roomId } = useParams ( )
36- const [ showStartingLights , setShowStartingLights ] = useState ( true )
37- const [ currentQuestion , setCurrentQuestion ] = useState ( 0 )
38- const [ quizStarted , setQuizStarted ] = useState ( false )
39- const [ quizCompleted , setQuizCompleted ] = useState ( false )
40- const [ showWinnerModal , setShowWinnerModal ] = useState ( false )
41- const [ winner , setWinner ] = useState ( "" )
42- const [ correctAnswer , setCorrectAnswer ] = useState ( "" )
43- const [ showResultsModal , setShowResultsModal ] = useState ( false )
23+ const { id : roomId } = useParams ( ) ;
24+ const [ showStartingLights , setShowStartingLights ] = useState ( true ) ;
25+ const [ quizStarted , setQuizStarted ] = useState ( false ) ;
26+ const [ quizCompleted , setQuizCompleted ] = useState ( false ) ;
27+ const [ showResultsModal , setShowResultsModal ] = useState ( false ) ;
28+ const questions = useRecoilValue ( questionsAtom ) ;
29+ const currentQuestion = useRecoilValue ( questionStartAtom ) ;
30+ const questionsResult = useRecoilValue ( questionResultAtom ) ;
31+ console . log ( 'questions: ' , questions , currentQuestion , questionsResult ) ;
4432
4533 const handleLightsComplete = ( ) => {
4634 setShowStartingLights ( false )
4735 setQuizStarted ( true )
4836 }
4937
5038 const handleTimeUp = ( ) => {
51- handleNextQuestion ( )
52- }
53-
54- const handleCorrectAnswer = ( winnerName , answer ) => {
55- setWinner ( winnerName )
56- setCorrectAnswer ( answer )
57- setShowWinnerModal ( true )
58- }
59-
60- const handleCloseWinnerModal = ( ) => {
61- setShowWinnerModal ( false )
62- handleNextQuestion ( )
63- }
64-
65- const handleNextQuestion = ( ) => {
66- if ( currentQuestion < sampleQuestions . length - 1 ) {
67- setCurrentQuestion ( ( prev ) => prev + 1 )
68- } else {
69- setQuizCompleted ( true )
70- setTimeout ( ( ) => {
71- setShowResultsModal ( true )
72- } , 1000 )
73- }
7439 }
7540
7641 const handleCloseResultsModal = ( ) => {
77- setShowResultsModal ( false )
42+ setShowResultsModal ( false ) ;
7843 }
7944
80- // 테스트를 위해 3초 후 정답 모달 표시
81- useEffect ( ( ) => {
82- if ( quizStarted && ! showWinnerModal && ! quizCompleted ) {
83- const timer = setTimeout ( ( ) => {
84- handleCorrectAnswer ( "Charles Leclerc" , sampleQuestions [ currentQuestion ] ?. answer || "" )
85- } , 3000 )
86- return ( ) => clearTimeout ( timer )
87- }
88- } , [ quizStarted , currentQuestion , showWinnerModal , quizCompleted ] )
89-
9045 return (
9146 < div className = "p-8 flex flex-col h-full" >
9247 { showStartingLights && < F1StartingLights onComplete = { handleLightsComplete } /> }
9348
94- < div
95- className = { `transition-opacity duration-500 ${ quizStarted ? "opacity-100" : "opacity-0" } h-full flex flex-col` }
96- >
49+ < div className = { `transition-opacity duration-500 ${ quizStarted ? "opacity-100" : "opacity-0" } h-full flex flex-col` } >
9750 { /* Quiz Header */ }
9851 < div className = "flex items-center justify-between mb-8" >
9952 < div className = "bg-white rounded-lg px-4 py-2 shadow-sm" >
10053 < span className = "text-lg text-gray-600" > 문제</ span >
10154 < span className = "text-lg font-bold text-gray-900 ml-2" >
102- { quizCompleted ? sampleQuestions . length : currentQuestion + 1 } / { sampleQuestions . length }
55+ { quizCompleted ? questions ? .length : currentQuestion ?. round } / { questions ? .length }
10356 </ span >
10457 </ div >
10558 < QuizTimer duration = { quizCompleted ? 0 : 30 } onTimeUp = { handleTimeUp } />
@@ -108,28 +61,15 @@ function GamePlay() {
10861 { /* Question Content */ }
10962 < div className = "flex-1 flex justify-center" >
11063 { quizCompleted ? (
111- < QuizCompleted totalQuestions = { sampleQuestions . length } />
64+ < QuizCompleted totalQuestions = { questions ? .length } />
11265 ) : (
11366 quizStarted &&
114- currentQuestion < sampleQuestions . length && (
115- < QuizQuestion
116- questionNumber = { currentQuestion + 1 }
117- totalQuestions = { sampleQuestions . length }
118- question = { sampleQuestions [ currentQuestion ] . question }
119- />
67+ currentQuestion ?. round <= questions ?. length && (
68+ < QuizQuestion question = { questions [ currentQuestion ?. round ] ?. question } />
12069 )
12170 ) }
12271 </ div >
12372 </ div >
124-
125- { /* Modals */ }
126- < WinnerModal
127- isVisible = { showWinnerModal }
128- winner = { winner }
129- correctAnswer = { correctAnswer }
130- onClose = { handleCloseWinnerModal }
131- />
132-
13373 < QuizResultsModal isVisible = { showResultsModal } results = { finalResults } onClose = { handleCloseResultsModal } />
13474 </ div >
13575 )
0 commit comments