From 76210411f838c73571ff403c40c9d9a11fca974a Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Mon, 15 Nov 2021 22:50:25 +0100 Subject: [PATCH] Fixed leadboard slicing wrong end of list, allowing points lookup by username. --- src/app.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/app.js b/src/app.js index 369eea2..83407e1 100644 --- a/src/app.js +++ b/src/app.js @@ -72,10 +72,14 @@ async function setPoints(defaultKey, user, value, { mode = 'add', key } = {}) { await fs.writeFile(`./points-${instance}.json`, JSON.stringify(points, null, 4)); } -function getPoints(game, { user, room, command }) { - const userPoints = (points[command] || points[game.key])?.[`${user.id}:${user.username}`]; +function getPoints(game, username, { user, room, command }) { + const gamePoints = points[command] || points[game.key]; - game.sendMessage(`You have scored **${userPoints || 0}** points in ${game.name}, @${user.username}`, room.id); + const userPoints = username + ? Object.entries(gamePoints || {}).find(([identifier]) => identifier.split(':')[1] === username)?.[1] + : gamePoints?.[`${user?.id}:${user?.username}`]; + + game.sendMessage(`${username ? `**${username}** has` : 'You have'} scored **${userPoints || 0}** points in ${game.name}, @${user.username}`, room.id); } function getLeaderboard(game, { user, room, command }) { @@ -93,7 +97,7 @@ function getLeaderboard(game, { user, room, command }) { const username = userKey.split(':')[1]; return `**${username === user.username ? '@' : ''}${username}** at **${score}** points`; }) - .slice(-10) + .slice(0, 10) .join(', '); game.sendMessage(`The top ${Math.min(Object.keys(leaderboard).length, 10)} *${game.name}* players are: ${curatedLeaderboard}`, room.id); @@ -140,20 +144,20 @@ function onMessage(message, bot, games) { const user = bot.users[message.userId] || message.user; const room = bot.rooms[message.roomId]; - if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) { - getLeaderboard(games[command], { user, room, command }); - return; - } - - if (['points', 'score'].includes(subcommand) && games[command]) { - getPoints(games[command], { user, room, command }); - return; - } - if (command) { const args = body.split(/\s+/).slice(1); const game = games[command]; + if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) { + getLeaderboard(games[command], { user, room, command }); + return; + } + + if (['points', 'score'].includes(subcommand) && games[command]) { + getPoints(games[command], args[0], { user, room, command }); + return; + } + if (game && game.onCommand) { if (user) { user.points = points[game.key]?.[`${user.id}:${user.username}`] || 0;