From 82adcd6756316e489e7bac4d4cf892656d6f6a1b Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Thu, 24 Nov 2022 19:58:49 +0100 Subject: [PATCH] =?UTF-8?q?Add:=20Possibilit=C3=A9=20de=20changer=20de=20s?= =?UTF-8?q?kin=20avec=20uniqument=20les=20skins=20de=20l'utilisateur=20Pos?= =?UTF-8?q?sibilit=C3=A9=20d'acheter=20des=20skins=20Store:=20uniquement?= =?UTF-8?q?=20les=20skins=20que=20l'utilisateur=20n'a=20pas=20et=20se=20me?= =?UTF-8?q?t=20=C3=A0=20jours?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bob_party/App.tsx | 6 +- bob_party/src/components/BotBar.tsx | 27 ++++++- bob_party/src/components/Skin.tsx | 73 ++++++++++++++++--- bob_party/src/components/TopBar.tsx | 3 +- bob_party/src/context/storeContext.tsx | 22 ++++++ bob_party/{ => src/context}/userContext.tsx | 2 +- bob_party/src/core/User/user.ts | 2 +- bob_party/src/core/User/userCoinsModifier.ts | 10 ++- bob_party/src/core/skin.ts | 4 + bob_party/src/screens/Profile.tsx | 6 +- bob_party/src/screens/Settings.tsx | 27 ++++--- bob_party/src/screens/SignIn.tsx | 4 +- bob_party/src/screens/SkinList.tsx | 5 +- bob_party/src/screens/Store.tsx | 17 +++-- .../services/userServices/loaderUserApi.ts | 3 +- 15 files changed, 164 insertions(+), 47 deletions(-) create mode 100644 bob_party/src/context/storeContext.tsx rename bob_party/{ => src/context}/userContext.tsx (90%) diff --git a/bob_party/App.tsx b/bob_party/App.tsx index d84f013..a15a665 100644 --- a/bob_party/App.tsx +++ b/bob_party/App.tsx @@ -5,7 +5,7 @@ import LoaderUserApi from './src/services/userServices/loaderUserApi' import ManagerUser from './src/services/userServices/ManagerUser' import FakeSaverUser from './src/services/userServices/fakeSaverUser' import React, { useCallback } from 'react'; -import { useUserStore } from './userContext'; +import { useUserStore } from './src/context/userContext'; export const MANAGER_USER = new ManagerUser(new LoaderUserApi, new FakeSaverUser); @@ -32,10 +32,6 @@ export const MANAGER_USER = new ManagerUser(new LoaderUserApi, new FakeSaverUser resetUser(); }, []); - const handleUserChange = useCallback(async () => { - MANAGER_USER.getCurrentUser()?.setCurrentCoins(MANAGER_USER.getCurrentUser()?.getCurrentCoins()+100); - setUser(MANAGER_USER.getCurrentUser()); - }, []); const test = useCallback(async () => { diff --git a/bob_party/src/components/BotBar.tsx b/bob_party/src/components/BotBar.tsx index 439906f..3c2cfe1 100644 --- a/bob_party/src/components/BotBar.tsx +++ b/bob_party/src/components/BotBar.tsx @@ -1,4 +1,4 @@ -import { FC, ReactNode } from "react" +import { FC, ReactNode, useCallback } from "react" import { Pressable, Image, View} from "react-native" import React from "react" @@ -6,6 +6,9 @@ import React from "react" Importing the correct stylesheet */ import styles from './style/BotBar.style'; +import { useStoreStore } from "../context/storeContext"; +import { MANAGER_USER } from "../../App"; +import tabSkinApp from "../constSkin"; /* Images that are required to create a bottom bar @@ -35,6 +38,26 @@ export const BotBar : FC<{nav: any, state?: String}> = ({nav, state}) => { + + const setTabSkin = useStoreStore((state) => state.setTabSkin); + + + const handleStoreChange = useCallback(async () => { + + let tabSkinStore=[...tabSkinApp]; + let tmp=MANAGER_USER.getCurrentUser()?.getTabSkin(); + if (tmp!=undefined){ + tmp.forEach(skin => { + for (let i=0; i = source={imgMid} /> - nav.navigate('StoreTab')}> + {handleStoreChange(); nav.navigate('StoreTab')}}> = ({nav, skin, state}) => { - console.log(nav); + const navigation = nav; const dispatch=useDispatch(); const setUser = useUserStore((state) => state.setUser); - function changerSkin(skin:Skin) { - MANAGER_USER.getCurrentUser()?.setCurrentSkin(skin); - setUser(MANAGER_USER.getCurrentUser()); - MANAGER_USER.getsaverUser().updateUser(MANAGER_USER.getCurrentUser()); + const setTabSkin = useStoreStore((state) => state.setTabSkin); + + + async function changerSkin(skin:Skin) { + const m=new UserSkinModifier(); + const tmp = MANAGER_USER.getCurrentUser(); + if (tmp!=null){ + await m.changeCurrentSkin(tmp, skin); + setUser(tmp); + MANAGER_USER.setCurrentUser(tmp); + } + } + + const handleStoreChange = useCallback(async () => { + + let tabSkinStore=[...tabSkinApp]; + let tmp=MANAGER_USER.getCurrentUser()?.getTabSkin(); + if (tmp!=undefined){ + tmp.forEach(skin => { + for (let i=0; i { + if(res==true){ + await mSkin.addSkin(tmp, skin); + setUser(tmp); + MANAGER_USER.setCurrentUser(tmp); + Alert.alert("Achat du skin"); + handleStoreChange(); + } + else{ + Alert.alert("Pas assez d'argent pour acheter le skin"); + } + }); + } + } + /* The display of this component depends of the screen from where it has been called: @@ -56,7 +109,7 @@ FC<{nav : any, skin: Skin, state: String}> = case 'shop': return( - Alert.alert("Achat du skin")} style={styles.imageWrapper}> + buySkin(skin)} style={styles.imageWrapper}> {skin.getSkinName()} = ) case 'liste': return( - {changerSkin(skin); nav.navigate('ProfileTab', {screen: 'Profile'})}} style={styles.imageWrapper}> + {changerSkin(skin); navigation.goBack()}} style={styles.imageWrapper}> {skin.getSkinName()} = ) case 'profile': return( - Alert.alert("Achat du skin")} style={styles.imageWrapperProfil}> + Alert.alert("cool")} style={styles.imageWrapperProfil}> {skin.getSkinName()} = ({nav, state}) => { - /* The display of this component depends of the screen from where it has been called: * From the Settings (icon) : Name of the page + cross button diff --git a/bob_party/src/context/storeContext.tsx b/bob_party/src/context/storeContext.tsx new file mode 100644 index 0000000..243111c --- /dev/null +++ b/bob_party/src/context/storeContext.tsx @@ -0,0 +1,22 @@ +import React from "react"; +import create from "zustand"; +import { MANAGER_USER } from "../../App"; +import tabSkinApp from "../constSkin"; +import { Skin } from "../core/Skin"; +import { User } from "../core/User/user"; + + +// Define store types +interface StoreState { + tabSkin: Skin[]; + setTabSkin: (tabSkin: Skin[]) => void; + resetTabSkin: () => void; + } + +// Define store data and methods +export const useStoreStore = create()((set, get) => ({ + tabSkin: tabSkinApp, + setTabSkin: (tabSkin) => set((state) => ({ tabSkin: tabSkin })), + resetTabSkin: () => set((state) => ({ tabSkin: tabSkinApp })), +})); + diff --git a/bob_party/userContext.tsx b/bob_party/src/context/userContext.tsx similarity index 90% rename from bob_party/userContext.tsx rename to bob_party/src/context/userContext.tsx index 8696bab..1f81bef 100644 --- a/bob_party/userContext.tsx +++ b/bob_party/src/context/userContext.tsx @@ -1,6 +1,6 @@ import React from "react"; import create from "zustand"; -import { User } from "./src/core/User/user"; +import { User } from "../core/User/user"; // Define store types diff --git a/bob_party/src/core/User/user.ts b/bob_party/src/core/User/user.ts index 085dbec..c337912 100644 --- a/bob_party/src/core/User/user.ts +++ b/bob_party/src/core/User/user.ts @@ -30,7 +30,7 @@ export class User{ this.currentCoins=currentCoins; this.totalCoins=totalCoins; this.currentSkin=currentSkin; - this.tabSkin=[]; + this.tabSkin=tabSkin; } diff --git a/bob_party/src/core/User/userCoinsModifier.ts b/bob_party/src/core/User/userCoinsModifier.ts index ee56c35..44974f1 100644 --- a/bob_party/src/core/User/userCoinsModifier.ts +++ b/bob_party/src/core/User/userCoinsModifier.ts @@ -11,9 +11,13 @@ export class ManagerCoinsUser{ await MANAGER_USER.getsaverUser().updateUser(u); } - async removeCoins(u:User, coins:number){ - u.setCurrentCoins(u.getCurrentCoins()-coins); - await MANAGER_USER.getsaverUser().updateUser(u); + async removeCoins(u:User, coins:number): Promise{ + if (u.getCurrentCoins()>=coins){ + u.setCurrentCoins(u.getCurrentCoins()-coins); + await MANAGER_USER.getsaverUser().updateUser(u); + return true; + } + return false; } async changeCurrentCoins(u:User, coins:number){ diff --git a/bob_party/src/core/skin.ts b/bob_party/src/core/skin.ts index 3ae712f..5351764 100644 --- a/bob_party/src/core/skin.ts +++ b/bob_party/src/core/skin.ts @@ -48,4 +48,8 @@ export class Skin{ setSkinCost(cost:number){ this.cost=cost; } + + isEqual(s:Skin){ + return this.id==s.id; + } } \ No newline at end of file diff --git a/bob_party/src/screens/Profile.tsx b/bob_party/src/screens/Profile.tsx index 334d443..45e0975 100644 --- a/bob_party/src/screens/Profile.tsx +++ b/bob_party/src/screens/Profile.tsx @@ -9,7 +9,7 @@ import { SkinComponent } from '../components/Skin'; import { ButtonGreySmall } from '../components/ButtonGreySmall'; import { ScreenIndicator } from '../components/ScreenIndicator'; import { MANAGER_USER } from '../../App'; -import { useUserStore } from '../../userContext'; +import { useUserStore } from '../context/userContext'; const coin = require('../../assets/Icons/Coin.png') @@ -35,8 +35,8 @@ function Profile(props: { navigation: any; }) { {useUserStore().user?.getCurrentCoins()} - - navigation.navigate('SkinList')} title='Changer de skin' state='Profile'/> + + {navigation.navigate('SkinList');}} title='Changer de skin' state='Profile'/> diff --git a/bob_party/src/screens/Settings.tsx b/bob_party/src/screens/Settings.tsx index 3724b3d..a86ef02 100644 --- a/bob_party/src/screens/Settings.tsx +++ b/bob_party/src/screens/Settings.tsx @@ -11,8 +11,9 @@ import Dialog from "react-native-dialog" import RNPickerSelect from "react-native-picker-select"; import { PickerGreySmall } from '../components/PickerGreySmall'; import { MANAGER_USER } from '../../App'; -import { useUserStore } from '../../userContext'; +import { useUserStore } from '../context/userContext'; import DialogInput from 'react-native-dialog-input'; +import UserModificationManager from '../core/User/userModificationManager'; function Settings(props: { navigation: any; }) { const { navigation } = props @@ -27,16 +28,24 @@ function Settings(props: { navigation: any; }) { const [selectedNationality, setSelectedNationality] = useState(""); - function changeUsername(username:string){ - MANAGER_USER.getCurrentUser()?.setUsername(username); - setUser(MANAGER_USER.getCurrentUser()); - MANAGER_USER.getsaverUser().updateUser(MANAGER_USER.getCurrentUser()); + async function changeUsername(username:string){ + const m = new UserModificationManager(); + let tmp=MANAGER_USER.getCurrentUser(); + if (tmp!=null){ + await m.changeUsername(tmp, username); + setUser(tmp); + MANAGER_USER.setCurrentUser(tmp); + } } - function changePassword(password:string){ - MANAGER_USER.getCurrentUser()?.setPassword(password); - setUser(MANAGER_USER.getCurrentUser()); - MANAGER_USER.getsaverUser().updateUser(MANAGER_USER.getCurrentUser()); + async function changePassword(password:string){ + const m = new UserModificationManager(); + let tmp=MANAGER_USER.getCurrentUser(); + if (tmp!=null){ + await m.changePassword(tmp, password); + setUser(tmp); + MANAGER_USER.setCurrentUser(tmp); + } } const dispatch=useDispatch(); diff --git a/bob_party/src/screens/SignIn.tsx b/bob_party/src/screens/SignIn.tsx index 0bc0db3..304b82a 100644 --- a/bob_party/src/screens/SignIn.tsx +++ b/bob_party/src/screens/SignIn.tsx @@ -10,7 +10,7 @@ import { RootState } from '../redux/store'; import { updateIncorrectCredentials } from '../redux/features/credentialErrorsSlice'; import Dialog from "react-native-dialog"; import { MANAGER_USER } from '../../App'; -import { useUserStore } from '../../userContext'; +import { useUserStore } from '../context/userContext'; @@ -35,8 +35,8 @@ function SignIn(props: { navigation: any; }) { const us =await MANAGER_USER.getLoaderUser().loadByUsernamePassword(pseudo, password).then((res) => { if (res!=null){ MANAGER_USER.setCurrentUser(res); - navigation.navigate('HomeTab'); setUser(MANAGER_USER.getCurrentUser()); + navigation.navigate('HomeTab'); } else{ console.log("wesh c'est null"); diff --git a/bob_party/src/screens/SkinList.tsx b/bob_party/src/screens/SkinList.tsx index de65d94..6b34a4f 100644 --- a/bob_party/src/screens/SkinList.tsx +++ b/bob_party/src/screens/SkinList.tsx @@ -8,6 +8,7 @@ import { FlatList } from 'react-native-gesture-handler'; import { SkinComponent } from '../components/Skin'; import tabSkinApp from '../constSkin'; import { ScreenIndicator } from '../components/ScreenIndicator'; +import { MANAGER_USER } from '../../App'; @@ -22,11 +23,11 @@ function SkinList(props: { navigation: any; }) { item.getSkinName()} - renderItem={({item}) => } /> + renderItem={({item}) => } /> item.getSkinName()} - renderItem={({item}) => } /> + renderItem={({item}) => } />