80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
|
|
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
|
|
}
|
|
|
|
export interface PenaltyPayload {
|
|
penaltyStart: number | null
|
|
penaltyEnd: number | null
|
|
}
|
|
|
|
export interface GameState {
|
|
playerId: number | null
|
|
runId: number | null
|
|
gameStarted: boolean
|
|
money: number
|
|
currentRunner: boolean
|
|
activeChallengeId: number | null
|
|
chaseFreeTime: number | null // date
|
|
penaltyStart: number | null // date
|
|
penaltyEnd: number | null //date
|
|
}
|
|
|
|
const initialState: GameState = {
|
|
playerId: null,
|
|
runId: null,
|
|
gameStarted: false,
|
|
money: 0,
|
|
currentRunner: false,
|
|
activeChallengeId: null,
|
|
chaseFreeTime: null,
|
|
penaltyStart: null,
|
|
penaltyEnd: null,
|
|
}
|
|
|
|
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
|
|
},
|
|
updateActiveChallengeId: (state, action: PayloadAction<number | null>) => {
|
|
state.activeChallengeId = action.payload
|
|
},
|
|
updateGameState: (state, action: PayloadAction<GamePayload>) => {
|
|
const game: GamePayload = action.payload
|
|
state.gameStarted = game.started
|
|
state.runId = game.currentRunId
|
|
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
|
|
},
|
|
updatePenalty: (state, action: PayloadAction<PenaltyPayload>) => {
|
|
state.penaltyStart = action.payload.penaltyStart
|
|
state.penaltyEnd = action.payload.penaltyEnd
|
|
}
|
|
},
|
|
})
|
|
|
|
export const { setPlayerId, updateMoney, updateActiveChallengeId, updateGameState, updatePenalty } = gameSlice.actions
|
|
|
|
export default gameSlice.reducer
|