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

24 lines
807 B
JavaScript
Raw Normal View History

2022-10-21 03:07:32 +00:00
'use strict';
const config = require('config');
const style = require('./style');
2023-04-10 03:54:27 +00:00
function getLeaders(points, user, { ping = true, limit = 20, skip = [] } = {}) {
return Object.entries(points)
2023-04-10 03:54:27 +00:00
.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
2022-10-23 19:53:59 +00:00
if (index === 0) {
2022-10-23 23:55:30 +00:00
return `${style.bold(style.yellow(`${ping || username === user.username ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`;
}
2022-10-21 03:07:32 +00:00
return `${style.bold(style.cyan(`${ping ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`;
})
.join(', ');
2022-10-21 03:07:32 +00:00
}
module.exports = getLeaders;