From 7aa9dde5a9b55488decaef7222883af7c1608271 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Wed, 11 Dec 2024 17:26:36 +0100 Subject: [PATCH] =?UTF-8?q?Structure=20de=20donn=C3=A9es=20jeu=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooks/mutations/useGeolocationMutation.ts | 2 +- client/hooks/mutations/useLoginMutation.ts | 2 +- client/hooks/useAuth.ts | 2 +- client/hooks/useChallengeActions.ts | 3 + client/hooks/useChallenges.ts | 3 + client/hooks/useGame.ts | 12 ++ client/hooks/useTrain.ts | 3 + client/utils/SecureStore.web.ts | 24 ---- .../features/{location => auth}/authSlice.ts | 0 .../challengeActions/challengeActionsSlice.ts | 33 ++++++ .../features/challenges/challengesSlice.ts | 27 +++++ client/utils/features/game/gameSlice.ts | 38 +++++++ client/utils/features/train/trainSlice.ts | 107 ++++++++++++++++++ client/utils/store.ts | 10 +- 14 files changed, 238 insertions(+), 28 deletions(-) create mode 100644 client/hooks/useChallengeActions.ts create mode 100644 client/hooks/useChallenges.ts create mode 100644 client/hooks/useGame.ts create mode 100644 client/hooks/useTrain.ts rename client/utils/features/{location => auth}/authSlice.ts (100%) create mode 100644 client/utils/features/challengeActions/challengeActionsSlice.ts create mode 100644 client/utils/features/challenges/challengesSlice.ts create mode 100644 client/utils/features/game/gameSlice.ts create mode 100644 client/utils/features/train/trainSlice.ts diff --git a/client/hooks/mutations/useGeolocationMutation.ts b/client/hooks/mutations/useGeolocationMutation.ts index 761f3a0..33f92ce 100644 --- a/client/hooks/mutations/useGeolocationMutation.ts +++ b/client/hooks/mutations/useGeolocationMutation.ts @@ -1,4 +1,4 @@ -import { AuthState } from "@/utils/features/location/authSlice" +import { AuthState } from "@/utils/features/auth/authSlice" import { useMutation } from "@tanstack/react-query" import { LocationObject } from "expo-location" diff --git a/client/hooks/mutations/useLoginMutation.ts b/client/hooks/mutations/useLoginMutation.ts index ab22664..a35c87f 100644 --- a/client/hooks/mutations/useLoginMutation.ts +++ b/client/hooks/mutations/useLoginMutation.ts @@ -1,4 +1,4 @@ -import { AuthPayload } from "@/utils/features/location/authSlice" +import { AuthPayload } from "@/utils/features/auth/authSlice" import { useMutation } from "@tanstack/react-query" type ErrorResponse = { diff --git a/client/hooks/useAuth.ts b/client/hooks/useAuth.ts index 0f414cd..43be8e0 100644 --- a/client/hooks/useAuth.ts +++ b/client/hooks/useAuth.ts @@ -1,5 +1,5 @@ import { useAppDispatch, useAppSelector } from "./useStore" -import { AuthPayload, login, logout } from "@/utils/features/location/authSlice" +import { AuthPayload, login, logout } from "@/utils/features/auth/authSlice" export const useAuth = () => useAppSelector((state) => state.auth) export const useAuthLogin = () => { diff --git a/client/hooks/useChallengeActions.ts b/client/hooks/useChallengeActions.ts new file mode 100644 index 0000000..44da90d --- /dev/null +++ b/client/hooks/useChallengeActions.ts @@ -0,0 +1,3 @@ +import { useAppSelector } from "./useStore" + +export const useChallengeActions = () => useAppSelector((state) => state.challengeActions) diff --git a/client/hooks/useChallenges.ts b/client/hooks/useChallenges.ts new file mode 100644 index 0000000..9dcd5e9 --- /dev/null +++ b/client/hooks/useChallenges.ts @@ -0,0 +1,3 @@ +import { useAppSelector } from "./useStore" + +export const useTrain = () => useAppSelector((state) => state.challenges) diff --git a/client/hooks/useGame.ts b/client/hooks/useGame.ts new file mode 100644 index 0000000..b7443b9 --- /dev/null +++ b/client/hooks/useGame.ts @@ -0,0 +1,12 @@ +import { useAppDispatch, useAppSelector } from "./useStore" +import { setPlayerId, updateMoney } from "@/utils/features/game/gameSlice" + +export const useGame = () => useAppSelector((state) => state.game) +export const useSetPlayerId = () => { + const dispath = useAppDispatch() + return (playerId: number) => dispath(setPlayerId(playerId)) +} +export const useUpdateMoney = () => { + const dispatch = useAppDispatch() + return (money: number) => dispatch(updateMoney(money)) +} diff --git a/client/hooks/useTrain.ts b/client/hooks/useTrain.ts new file mode 100644 index 0000000..b370af8 --- /dev/null +++ b/client/hooks/useTrain.ts @@ -0,0 +1,3 @@ +import { useAppSelector } from "./useStore" + +export const useTrain = () => useAppSelector((state) => state.train) diff --git a/client/utils/SecureStore.web.ts b/client/utils/SecureStore.web.ts index 68a23a1..ed0822b 100644 --- a/client/utils/SecureStore.web.ts +++ b/client/utils/SecureStore.web.ts @@ -17,27 +17,3 @@ export function setItem(key: string, value: string): void { export async function setItemAsync(key: string, value: string): Promise { localStorage.setItem(key, value) } - -/** -import AsyncStorage from '@react-native-async-storage/async-storage' - -export async function deleteItemAsync(key: string): Promise { - return AsyncStorage.removeItem(key) -} - -export function getItem(key: string): string | null { - return Promise.apply(AsyncStorage.getItem(key)) -} - -export async function getItemAsync(key: string): Promise { - return AsyncStorage.getItem(key) -} - -export function setItem(key: string, value: string): void { - AsyncStorage.setItem(key, value) -} - -export async function setItemAsync(key: string, value: string): Promise { - return AsyncStorage.setItem(key, value) -} - */ \ No newline at end of file diff --git a/client/utils/features/location/authSlice.ts b/client/utils/features/auth/authSlice.ts similarity index 100% rename from client/utils/features/location/authSlice.ts rename to client/utils/features/auth/authSlice.ts diff --git a/client/utils/features/challengeActions/challengeActionsSlice.ts b/client/utils/features/challengeActions/challengeActionsSlice.ts new file mode 100644 index 0000000..9eed4f7 --- /dev/null +++ b/client/utils/features/challengeActions/challengeActionsSlice.ts @@ -0,0 +1,33 @@ +import { createSlice } from '@reduxjs/toolkit' + +export interface ChallengeAction { + id: number + challengeId: number + title: string, + description: string, + reward: number, + success: boolean, + start: Date, + end: Date | null, + penaltyStart: Date | null, + penaltyEnd: Date | null, +} + +export interface ActionsState { + challengeActions: ChallengeAction[] +} + +const initialState: ActionsState = { + challengeActions: [] +} + +export const challengeActionsSlice = createSlice({ + name: 'challengeActions', + initialState: initialState, + reducers: { + }, +}) + +export const { } = challengeActionsSlice.actions + +export default challengeActionsSlice.reducer diff --git a/client/utils/features/challenges/challengesSlice.ts b/client/utils/features/challenges/challengesSlice.ts new file mode 100644 index 0000000..95a4e33 --- /dev/null +++ b/client/utils/features/challenges/challengesSlice.ts @@ -0,0 +1,27 @@ +import { createSlice } from '@reduxjs/toolkit' + +export interface Challenge { + id: number + title: string, + description: string, + reward: number, +} + +export interface ChallengesState { + challenges: Challenge[] +} + +const initialState: ChallengesState = { + challenges: [] +} + +export const challengesSlice = createSlice({ + name: 'challenges', + initialState: initialState, + reducers: { + }, +}) + +export const { } = challengesSlice.actions + +export default challengesSlice.reducer diff --git a/client/utils/features/game/gameSlice.ts b/client/utils/features/game/gameSlice.ts new file mode 100644 index 0000000..22ee376 --- /dev/null +++ b/client/utils/features/game/gameSlice.ts @@ -0,0 +1,38 @@ +import { createSlice, PayloadAction } from '@reduxjs/toolkit' + +export interface GameState { + playerId: number | null + gameStarted: boolean + money: number + currentRunner: boolean + chaseFreeTime: Date | null + penaltyStart: Date | null + penaltyEnd: Date | null +} + +const initialState: GameState = { + playerId: null, + gameStarted: false, + money: 0, + currentRunner: false, + 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 + }, + }, +}) + +export const { setPlayerId, updateMoney } = gameSlice.actions + +export default gameSlice.reducer diff --git a/client/utils/features/train/trainSlice.ts b/client/utils/features/train/trainSlice.ts new file mode 100644 index 0000000..2ff066f --- /dev/null +++ b/client/utils/features/train/trainSlice.ts @@ -0,0 +1,107 @@ +import { createSlice } from '@reduxjs/toolkit' + +export interface InterrailLeg { + infoJson?: string + info?: InterrailLegInfo + sortOrder: number +} + +export interface InterrailTravel { + date: string + infoJson?: string + info?: InterrailTravelInfo + from: string + to: string + type: number + legs: InterrailLeg[] +} + +export interface InterrailJourneyData { + travels: InterrailTravel[] +} + +export interface InterrailJourney { + data: InterrailJourneyData +} + +export interface InterrailTime { + hours: number + minutes: number + offset: number +} + +export interface InterrailDate { + day: number + month: number + year: number +} + +export interface InterrailTravelInfo { + arrivalTime: InterrailTime + date: InterrailDate + departureTime: InterrailTime + haconVersion: number + dataSource: number +} + +export interface InterrailStopExtraInfo { + departureTime: InterrailTime + index: number +} + +export interface InterrailStopCoordinates { + latitude: number + longitude: number +} + +export interface InterrailStopStation { + coordinates: InterrailStopCoordinates + country: string + name: string + stationId: number +} + +export interface InterrailLegInfo { + attributeCodes: string[] + attributes: object + duration: InterrailTime + directionStation: string + endTime: InterrailTime + isSeparateTicket: boolean + operationDays: string + operator: object + dataSource: number + startTime: InterrailTime + stopExtraInfo: InterrailStopExtraInfo[] + trainName: string + trainStopStations: InterrailStopStation[] + trainType: number +} + +export interface TrainTrip { + id: string + distance: number, + from: string, + to: string, + departureTime: Date, + arrivalTime: Date, +} + +export interface TrainsState { + trains: TrainTrip[] +} + +const initialState: TrainsState = { + trains: [] +} + +export const trainSlice = createSlice({ + name: 'train', + initialState: initialState, + reducers: { + }, +}) + +export const { } = trainSlice.actions + +export default trainSlice.reducer diff --git a/client/utils/store.ts b/client/utils/store.ts index 68f6892..5ba312a 100644 --- a/client/utils/store.ts +++ b/client/utils/store.ts @@ -1,11 +1,19 @@ import { configureStore } from '@reduxjs/toolkit' -import authReducer from './features/location/authSlice' +import authReducer from './features/auth/authSlice' +import challengesReducer from './features/challenges/challengesSlice' +import challengeActionsReducer from './features/challengeActions/challengeActionsSlice' +import gameReducer from './features/game/gameSlice' import locationReducer from './features/location/locationSlice' +import trainReducer from './features/train/trainSlice' const store = configureStore({ reducer: { auth: authReducer, + challenges: challengesReducer, + challengeActions: challengeActionsReducer, + game: gameReducer, location: locationReducer, + train: trainReducer, }, })