From 0dd98f20f6ff7610e40548079d5d317bf435e064 Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Mon, 9 Jan 2023 10:19:28 +0100 Subject: [PATCH] ADD: SaverMatchApi + persistance pour match --- api-rest/gateways/matchGateway.php | 4 +- api-rest/index.php | 2 +- bob_party/server.js | 11 +++- bob_party/src/components/GameComponent.tsx | 13 ++-- bob_party/src/components/GameList.tsx | 1 - bob_party/src/components/LobbyComponent.tsx | 46 +++++++++++-- bob_party/src/components/TopBar.tsx | 21 +++++- bob_party/src/components/UserPreview.tsx | 11 +--- bob_party/src/context/matchContext.tsx | 4 +- bob_party/src/core/Match/matchCreator.ts | 11 ---- bob_party/src/core/Match/matchModifier.ts | 26 ++++++++ bob_party/src/screens/MatchMaking.tsx | 2 + bob_party/src/screens/SignIn.tsx | 2 - .../src/services/matchServices/ISaverMatch.ts | 13 +++- .../services/matchServices/saverMatchApi.ts | 66 ++++++++++++++++--- 15 files changed, 180 insertions(+), 53 deletions(-) delete mode 100644 bob_party/src/core/Match/matchCreator.ts create mode 100644 bob_party/src/core/Match/matchModifier.ts diff --git a/api-rest/gateways/matchGateway.php b/api-rest/gateways/matchGateway.php index e3f05bd..31611aa 100644 --- a/api-rest/gateways/matchGateway.php +++ b/api-rest/gateways/matchGateway.php @@ -41,7 +41,7 @@ class MatchGateway{ } /// Brief : Adding a NEW match in database - public function postMatch(int $idGame, int $idCreator){ + public function postMatch(int $idGame, int $idCreator): ?Matchs{ $insertMatchQuery="INSERT INTO T_E_MATCH_MTC VALUES(NULL,0,:idGame)"; $insertPlayQuery = "INSERT INTO T_J_PLAY_MATCH_PLM VALUES(:idCreator,:id);"; $argInsertMatch=array('idGame'=>array($idGame, PDO::PARAM_INT)); @@ -57,7 +57,7 @@ class MatchGateway{ $argInsertPlay= array('idCreator'=>array($idCreator,PDO::PARAM_INT), 'id'=>array($id,PDO::PARAM_INT)); $this->connection->execQuery($insertPlayQuery,$argInsertPlay); - return; + return new Matchs($id, 0, $idGame, [$idCreator]); } /// Brief : Modifying an EXISTING match in database diff --git a/api-rest/index.php b/api-rest/index.php index ea36d44..715cd49 100644 --- a/api-rest/index.php +++ b/api-rest/index.php @@ -164,6 +164,7 @@ $idCreator = !empty($url[5]) ? (int) $url[5] : null; if ($idGame != null || $idCreator != null){ $match =$matchgw->postMatch($idGame,$idCreator); + echo json_encode($match, JSON_PRETTY_PRINT); http_response_code(200); } else{ header("HTTP/1.0 400 idGame or idCreator not given"); @@ -203,7 +204,6 @@ $totalnbCoins = (int) $url[10]; $nbGames = (int) $url[11]; $currentSkin = !empty($url[12]) ? (int) $url[12] : null; - echo ($nbCurrentCoins . ' ' . $totalnbCoins . " ". $nbGames); $usergw->putUser($id,$username,$password,$sexe, $nationality, $nbCurrentCoins,$totalnbCoins,$nbGames,$currentSkin); http_response_code(200); } diff --git a/bob_party/server.js b/bob_party/server.js index 4d9a866..9be4546 100644 --- a/bob_party/server.js +++ b/bob_party/server.js @@ -17,6 +17,10 @@ io.on('connection', (socket) => { socket.join("C" + conv.id); }); + socket.on('quitConv', (conv) => { + socket.off("C" + conv); + }); + socket.on("messageSent", (conv) =>{ socket.to("C"+conv.id).emit("messageReceived"); console.log("Message envoyé"); @@ -28,10 +32,15 @@ io.on('connection', (socket) => { }); }); - socket.on('inMatch', (match) => { + + socket.on('joinMatch', (match) => { socket.join("M" + match); }); + socket.on('quitMatch', (match) => { + socket.off("M" + match); + }); + socket.on("playTicTacToe", (match, rowIndex, columnIndex, turn) =>{ socket.to("M"+match).emit("oppPlayTicTacToe", rowIndex, columnIndex, turn); }); diff --git a/bob_party/src/components/GameComponent.tsx b/bob_party/src/components/GameComponent.tsx index 0d6a4e8..f181c13 100644 --- a/bob_party/src/components/GameComponent.tsx +++ b/bob_party/src/components/GameComponent.tsx @@ -10,9 +10,11 @@ import { Game } from "../core/game" import styles from './style/Game.style'; import Lobby from "../screens/Lobby" import ManagerMatch from "../services/matchServices/managerMatch" -import MatchCreator from "../core/Match/matchCreator" +import MatchModifier from "../core/Match/matchModifier" import { useMatchStore } from "../context/matchContext" -import { MANAGER_MATCH, MANAGER_USER } from "../../appManagers" +import { MANAGER_GAME, MANAGER_MATCH, MANAGER_USER } from "../../appManagers" +import { GameSolo } from "../core/gameSolo" +import { socket } from "../../socketConfig" export const GameComponent : @@ -27,12 +29,13 @@ FC<{game: Game, nav: any}> = const setMatch = useMatchStore((state) => state.setMatch); - const createNewMatchSolo = useCallback(async (game : Game, nav: any) => { + const createNewMatch = useCallback(async (game : Game, nav: any) => { - const m=new MatchCreator(); + const m=new MatchModifier(); const tmp=MANAGER_USER.getCurrentUser(); if (tmp!==null){ let match=await m.createMatch(tmp, game); + socket.emit("joinMatch", match); MANAGER_MATCH.setCurrentMatch(match); setMatch(match); nav.navigate("GameSolo"); @@ -42,7 +45,7 @@ FC<{game: Game, nav: any}> = return ( - createNewMatchSolo(game, nav)}> + createNewMatch(game, nav)}> = if(MANAGER_GAME.currentGameType === "solo" ){ gameList = MANAGER_GAME.getTabGameSolo(); - console.log(gameList); } else if(MANAGER_GAME.currentGameType === "multi"){ gameList = MANAGER_GAME.getTabGameMulti(); diff --git a/bob_party/src/components/LobbyComponent.tsx b/bob_party/src/components/LobbyComponent.tsx index e94474e..acaecbf 100644 --- a/bob_party/src/components/LobbyComponent.tsx +++ b/bob_party/src/components/LobbyComponent.tsx @@ -1,5 +1,5 @@ -import { FC} from "react" -import { FlatList } from "react-native" +import { FC, useState} from "react" +import { Button, FlatList } from "react-native" import React from "react" import { Game } from "../core/game" @@ -22,8 +22,28 @@ export const LobbyComponent : FC<{nav: any}> = ({nav}) => { + const setTabUser = useMatchStore((state) => state.setTabUser); + + const [initUsers, setInitUsers] = useState(0); + + function getUsers(){ + if (initUsers===0){ + setInitUsers(1); + const tmp:any=[]; + MANAGER_MATCH.getCurrentMatch()?.getTabUsers().forEach(user => { + tmp.push(user); + }); + const tmpGame=MANAGER_MATCH.getCurrentMatch()?.getGame(); + if (tmpGame!=undefined){ + for (let i=tmp.length; i @@ -35,16 +55,32 @@ FC<{nav: any}> = /> ); } else{ + getUsers(); return( - + usr?.getUsername() || usr} + renderItem={({item}) => } + /> + +