@@ -40,116 +40,90 @@ const HomeScreen: React.FC<HomeScreenProps> = ({ username }) => {
4040 const [ gameState , setGameState ] = useState < GameState > ( "idle" ) ;
4141 const [ currentQuestion , setCurrentQuestion ] = useState < number > ( - 1 ) ;
4242 const [ game , setGame ] = useState < Schema [ "Game" ] [ "type" ] > ( ) ;
43- const handleSearchGame = async ( ) : Promise < void > => {
44- const currentGames = await client . models . Game . list ( {
45- filter : {
46- playerTwoId : {
47- eq : "notAssigned" ,
48- } ,
49- } ,
50- } ) ;
51-
52- if ( currentGames . data . length > 0 ) {
53- await client . models . Game . update ( {
54- id : currentGames . data [ 0 ] . id ,
55- playerTwoId : username ,
56- } ) ;
57- setGameState ( "found" ) ;
5843
59- client . models . Game . observeQuery ( {
44+ const handleSearchGame = async ( ) : Promise < void > => {
45+ try {
46+ const currentGames = await client . models . Game . list ( {
6047 filter : {
61- id : {
62- eq : currentGames . data [ 0 ] . id ,
48+ playerTwoId : {
49+ eq : "notAssigned" ,
6350 } ,
6451 } ,
65- } ) . subscribe ( async ( game ) => {
66- if ( game . items [ 0 ] . questions . length > 0 ) {
67- setGameState ( "quiz" ) ;
68- setGame ( game . items [ 0 ] ) ;
69- }
70- if ( game . items [ 0 ] . currentQuestion !== currentQuestion ) {
71- setCurrentQuestion ( ( game . items [ 0 ] . currentQuestion ?? 0 ) + 1 ) ;
72- }
7352 } ) ;
74- const result = await client . generations . generateQuestions ( {
75- description : "" ,
76- } ) ;
77-
78- if ( result . errors ) {
79- setGameState ( "error" ) ;
80- return ;
81- }
8253
83- const updatedGame = await client . models . Game . update ( {
84- id : currentGames . data [ 0 ] . id ,
85- questions : result . data as Schema [ "Question" ] [ "type" ] [ ] ,
86- } ) ;
54+ if ( currentGames . data . length > 0 ) {
55+ await client . models . Game . update ( {
56+ id : currentGames . data [ 0 ] . id ,
57+ playerTwoId : username ,
58+ } ) ;
59+ setGameState ( "found" ) ;
8760
88- if ( updatedGame . data ) {
89- setGame ( updatedGame . data ) ;
90- }
91- // await client.models.Game.update({
92- // id: currentGames.data[0].id,
93- // questions: [
94- // {
95- // question: "Which country won the FIFA World Cup in 2022?",
96- // options: ["Brazil", "France", "Argentina", "Germany"],
97- // correctAnswer: "Argentina",
98- // category: "Soccer",
99- // },
100- // {
101- // question: "In which sport would you perform a 'slam dunk'?",
102- // options: ["Volleyball", "Tennis", "Basketball", "Cricket"],
103- // correctAnswer: "Basketball",
104- // category: "Basketball",
105- // },
106- // {
107- // question:
108- // "How many players are there on a standard ice hockey team?",
109- // options: ["5", "6", "7", "8"],
110- // correctAnswer: "6",
111- // category: "Ice Hockey",
112- // },
113- // {
114- // question:
115- // "In which Olympic sport might you use the 'Fosbury Flop' technique?",
116- // options: ["Swimming", "Diving", "High Jump", "Gymnastics"],
117- // correctAnswer: "High Jump",
118- // category: "Athletics",
119- // },
120- // {
121- // question:
122- // "Which Grand Slam tennis tournament is played on clay courts?",
123- // options: ["Wimbledon", "US Open", "Australian Open", "French Open"],
124- // correctAnswer: "French Open",
125- // category: "Tennis",
126- // },
127- // ],
128- // });
129- } else {
130- setGameState ( "searching" ) ;
131- const newGame = await client . models . Game . create ( {
132- playerOneId : username ,
133- playerTwoId : "notAssigned" ,
134- questions : [ ] ,
135- } ) ;
136- client . models . Game . observeQuery ( {
137- filter : {
138- id : {
139- eq : newGame . data ?. id ,
61+ client . models . Game . observeQuery ( {
62+ filter : {
63+ id : {
64+ eq : currentGames . data [ 0 ] . id ,
65+ } ,
14066 } ,
141- } ,
142- } ) . subscribe ( ( game ) => {
143- if ( game . items [ 0 ] . questions . length > 0 ) {
144- setGameState ( "quiz" ) ;
145- setGame ( game . items [ 0 ] ) ;
146- } else if ( game . items [ 0 ] . playerTwoId !== "notAssigned" ) {
147- setGameState ( "found" ) ;
67+ } ) . subscribe ( async ( observedGame ) => {
68+ if ( observedGame . items [ 0 ] . questions . length > 0 ) {
69+ setGameState ( "quiz" ) ;
70+ setGame ( observedGame . items [ 0 ] ) ;
71+ }
72+ if ( observedGame . items [ 0 ] . currentQuestion !== currentQuestion ) {
73+ setCurrentQuestion (
74+ ( observedGame . items [ 0 ] . currentQuestion ?? 0 ) + 1
75+ ) ;
76+ }
77+ } ) ;
78+
79+ const result = await client . generations . generateQuestions ( {
80+ description : "" ,
81+ } ) ;
82+
83+ if ( result . errors ) {
84+ console . log ( result . errors ) ;
85+ setGameState ( "error" ) ;
86+ return ;
14887 }
149- if ( game . items [ 0 ] . currentQuestion !== currentQuestion ) {
150- setCurrentQuestion ( ( game . items [ 0 ] . currentQuestion ?? 0 ) + 1 ) ;
88+
89+ const updatedGame = await client . models . Game . update ( {
90+ id : currentGames . data [ 0 ] . id ,
91+ questions : result . data as Schema [ "Question" ] [ "type" ] [ ] ,
92+ } ) ;
93+
94+ if ( updatedGame . data ) {
95+ setGame ( updatedGame . data ) ;
15196 }
152- } ) ;
97+ } else {
98+ setGameState ( "searching" ) ;
99+ const newGame = await client . models . Game . create ( {
100+ playerOneId : username ,
101+ playerTwoId : "notAssigned" ,
102+ questions : [ ] ,
103+ } ) ;
104+ client . models . Game . observeQuery ( {
105+ filter : {
106+ id : {
107+ eq : newGame . data ?. id ,
108+ } ,
109+ } ,
110+ } ) . subscribe ( ( observedGame ) => {
111+ if ( observedGame . items [ 0 ] . questions . length > 0 ) {
112+ setGameState ( "quiz" ) ;
113+ setGame ( observedGame . items [ 0 ] ) ;
114+ } else if ( observedGame . items [ 0 ] . playerTwoId !== "notAssigned" ) {
115+ setGameState ( "found" ) ;
116+ }
117+ if ( observedGame . items [ 0 ] . currentQuestion !== currentQuestion ) {
118+ setCurrentQuestion (
119+ ( observedGame . items [ 0 ] . currentQuestion ?? 0 ) + 1
120+ ) ;
121+ }
122+ } ) ;
123+ }
124+ } catch ( error ) {
125+ console . error ( "Error searching for game:" , error ) ;
126+ setGameState ( "error" ) ;
153127 }
154128 } ;
155129
@@ -179,21 +153,23 @@ const HomeScreen: React.FC<HomeScreenProps> = ({ username }) => {
179153 </ >
180154 ) ;
181155 case "quiz" :
182- const question = game ?. questions [ currentQuestion ] ;
183- if ( currentQuestion === game ?. questions . length ) {
156+ if ( ! game ) return < Text > Loading game...</ Text > ;
157+
158+ const question = game . questions [ currentQuestion ] ;
159+ if ( currentQuestion === game . questions . length ) {
184160 return (
185161 < >
186162 < Text style = { styles . quizText } > Quiz is over!</ Text >
187163 < Text style = { styles . quizText } >
188- { game ? .playerOneScore === game ? .playerTwoScore
189- ? " It's a tie!"
190- : ( game ? .playerOneScore ?? 0 ) > ( game ? .playerTwoScore ?? 0 )
164+ { game . playerOneScore === game . playerTwoScore
165+ ? ` It's a tie with ${ game . playerOneScore } !`
166+ : ( game . playerOneScore ?? 0 ) > ( game . playerTwoScore ?? 0 )
191167 ? `${
192- game ? .playerOneId === username ? "You" : game ? .playerOneId
193- } won with ${ game ? .playerOneScore } points!`
168+ game . playerOneId === username ? "You" : game . playerOneId
169+ } won with ${ game . playerOneScore } points!`
194170 : `${
195- game ? .playerTwoId === username ? "You" : game ? .playerTwoId
196- } won with ${ game ? .playerTwoScore } points!`}
171+ game . playerTwoId === username ? "You" : game . playerTwoId
172+ } won with ${ game . playerTwoScore } points!`}
197173 </ Text >
198174 </ >
199175 ) ;
@@ -204,26 +180,26 @@ const HomeScreen: React.FC<HomeScreenProps> = ({ username }) => {
204180 { question ?. options . map ( ( option ) => (
205181 < Button
206182 key = { option }
207- title = { option ! }
183+ title = { option }
208184 onPress = { ( ) => {
209185 if ( option === question . correctAnswer ) {
210- if ( game ? .playerOneId === username ) {
186+ if ( game . playerOneId === username ) {
211187 client . models . Game . update ( {
212- id : game ! . id ,
213- playerOneScore : ( game ? .playerOneScore ?? 0 ) + 10 ,
214- currentQuestion : currentQuestion ,
188+ id : game . id ,
189+ playerOneScore : ( game . playerOneScore ?? 0 ) + 10 ,
190+ currentQuestion,
215191 } ) ;
216192 } else {
217193 client . models . Game . update ( {
218- id : game ! . id ,
219- playerTwoScore : ( game ? .playerTwoScore ?? 0 ) + 10 ,
220- currentQuestion : currentQuestion ,
194+ id : game . id ,
195+ playerTwoScore : ( game . playerTwoScore ?? 0 ) + 10 ,
196+ currentQuestion,
221197 } ) ;
222198 }
223199 } else {
224200 client . models . Game . update ( {
225- id : game ! . id ,
226- currentQuestion : currentQuestion ,
201+ id : game . id ,
202+ currentQuestion,
227203 } ) ;
228204 }
229205 } }
@@ -232,11 +208,7 @@ const HomeScreen: React.FC<HomeScreenProps> = ({ username }) => {
232208 </ >
233209 ) ;
234210 case "error" :
235- return (
236- < >
237- < Text style = { styles . welcomeText } > There is an error.</ Text >
238- </ >
239- ) ;
211+ return < Text style = { styles . welcomeText } > There is an error.</ Text > ;
240212 default :
241213 return < Text > Unknown state</ Text > ;
242214 }
0 commit comments