Skip to content

Commit

Permalink
Added games to the user database
Browse files Browse the repository at this point in the history
  • Loading branch information
iyanfdezz committed Feb 22, 2024
1 parent 88bdb06 commit 61ce2fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 38 deletions.
75 changes: 37 additions & 38 deletions stats/model/stats-getter.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions users/userservice/game-model.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions users/userservice/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 61ce2fe

Please sign in to comment.