From 1de57a2e9b99e62608b4f44eb45065f1e2a2ba1a Mon Sep 17 00:00:00 2001 From: Baptiste Marcel Date: Tue, 5 Dec 2023 09:20:46 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20stats=20enigme=20facile=20et=20difficil?= =?UTF-8?q?e=20mais=20sur=20les=20anciens=20crit=C3=A8res?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controllers/SessionController.js | 60 +++++++++++++++++-- .../server/api/routes/AuthRoutes.js | 3 + .../server/api/services/DatabaseService.js | 28 +++++++++ .../src/Components/GraphContainer.tsx | 9 +++ .../src/model/DataManagers/DbUserService.ts | 39 ++++++++++++ .../src/model/DataManagers/IUserService.ts | 3 + .../src/services/SessionService.tsx | 58 ++++++++++++++++++ 7 files changed, 196 insertions(+), 4 deletions(-) diff --git a/cryptide_project/server/api/controllers/SessionController.js b/cryptide_project/server/api/controllers/SessionController.js index 6d53b80..5784ae2 100644 --- a/cryptide_project/server/api/controllers/SessionController.js +++ b/cryptide_project/server/api/controllers/SessionController.js @@ -1,12 +1,12 @@ const path = require('path'); const DatabaseService = require(path.resolve(__dirname, '../services/DatabaseService')); +const ENIGME_FACILE = "enigme_facile"; +const ENIGME_MOYEN = "enigme_moyenne"; +const ENIGME_DIFFICILE = "enigme_difficile"; + class SessionController { static async getUserInformation(req, res) { - const ENIGME_FACILE = "enigme_facile"; - const ENIGME_MOYEN = "enigme_moyenne"; - const ENIGME_DIFFICILE = "enigme_difficile"; - const db = new DatabaseService(); const date = new Date(); const hour = date.getHours(); @@ -139,6 +139,58 @@ class SessionController { } } + static async addEasyEnigmaStats(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.addEasyEnigmaStats(user.idUser, ENIGME_FACILE, req.body.win, req.body.time); + + 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 de l\'énigme facile de l\'utilisateur.' }); + } + finally{ + await db.disconnect(); + } + } + + // static async addMediumEnigmaStats(req, res) + + static async addHardEnigmaStats(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.addHardEnigmaStats(user.idUser, ENIGME_DIFFICILE, req.body.win, req.body.time); + + 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 de l\'énigme difficile de l\'utilisateur.' }); + } + finally{ + await db.disconnect(); + } + } + static async addOnlineStats(req, res){ const db = new DatabaseService(); diff --git a/cryptide_project/server/api/routes/AuthRoutes.js b/cryptide_project/server/api/routes/AuthRoutes.js index ce84834..3e07d03 100644 --- a/cryptide_project/server/api/routes/AuthRoutes.js +++ b/cryptide_project/server/api/routes/AuthRoutes.js @@ -12,6 +12,9 @@ router.delete('/auth/delAccount', AuthController.delAccount) // Routes pour les sessions router.get('/session', SessionController.getUserInformation); router.post('/session/addMastermindStats', SessionController.addMastermindStats); +router.post('/session/addEasyEnigmaStats', SessionController.addEasyEnigmaStats); +// router.post('/session/addMediumEnigmaStats', SessionController.addMediumEnigmaStats); +router.post('/session/addHardEnigmaStats', SessionController.addHardEnigmaStats); router.post('/session/addOnlineStats', SessionController.addOnlineStats); router.put('/session/updatePseudo', SessionController.UpdatePseudo); diff --git a/cryptide_project/server/api/services/DatabaseService.js b/cryptide_project/server/api/services/DatabaseService.js index 47051f8..e6a80e8 100644 --- a/cryptide_project/server/api/services/DatabaseService.js +++ b/cryptide_project/server/api/services/DatabaseService.js @@ -293,6 +293,34 @@ class DatabaseService { }); }); } + + async addEasyEnigmaStats(userId, enigmaLevel, win, time){ + return new Promise((resolve, reject) => { + this.client.run('INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)', userId, enigmaLevel, win, 0, time, (err, result) => { + if(err){ + reject(err); + } + else{ + resolve(result); + } + }); + }); + } + + // async addMediumEnigmaStats(userId, enigmaLevel, score) + + async addHardEnigmaStats(userId, enigmaLevel, win, time){ + return new Promise((resolve, reject) => { + this.client.run('INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)', userId, enigmaLevel, win, 0, time, (err, result) => { + if(err){ + reject(err); + } + else{ + resolve(result); + } + }); + }); + } } module.exports = DatabaseService; \ No newline at end of file diff --git a/cryptide_project/src/Components/GraphContainer.tsx b/cryptide_project/src/Components/GraphContainer.tsx index d8ca9b5..dc38f43 100644 --- a/cryptide_project/src/Components/GraphContainer.tsx +++ b/cryptide_project/src/Components/GraphContainer.tsx @@ -780,9 +780,17 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS endgame = true try{ + if(user && isLoggedIn){ if(solo){ if(isDaily){ // TODO: verif difficulté et add les stats + // TODO: verif pour facile et difficile, si réussi en one shot ou non + if(isEasy){ + manager.userService.addEasyEnigmaStats(user.pseudo, 1, elapsedTime); + } + else{ + manager.userService.addHardEnigmaStats(user.pseudo, 1, elapsedTime); + } } else{ // add stats mastermind @@ -791,6 +799,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS } } } + } } catch(error){ console.log(error); diff --git a/cryptide_project/src/model/DataManagers/DbUserService.ts b/cryptide_project/src/model/DataManagers/DbUserService.ts index 770de48..18dafcc 100644 --- a/cryptide_project/src/model/DataManagers/DbUserService.ts +++ b/cryptide_project/src/model/DataManagers/DbUserService.ts @@ -97,6 +97,45 @@ class DbUserService implements IUserService{ } } + async addEasyEnigmaStats(pseudo: string, win: number, time: number): Promise { + try { + const result = await SessionService.addEasyEnigmaStats(pseudo, win, time); + if (result) { + console.log("Stats easy updated"); + } else { + console.log("Stats easy not updated"); + } + } catch (error) { + console.error(error); + } + } + + // async addMediumEnigmaStats(pseudo: string, win: number, time: number): Promise { + // try { + // const result = await SessionService.addMediumEnigmaStats(pseudo, win, time); + // if (result) { + // console.log("Stats medium updated"); + // } else { + // console.log("Stats medium not updated"); + // } + // } catch (error) { + // console.error(error); + // } + // } + + async addHardEnigmaStats(pseudo: string, win: number, time: number): Promise { + try { + const result = await SessionService.addHardEnigmaStats(pseudo, win, time); + if (result) { + console.log("Stats hard updated"); + } else { + console.log("Stats hard not updated"); + } + } catch (error) { + console.error(error); + } + } + async addOnlineStats(pseudo: string, win: number, time: number): Promise { try { const result = await SessionService.addOnlineStats(pseudo, win, time); diff --git a/cryptide_project/src/model/DataManagers/IUserService.ts b/cryptide_project/src/model/DataManagers/IUserService.ts index e86add8..6ec24a9 100644 --- a/cryptide_project/src/model/DataManagers/IUserService.ts +++ b/cryptide_project/src/model/DataManagers/IUserService.ts @@ -3,6 +3,9 @@ import User from "../User"; interface IUserService{ fetchUserInformation(): Promise<[User | null, boolean]> addMastermindStats(pseudo: string, score: number, time: number): Promise + addEasyEnigmaStats(pseudo: string, win: number, time: number): Promise + // addMediumEnigmaStats(pseudo: string, win: number, time: number): Promise + addHardEnigmaStats(pseudo: string, win: number, time: number): Promise addOnlineStats(pseudo: string, win: number, time: number): Promise } diff --git a/cryptide_project/src/services/SessionService.tsx b/cryptide_project/src/services/SessionService.tsx index bf2ae62..f555132 100644 --- a/cryptide_project/src/services/SessionService.tsx +++ b/cryptide_project/src/services/SessionService.tsx @@ -52,6 +52,64 @@ class SessionService { } } + static async addEasyEnigmaStats(pseudo: string, win: number, time: number){ + try { + const response = await fetch(ADRESSE_DBSERVER + '/session/addEasyEnigmaStats', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + pseudo, + win, + time + }), + }); + + if (response.ok) { + const result = await response.json(); + return result; + } else { + const errorResponse = await response.json(); + throw new Error(errorResponse.error); + } + } catch (error) { + console.error(error); + throw error; + } + } + + // static async addMediumEnigmaStats(pseudo: string, win: number, time: number) + + static async addHardEnigmaStats(pseudo: string, win: number, time: number){ + try { + const response = await fetch(ADRESSE_DBSERVER + '/session/addHardEnigmaStats', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + pseudo, + win, + time + }), + }); + + if (response.ok) { + const result = await response.json(); + return result; + } else { + const errorResponse = await response.json(); + throw new Error(errorResponse.error); + } + } catch (error) { + console.error(error); + throw error; + } + } + static async addOnlineStats(pseudo: string, win: number, time: number){ try { const response = await fetch(ADRESSE_DBSERVER + '/session/addOnlineStats', {