From 91c99428fff85057de427c9673d9db7486ebfd33 Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Fri, 15 Jul 2022 17:46:12 +0200 Subject: [PATCH] Showing game leaderboard when Trivia game is stopped. --- src/games/trivia.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/games/trivia.js b/src/games/trivia.js index 5564f7b..0c939c2 100644 --- a/src/games/trivia.js +++ b/src/games/trivia.js @@ -33,6 +33,16 @@ function scoreRound(context, round) { }).filter(Boolean).join(', '); } +function getLeaders() { + return Object.entries(game.points).sort(([, scoreA], [, scoreB]) => scoreB - scoreA).map(([username, score], index) => { + if (index === 0) { + return `**@${username}** with **${score}** points`; + } + + return `**@${username}** with **${score}** points`; + }).join(', '); +} + async function playRound(context, round = 0) { const ac = new AbortController(); // eslint-disable-line no-undef const now = new Date(); @@ -74,7 +84,7 @@ async function playRound(context, round = 0) { } if (game.stopped) { - context.sendMessage(`The game was stopped by @${game.stopped.username}. The answer to the last question was: **${question.answer}**`, context.room.id); + context.sendMessage(`The game was stopped by @${game.stopped.username}. The answer to the last question was: **${question.answer}**. Best players: ${getLeaders()}`, context.room.id); game = null; return; @@ -98,7 +108,7 @@ async function playRound(context, round = 0) { await timers.setTimeout(5000); if (game.stopped) { - context.sendMessage(`The game was stopped by ${game.stopped.username}`, context.room.id); + context.sendMessage(`The game was stopped by @${game.stopped.username}. The answer to the last question was: **${question.answer}**. Best players: ${getLeaders()}`, context.room.id); game = null; return; @@ -110,15 +120,7 @@ async function playRound(context, round = 0) { await timers.setTimeout(2000); - const leaders = Object.entries(game.points).sort(([, scoreA], [, scoreB]) => scoreB - scoreA).map(([username, score], index) => { - if (index === 0) { - return `**@${username}** with **${score}** points`; - } - - return `**@${username}** with **${score}** points`; - }).join(', '); - - context.sendMessage(`That's the end of the game! Best players: ${leaders}`, context.room.id); + context.sendMessage(`That's the end of the game! Best players: ${getLeaders()}`, context.room.id); game = null; }