From 2a9311f0218e833715b4f82cce5c4f2729ef2a4a Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Mon, 8 Nov 2021 02:48:26 +0100 Subject: [PATCH] Check room exists before removing user. --- src/app.js | 4 +++- src/games/trivia.js | 9 ++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/app.js b/src/app.js index f1a18b5..a729973 100644 --- a/src/app.js +++ b/src/app.js @@ -123,7 +123,9 @@ function onJoin(data, bot) { } function onLeave(data, bot) { - bot.rooms[data.roomId].users = bot.rooms[data.roomId].users.filter((userId) => userId !== data.userId); + if (bot.rooms[data.roomId]) { + bot.rooms[data.roomId].users = bot.rooms[data.roomId].users.filter((userId) => userId !== data.userId); + } } function onMessage(message, bot, games) { diff --git a/src/games/trivia.js b/src/games/trivia.js index 54c515c..47551c0 100644 --- a/src/games/trivia.js +++ b/src/games/trivia.js @@ -5,11 +5,7 @@ const timers = require('timers/promises'); const questions = require('../../assets/jeopardy.json'); -const settings = { - rounds: config.trivia.rounds, - timeout: config.trivia.timeout, - mode: config.trivia.mode, -}; +const settings = { ...config.trivia }; let game = null; @@ -100,6 +96,8 @@ async function playRound(context, round = 0) { return; } + 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`; @@ -107,6 +105,7 @@ async function playRound(context, round = 0) { return `**@${username}** with **${score}** points`; }).join(', '); + context.sendMessage(`That's the end of the game! Best players: ${leaders}`, context.room.id); game = null;