2024-12-11 16:26:36 +00:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
2024-12-19 15:47:41 +00:00
|
|
|
import { Accuracy } from 'expo-location'
|
2024-12-19 16:33:17 +00:00
|
|
|
import * as SecureStore from '@/utils/SecureStore'
|
2024-12-11 16:26:36 +00:00
|
|
|
|
2024-12-11 20:33:51 +00:00
|
|
|
export interface RunPayload {
|
|
|
|
id: number
|
|
|
|
gameId: number
|
|
|
|
runnerId: number
|
|
|
|
start: string
|
|
|
|
end: string | null
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GamePayload {
|
|
|
|
id: number
|
|
|
|
started: boolean
|
|
|
|
currentRunId: number | null
|
|
|
|
currentRun: RunPayload | null
|
|
|
|
}
|
|
|
|
|
2024-12-12 23:02:58 +00:00
|
|
|
export interface PenaltyPayload {
|
|
|
|
penaltyStart: number | null
|
|
|
|
penaltyEnd: number | null
|
|
|
|
}
|
|
|
|
|
2024-12-17 21:58:47 +00:00
|
|
|
export interface Player {
|
|
|
|
id: number
|
|
|
|
name: string
|
|
|
|
money: number
|
|
|
|
activeChallengeId: number | null
|
|
|
|
}
|
|
|
|
|
2024-12-11 16:26:36 +00:00
|
|
|
export interface GameState {
|
|
|
|
playerId: number | null
|
2024-12-14 12:35:09 +00:00
|
|
|
runId: number | null
|
2024-12-11 16:26:36 +00:00
|
|
|
gameStarted: boolean
|
|
|
|
money: number
|
|
|
|
currentRunner: boolean
|
2024-12-12 21:55:59 +00:00
|
|
|
activeChallengeId: number | null
|
2024-12-11 20:33:51 +00:00
|
|
|
chaseFreeTime: number | null // date
|
|
|
|
penaltyStart: number | null // date
|
2024-12-12 18:33:44 +00:00
|
|
|
penaltyEnd: number | null //date
|
2024-12-19 15:47:41 +00:00
|
|
|
settings: Settings
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Settings {
|
|
|
|
locationAccuracy: Accuracy | null
|
2024-12-11 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: GameState = {
|
|
|
|
playerId: null,
|
2024-12-14 12:35:09 +00:00
|
|
|
runId: null,
|
2024-12-11 16:26:36 +00:00
|
|
|
gameStarted: false,
|
|
|
|
money: 0,
|
|
|
|
currentRunner: false,
|
2024-12-12 21:55:59 +00:00
|
|
|
activeChallengeId: null,
|
2024-12-11 16:26:36 +00:00
|
|
|
chaseFreeTime: null,
|
|
|
|
penaltyStart: null,
|
|
|
|
penaltyEnd: null,
|
2024-12-19 15:47:41 +00:00
|
|
|
settings: {
|
|
|
|
locationAccuracy: Accuracy.Highest,
|
|
|
|
}
|
2024-12-11 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const gameSlice = createSlice({
|
|
|
|
name: 'game',
|
|
|
|
initialState: initialState,
|
|
|
|
reducers: {
|
|
|
|
setPlayerId: (state, action: PayloadAction<number>) => {
|
|
|
|
state.playerId = action.payload
|
|
|
|
},
|
|
|
|
updateMoney: (state, action: PayloadAction<number>) => {
|
|
|
|
state.money = action.payload
|
|
|
|
},
|
2024-12-12 21:55:59 +00:00
|
|
|
updateActiveChallengeId: (state, action: PayloadAction<number | null>) => {
|
|
|
|
state.activeChallengeId = action.payload
|
|
|
|
},
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState: (state, action: PayloadAction<GamePayload>) => {
|
|
|
|
const game: GamePayload = action.payload
|
|
|
|
state.gameStarted = game.started
|
2024-12-14 12:35:09 +00:00
|
|
|
state.runId = game.currentRunId
|
2024-12-11 20:33:51 +00:00
|
|
|
state.currentRunner = state.playerId === game.currentRun?.runnerId
|
|
|
|
if (state.currentRunner)
|
|
|
|
state.chaseFreeTime = null
|
|
|
|
else if (game.currentRun)
|
|
|
|
state.chaseFreeTime = new Date(game.currentRun?.start).getTime() + 45 * 60 * 1000
|
2024-12-12 23:02:58 +00:00
|
|
|
},
|
|
|
|
updatePenalty: (state, action: PayloadAction<PenaltyPayload>) => {
|
|
|
|
state.penaltyStart = action.payload.penaltyStart
|
|
|
|
state.penaltyEnd = action.payload.penaltyEnd
|
2024-12-19 15:47:41 +00:00
|
|
|
},
|
|
|
|
setLocationAccuracy: (state, action: PayloadAction<Accuracy | null>) => {
|
|
|
|
state.settings.locationAccuracy = action.payload
|
2024-12-19 16:33:17 +00:00
|
|
|
SecureStore.setItem('locationAccuracy', action.payload?.toString() ?? 'null')
|
2024-12-11 20:33:51 +00:00
|
|
|
}
|
2024-12-11 16:26:36 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-12-19 15:47:41 +00:00
|
|
|
export const { setLocationAccuracy, setPlayerId, updateMoney, updateActiveChallengeId, updateGameState, updatePenalty } = gameSlice.actions
|
2024-12-11 16:26:36 +00:00
|
|
|
|
|
|
|
export default gameSlice.reducer
|