Boutons pour démarrer le jeu

This commit is contained in:
Emmy D'Anello 2024-12-11 19:35:36 +01:00
parent 7aa9dde5a9
commit c28097d443
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
3 changed files with 224 additions and 5 deletions

View File

@ -1,18 +1,81 @@
import { useGameRepairMutation, useGameResetMutation, useGameStartMutation, useGameStopMutation, useGameSwitchPlayerMutation } from '@/hooks/mutations/useGameMutation'
import { useAuth } from '@/hooks/useAuth'
import { useGame } from '@/hooks/useGame'
import { useRouter } from 'expo-router'
import { FAB, List, Surface } from 'react-native-paper'
export default function HistoryScreen() {
const router = useRouter()
const auth = useAuth()
const game = useGame()
const gameStartMutation = useGameStartMutation({
auth,
game,
})
const gameStopMutation = useGameStopMutation({
auth,
game,
})
const gameSwitchMutation = useGameSwitchPlayerMutation({
auth,
game,
})
const gameRepairMutation = useGameRepairMutation({
auth,
game,
})
const gameResetMutation = useGameResetMutation({
auth,
game,
})
return (
<Surface
style={{ flex: 1 }}>
<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"}
right={() => <FAB icon="login" size="small" onPress={() => router.navigate('/login')} />}
onPress={() => router.navigate('/login')} />
</List.Section>
<List.Section title={"Gestion du jeu"}>
<List.Item
key={"start"}
title="Démarrer le jeu"
disabled={game.gameStarted}
right={() => <FAB icon="play" size="small" disabled={game.gameStarted} />}
onPress={() => gameStartMutation.mutate()} />
<List.Item
key={"stop"}
title="Arrêter le jeu"
disabled={!game.gameStarted}
right={() => <FAB icon="stop" size="small" disabled={!game.gameStarted} />}
onPress={() => gameStopMutation.mutate()} />
<List.Item
key={"switch"}
title="Changer de joueur⋅se en course"
description="À utiliser après une capture"
disabled={!game.gameStarted}
right={() => <FAB icon="exit-run" size="small" disabled={!game.gameStarted} />}
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."
right={() => <FAB icon="reload-alert" size="small" variant={'tertiary'} />}
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."
right={() => <FAB icon="reload-alert" size="small" variant={'tertiary'} />}
onPress={() => gameResetMutation.mutate()} />
</List.Section>
</Surface>
)
}

View File

@ -0,0 +1,155 @@
import { AuthState } from "@/utils/features/auth/authSlice"
import { GameState } from "@/utils/features/game/gameSlice"
import { useMutation } from "@tanstack/react-query"
type ErrorResponse = {
error: string
message: string
statusCode: number
}
type onPostSuccessFunc = () => void
type ErrorFuncProps = { response?: ErrorResponse, error?: Error }
type onErrorFunc = (props: ErrorFuncProps) => void
type GameProps = {
game: GameState
auth: AuthState
onPostSuccess?: onPostSuccessFunc
onError?: onErrorFunc
}
export const useGameStartMutation = ({ game, auth, onPostSuccess, onError }: GameProps) => {
return useMutation({
mutationFn: async () => {
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/start/`, {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
}).then(resp => resp.json())
},
onSuccess: async (data) => {
if (data.error) {
if (onError)
onError({ response: data })
return
}
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}
export const useGameStopMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => {
return useMutation({
mutationFn: async () => {
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/stop/`, {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
}).then(resp => resp.json())
},
onSuccess: async (data) => {
if (data.error) {
if (onError)
onError({ response: data })
return
}
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}
export const useGameSwitchPlayerMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => {
return useMutation({
mutationFn: async () => {
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/switch-running-player/`, {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
}).then(resp => resp.json())
},
onSuccess: async (data) => {
if (data.error) {
if (onError)
onError({ response: data })
return
}
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}
export const useGameRepairMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => {
return useMutation({
mutationFn: async () => {
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/repair/`, {
method: "PUT",
headers: {
"Authorization": `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
}).then(resp => resp.json())
},
onSuccess: async (data) => {
if (data.error) {
if (onError)
onError({ response: data })
return
}
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}
export const useGameResetMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => {
return useMutation({
mutationFn: async () => {
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/reset/`, {
method: "DELETE",
headers: {
"Authorization": `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
}).then(resp => resp.json())
},
onSuccess: async (data) => {
if (data.error) {
if (onError)
onError({ response: data })
return
}
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}

View File

@ -35,6 +35,7 @@ export async function startGeolocationService(): Promise<void | (() => void)> {
accuracy: Location.Accuracy.BestForNavigation,
activityType: Location.ActivityType.OtherNavigation,
deferredUpdatesInterval: 100,
timeInterval: 100,
foregroundService: {
killServiceOnDestroy: false,
notificationBody: "Géolocalisation activée pour « Traintrape-moi »",