Fix game reset and highscore updates

This commit is contained in:
Arnaud Vergnet 2021-05-22 15:45:24 +02:00
parent 9ae585bdf8
commit 8f9c02ff75
2 changed files with 14 additions and 6 deletions

View file

@ -30,12 +30,12 @@ export default function FullGamePodium(props: Props) {
<View style={styles.topScoreSubcontainer}> <View style={styles.topScoreSubcontainer}>
<GamePodium <GamePodium
place={3} place={3}
score={silver.toString()} score={bronze.toString()}
isHighScore={isHighScore} isHighScore={isHighScore}
/> />
<GamePodium <GamePodium
place={2} place={2}
score={bronze.toString()} score={silver.toString()}
isHighScore={isHighScore} isHighScore={isHighScore}
/> />
</View> </View>

View file

@ -17,7 +17,7 @@
* along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
*/ */
import React, { useLayoutEffect, useRef, useState } from 'react'; import React, { useCallback, useLayoutEffect, useRef, useState } from 'react';
import { StyleSheet, View } from 'react-native'; import { StyleSheet, View } from 'react-native';
import { useTheme } from 'react-native-paper'; import { useTheme } from 'react-native-paper';
import i18n from 'i18n-js'; import i18n from 'i18n-js';
@ -41,7 +41,7 @@ import {
getPreferenceObject, getPreferenceObject,
PreferenceKeys, PreferenceKeys,
} from '../../../utils/asyncStorage'; } from '../../../utils/asyncStorage';
import { useNavigation } from '@react-navigation/core'; import { useFocusEffect, useNavigation } from '@react-navigation/core';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
@ -110,6 +110,13 @@ export default function GameMainScreen() {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [navigation]); }, [navigation]);
useFocusEffect(
useCallback(() => {
const l = logic.current;
return () => l.endGame(true);
}, [])
);
const getRightButton = () => ( const getRightButton = () => (
<MaterialHeaderButtons> <MaterialHeaderButtons>
<Item title={'pause'} iconName={'pause'} onPress={togglePause} /> <Item title={'pause'} iconName={'pause'} onPress={togglePause} />
@ -146,8 +153,9 @@ export default function GameMainScreen() {
if (newScores.length > 3) { if (newScores.length > 3) {
newScores.splice(3, 1); newScores.splice(3, 1);
} }
console.log(newScores); if (newScores.some((item, i) => item !== savedScores[i])) {
updatePreferences(PreferenceKeys.gameScores, newScores); updatePreferences(PreferenceKeys.gameScores, newScores);
}
if (!isRestart) { if (!isRestart) {
navigation.replace(MainRoutes.GameStart, { navigation.replace(MainRoutes.GameStart, {
score: score, score: score,