import { createSlice, PayloadAction } from '@reduxjs/toolkit' import { ChallengeAction } from '../challengeActions/challengeActionsSlice' 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 GameState { playerId: number | null gameStarted: boolean money: number currentRunner: boolean currentChallengeId: number | null chaseFreeTime: number | null // date penaltyStart: number | null // date penaltyEnd: number | null //date } const initialState: GameState = { playerId: null, gameStarted: false, money: 0, currentRunner: false, currentChallengeId: null, chaseFreeTime: null, penaltyStart: null, penaltyEnd: null, } export const gameSlice = createSlice({ name: 'game', initialState: initialState, reducers: { setPlayerId: (state, action: PayloadAction) => { state.playerId = action.payload }, updateMoney: (state, action: PayloadAction) => { state.money = action.payload }, updateGameState: (state, action: PayloadAction) => { const game: GamePayload = action.payload state.gameStarted = game.started 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 } }, }) export const { setPlayerId, updateMoney, updateGameState } = gameSlice.actions export default gameSlice.reducer