Récupération de défis et tirage d'un nouveau défi via des boutons

This commit is contained in:
2024-12-12 22:55:59 +01:00
parent 9d0b5cb254
commit 04f30e3ac2
12 changed files with 193 additions and 34 deletions

View File

@ -1,11 +1,9 @@
import { createSlice } from '@reduxjs/toolkit'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { Challenge } from '../challenges/challengesSlice'
export interface ChallengeAction {
id: number
challengeId: number
title: string,
description: string,
reward: number,
success: boolean,
start: number, // date
end: number | null, // date
@ -21,13 +19,45 @@ const initialState: ActionsState = {
challengeActions: []
}
export interface ChallengeActionPayload {
id: number
playerId: number
challengeId: number
success: boolean,
start: string,
end: string | null,
penaltyStart: string | null,
penaltyEnd: string | null,
}
export interface ChallengeActionsPayload {
data: (Challenge & { action: ChallengeActionPayload })[]
}
export const challengeActionsSlice = createSlice({
name: 'challengeActions',
initialState: initialState,
reducers: {
downloadChallengeActions(state, action: PayloadAction<ChallengeActionsPayload>) {
if (state.challengeActions)
state.challengeActions = state.challengeActions.filter(challengeAction => action.payload.data.filter(dlChallenge => dlChallenge.action.id === challengeAction.id) === null)
for (const dlChallenge of action.payload.data) {
state.challengeActions.push({
id: dlChallenge.action.id,
challengeId: dlChallenge.id,
success: dlChallenge.action.success,
start: new Date(dlChallenge.action.start).getTime(),
end: dlChallenge.action.end ? new Date(dlChallenge.action.end).getTime() : null,
penaltyStart: dlChallenge.action.penaltyStart ? new Date(dlChallenge.action.penaltyStart).getTime() : null,
penaltyEnd: dlChallenge.action.penaltyEnd ? new Date(dlChallenge.action.penaltyEnd).getTime() : null,
})
}
state.challengeActions.sort((c1, c2) => c2.id - c1.id)
},
},
})
export const { } = challengeActionsSlice.actions
export const { downloadChallengeActions } = challengeActionsSlice.actions
export default challengeActionsSlice.reducer

View File

@ -1,4 +1,6 @@
import { createSlice } from '@reduxjs/toolkit'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { PaginationMeta } from '../common'
import { ChallengeAction } from '../challengeActions/challengeActionsSlice'
export interface Challenge {
id: number
@ -15,13 +17,31 @@ const initialState: ChallengesState = {
challenges: []
}
export interface ChallengesPayload {
data: (Challenge & { action: ChallengeAction | null })[]
meta: PaginationMeta
}
export const challengesSlice = createSlice({
name: 'challenges',
initialState: initialState,
reducers: {
downloadChallenges(state, action: PayloadAction<ChallengesPayload>) {
if (state.challenges)
state.challenges = state.challenges.filter(challenge => action.payload.data.filter(dlChallenge => dlChallenge.id === challenge.id) === null)
for (const dlChallenge of action.payload.data) {
state.challenges.push({
id: dlChallenge.id,
title: dlChallenge.title,
description: dlChallenge.description,
reward: dlChallenge.reward,
})
}
state.challenges.sort((c1, c2) => c2.id - c1.id)
},
},
})
export const { } = challengesSlice.actions
export const { downloadChallenges } = challengesSlice.actions
export default challengesSlice.reducer

View File

@ -0,0 +1,8 @@
export interface PaginationMeta {
currentPage: number
lastPage: number
nextPage: number
prevPage: number
total: number
totalPerPage: number
}

View File

@ -21,7 +21,7 @@ export interface GameState {
gameStarted: boolean
money: number
currentRunner: boolean
currentChallengeId: number | null
activeChallengeId: number | null
chaseFreeTime: number | null // date
penaltyStart: number | null // date
penaltyEnd: number | null //date
@ -32,7 +32,7 @@ const initialState: GameState = {
gameStarted: false,
money: 0,
currentRunner: false,
currentChallengeId: null,
activeChallengeId: null,
chaseFreeTime: null,
penaltyStart: null,
penaltyEnd: null,
@ -48,6 +48,9 @@ export const gameSlice = createSlice({
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
@ -60,6 +63,6 @@ export const gameSlice = createSlice({
},
})
export const { setPlayerId, updateMoney, updateGameState } = gameSlice.actions
export const { setPlayerId, updateMoney, updateActiveChallengeId, updateGameState } = gameSlice.actions
export default gameSlice.reducer

View File

@ -1,4 +1,5 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { PaginationMeta } from '../common'
export interface InterrailTime {
hours: number
@ -73,15 +74,6 @@ const initialState: TrainsState = {
trains: []
}
export interface PaginationMeta {
currentPage: number
lastPage: number
nextPage: number
prevPage: number
total: number
totalPerPage: number
}
export interface TrainsPayload {
data: TrainTrip[]
meta: PaginationMeta
@ -105,8 +97,8 @@ export const trainSlice = createSlice({
arrivalTime: dlTrain.arrivalTime,
info: info,
})
state.trains.sort((t1, t2) => t1.departureTime > t2.departureTime ? -1 : t1.departureTime == t2.arrivalTime ? 0 : 1)
}
state.trains.sort((t1, t2) => t1.departureTime > t2.departureTime ? -1 : t1.departureTime == t2.arrivalTime ? 0 : 1)
}
},
})