UNPKG

@andreabiagini5/applicazioni-e-servizi-web-project

Version:
62 lines 2.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccountStats = exports.getAccountRanking = exports.getTopAccounts = void 0; const account_1 = require("../repositories/account"); const account_2 = require("../repositories/account"); /** * Get the leaderboard of the top 5 accounts sorted by rating and name. * @returns the sorted leaderboard of accounts. */ const getTopAccounts = async (count) => { const leaderboard = await getLeaderboard(); return leaderboard.slice(0, count).map(account => ({ username: account.username, rating: account.rating.value, })); }; exports.getTopAccounts = getTopAccounts; /** * Get the ranking of a account in the leaderboard. * @param username the username of the account. * @returns the ranking of the account in the leaderboard. */ const getAccountRanking = async (username) => { const leaderboard = await getLeaderboard(); return leaderboard.findIndex(account => account.username === username); }; exports.getAccountRanking = getAccountRanking; /** * Get the account position and rating. * @param username the username of the account. * @returns the account position and rating. */ const getAccountStats = async (username) => { const position = await (0, exports.getAccountRanking)(username); const rating = (await (0, account_2.readAccountByUsername)(username)).rating.value; return { position: position + 1, // Convert to 1-based index rating: rating, }; }; exports.getAccountStats = getAccountStats; /** * Get the leaderboard of accounts sorted by rating and name. * @returns the sorted leaderboard of accounts. */ const getLeaderboard = async () => { const accounts = await (0, account_1.readAllAccounts)(); return accounts.sort((a, b) => { // sort by rating in descending order if (a.rating > b.rating) return -1; if (a.rating < b.rating) return 1; // if ratings are equal, sort by name in ascending order if (a.username < b.username) return -1; if (a.username > b.username) return 1; return 0; }); }; //# sourceMappingURL=leaderboard.js.map