|
1 | | -import { StatusBar } from 'expo-status-bar'; |
2 | | -import { StyleSheet, Text, View } from 'react-native'; |
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + View, |
| 5 | + StyleSheet, |
| 6 | + SafeAreaView, |
| 7 | + Text, |
| 8 | + ActivityIndicator, |
| 9 | +} from "react-native"; |
| 10 | + |
| 11 | +import { Amplify } from "aws-amplify"; |
| 12 | +import { Authenticator, useAuthenticator } from "@aws-amplify/ui-react-native"; |
| 13 | + |
| 14 | +import outputs from "./amplify_outputs.json"; |
| 15 | +import { fetchUserAttributes } from "aws-amplify/auth"; |
| 16 | +import { Schema } from "./amplify/data/resource"; |
| 17 | +import { generateClient } from "aws-amplify/data"; |
| 18 | + |
| 19 | +const client = generateClient<Schema>(); |
| 20 | + |
| 21 | +Amplify.configure(outputs); |
| 22 | + |
| 23 | +const SignOutButton: React.FC = () => { |
| 24 | + const { signOut } = useAuthenticator(); |
3 | 25 |
|
4 | | -export default function App() { |
5 | 26 | return ( |
6 | | - <View style={styles.container}> |
7 | | - <Text>Open up App.tsx to start working on your app!</Text> |
8 | | - <StatusBar style="auto" /> |
| 27 | + <View style={styles.signOutButton}> |
| 28 | + <Button title="Sign Out" onPress={signOut} /> |
9 | 29 | </View> |
10 | 30 | ); |
| 31 | +}; |
| 32 | + |
| 33 | +type GameState = "idle" | "searching" | "found" | "quiz" | "error"; |
| 34 | + |
| 35 | +interface HomeScreenProps { |
| 36 | + username: string; |
11 | 37 | } |
12 | 38 |
|
| 39 | +const HomeScreen: React.FC<HomeScreenProps> = ({ username }) => { |
| 40 | + const [gameState, setGameState] = useState<GameState>("idle"); |
| 41 | + const [currentQuestion, setCurrentQuestion] = useState<number>(-1); |
| 42 | + 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"); |
| 58 | + |
| 59 | + client.models.Game.observeQuery({ |
| 60 | + filter: { |
| 61 | + id: { |
| 62 | + eq: currentGames.data[0].id, |
| 63 | + }, |
| 64 | + }, |
| 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 | + } |
| 73 | + }); |
| 74 | + const result = await client.generations.generateQuestions({ |
| 75 | + description: "", |
| 76 | + }); |
| 77 | + |
| 78 | + if (result.errors) { |
| 79 | + setGameState("error"); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const updatedGame = await client.models.Game.update({ |
| 84 | + id: currentGames.data[0].id, |
| 85 | + questions: result.data as Schema["Question"]["type"][], |
| 86 | + }); |
| 87 | + |
| 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, |
| 140 | + }, |
| 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"); |
| 148 | + } |
| 149 | + if (game.items[0].currentQuestion !== currentQuestion) { |
| 150 | + setCurrentQuestion((game.items[0].currentQuestion ?? 0) + 1); |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
| 154 | + }; |
| 155 | + |
| 156 | + const renderContent = (): JSX.Element => { |
| 157 | + switch (gameState) { |
| 158 | + case "idle": |
| 159 | + return ( |
| 160 | + <> |
| 161 | + <Text style={styles.welcomeText}>Welcome {username}!</Text> |
| 162 | + <Button title="Search Game" onPress={handleSearchGame} /> |
| 163 | + </> |
| 164 | + ); |
| 165 | + case "searching": |
| 166 | + return ( |
| 167 | + <> |
| 168 | + <Text style={styles.quizText}>Searching for a game now</Text> |
| 169 | + <ActivityIndicator style={styles.activityIndicator} size="large" /> |
| 170 | + </> |
| 171 | + ); |
| 172 | + case "found": |
| 173 | + return ( |
| 174 | + <> |
| 175 | + <Text style={styles.quizText}> |
| 176 | + Questions are getting generated now... |
| 177 | + </Text> |
| 178 | + <ActivityIndicator style={styles.activityIndicator} size="large" /> |
| 179 | + </> |
| 180 | + ); |
| 181 | + case "quiz": |
| 182 | + const question = game?.questions[currentQuestion]; |
| 183 | + if (currentQuestion === game?.questions.length) { |
| 184 | + return ( |
| 185 | + <> |
| 186 | + <Text style={styles.quizText}>Quiz is over!</Text> |
| 187 | + <Text style={styles.quizText}> |
| 188 | + {game?.playerOneScore === game?.playerTwoScore |
| 189 | + ? "It's a tie!" |
| 190 | + : (game?.playerOneScore ?? 0) > (game?.playerTwoScore ?? 0) |
| 191 | + ? `${ |
| 192 | + game?.playerOneId === username ? "You" : game?.playerOneId |
| 193 | + } won with ${game?.playerOneScore} points!` |
| 194 | + : `${ |
| 195 | + game?.playerTwoId === username ? "You" : game?.playerTwoId |
| 196 | + } won with ${game?.playerTwoScore} points!`} |
| 197 | + </Text> |
| 198 | + </> |
| 199 | + ); |
| 200 | + } |
| 201 | + return ( |
| 202 | + <> |
| 203 | + <Text>{question?.question}</Text> |
| 204 | + {question?.options.map((option) => ( |
| 205 | + <Button |
| 206 | + key={option} |
| 207 | + title={option!} |
| 208 | + onPress={() => { |
| 209 | + if (option === question.correctAnswer) { |
| 210 | + if (game?.playerOneId === username) { |
| 211 | + client.models.Game.update({ |
| 212 | + id: game!.id, |
| 213 | + playerOneScore: (game?.playerOneScore ?? 0) + 10, |
| 214 | + currentQuestion: currentQuestion, |
| 215 | + }); |
| 216 | + } else { |
| 217 | + client.models.Game.update({ |
| 218 | + id: game!.id, |
| 219 | + playerTwoScore: (game?.playerTwoScore ?? 0) + 10, |
| 220 | + currentQuestion: currentQuestion, |
| 221 | + }); |
| 222 | + } |
| 223 | + } else { |
| 224 | + client.models.Game.update({ |
| 225 | + id: game!.id, |
| 226 | + currentQuestion: currentQuestion, |
| 227 | + }); |
| 228 | + } |
| 229 | + }} |
| 230 | + /> |
| 231 | + ))} |
| 232 | + </> |
| 233 | + ); |
| 234 | + case "error": |
| 235 | + return ( |
| 236 | + <> |
| 237 | + <Text style={styles.welcomeText}>There is an error.</Text> |
| 238 | + </> |
| 239 | + ); |
| 240 | + default: |
| 241 | + return <Text>Unknown state</Text>; |
| 242 | + } |
| 243 | + }; |
| 244 | + |
| 245 | + return <View style={styles.contentContainer}>{renderContent()}</View>; |
| 246 | +}; |
| 247 | + |
| 248 | +const App: React.FC = () => { |
| 249 | + const [username, setUsername] = useState<string>(""); |
| 250 | + |
| 251 | + useEffect(() => { |
| 252 | + const fetchUsername = async (): Promise<void> => { |
| 253 | + const userAttributes = await fetchUserAttributes(); |
| 254 | + const fetchedUsername = userAttributes?.preferred_username ?? ""; |
| 255 | + setUsername(fetchedUsername); |
| 256 | + }; |
| 257 | + void fetchUsername(); |
| 258 | + }, []); |
| 259 | + |
| 260 | + return ( |
| 261 | + <Authenticator.Provider> |
| 262 | + <Authenticator> |
| 263 | + <SafeAreaView style={styles.container}> |
| 264 | + <SignOutButton /> |
| 265 | + <HomeScreen username={username} /> |
| 266 | + </SafeAreaView> |
| 267 | + </Authenticator> |
| 268 | + </Authenticator.Provider> |
| 269 | + ); |
| 270 | +}; |
| 271 | + |
13 | 272 | const styles = StyleSheet.create({ |
14 | 273 | container: { |
15 | 274 | flex: 1, |
16 | | - backgroundColor: '#fff', |
17 | | - alignItems: 'center', |
18 | | - justifyContent: 'center', |
| 275 | + }, |
| 276 | + signOutButton: { |
| 277 | + alignSelf: "flex-end", |
| 278 | + }, |
| 279 | + contentContainer: { |
| 280 | + flex: 1, |
| 281 | + justifyContent: "center", |
| 282 | + alignItems: "center", |
| 283 | + }, |
| 284 | + welcomeText: { |
| 285 | + fontSize: 18, |
| 286 | + fontWeight: "bold", |
| 287 | + }, |
| 288 | + quizText: { |
| 289 | + fontSize: 16, |
| 290 | + marginBottom: 16, |
| 291 | + }, |
| 292 | + activityIndicator: { |
| 293 | + padding: 16, |
19 | 294 | }, |
20 | 295 | }); |
| 296 | + |
| 297 | +export default App; |
0 commit comments