diff --git a/cryptide_project/server/api/controllers/SessionController.js b/cryptide_project/server/api/controllers/SessionController.js index c9a5623..08f1935 100644 --- a/cryptide_project/server/api/controllers/SessionController.js +++ b/cryptide_project/server/api/controllers/SessionController.js @@ -113,6 +113,36 @@ class SessionController { await db.disconnect(); } } + + static async addOnlineStats(req, res){ + const db = new DatabaseService(); + + try{ + await db.connect(); + + const user = await db.getUserByPseudo(req.body.pseudo); + if (!user) { + res.status(200).json({ error: "true", message: 'User not found' }); + return; + } + + await db.addOnlineStats(user.idUser, req.body.win, req.body.time); + + const updatedUser = await db.getUserByPseudo(req.body.pseudo); + req.session.user.onlineStats.nbGames = updatedUser.nbGames; + req.session.user.onlineStats.nbWins = updatedUser.nbWins; + req.session.user.onlineStats.ratio = updatedUser.ratio; + + res.status(200).json({ user: req.session.user }); //verif rep + } + catch(error){ + console.error(error); + res.status(500).json({ error: 'Erreur lors de la modification des stats en ligne de l\'utilisateur.' }); + } + finally{ + await db.disconnect(); + } + } } module.exports = SessionController; \ No newline at end of file diff --git a/cryptide_project/server/api/routes/AuthRoutes.js b/cryptide_project/server/api/routes/AuthRoutes.js index a0da069..5028f01 100644 --- a/cryptide_project/server/api/routes/AuthRoutes.js +++ b/cryptide_project/server/api/routes/AuthRoutes.js @@ -11,6 +11,7 @@ router.delete('/auth/delAccount', AuthController.delAccount) // Routes pour les sessions router.get('/session', SessionController.getUserInformation); +router.post('/session/addOnlineStats', SessionController.addOnlineStats); router.put('/session/updatePseudo', SessionController.UpdatePseudo); module.exports = router; diff --git a/cryptide_project/server/api/services/DatabaseService.js b/cryptide_project/server/api/services/DatabaseService.js index 08af723..3790ba3 100644 --- a/cryptide_project/server/api/services/DatabaseService.js +++ b/cryptide_project/server/api/services/DatabaseService.js @@ -187,6 +187,19 @@ class DatabaseService { }); } + async addOnlineStats(userId, win, time){ + return new Promise((resolve, reject) => { + this.client.run('INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)', userId, "multijoueur", win, 0, time, (err, result) => { + if(err){ + reject(err); + } + else{ + resolve(result); + } + }); + }); + } + // ------------------------------------------------------------- // ------------------- STATS ENIGME ---------------------------- // ------------------------------------------------------------- diff --git a/cryptide_project/src/Components/GraphContainer.tsx b/cryptide_project/src/Components/GraphContainer.tsx index ddacc82..671165f 100644 --- a/cryptide_project/src/Components/GraphContainer.tsx +++ b/cryptide_project/src/Components/GraphContainer.tsx @@ -14,6 +14,7 @@ import NodePerson from "../model/Graph/NodePerson"; import { useAuth } from "../Contexts/AuthContext"; import Indice from "../model/Indices/Indice"; import Pair from "../model/Pair"; +import { times } from "lodash"; interface MyGraphComponentProps { onNodeClick: (shouldShowChoiceBar: boolean) => void; handleShowTurnBar: (shouldShowTurnBar: boolean) => void @@ -577,13 +578,11 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS // console.log("nbGames: " + user.onlineStats.nbGames + " nbWins: " + user.onlineStats.nbWins); if(winner.id === currentPlayer.id){ // Ajouter une victoire - user.onlineStats.nbWins = null ? user.onlineStats.nbWins = 1 : user.onlineStats.nbWins += 1; - } - // Update les stats - user.onlineStats.nbGames = null ? user.onlineStats.nbGames = 1 : user.onlineStats.nbGames += 1; - user.onlineStats.ratio = user.onlineStats.nbWins / user.onlineStats.nbGames; - - manager.userService.updateOnlineStats(user.pseudo, user.onlineStats.nbGames, user.onlineStats.nbWins, user.onlineStats.ratio); + manager.userService.updateOnlineStats(user.pseudo, 1, elapsedTime); + } + else{ + manager.userService.updateOnlineStats(user.pseudo, 0, elapsedTime); + } } else{ console.error("User not found"); diff --git a/cryptide_project/src/Components/ScoreBoard.tsx b/cryptide_project/src/Components/ScoreBoard.tsx index bd3d652..9bba9af 100644 --- a/cryptide_project/src/Components/ScoreBoard.tsx +++ b/cryptide_project/src/Components/ScoreBoard.tsx @@ -92,6 +92,14 @@ const ScoreBoard: React.FC<{ Player: User }> = ({ Player }) => { Partie jouée : {Player !== null ? Player.hardEnigmaStats.nbGames : "0"} + + Nombre de victoire : + {Player !== null ? Player.hardEnigmaStats.nbWins : "0"} + + + Ratio V/D : + {Player !== null ? Player.hardEnigmaStats.ratio.toFixed(2) + "%" : "0"} + Meilleur temps : {Player !== null ? Player.hardEnigmaStats.bestTime : "0"} diff --git a/cryptide_project/src/model/DataManagers/DbUserService.ts b/cryptide_project/src/model/DataManagers/DbUserService.ts index 0f68682..5818b0d 100644 --- a/cryptide_project/src/model/DataManagers/DbUserService.ts +++ b/cryptide_project/src/model/DataManagers/DbUserService.ts @@ -97,9 +97,9 @@ class DbUserService implements IUserService{ } } - async updateOnlineStats(pseudo: string, nbGames: number, bestScore: number, ratio: number): Promise { + async updateOnlineStats(pseudo: string, win: number, time: number): Promise { try { - const result = await SessionService.updateOnlineStats(pseudo, nbGames, bestScore, ratio); + const result = await SessionService.addOnlineStats(pseudo, win, time); if (result) { console.log("Stats online updated"); } else { diff --git a/cryptide_project/src/model/DataManagers/IUserService.ts b/cryptide_project/src/model/DataManagers/IUserService.ts index dafc349..412df04 100644 --- a/cryptide_project/src/model/DataManagers/IUserService.ts +++ b/cryptide_project/src/model/DataManagers/IUserService.ts @@ -3,7 +3,7 @@ import User from "../User"; interface IUserService{ fetchUserInformation(): Promise<[User | null, boolean]> updateSoloStats(pseudo: string, nbGames: number, bestScore: number, avgNbTry: number): Promise - updateOnlineStats(pseudo: string, nbGames: number, bestScore: number, ratio: number): Promise + updateOnlineStats(pseudo: string, win: number, time: number): Promise } diff --git a/cryptide_project/src/services/SessionService.tsx b/cryptide_project/src/services/SessionService.tsx index f4290fc..7c1b235 100644 --- a/cryptide_project/src/services/SessionService.tsx +++ b/cryptide_project/src/services/SessionService.tsx @@ -53,21 +53,18 @@ class SessionService { } } - static async updateOnlineStats(pseudo: string, nbGames: number, nbWins: number, ratio: number){ + static async addOnlineStats(pseudo: string, win: number, time: number){ try { - - console.log("updateOnlineStats : ", pseudo, nbGames, nbWins, ratio); - const response = await fetch(ADRESSE_DBSERVER + '/session/updateOnlineStats', { - method: 'PUT', + const response = await fetch(ADRESSE_DBSERVER + '/session/addOnlineStats', { + method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify({ pseudo, - nbGames, - nbWins, - ratio + win, + time }), });