schat2-clive/src/utils/get-leaders.js

29 lines
842 B
JavaScript
Executable File

'use strict';
const config = require('config');
const style = require('./style');
function getLeaders(points, user, {
ping = true,
limit = 20,
skip = [],
pointsWord = 'points',
} = {}) {
return Object.entries(points)
.filter(([userKey]) => !skip.includes(userKey))
.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 || username === user.username ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} ${pointsWord}`;
}
return `${style.bold(style.cyan(`${ping ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`;
})
.join(', ');
}
module.exports = getLeaders;