From 5adb55692fd8049fdddc8616d804b9855f5ceb97 Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Fri, 7 Jan 2022 22:52:44 +0100 Subject: [PATCH] Added whisper option. Whispering answer feedback in Trivia timeout mode, added command help. --- src/app.js | 4 +++- src/games/trivia.js | 29 +++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/app.js b/src/app.js index 9d83c28..c4054b5 100644 --- a/src/app.js +++ b/src/app.js @@ -215,9 +215,11 @@ function getGames(bot) { const games = config.games.reduce((acc, key) => { const game = require(`./games/${key.game || key}`); // eslint-disable-line global-require, import/no-dynamic-require - const sendMessage = (body, roomId, options) => { + const sendMessage = (body, roomId, options, recipient) => { bot.socket.transmit('message', { roomId, + recipient, + type: recipient ? 'whisper' : 'message', body: options?.label === false ? body : `[${game.name || key}] ${body}`, style: config.style, }); diff --git a/src/games/trivia.js b/src/games/trivia.js index 9e69597..ffb4a62 100644 --- a/src/games/trivia.js +++ b/src/games/trivia.js @@ -8,6 +8,11 @@ const questions = require('../../assets/jeopardy.json'); const shuffle = require('../utils/shuffle'); const settings = { ...config.trivia }; +const help = { + mode: '\'first\' or \'timeout\'', + rounds: 'rounds per game as a number', + timeout: 'seconds as a number', +}; let game = null; @@ -85,7 +90,7 @@ async function playRound(context, round = 0) { } if (game.mode === 'timeout') { - context.sendMessage(`**STOP!** The correct answer is **${question.answer}**. ${scores}`, context.room.id); + context.sendMessage(`**STOP!** The correct answer is **${question.fullAnswer || question.answer}**. ${scores}`, context.room.id); } } @@ -156,12 +161,20 @@ function onCommand(args, context) { context.sendMessage(`There is no game going on at the moment. Start one with ${config.prefix}trivia!`, context.room.id); } + if (['help', 'commands'].includes(context.subcommand)) { + context.sendMessage(`Available subcommands for ${config.prefix}trivia are :stop, :mode, :rounds and :timeout`, context.room.id); + } + const subcommand = context.subcommand?.toLowerCase(); if (subcommand && settings[subcommand]) { - settings[subcommand] = typeof settings[subcommand] === 'number' ? (Number(args[0]) || settings[subcommand]) : args[0]; + if (args[0]) { + settings[subcommand] = typeof settings[subcommand] === 'number' ? (Number(args[0]) || settings[subcommand]) : args[0]; - context.sendMessage(`${subcommand} set to ${settings[subcommand]}`, context.room.id); + context.sendMessage(`${subcommand} set to ${settings[subcommand]}`, context.room.id); + } else if (help[subcommand]) { + context.sendMessage(`Please give ${help[subcommand]}`, context.room.id); + } } } @@ -170,7 +183,7 @@ async function onMessage(message, context) { return; } - const { answer } = game.questions[game.round]; + const { answer, fullAnswer } = game.questions[game.round]; if (new RegExp(answer, 'i').test(decode(message.originalBody || message.body))) { // resolve HTML entities in case original body is not available, such as & to & game.answers.push({ @@ -181,6 +194,14 @@ async function onMessage(message, context) { if (settings.mode === 'first') { game.ac.abort(); } + + if (settings.mode === 'timeout') { + if (message.type === 'message') { + context.sendMessage(`**${fullAnswer || answer}** is the correct answer! You might want to **/whisper** the answer to me instead, so others can't leech off your impeccable knowledge. You will receive a point at the end of the round.`, context.room.id, null, context.user.username); + } else { + context.sendMessage(`**${fullAnswer || answer}** is the correct answer! You will receive a point at the end of the round.`, context.room.id, null, context.user.username); + } + } } }