@ -1,36 +1,99 @@
const sqlite3 = require ( 'sqlite3 ') ;
const mysql = require ( 'mysql2 ') ;
const path = require ( 'path' ) ;
const { rejects } = require ( 'assert' ) ;
class DatabaseService {
constructor ( ) {
this . db _name = "socialgraph" ;
}
// ----------------------------------------------------
// ------------------- UTILITAIRE ---------------------
// ----------------------------------------------------
async connect ( client ) {
const dbPath = path . resolve ( _ _dirname , ` ../../../db/ ${ this . db _name } .db ` )
async connect ( ) {
const mysqlHost = process . env . MYSQL _HOST ;
const mysqlUser = process . env . MYSQL _USER ;
const mysqlPassword = process . env . MYSQL _PASSWORD ;
const mysqlDatabase = process . env . MYSQL _DATABASE ;
const createTables = process . env . CREATETABLES
const dbConfig = {
host : mysqlHost ,
user : mysqlUser ,
password : mysqlPassword ,
database : mysqlDatabase
} ;
return new Promise ( ( resolve , reject ) => {
this . client = new sqlite3 . Database ( dbPath ,
sqlite3 . OPEN _READWRITE | sqlite3 . OPEN _CREATE ,
( err ) => {
if ( err ) {
this . client = mysql . createConnection ( dbConfig ) ;
this . client . connect ( async ( err ) => {
if ( err ) {
console . log ( err )
reject ( err ) ;
}
else {
} else {
if ( createTables === "true" ) {
await this . createTables ( )
}
resolve ( ) ;
}
} ) ;
} ) ;
}
async createTables ( ) {
try {
// Requête SQL pour créer la table 'users'
const createTableQuery = `
CREATE TABLE IF NOT EXISTS users (
idUser INT AUTO _INCREMENT PRIMARY KEY ,
pseudo VARCHAR ( 50 ) NOT NULL ,
password VARCHAR ( 60 ) NOT NULL ,
profilePicture LONGBLOB ,
createdDate TIMESTAMP DEFAULT CURRENT _TIMESTAMP
) ;
` ;
// Exécuter la requête SQL
this . client . query ( createTableQuery , ( err , result ) => {
if ( err ) {
console . log ( err )
}
else {
console . log ( "Table users créée" )
}
} ) ;
const createTableGameQuery = `
CREATE TABLE IF NOT EXISTS games (
idGame INT AUTO _INCREMENT PRIMARY KEY ,
idUser INT NOT NULL ,
gameType VARCHAR ( 50 ) CHECK ( gameType IN ( 'mastermind' , 'enigme_facile' , 'enigme_moyenne' , 'enigme_difficile' , 'multijoueur' ) ) ,
win INT CHECK ( win IN ( 0 , 1 ) ) DEFAULT 0 ,
score INT NOT NULL DEFAULT 0 ,
time REAL NOT NULL DEFAULT 0 ,
playedDate TIMESTAMP DEFAULT CURRENT _TIMESTAMP ,
FOREIGN KEY ( idUser ) REFERENCES users ( idUser )
) ;
` ;
this . client . query ( createTableGameQuery , ( err , result ) => {
if ( err ) {
console . log ( err )
}
else {
console . log ( "Table games créée" )
}
} ) ;
}
catch ( error ) {
console . log ( error )
}
}
async disconnect ( ) {
return new Promise ( ( resolve , reject ) => {
this . client . close ( ( err ) => {
this . client . end ( ( err ) => {
if ( err ) {
reject ( err ) ;
}
@ -48,83 +111,86 @@ class DatabaseService {
// Récupère l'utilisateur par son id
async getUserByID ( id ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT * FROM users WHERE idUser = ?' , id , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT * FROM users WHERE idUser = ?' , [ id ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
// Récupère l'utilisateur par son pseudo
async getUserByPseudo ( pseudo ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT * FROM users WHERE pseudo = ?' , pseudo , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT * FROM users WHERE pseudo = ?' , [ pseudo ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getUsers ( ) {
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT * FROM users' ) ;
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getGames ( ) {
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT * FROM games' ) ;
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching games: ${ err . message } ` ) ;
}
}
// insère un utilisateur dans la base de données
async insertUser ( user ) {
return new Promise ( ( resolve , reject ) => {
try {
const { pseudo , password } = user ;
this . client . run ( 'INSERT INTO users (pseudo, password) VALUES (?, ?)' , [ pseudo , password ] , ( err , result ) => {
if ( err ) {
reject ( err ) ;
} else {
resolve ( result ) ;
}
} ) ;
} ) ;
const [ rows ] = await this . client . promise ( ) . query ( 'INSERT INTO users (pseudo, password) VALUES (?, ?)' , [ pseudo , password ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async deleteUser ( userId ) {
return new Promise ( ( resolve , reject ) => {
this . client . run ( 'DELETE FROM users WHERE idUser=?' , userId , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'DELETE FROM users WHERE idUser=?' , [ userId ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async updatePseudo ( userId , newPseudo ) {
return new Promise ( ( resolve , reject ) => {
this . client . run ( 'UPDATE users SET pseudo = ? WHERE idUser = ?' , newPseudo , userId , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'UPDATE users SET pseudo = ? WHERE idUser = ?' , [ newPseudo , userId ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async updatePassword ( userId , newPassword ) {
return new Promise ( ( resolve , reject ) => {
this . client . run ( 'UPDATE users SET password = ? WHERE idUser = ?' , newPassword , userId , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'UPDATE users SET password = ? WHERE idUser = ?' , [ newPassword , userId ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
// ---------------------------------------------------------------
@ -132,61 +198,39 @@ class DatabaseService {
// ---------------------------------------------------------------
async getDailyMastermindStats ( ) {
return new Promise ( ( resolve , reject ) => {
// Obtenez la date actuelle au format AAAA-MM-JJ
try {
const currentDate = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
// Récupérer les 5 meilleurs scores de la journée
this . client . all (
'SELECT pseudo, score FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) = ? ORDER BY score ASC LIMIT 10' ,
"mastermind" ,
currentDate ,
( err , result ) => {
if ( err ) {
reject ( err ) ;
} else {
resolve ( result ) ;
}
}
) ;
} ) ;
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT pseudo, score FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) = ? ORDER BY score ASC LIMIT 10' ,
[ "mastermind" ,
currentDate ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getDailyEnigmaStats ( enigmaLevel ) {
return new Promise ( ( resolve , reject ) => {
try {
const currentDate = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
this . client . all (
'SELECT pseudo, time FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) = ? ORDER BY time ASC LIMIT 10' ,
enigmaLevel ,
currentDate ,
( err , result ) => {
if ( err ) {
reject ( err ) ;
} else {
resolve ( result ) ;
}
}
) ;
} ) ;
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT pseudo, time FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) = ? ORDER BY time ASC LIMIT 10' ,
[ enigmaLevel ,
currentDate ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getDailyOnlineStats ( ) {
return new Promise ( ( resolve , reject ) => {
try {
const currentDate = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
this . client . all (
'SELECT pseudo, COUNT(*) AS wins FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) = ? AND win = ? GROUP BY users.idUser ORDER BY wins ASC LIMIT 10' ,
"multijoueur" , currentDate , 1 ,
( err , result ) => {
if ( err ) {
reject ( err ) ;
} else {
resolve ( result ) ;
}
}
) ;
} ) ;
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT pseudo, COUNT(*) AS wins FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) = ? AND win = ? GROUP BY users.idUser ORDER BY wins ASC LIMIT 10' ,
[ "multijoueur" , currentDate , 1 ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
@ -195,70 +239,57 @@ class DatabaseService {
// ---------------------------------------------------------------
async getWeeklyMastermindStats ( ) {
return new Promise ( ( resolve , reject ) => {
try {
const currentDate = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
const currentDay = new Date ( ) . getDay ( ) ;
const firstDayOfWeek = new Date ( new Date ( ) . setDate ( new Date ( ) . getDate ( ) - currentDay ) ) . toISOString ( ) . slice ( 0 , 10 ) ;
this . client . all (
'SELECT pseudo, score FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) BETWEEN ? AND ? ORDER BY score ASC LIMIT 10' ,
"mastermind" ,
firstDayOfWeek ,
currentDate ,
( err , result ) => {
if ( err ) {
reject ( err ) ;
} else {
resolve ( result ) ;
}
}
) ;
} ) ;
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT pseudo, score FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) BETWEEN ? AND ? ORDER BY score ASC LIMIT 10' ,
[ "mastermind" ,
firstDayOfWeek ,
currentDate ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getWeeklyEnigmaStats ( enigmaLevel ) {
return new Promise ( ( resolve , reject ) => {
try {
const currentDate = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
const currentDay = new Date ( ) . getDay ( ) ;
const firstDayOfWeek = new Date ( new Date ( ) . setDate ( new Date ( ) . getDate ( ) - currentDay ) ) . toISOString ( ) . slice ( 0 , 10 ) ;
this . client . all (
'SELECT pseudo, time FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) BETWEEN ? AND ? ORDER BY time ASC LIMIT 10' ,
enigmaLevel ,
firstDayOfWeek ,
currentDate ,
( err , result ) => {
if ( err ) {
reject ( err ) ;
} else {
resolve ( result ) ;
}
}
) ;
} ) ;
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT pseudo, time FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) BETWEEN ? AND ? ORDER BY time ASC LIMIT 10' ,
[ enigmaLevel ,
firstDayOfWeek ,
currentDate ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getWeeklyOnlineStats ( ) {
return new Promise ( ( resolve , reject ) => {
try {
const currentDate = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
const currentDay = new Date ( ) . getDay ( ) ;
const firstDayOfWeek = new Date ( new Date ( ) . setDate ( new Date ( ) . getDate ( ) - currentDay ) ) . toISOString ( ) . slice ( 0 , 10 ) ;
this . client . all (
'SELECT pseudo, COUNT(*) as wins FROM users INNER JOIN games ON users.idUser = games.idUser WHERE gameType = ? AND SUBSTR(playedDate, 1, 10) BETWEEN ? AND ? AND win = ? ORDER BY wins ASC LIMIT 10' ,
"multijoueur" ,
firstDayOfWeek ,
currentDate ,
1 ,
( err , result ) => {
if ( err ) {
reject ( err ) ;
} else {
resolve ( result ) ;
}
}
) ;
} ) ;
const [ rows ] = await this . client . promise ( ) . query ( `
SELECT users . pseudo , COUNT ( * ) as wins
FROM users
INNER JOIN games ON users . idUser = games . idUser
WHERE gameType = ? AND SUBSTR ( playedDate , 1 , 10 ) BETWEEN ? AND ? AND win = ?
GROUP BY users . pseudo
ORDER BY wins ASC
LIMIT 10 ;
` ,
[ "multijoueur" ,
firstDayOfWeek ,
currentDate ,
1 ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
// -------------------------------------------------------------
@ -266,97 +297,69 @@ class DatabaseService {
// -------------------------------------------------------------
async getNbGamesMastermindByUserId ( userId ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT COUNT(*) AS nbGames FROM games WHERE idUser = ? AND gameType = ?' , userId , "mastermind" , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT COUNT(*) AS nbGames FROM games WHERE idUser = ? AND gameType = ?' , [ userId , "mastermind" ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getBestScoreMastermindByUserId ( userId ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT MIN(score) AS bestScore FROM games WHERE idUser = ? AND gameType = ?' , userId , "mastermind" , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT MIN(score) AS bestScore FROM games WHERE idUser = ? AND gameType = ?' , [ userId , "mastermind" ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getAvgNbTryMastermindByUserId ( userId ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT AVG(score) AS avgNbTry FROM games WHERE idUser = ? AND gameType = ?' , userId , "mastermind" , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT AVG(score) AS avgNbTry FROM games WHERE idUser = ? AND gameType = ?' , [ userId , "mastermind" ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async addMastermindStats ( userId , score , time ) {
return new Promise ( ( resolve , reject ) => {
this . client . run ( 'INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)' , userId , "mastermind" , 1 , score , time , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)' , [ userId , "mastermind" , 1 , score , time ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
// -------------------------------------------------------------
// ------------------- STATS EN LIGNE --------------------------
// -------------------------------------------------------------
async getNbGamesOnlineByUserId ( userId ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT COUNT(*) AS nbGames FROM games WHERE idUser = ? AND gameType = ?' , userId , "multijoueur" , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT COUNT(*) AS nbGames FROM games WHERE idUser = ? AND gameType = ?' , [ userId , "multijoueur" ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getNbWinsOnlineByUserId ( userId ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT COUNT(*) AS nbWins FROM games WHERE idUser = ? AND gameType = ? AND win = ?' , userId , "multijoueur" , 1 , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT COUNT(*) AS nbWins FROM games WHERE idUser = ? AND gameType = ? AND win = ?' , [ userId , "multijoueur" , 1 ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
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 ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)' , [ userId , "multijoueur" , win , 0 , time ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
// -------------------------------------------------------------
@ -364,109 +367,84 @@ class DatabaseService {
// -------------------------------------------------------------
async getNbGamesEnigmeByUserId ( userId , enigmaLevel ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT COUNT(*) AS nbGames FROM games WHERE idUser = ? AND gameType = ?' , userId , enigmaLevel , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT COUNT(*) AS nbGames FROM games WHERE idUser = ? AND gameType = ?' , [ userId , enigmaLevel ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getNbWinsEnigmeByUserId ( userId , enigmaLevel ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT COUNT(*) AS nbWins FROM games WHERE idUser = ? AND gameType = ? AND win = ?' , userId , enigmaLevel , 1 , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT COUNT(*) AS nbWins FROM games WHERE idUser = ? AND gameType = ? AND win = ?' , [ userId , enigmaLevel , 1 ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getBestScoreEnigmeByUserId ( userId , enigmaLevel ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT MAX(score) AS bestScore FROM games WHERE idUser = ? AND gameType = ?' , userId , enigmaLevel , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT MAX(score) AS bestScore FROM games WHERE idUser = ? AND gameType = ?' , [ userId , enigmaLevel ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getAvgScoreEnigmeByUserId ( userId , enigmaLevel ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT AVG(score) AS avgScore FROM games WHERE idUser = ? AND gameType = ?' , userId , enigmaLevel , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT AVG(score) AS avgScore FROM games WHERE idUser = ? AND gameType = ?' , [ userId , enigmaLevel ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getBestTimeEnigmeByUserId ( userId , enigmaLevel ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT MIN(time) AS bestTime FROM games WHERE idUser = ? AND gameType = ?' , userId , enigmaLevel , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT MIN(time) AS bestTime FROM games WHERE idUser = ? AND gameType = ?' , [ userId , enigmaLevel ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
async getAvgTimeEnigmeByUserId ( userId , enigmaLevel ) {
return new Promise ( ( resolve , reject ) => {
this . client . get ( 'SELECT AVG(time) AS avgTime FROM games WHERE idUser = ? AND gameType = ?' , userId , enigmaLevel , ( err , result ) => {
if ( err ) {
reject ( err ) ;
}
else {
resolve ( result ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'SELECT AVG(time) AS avgTime FROM games WHERE idUser = ? AND gameType = ?' , [ userId , enigmaLevel ] )
return rows [ 0 ] ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
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 ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)' , [ userId , enigmaLevel , win , 0 , time ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
// async addMediumEnigmaStats(userId, enigmaLevel, score)
async addMediumEnigmaStats ( userId , enigmaLevel , score , time ) {
try {
const [ rows ] = await this . client . promise ( ) . query ( 'INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)' , [ userId , enigmaLevel , 1 , score , time ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
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 ) ;
}
} ) ;
} ) ;
try {
const [ rows ] = await this . client . promise ( ) . query ( 'INSERT INTO games (idUser, gameType, win, score, time) VALUES (?, ?, ?, ?, ?)' , [ userId , enigmaLevel , win , 0 , time ] )
return rows ;
} catch ( err ) {
throw new Error ( ` Error fetching users: ${ err . message } ` ) ;
}
}
}