ajout stats enigme facile et difficile mais sur les anciens critères
continuous-integration/drone/push Build is passing Details

pull/97/head^2
Baptiste MARCEL 2 years ago
parent 1428c432cb
commit 1de57a2e9b

@ -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();

@ -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);

@ -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;

@ -780,9 +780,17 @@ const MyGraphComponent: React.FC<MyGraphComponentProps> = ({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<MyGraphComponentProps> = ({onNodeClick, handleS
}
}
}
}
}
catch(error){
console.log(error);

@ -97,6 +97,45 @@ class DbUserService implements IUserService{
}
}
async addEasyEnigmaStats(pseudo: string, win: number, time: number): Promise<void> {
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<void> {
// 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<void> {
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<void> {
try {
const result = await SessionService.addOnlineStats(pseudo, win, time);

@ -3,6 +3,9 @@ import User from "../User";
interface IUserService{
fetchUserInformation(): Promise<[User | null, boolean]>
addMastermindStats(pseudo: string, score: number, time: number): Promise<void>
addEasyEnigmaStats(pseudo: string, win: number, time: number): Promise<void>
// addMediumEnigmaStats(pseudo: string, win: number, time: number): Promise<void>
addHardEnigmaStats(pseudo: string, win: number, time: number): Promise<void>
addOnlineStats(pseudo: string, win: number, time: number): Promise<void>
}

@ -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', {

Loading…
Cancel
Save