Récupération du solde de points et affichage sur la carte
This commit is contained in:
parent
db560c401a
commit
49c320b2db
@ -2,16 +2,33 @@ import { StyleSheet } from 'react-native'
|
|||||||
import "maplibre-gl/dist/maplibre-gl.css"
|
import "maplibre-gl/dist/maplibre-gl.css"
|
||||||
import { useBackgroundPermissions } from 'expo-location'
|
import { useBackgroundPermissions } from 'expo-location'
|
||||||
import Map from '@/components/Map'
|
import Map from '@/components/Map'
|
||||||
import { Surface, Text } from 'react-native-paper'
|
import { FAB, Surface, Text } from 'react-native-paper'
|
||||||
|
import { useGame } from '@/hooks/useGame'
|
||||||
|
import { FontAwesome6 } from '@expo/vector-icons'
|
||||||
|
|
||||||
export default function MapScreen() {
|
export default function MapScreen() {
|
||||||
const [backgroundStatus, requestBackgroundPermission] = useBackgroundPermissions()
|
const [backgroundStatus, requestBackgroundPermission] = useBackgroundPermissions()
|
||||||
if (!backgroundStatus?.granted && backgroundStatus?.canAskAgain)
|
if (!backgroundStatus?.granted && backgroundStatus?.canAskAgain)
|
||||||
requestBackgroundPermission()
|
requestBackgroundPermission()
|
||||||
|
|
||||||
|
const game = useGame()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Surface style={styles.page}>
|
<Surface style={styles.page}>
|
||||||
{backgroundStatus?.granted ? <Map /> : <Text>La géolocalisation est requise pour utiliser la carte.</Text>}
|
{backgroundStatus?.granted ? <Map /> : <Text>La géolocalisation est requise pour utiliser la carte.</Text>}
|
||||||
|
<FAB
|
||||||
|
style={styles.moneyBadge}
|
||||||
|
visible={game.gameStarted || game.money > 0}
|
||||||
|
icon={(props) => <FontAwesome6 {...props} name='coins' size={20} />}
|
||||||
|
color='black'
|
||||||
|
label={`${game.money}`} />
|
||||||
|
<FAB
|
||||||
|
style={styles.statusBadge}
|
||||||
|
visible={game.gameStarted || game.money > 0}
|
||||||
|
size='small'
|
||||||
|
color='black'
|
||||||
|
icon={game.currentRunner ? 'run-fast' : 'police-badge'}
|
||||||
|
label={game.currentRunner ? "Coureuse" : "Poursuiveuse"} />
|
||||||
</Surface>
|
</Surface>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -26,4 +43,16 @@ const styles = StyleSheet.create({
|
|||||||
flex: 1,
|
flex: 1,
|
||||||
alignSelf: 'stretch',
|
alignSelf: 'stretch',
|
||||||
},
|
},
|
||||||
|
moneyBadge: {
|
||||||
|
position: 'absolute',
|
||||||
|
top: 40,
|
||||||
|
right: 20,
|
||||||
|
backgroundColor: 'orange',
|
||||||
|
},
|
||||||
|
statusBadge: {
|
||||||
|
position: 'absolute',
|
||||||
|
top: 40,
|
||||||
|
left: 20,
|
||||||
|
backgroundColor: 'pink',
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
@ -15,6 +15,7 @@ import { useStartBackgroundFetchServiceEffect } from '@/utils/background'
|
|||||||
import LoginProvider from '@/components/LoginProvider'
|
import LoginProvider from '@/components/LoginProvider'
|
||||||
import GeolocationProvider from '@/components/GeolocationProvider'
|
import GeolocationProvider from '@/components/GeolocationProvider'
|
||||||
import GameProvider from '@/components/GameProvider'
|
import GameProvider from '@/components/GameProvider'
|
||||||
|
import { FontAwesome6 } from '@expo/vector-icons'
|
||||||
|
|
||||||
const queryClient = new QueryClient({
|
const queryClient = new QueryClient({
|
||||||
defaultOptions: {
|
defaultOptions: {
|
||||||
@ -47,7 +48,11 @@ export default function RootLayout() {
|
|||||||
<LoginProvider loginRedirect={'/login'}>
|
<LoginProvider loginRedirect={'/login'}>
|
||||||
<GeolocationProvider>
|
<GeolocationProvider>
|
||||||
<GameProvider>
|
<GameProvider>
|
||||||
<PaperProvider theme={colorScheme === 'dark' ? MD3DarkTheme : MD3LightTheme}>
|
<PaperProvider
|
||||||
|
theme={colorScheme === 'dark' ? MD3DarkTheme : MD3LightTheme}
|
||||||
|
settings={{
|
||||||
|
// icon: (props) => <FontAwesome6 {...props} />
|
||||||
|
}}>
|
||||||
<ThemeProvider value={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
|
<ThemeProvider value={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
|
||||||
<Stack>
|
<Stack>
|
||||||
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
||||||
|
@ -1,24 +1,39 @@
|
|||||||
import { useAuth } from '@/hooks/useAuth'
|
import { useAuth } from '@/hooks/useAuth'
|
||||||
import { useUpdateGameState } from '@/hooks/useGame'
|
import { useGame, useUpdateGameState, useUpdateMoney } from '@/hooks/useGame'
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { ReactNode, useEffect } from 'react'
|
import { ReactNode, useEffect } from 'react'
|
||||||
|
|
||||||
export default function GameProvider({ children }: { children: ReactNode }) {
|
export default function GameProvider({ children }: { children: ReactNode }) {
|
||||||
const auth = useAuth()
|
const auth = useAuth()
|
||||||
|
const game = useGame()
|
||||||
const updateGameState = useUpdateGameState()
|
const updateGameState = useUpdateGameState()
|
||||||
|
const updateMoney = useUpdateMoney()
|
||||||
|
|
||||||
const gameQuery = useQuery({
|
const gameQuery = useQuery({
|
||||||
queryKey: ['update-game'],
|
queryKey: ['get-game'],
|
||||||
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/`, {
|
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/`, {
|
||||||
headers: { "Authorization": `Bearer ${auth.token}` }}
|
headers: { "Authorization": `Bearer ${auth.token}` }}
|
||||||
).then(resp => resp.json()),
|
).then(resp => resp.json()),
|
||||||
enabled: auth.loggedIn,
|
enabled: auth.loggedIn,
|
||||||
refetchInterval: 5000,
|
refetchInterval: 5000,
|
||||||
})
|
})
|
||||||
const game = gameQuery.data
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (game)
|
if (gameQuery.isSuccess && gameQuery.data)
|
||||||
updateGameState(game)
|
updateGameState(gameQuery.data)
|
||||||
}, [game])
|
}, [gameQuery])
|
||||||
|
|
||||||
|
const playerQuery = useQuery({
|
||||||
|
queryKey: ['get-player'],
|
||||||
|
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/players/${game.playerId}/`, {
|
||||||
|
headers: { "Authorization": `Bearer ${auth.token}` }}
|
||||||
|
).then(resp => resp.json()),
|
||||||
|
enabled: auth.loggedIn && !!game.playerId,
|
||||||
|
refetchInterval: 5000,
|
||||||
|
})
|
||||||
|
useEffect(() => {
|
||||||
|
if (playerQuery.isSuccess && playerQuery.data)
|
||||||
|
updateMoney(playerQuery.data.money)
|
||||||
|
}, [playerQuery])
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
{children}
|
{children}
|
||||||
|
Loading…
Reference in New Issue
Block a user