2024-12-11 18:35:36 +00:00
|
|
|
import { useGameRepairMutation, useGameResetMutation, useGameStartMutation, useGameStopMutation, useGameSwitchPlayerMutation } from '@/hooks/mutations/useGameMutation'
|
2024-12-10 17:56:50 +00:00
|
|
|
import { useAuth } from '@/hooks/useAuth'
|
2024-12-19 15:47:41 +00:00
|
|
|
import { useGame, useSetLocationAccuracy, useUpdateGameState } from '@/hooks/useGame'
|
2024-12-14 00:44:32 +00:00
|
|
|
import { useQueryClient } from '@tanstack/react-query'
|
2024-12-09 21:29:48 +00:00
|
|
|
import { useRouter } from 'expo-router'
|
2024-12-11 20:33:51 +00:00
|
|
|
import { useState } from 'react'
|
2024-12-19 15:47:41 +00:00
|
|
|
import { Button, Dialog, List, MD3Colors, Portal, Snackbar, Surface, Text } from 'react-native-paper'
|
|
|
|
import { Dropdown } from 'react-native-paper-dropdown'
|
|
|
|
import { Accuracy } from 'expo-location'
|
2024-12-09 17:29:48 +00:00
|
|
|
|
|
|
|
export default function HistoryScreen() {
|
2024-12-14 00:44:32 +00:00
|
|
|
const [successVisible, setSuccessVisible] = useState(false)
|
|
|
|
const [successMessage, setSuccessMessage] = useState("")
|
|
|
|
const [errorVisible, setErrorVisible] = useState(false)
|
|
|
|
const [error, setError] = useState([200, ""])
|
|
|
|
|
2024-12-09 21:29:48 +00:00
|
|
|
const router = useRouter()
|
2024-12-14 00:44:32 +00:00
|
|
|
const queryClient = useQueryClient()
|
2024-12-10 17:56:50 +00:00
|
|
|
const auth = useAuth()
|
2024-12-11 18:35:36 +00:00
|
|
|
const game = useGame()
|
2024-12-11 20:33:51 +00:00
|
|
|
const updateGameState = useUpdateGameState()
|
2024-12-19 15:47:41 +00:00
|
|
|
const setLocationAccuracy = useSetLocationAccuracy()
|
|
|
|
|
|
|
|
const accuracyArrayList = [
|
|
|
|
{ value: Accuracy.BestForNavigation.toString(), label: "Navigation" },
|
|
|
|
{ value: Accuracy.Highest.toString(), label: "Plus haute" },
|
|
|
|
{ value: Accuracy.High.toString(), label: "Haute" },
|
|
|
|
{ value: Accuracy.Balanced.toString(), label: "Équilibrée" },
|
|
|
|
{ value: Accuracy.Low.toString(), label: "Basse" },
|
|
|
|
{ value: Accuracy.Lowest.toString(), label: "Plus basse" },
|
|
|
|
{ value: 'null', label: "Désactivée" },
|
|
|
|
]
|
2024-12-11 18:35:36 +00:00
|
|
|
|
|
|
|
const gameStartMutation = useGameStartMutation({
|
|
|
|
auth,
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState,
|
2024-12-14 00:44:32 +00:00
|
|
|
onPostSuccess: () => {
|
|
|
|
setSuccessVisible(true)
|
|
|
|
setSuccessMessage("Jeu démarré avec succès")
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-game' })
|
|
|
|
},
|
|
|
|
onError: ({ response, error }) => {
|
|
|
|
setErrorVisible(true)
|
|
|
|
if (response)
|
|
|
|
setError([response.statusCode, response.message])
|
|
|
|
else if (error)
|
|
|
|
setError([400, error.message])
|
|
|
|
},
|
2024-12-11 18:35:36 +00:00
|
|
|
})
|
|
|
|
const gameStopMutation = useGameStopMutation({
|
|
|
|
auth,
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState,
|
2024-12-14 00:44:32 +00:00
|
|
|
onPostSuccess: () => {
|
|
|
|
setSuccessVisible(true)
|
|
|
|
setSuccessMessage("Jeu arrêté avec succès")
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-game' })
|
|
|
|
},
|
|
|
|
onError: ({ response, error }) => {
|
|
|
|
setErrorVisible(true)
|
|
|
|
if (response)
|
|
|
|
setError([response.statusCode, response.message])
|
|
|
|
else if (error)
|
|
|
|
setError([400, error.message])
|
|
|
|
},
|
2024-12-11 18:35:36 +00:00
|
|
|
})
|
|
|
|
const gameSwitchMutation = useGameSwitchPlayerMutation({
|
|
|
|
auth,
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState,
|
2024-12-14 00:44:32 +00:00
|
|
|
onPostSuccess: () => {
|
|
|
|
setSuccessVisible(true)
|
|
|
|
setSuccessMessage("Échange de joueuse en course réalisé avec succès")
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-game' })
|
|
|
|
},
|
|
|
|
onError: ({ response, error }) => {
|
|
|
|
setErrorVisible(true)
|
|
|
|
if (response)
|
|
|
|
setError([response.statusCode, response.message])
|
|
|
|
else if (error)
|
|
|
|
setError([400, error.message])
|
|
|
|
},
|
2024-12-11 18:35:36 +00:00
|
|
|
})
|
|
|
|
const gameRepairMutation = useGameRepairMutation({
|
|
|
|
auth,
|
2024-12-14 00:44:32 +00:00
|
|
|
onPostSuccess: () => {
|
|
|
|
setSuccessVisible(true)
|
|
|
|
setSuccessMessage("Réparation du jeu effectuée avec succès")
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-game' })
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-player' })
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'money-updates' })
|
|
|
|
},
|
|
|
|
onError: ({ response, error }) => {
|
|
|
|
setErrorVisible(true)
|
|
|
|
if (response)
|
|
|
|
setError([response.statusCode, response.message])
|
|
|
|
else if (error)
|
|
|
|
setError([400, error.message])
|
|
|
|
},
|
2024-12-11 18:35:36 +00:00
|
|
|
})
|
|
|
|
const gameResetMutation = useGameResetMutation({
|
|
|
|
auth,
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState,
|
2024-12-14 00:44:32 +00:00
|
|
|
onPostSuccess: () => {
|
|
|
|
setSuccessVisible(true)
|
|
|
|
setSuccessMessage("Jeu réinitialisé avec succès")
|
|
|
|
queryClient.invalidateQueries()
|
|
|
|
},
|
|
|
|
onError: ({ response, error }) => {
|
|
|
|
setErrorVisible(true)
|
|
|
|
if (response)
|
|
|
|
setError([response.statusCode, response.message])
|
|
|
|
else if (error)
|
|
|
|
setError([400, error.message])
|
|
|
|
},
|
2024-12-11 18:35:36 +00:00
|
|
|
})
|
|
|
|
|
2024-12-11 20:33:51 +00:00
|
|
|
const [resetConfirmVisible, setResetConfirmVisible] = useState(false)
|
|
|
|
|
2024-12-09 17:29:48 +00:00
|
|
|
return (
|
2024-12-09 21:29:48 +00:00
|
|
|
<Surface
|
|
|
|
style={{ flex: 1 }}>
|
2024-12-11 18:35:36 +00:00
|
|
|
<List.Section title={"Paramètres"}>
|
|
|
|
<List.Item
|
|
|
|
key={"login"}
|
|
|
|
title="Connexion au serveur"
|
|
|
|
description={auth.loggedIn ? "Vous êtes déjà connecté⋅e" : "Vous n'êtes pas connecté⋅e"}
|
2024-12-14 11:53:56 +00:00
|
|
|
right={() => <List.Icon icon="login" />}
|
2024-12-11 18:35:36 +00:00
|
|
|
onPress={() => router.navigate('/login')} />
|
2024-12-19 15:47:41 +00:00
|
|
|
<List.Item
|
|
|
|
key={"location-accuracy"}
|
|
|
|
title="Précision de la géolocalisation"
|
|
|
|
description="Réglez le niveau de précision de la géolocalisation. Une valeur élevée indique une consommation de batterie accrue."
|
|
|
|
right={() => <Dropdown label={"Géolocalisation"} hideMenuHeader={true} options={accuracyArrayList} value={game.settings.locationAccuracy?.toString() ?? 'null'} onSelect={(value) => setLocationAccuracy(!value || value == 'null' ? null : +value)} />} />
|
2024-12-11 18:35:36 +00:00
|
|
|
</List.Section>
|
|
|
|
<List.Section title={"Gestion du jeu"}>
|
|
|
|
<List.Item
|
|
|
|
key={"start"}
|
|
|
|
title="Démarrer le jeu"
|
|
|
|
disabled={game.gameStarted}
|
2024-12-14 11:53:56 +00:00
|
|
|
right={() => <List.Icon icon="play" color={!game.gameStarted ? undefined : MD3Colors.secondary30} />}
|
2024-12-11 18:35:36 +00:00
|
|
|
onPress={() => gameStartMutation.mutate()} />
|
|
|
|
<List.Item
|
|
|
|
key={"stop"}
|
|
|
|
title="Arrêter le jeu"
|
|
|
|
disabled={!game.gameStarted}
|
2024-12-14 11:53:56 +00:00
|
|
|
right={() => <List.Icon icon="stop" color={game.gameStarted ? undefined : MD3Colors.secondary30} />}
|
2024-12-11 18:35:36 +00:00
|
|
|
onPress={() => gameStopMutation.mutate()} />
|
|
|
|
<List.Item
|
|
|
|
key={"switch"}
|
|
|
|
title="Changer de joueur⋅se en course"
|
|
|
|
description="À utiliser après une capture"
|
|
|
|
disabled={!game.gameStarted}
|
2024-12-14 11:53:56 +00:00
|
|
|
right={() => <List.Icon icon="exit-run" color={game.gameStarted ? undefined : MD3Colors.secondary30} />}
|
2024-12-11 18:35:36 +00:00
|
|
|
onPress={() => gameSwitchMutation.mutate()} />
|
|
|
|
</List.Section>
|
|
|
|
<List.Section title={"Avancé"}>
|
|
|
|
<List.Item
|
|
|
|
key={"repair"}
|
|
|
|
title="Réparer"
|
|
|
|
description="Permet de réparer les soldes des joueur⋅ses à partir des défis réalisés et des trains emprunter. À manipuler avec précaution."
|
2024-12-14 11:53:56 +00:00
|
|
|
right={() => <List.Icon icon='tools' color={MD3Colors.error60} />}
|
2024-12-11 18:35:36 +00:00
|
|
|
onPress={() => gameRepairMutation.mutate()} />
|
|
|
|
<List.Item
|
|
|
|
key={"reset"}
|
|
|
|
title="Réinitialiser les données de jeu"
|
|
|
|
description="Permet de détruire toutes les données. À manipuler avec précaution."
|
2024-12-14 11:53:56 +00:00
|
|
|
right={() => <List.Icon icon="reload-alert" color={MD3Colors.error60} />}
|
2024-12-11 20:33:51 +00:00
|
|
|
onPress={() => setResetConfirmVisible(true)} />
|
2024-12-11 18:35:36 +00:00
|
|
|
</List.Section>
|
2024-12-14 00:44:32 +00:00
|
|
|
<Snackbar
|
|
|
|
key='success-snackbar'
|
|
|
|
visible={successVisible}
|
|
|
|
icon={'close'}
|
|
|
|
onDismiss={() => setSuccessVisible(false)}
|
|
|
|
onIconPress={() => setSuccessVisible(false)}>
|
|
|
|
<Text variant='bodyMedium' style={{ color: MD3Colors.secondary0 }}>
|
|
|
|
{successMessage}
|
|
|
|
</Text>
|
|
|
|
</Snackbar>
|
|
|
|
<Snackbar
|
|
|
|
key='error-snackbar'
|
|
|
|
visible={errorVisible}
|
|
|
|
icon={'close'}
|
|
|
|
onDismiss={() => setErrorVisible(false)}
|
|
|
|
onIconPress={() => setErrorVisible(false)}>
|
|
|
|
<Text variant='bodyMedium' style={{ color: MD3Colors.secondary0 }}>
|
|
|
|
Erreur {error[0]} : {error[1]}
|
|
|
|
</Text>
|
|
|
|
</Snackbar>
|
2024-12-11 20:33:51 +00:00
|
|
|
<Portal>
|
|
|
|
<Dialog key="confirmReset" visible={resetConfirmVisible} onDismiss={() => setResetConfirmVisible(false)}>
|
|
|
|
<Dialog.Title>Confirmer</Dialog.Title>
|
|
|
|
<Dialog.Content>
|
|
|
|
<Text variant="bodyMedium">
|
|
|
|
Cette action va réinitialiser TOUTES les données de jeu : l'historique des positions, les défis réalisés et les trains empruntés.
|
|
|
|
Êtes-vous réellement sûr⋅e de vouloir tout supprimer ?
|
|
|
|
</Text>
|
|
|
|
</Dialog.Content>
|
|
|
|
<Dialog.Actions>
|
|
|
|
<Button onPress={() => setResetConfirmVisible(false)}>Annuler</Button>
|
|
|
|
<Button onPress={() => { setResetConfirmVisible(false); gameResetMutation.mutate() }}>Confirmer</Button>
|
|
|
|
</Dialog.Actions>
|
|
|
|
</Dialog>
|
|
|
|
</Portal>
|
2024-12-09 20:00:15 +00:00
|
|
|
</Surface>
|
2024-12-09 17:29:48 +00:00
|
|
|
)
|
|
|
|
}
|