WIP: Add points system for movies and picks, admin configuration, and stats page updates

This commit is contained in:
quoije
2025-12-26 12:37:30 -05:00
parent 7722ac89c3
commit 1281d77c4e
22 changed files with 4632 additions and 9 deletions
+41 -1
View File
@@ -6,6 +6,7 @@ const User = require("../models/User");
const Movie = require("../models/Movie");
const Setting = require("../models/Setting");
const PlaybackProgress = require("../models/PlaybackProgress");
const OscarPick = require("../models/OscarPick");
const axios = require("axios");
const router = express.Router();
@@ -142,6 +143,32 @@ router.get("/stats", verifyToken, async (req, res) => {
return res.status(400).json({ message: "Aucun films disponible." });
}
// Get points configuration
const POINTS_CONFIG_KEY = "points_config";
const pointsConfigSetting = await Setting.findOne({ key: POINTS_CONFIG_KEY }).select("value").lean();
const pointsConfig = pointsConfigSetting?.value && typeof pointsConfigSetting.value === "object"
? {
pointsPerMovie: typeof pointsConfigSetting.value.pointsPerMovie === "number" ? pointsConfigSetting.value.pointsPerMovie : 1,
pointsPerCorrectPick: typeof pointsConfigSetting.value.pointsPerCorrectPick === "number" ? pointsConfigSetting.value.pointsPerCorrectPick : 1,
}
: { pointsPerMovie: 1, pointsPerCorrectPick: 1 };
// Get all picks for this year with scores
let picksByUserId = new Map();
try {
const allPicks = await OscarPick.find({ year })
.select("userId score")
.lean();
allPicks.forEach(pick => {
const userId = String(pick.userId);
picksByUserId.set(userId, pick.score !== null && pick.score !== undefined ? pick.score : 0);
});
} catch (err) {
console.error("Error fetching picks for stats:", err);
// Continue without picks - users will just have 0 pick points
}
// Build stats for each user
const userStats = users.map((user) => {
const movieByImdbId = new Map(allMovies.map((m) => [m.imdb_id, m]));
@@ -175,13 +202,26 @@ router.get("/stats", verifyToken, async (req, res) => {
const watchedCount = watchedMoviesDetails.length;
const watchedRatio = ((watchedCount / totalMoviesCount) * 100).toFixed(1);
// Calculate points
const userId = String(user._id);
const correctPicks = picksByUserId.get(userId) || 0;
const moviePoints = watchedCount * pointsConfig.pointsPerMovie;
const pickPoints = correctPicks * pointsConfig.pointsPerCorrectPick;
const totalPoints = moviePoints + pickPoints;
return {
name: user.name,
userId: userId,
year,
watchedCount,
totalMoviesCount,
watchedRatio: `${watchedRatio}%`,
watchedMovies: watchedMoviesDetails // Include movie details
watchedMovies: watchedMoviesDetails, // Include movie details
correctPicks: correctPicks,
moviePoints: moviePoints,
pickPoints: pickPoints,
totalPoints: totalPoints,
pointsConfig: pointsConfig
};
});