diff --git a/App.js b/App.js index c6e9270..9c207d1 100644 --- a/App.js +++ b/App.js @@ -9,6 +9,7 @@ import SplashScreen from 'react-native-splash-screen'; import messaging from '@react-native-firebase/messaging'; import AppStore from './src/store/AppStore'; import AuthStore from './src/store/AuthStore'; +import GameStore from './src/store/GameStore'; const App = () => { const getTheme = async () => { @@ -31,6 +32,8 @@ const App = () => { }; useEffect(() => { SplashScreen.hide(); + GameStore.scoreGraber(); + // AsyncStorage.removeItem(StorageConstants.SCORE); setTimeout(() => { AuthStore.setField('isLoggedin', true); }, 1500); diff --git a/src/assets/all.png b/src/assets/all.png new file mode 100644 index 0000000..e7c0cbd Binary files /dev/null and b/src/assets/all.png differ diff --git a/src/assets/score.png b/src/assets/score.png new file mode 100644 index 0000000..dff8bf5 Binary files /dev/null and b/src/assets/score.png differ diff --git a/src/screens/Game.js b/src/screens/Game.js index 4ca03ed..db74ed3 100644 --- a/src/screens/Game.js +++ b/src/screens/Game.js @@ -66,15 +66,7 @@ const Game = () => { source={IconPack.CHESS} /> - - {KEY} - + {KEY} { setShowCords(!showCords); @@ -100,6 +92,10 @@ const Game = () => { var rowValuse = ROW_NAME[secondLatter]; if (colValuse === col + 1 && rowValuse === row + 1) { randomGenrator(); + GameStore.setField( + 'currentScore', + GameStore.currentScore + 1, + ); } else { setColor('red'); setTimeout(() => { @@ -111,6 +107,9 @@ const Game = () => { showCords={showCords} /> + + {GameStore.currentScore} + )} @@ -128,4 +127,28 @@ const styles = StyleSheet.create({ }, eyeImageStyle: {height: 25, width: 25}, tintColorWhite: {tintColor: '#fff'}, + keyText: { + color: '#fff', + fontSize: 37, + fontWeight: '900', + textAlign: 'center', + }, + scoreView: { + padding: 26, + backgroundColor: ThemeStore.darkColor, + borderRadius: 10, + justifyContent: 'center', + alignItems: 'center', + + shadowColor: ThemeStore.lightColor, + shadowOffset: { + width: 0, + height: 4, + }, + shadowOpacity: 0.32, + shadowRadius: 5.46, + + elevation: 9, + }, + scoreText: {color: '#fff', fontSize: 20, fontWeight: 'bold'}, }); diff --git a/src/screens/Menu.js b/src/screens/Menu.js index 20abd0d..764af3a 100644 --- a/src/screens/Menu.js +++ b/src/screens/Menu.js @@ -1,53 +1,71 @@ -import React, {useEffect, useState} from 'react'; -import { - Image, - StyleSheet, - Text, - TouchableHighlight, - View, - // Animated, - ScrollView, -} from 'react-native'; +import React from 'react'; +import {Image, StyleSheet, Text, TouchableHighlight, View} from 'react-native'; import AppStore from '../store/AppStore'; import ThemeStore from '../store/ThemeStore'; import IconPack from '../utils/IconPack'; import GameTypeModal from './components/GameTypeModal'; import GameStore from '../store/GameStore'; import Animated, {FadeIn} from 'react-native-reanimated'; +import {Observer} from 'mobx-react'; const Menu = () => { return ( - - - - - { - AppStore.handelScreenNavigation('Theme'); - }}> - - - Theme - - - { - // AppStore.handelScreenNavigation('Game'); - GameStore.setField('showGameTypeModal', true); - }}> - - - Game + + {() => ( + + + + + { + AppStore.handelScreenNavigation('Theme'); + }}> + + + Theme + + + { + GameStore.setField('showGameTypeModal', true); + }}> + + + Game + + + {GameStore.avgScore > 0 && ( + + + + + + {GameStore.avgScore} + + + + )} + {GameStore.totalScore > 0 && ( + + + + + + {GameStore.totalScore} + + + + )} - + - - + )} + ); }; @@ -68,14 +86,19 @@ const styles = StyleSheet.create({ flexWrap: { flexDirection: 'row', flexWrap: 'wrap', - justifyContent: 'space-between', - paddingHorizontal: 16, + justifyContent: 'space-around', + paddingHorizontal: 10, }, iconStyle: {height: 25, width: 25}, highlightStyle: { - padding: 24, + // padding: 24, + height: 100, + width: 100, + justifyContent: 'center', + alignItems: 'center', backgroundColor: '#414149', marginHorizontal: 10, borderRadius: 10, + marginTop: 10, }, }); diff --git a/src/screens/components/GameOver.js b/src/screens/components/GameOver.js index 6e3ee50..b0719e3 100644 --- a/src/screens/components/GameOver.js +++ b/src/screens/components/GameOver.js @@ -3,8 +3,20 @@ import {View, Text, Modal, TouchableOpacity, StyleSheet} from 'react-native'; import GameStore from '../../store/GameStore'; import {Observer} from 'mobx-react'; import AppStore from '../../store/AppStore'; +import {getFromAsyncStorage, storeToAsyncStorage} from '../../utils/helper'; +import StorageConstants from '../../utils/StorageConstants'; const GameOver = () => { + const onPressRestart = () => { + GameStore.setField('showGameOverModal', false); + GameStore.scoreSaver(); + GameStore.setField('counter', GameStore.selectedGameTime); + }; + const onPressClose = () => { + GameStore.setField('showGameOverModal', false); + GameStore.scoreSaver(); + AppStore.handelScreenGoBack(); + }; return ( {() => ( @@ -15,11 +27,7 @@ const GameOver = () => { transparent={true}> - { - GameStore.setField('showGameOverModal', false); - AppStore.handelScreenGoBack(); - }}> + onPressClose()}> Close @@ -34,10 +42,7 @@ const GameOver = () => { { - GameStore.setField('showGameOverModal', false); - GameStore.setField('counter', GameStore.selectedGameTime); - }}> + onPress={() => onPressRestart()}> Restart diff --git a/src/screens/components/GameTypeModal.js b/src/screens/components/GameTypeModal.js index 0eb3c80..62da718 100644 --- a/src/screens/components/GameTypeModal.js +++ b/src/screens/components/GameTypeModal.js @@ -11,9 +11,21 @@ import GameStore from '../../store/GameStore'; import {Observer} from 'mobx-react'; import AppStore from '../../store/AppStore'; import IconPack from '../../utils/IconPack'; +import {storeToAsyncStorage} from '../../utils/helper'; +import StorageConstants from '../../utils/StorageConstants'; const GameTypeModal = () => { - const TimeData = [30, 60, 120, 180, 240, '∞']; + const TimeData = [5, 30, 60, 120, 180, 240, '∞']; + const onPressTime = i => { + storeToAsyncStorage( + StorageConstants.GAME_PLAYED_TIME, + JSON.stringify(GameStore.gamePlayedTime + 1), + ); + GameStore.setField('showGameTypeModal', false); + GameStore.setField('selectedGameTime', i != '∞' ? i : -1); + GameStore.setField('counter', i != '∞' ? i : -1); + AppStore.handelScreenNavigation('Game'); + }; return ( {() => ( @@ -32,12 +44,7 @@ const GameTypeModal = () => { key={index} style={styles.highlightStyle} underlayColor={'#4141498f'} - onPress={() => { - GameStore.setField('showGameTypeModal', false); - GameStore.setField('selectedGameTime', i != '∞' ? i : -1); - GameStore.setField('counter', i != '∞' ? i : -1); - AppStore.handelScreenNavigation('Game'); - }}> + onPress={() => onPressTime(i)}> { + try { + let score = 0; + try { + score = JSON.parse(await getFromAsyncStorage(StorageConstants.SCORE)); + } catch (error) { + console.log('error getting score', error); + } + + if (score != 0) { + await storeToAsyncStorage( + StorageConstants.SCORE, + JSON.stringify(score + this.currentScore), + ); + this.setField('totalScore', score + this.currentScore); + } else { + await storeToAsyncStorage( + StorageConstants.SCORE, + JSON.stringify(this.currentScore), + ); + this.setField('totalScore', this.currentScore); + } + + let gamePlayedTime = 0; + try { + gamePlayedTime = JSON.parse( + await getFromAsyncStorage(StorageConstants.GAME_PLAYED_TIME), + ); + } catch (error) { + console.log('error getting score', error); + } + if (gamePlayedTime) { + this.gamePlayedTime = gamePlayedTime; + } + + this.setField('avgScore', this.totalScore / this.gamePlayedTime); + + setTimeout(() => { + this.setField('currentScore', 0); + }, 700); + } catch (error) { + console.log(error, 'error scoreSaver'); + } + }; + + scoreGraber = async () => { + try { + let score = 0; + try { + score = JSON.parse(await getFromAsyncStorage(StorageConstants.SCORE)); + } catch (error) { + console.log('error getting score', error); + } + if (score) { + this.totalScore = score; + } + + let gamePlayedTime = 0; + try { + gamePlayedTime = JSON.parse( + await getFromAsyncStorage(StorageConstants.GAME_PLAYED_TIME), + ); + } catch (error) { + console.log('error getting score', error); + } + if (gamePlayedTime) { + this.gamePlayedTime = gamePlayedTime; + } + } catch (error) { + console.log(error, 'error scoreGraber'); + } + }; } + const GameStore = new gameStore(); export default GameStore; diff --git a/src/utils/IconPack.js b/src/utils/IconPack.js index 2d0e4a1..8fc51ff 100644 --- a/src/utils/IconPack.js +++ b/src/utils/IconPack.js @@ -4,4 +4,6 @@ export default { THEME: require('../assets/theme.png'), CHESS: require('../assets/chess.png'), CHESS_CLOCK: require('../assets/chessClock.png'), + SCORE: require('../assets/score.png'), + ALL: require('../assets/all.png'), }; diff --git a/src/utils/StorageConstants.js b/src/utils/StorageConstants.js index 8f5eae0..8e4b5bd 100644 --- a/src/utils/StorageConstants.js +++ b/src/utils/StorageConstants.js @@ -1,3 +1,5 @@ export default { DEFAULT_THEME: 'Default_Theme', + SCORE: 'Score', + GAME_PLAYED_TIME: 'Game_Played_Time', };