-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
181 lines (155 loc) · 4.95 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {
Button,
Dimensions,
Image,
ImageBackground,
Platform,
SafeAreaView,
ScrollView,
StyleSheet,
View
} from 'react-native';
import {Table} from "./components/Table";
import {Score} from "./components/Score";
import React, {useEffect, useRef, useState} from "react";
import Toast from 'react-native-toast-message';
import {toastConfig, toastSettings} from "./common/ToastConfig";
import {Success} from "./components/Success";
import {LevelSelect} from "./components/LevelSelect";
import {GameService} from "./services/GamseService";
import {Variants} from "./components/Variants";
import * as SplashScreen from 'expo-splash-screen';
import { Asset } from 'expo-asset';
function cacheImages(images) {
return images.map(image => {
if (typeof image === 'string') {
return Image.prefetch(image);
} else {
return Asset.fromModule(image).downloadAsync();
}
});
}
SplashScreen.preventAutoHideAsync();
export default function App() {
const gameRef = useRef(new GameService());
const game = gameRef.current;
const [render, setRender] = useState(0);
const showSelectLevel = () => {
game.stop()
setRender(Math.random())
}
const handleLevelClick = (num: number) => {
game.startNewGame(num)
next()
}
const next = () => {
game.getNextQuestion()
setRender(Math.random())
}
const check = (answer: number) => {
if (game.checkAnswer(answer)) {
Toast.show({...toastSettings, ...{type: 'success', text1: '👍'}});
} else {
Toast.show({...toastSettings, ...{type: 'error', text1: ':('}});
}
setTimeout(next, 500)
}
const onReloadClick = () => {
showSelectLevel()
}
useEffect(() => {
showSelectLevel()
}, [])
const [appIsReady, setAppIsReady] = useState(false);
// Load any resources or data that you need prior to rendering the app
useEffect(() => {
async function loadResourcesAndDataAsync() {
try {
const imageAssets = cacheImages([
require('./assets/splash.png'),
require('./assets/back.png'),
]);
await Promise.all([...imageAssets]);
} catch (e) {
// You might want to provide this error information to an error reporting service
console.warn(e);
} finally {
setAppIsReady(true);
SplashScreen.hideAsync();
}
}
loadResourcesAndDataAsync();
}, []);
if (!appIsReady) {
return null;
}
if (game.gameStarted && game.opened.size >= game.variantsCount) {
return <View style={styles.container}>
<Success/>
<View style={{marginBottom: 40}}>
<Button title={"start over / начать сначала"} onPress={onReloadClick}></Button>
</View>
</View>
}
const mainRender = () => {
if (!game.gameStarted) {
return (
<ImageBackground source={require('./assets/splash.png')} style={styles.image}>
<LevelSelect onSelect={handleLevelClick}/>
</ImageBackground>
)
}
return (
<ImageBackground source={require('./assets/back.png')} style={styles.image}>
<View style={styles.container}>
<View style={{...styles.block, minHeight: 60}}>
<Score counters={game.counters}/>
</View>
<View style={styles.block}>
<Table opened={game.opened} quest={game.currentQuestion} maxNumber={game.maxNumber}/>
</View>
<View style={styles.block}>
<Variants variants={game.proposedVariants} lastAnswerState={game.lastAnswerState} onClick={check}/>
</View>
<Toast config={toastConfig}/>
</View>
</ImageBackground>
);
}
let ScreenHeight = Dimensions.get("window").height;
return (
<ScrollView scrollEnabled={false} style={styles.containerWrapper}>
<View style={{height:ScreenHeight}}>
{mainRender()}
</View>
</ScrollView>
)
}
const styles = StyleSheet.create({
containerWrapper: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#15131B',
},
block: {
width:100+'%',
justifyContent: 'center',
alignItems: 'center',
},
container: {
marginTop: Platform.OS === 'ios' ? 40 : 20,
marginBottom: Platform.OS === 'ios' ? 60 : 40,
flex: 1,
alignItems: 'center',
justifyContent: 'space-between',
},
score: {
paddingTop: 40,
flexDirection: 'row',
},
image: {
flex: 1,
resizeMode: 'cover',
justifyContent: 'center',
},
});