diff --git a/stats/model/stats-getter.js b/stats/model/stats-getter.js index d886ad16..239c44ec 100644 --- a/stats/model/stats-getter.js +++ b/stats/model/stats-getter.js @@ -1,50 +1,49 @@ -const fs = require('fs'); +const User = require('../../users/userservice/user-mode.js/User'); class StatsForUser { - getStatsForUser(username){ - // Leer el archivo JSON de partidas - const data = fs.readFileSync("./model/partidas.json"); - const partidas = JSON.parse(data); + async getStatsForUser(username){ + try { + const user = await User.findOne({ username: username }); - let nGamesPlayed = 0; - let totalPoints = 0; - let totalCorrectQuestions = 0; - let totalIncorrectQuestions = 0; - - // Calcular las estadísticas para el usuario - for (const partida of partidas){ - if (partida.username === this.username){ - nGamesPlayed++; - totalPoints += partida.points; - totalCorrectQuestions += partida.correctQuestions; - totalIncorrectQuestions += partida.incorrectQuestions; + if (!user) { + throw new Error('Usuario no encontrado'); } - } - - // Calcular el promedio de puntos por juego - const avgPoints = nGamesPlayed > 0 ? totalPoints / nGamesPlayed : 0; - // Calcular el ratio de preguntas acertadas/falladas - const ratioCorrectToIncorrect = totalIncorrectQuestions !== 0 ? totalCorrectQuestions / totalIncorrectQuestions : totalCorrectQuestions; + const partidas = user.games; - // Construir el objeto JSON con las estadísticas - const statsJSON = { - username: this.username, - nGamesPlayed: nGamesPlayed, - avgPoints: avgPoints, - totalPoints: totalPoints, - totalCorrectQuestions: totalCorrectQuestions, - totalIncorrectQuestions: totalIncorrectQuestions, - ratioCorrectToIncorrect: ratioCorrectToIncorrect - }; + var nGamesPlayed = partidas.length; + var totalPoints = 0; + var totalCorrectQuestions = 0; + var totalIncorrectQuestions = 0; - return statsJSON; - } + for (const partida of partidas){ + totalPoints += partida.points; + totalCorrectQuestions += partida.correctAnswers; + totalIncorrectQuestions += partida.incorrectAnswers; + } - existsUser(username){ - //TODO - return true; + const avgPoints = nGamesPlayed > 0 ? + totalPoints / nGamesPlayed : 0; + + const ratioCorrectToIncorrect = totalIncorrectQuestions !== 0 ? + totalCorrectQuestions / totalIncorrectQuestions : totalCorrectQuestions; + + const statsJSON = { + username: username, + nGamesPlayed: nGamesPlayed, + avgPoints: avgPoints, + totalPoints: totalPoints, + totalCorrectQuestions: totalCorrectQuestions, + totalIncorrectQuestions: totalIncorrectQuestions, + ratioCorrectToIncorrect: ratioCorrectToIncorrect + }; + + return statsJSON; + } catch (error) { + console.error('Error al obtener las estadísticas del usuario:', error); + throw error; + } } } diff --git a/users/userservice/game-model.js b/users/userservice/game-model.js new file mode 100644 index 00000000..73311be0 --- /dev/null +++ b/users/userservice/game-model.js @@ -0,0 +1,21 @@ +const mongoose = require('mongoose'); + +const gameSchema = new mongoose.Schema({ + // Atributos de la partida + correctAnswers: { + type: Number, + default: 0 + }, + incorrectAnswers: { + type: Number, + default: 0 + }, + points: { + type: Number, + default: 0 + }, +}); + +const Game = mongoose.model('Game', gameSchema); + +module.exports = Game; diff --git a/users/userservice/user-model.js b/users/userservice/user-model.js index 71d81b5f..99e3a111 100644 --- a/users/userservice/user-model.js +++ b/users/userservice/user-model.js @@ -13,6 +13,10 @@ const userSchema = new mongoose.Schema({ type: Date, default: Date.now, }, + games: [{ + type: mongoose.Schema.Types.ObjectId, + ref: 'Game' + }], }); const User = mongoose.model('User', userSchema);