Pénalité lorsqu'on échoue un défi
This commit is contained in:
parent
63ad84eb8c
commit
4cb2677f45
@ -70,7 +70,7 @@ function ChallengeScreenBody() {
|
|||||||
challenge={currentChallenge}
|
challenge={currentChallenge}
|
||||||
onSuccess={() => { setLoading(true); endChallenge.mutate({ success: true }) }}
|
onSuccess={() => { setLoading(true); endChallenge.mutate({ success: true }) }}
|
||||||
onFail={() => endChallenge.mutate({ success: false })} />}
|
onFail={() => endChallenge.mutate({ success: false })} />}
|
||||||
{!loading && !currentChallenge && game.currentRunner && <>
|
{!loading && !game.penaltyEnd && !currentChallenge && game.currentRunner && <>
|
||||||
<Banner
|
<Banner
|
||||||
visible={!currentChallenge && game.currentRunner && !loading}
|
visible={!currentChallenge && game.currentRunner && !loading}
|
||||||
icon='cancel'
|
icon='cancel'
|
||||||
|
@ -29,7 +29,7 @@ export default function ChallengeCard({ challenge, onSuccess, onFail, onDelete,
|
|||||||
</View>
|
</View>
|
||||||
<View style={{ flexWrap: 'wrap', flexDirection: 'row', justifyContent: 'space-around', padding: 15 }}>
|
<View style={{ flexWrap: 'wrap', flexDirection: 'row', justifyContent: 'space-around', padding: 15 }}>
|
||||||
{onFail && <Button key='failBtn' mode='outlined' icon='cancel' onPress={() => onFail()}>
|
{onFail && <Button key='failBtn' mode='outlined' icon='cancel' onPress={() => onFail()}>
|
||||||
Passer
|
Échouer
|
||||||
</Button>}
|
</Button>}
|
||||||
{onSuccess && <Button key='successBtn' mode='contained' icon='check' onPress={() => onSuccess()}>
|
{onSuccess && <Button key='successBtn' mode='contained' icon='check' onPress={() => onSuccess()}>
|
||||||
Terminer
|
Terminer
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import { useAuth } from '@/hooks/useAuth'
|
import { useAuth } from '@/hooks/useAuth'
|
||||||
import { useDownloadChallengeActions } from '@/hooks/useChallengeActions'
|
import { useChallengeActions, useDownloadChallengeActions } from '@/hooks/useChallengeActions'
|
||||||
import { useDownloadChallenges } from '@/hooks/useChallenges'
|
import { useDownloadChallenges } from '@/hooks/useChallenges'
|
||||||
import { useGame, useUpdateActiveChallengeId, useUpdateGameState, useUpdateMoney } from '@/hooks/useGame'
|
import { useGame, useUpdateActiveChallengeId, useUpdateGameState, useUpdateMoney, useUpdatePenalty } from '@/hooks/useGame'
|
||||||
import { useDownloadTrains } from '@/hooks/useTrain'
|
import { useDownloadTrains } from '@/hooks/useTrain'
|
||||||
import { isAuthValid } from '@/utils/features/auth/authSlice'
|
import { isAuthValid } from '@/utils/features/auth/authSlice'
|
||||||
import { ChallengeActionPayload } from '@/utils/features/challengeActions/challengeActionsSlice'
|
import { ChallengeAction, ChallengeActionPayload } from '@/utils/features/challengeActions/challengeActionsSlice'
|
||||||
import { Challenge } from '@/utils/features/challenges/challengesSlice'
|
import { Challenge } from '@/utils/features/challenges/challengesSlice'
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { ReactNode, useEffect } from 'react'
|
import { ReactNode, useEffect } from 'react'
|
||||||
@ -12,7 +12,9 @@ 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 game = useGame()
|
||||||
|
const challengeActions = useChallengeActions()
|
||||||
const updateGameState = useUpdateGameState()
|
const updateGameState = useUpdateGameState()
|
||||||
|
const updatePenalty = useUpdatePenalty()
|
||||||
const updateMoney = useUpdateMoney()
|
const updateMoney = useUpdateMoney()
|
||||||
const updateActiveChallengeId = useUpdateActiveChallengeId()
|
const updateActiveChallengeId = useUpdateActiveChallengeId()
|
||||||
const downloadTrains = useDownloadTrains()
|
const downloadTrains = useDownloadTrains()
|
||||||
@ -77,6 +79,17 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
|||||||
}
|
}
|
||||||
}, [challengesQuery.status, challengesQuery.dataUpdatedAt])
|
}, [challengesQuery.status, challengesQuery.dataUpdatedAt])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const now = new Date().getTime()
|
||||||
|
const activeChallenge: ChallengeAction | undefined = challengeActions.challengeActions
|
||||||
|
.find(challengeAction => challengeAction.penaltyStart && challengeAction.penaltyEnd
|
||||||
|
&& challengeAction.penaltyStart <= now && now <= challengeAction.penaltyEnd)
|
||||||
|
if (!activeChallenge || !game.currentRunner)
|
||||||
|
updatePenalty({ penaltyStart: null, penaltyEnd: null })
|
||||||
|
else if (activeChallenge && (activeChallenge.penaltyStart !== game.penaltyStart || activeChallenge.penaltyEnd))
|
||||||
|
updatePenalty({ penaltyStart: activeChallenge.penaltyStart, penaltyEnd: activeChallenge.penaltyEnd })
|
||||||
|
}, [game.currentRunner, challengeActions])
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
{children}
|
{children}
|
||||||
</>
|
</>
|
||||||
|
@ -41,7 +41,7 @@ export default function PenaltyBanner() {
|
|||||||
</Banner>
|
</Banner>
|
||||||
<ProgressBar
|
<ProgressBar
|
||||||
visible={hasPenalty}
|
visible={hasPenalty}
|
||||||
animatedValue={1 - remainingTime / (3 * 60)}
|
animatedValue={1 - remainingTime / (30 * 60)}
|
||||||
color={MD3Colors.error40}
|
color={MD3Colors.error40}
|
||||||
style={{ height: 6 }} />
|
style={{ height: 6 }} />
|
||||||
</View>
|
</View>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { useAppDispatch, useAppSelector } from "./useStore"
|
import { useAppDispatch, useAppSelector } from "./useStore"
|
||||||
import { GamePayload, setPlayerId, updateActiveChallengeId, updateGameState, updateMoney } from "@/utils/features/game/gameSlice"
|
import { GamePayload, PenaltyPayload, setPlayerId, updateActiveChallengeId, updateGameState, updateMoney, updatePenalty } from "@/utils/features/game/gameSlice"
|
||||||
|
|
||||||
export const useGame = () => useAppSelector((state) => state.game)
|
export const useGame = () => useAppSelector((state) => state.game)
|
||||||
export const useSetPlayerId = () => {
|
export const useSetPlayerId = () => {
|
||||||
@ -18,3 +18,7 @@ export const useUpdateGameState = () => {
|
|||||||
const dispatch = useAppDispatch()
|
const dispatch = useAppDispatch()
|
||||||
return (game: GamePayload) => dispatch(updateGameState(game))
|
return (game: GamePayload) => dispatch(updateGameState(game))
|
||||||
}
|
}
|
||||||
|
export const useUpdatePenalty = () => {
|
||||||
|
const dispatch = useAppDispatch()
|
||||||
|
return (penalty: PenaltyPayload) => dispatch(updatePenalty(penalty))
|
||||||
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||||
import { ChallengeAction } from '../challengeActions/challengeActionsSlice'
|
|
||||||
|
|
||||||
export interface RunPayload {
|
export interface RunPayload {
|
||||||
id: number
|
id: number
|
||||||
@ -16,6 +15,11 @@ export interface GamePayload {
|
|||||||
currentRun: RunPayload | null
|
currentRun: RunPayload | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PenaltyPayload {
|
||||||
|
penaltyStart: number | null
|
||||||
|
penaltyEnd: number | null
|
||||||
|
}
|
||||||
|
|
||||||
export interface GameState {
|
export interface GameState {
|
||||||
playerId: number | null
|
playerId: number | null
|
||||||
gameStarted: boolean
|
gameStarted: boolean
|
||||||
@ -59,10 +63,14 @@ export const gameSlice = createSlice({
|
|||||||
state.chaseFreeTime = null
|
state.chaseFreeTime = null
|
||||||
else if (game.currentRun)
|
else if (game.currentRun)
|
||||||
state.chaseFreeTime = new Date(game.currentRun?.start).getTime() + 45 * 60 * 1000
|
state.chaseFreeTime = new Date(game.currentRun?.start).getTime() + 45 * 60 * 1000
|
||||||
|
},
|
||||||
|
updatePenalty: (state, action: PayloadAction<PenaltyPayload>) => {
|
||||||
|
state.penaltyStart = action.payload.penaltyStart
|
||||||
|
state.penaltyEnd = action.payload.penaltyEnd
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export const { setPlayerId, updateMoney, updateActiveChallengeId, updateGameState } = gameSlice.actions
|
export const { setPlayerId, updateMoney, updateActiveChallengeId, updateGameState, updatePenalty } = gameSlice.actions
|
||||||
|
|
||||||
export default gameSlice.reducer
|
export default gameSlice.reducer
|
||||||
|
Loading…
Reference in New Issue
Block a user