From 4ef8b550213c21b57b9d5300b0021f87325380ae Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Mon, 24 Oct 2022 00:53:32 +0200 Subject: [PATCH] Fixed leaderboard dumping every single player. --- src/play.js | 2 +- src/utils/get-leaders.js | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/play.js b/src/play.js index a114a6d..bdbb652 100644 --- a/src/play.js +++ b/src/play.js @@ -73,7 +73,7 @@ function getLeaderboard(game, { user, room, command }) { return; } - game.sendMessage(`The top ${Math.min(Object.keys(leaderboard).length, 10)} ${style.italic(game.name)} players are: ${getLeaders(leaderboard, user, false)}`, room.id); + game.sendMessage(`The top ${Math.min(Object.keys(leaderboard).length, 10)} ${style.italic(game.name)} players are: ${getLeaders(leaderboard, user, false, 10)}`, room.id); } /* eslint-disable no-irregular-whitespace */ diff --git a/src/utils/get-leaders.js b/src/utils/get-leaders.js index 0dc80f0..8b30578 100644 --- a/src/utils/get-leaders.js +++ b/src/utils/get-leaders.js @@ -3,16 +3,20 @@ const config = require('config'); const style = require('./style'); -function getLeaders(points, user, ping = true) { - return Object.entries(points).sort(([, scoreA], [, scoreB]) => scoreB - scoreA).map(([userKey, score], index) => { - const username = userKey.split(':')[1] || userKey; // process the points file +function getLeaders(points, user, ping = true, limit = Infinity) { + return Object.entries(points) + .sort(([, scoreA], [, scoreB]) => scoreB - scoreA) + .slice(0, limit) + .map(([userKey, score], index) => { + const username = userKey.split(':')[1] || userKey; // process the points file - if (index === 0) { - return `${style.bold(style.yellow(`${ping ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`; - } + if (index === 0) { + return `${style.bold(style.yellow(`${ping ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`; + } - return `${style.bold(style.cyan(`${ping ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`; - }).join(', '); + return `${style.bold(style.cyan(`${ping ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`; + }) + .join(', '); } module.exports = getLeaders;