From d98c4c12fa14990b57c6d6f36cc4d3a9c2b4461e Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Thu, 18 Nov 2021 01:14:35 +0100 Subject: [PATCH] Added definitions to wordmash hints, listening broadly for wordmash answers. --- src/games/mash.js | 49 +++++++++++++++++++++++++++------------------ src/games/trivia.js | 2 +- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/games/mash.js b/src/games/mash.js index acb2c13..12d907c 100644 --- a/src/games/mash.js +++ b/src/games/mash.js @@ -48,32 +48,34 @@ function start(length, context, attempt = 0) { context.logger.info(`Mash started, '${anagram}' with answers ${answers.map((answer) => `'${answer.word}'`).join(', ')}`); } -function play(rawWord, context) { +function play(rawWord, context, shouted) { const word = rawWord.toLowerCase(); - - if (word.length !== mash.key.length) { - context.sendMessage(`Your answer needs to be ${mash.key.length} letters, @${context.user.username}`, context.room.id); - return; - } - const key = getWordKey(word); + const answer = mash.answers.find((answerX) => answerX.word === word); - if (key !== mash.key) { - context.sendMessage(`You are not using the letters in **${mash.anagram}**, @${context.user.username}`, context.room.id); - return; + if (!shouted) { + if (word.length !== mash.key.length) { + context.sendMessage(`Your answer needs to be ${mash.key.length} letters, @${context.user.username}`, context.room.id); + return; + } + + if (key !== mash.key) { + context.sendMessage(`You are not using the letters in **${mash.anagram}**, @${context.user.username}`, context.room.id); + return; + } + + if (word === mash.anagram) { + context.sendMessage(`@${context.user.username}... :expressionless:`, context.room.id); + return; + } } - if (word === mash.anagram) { - context.sendMessage(`@${context.user.username}... :expressionless:`, context.room.id); - return; - } - - if (mash.answers.some((answer) => answer.word === word)) { - const definition = mash.answers[0].definitions[0] ? `: *${mash.answers[0].definitions[0].slice(0, 100)}${mash.answers[0].definitions[0].length > 100 ? '...*' : '*'}` : ''; + if (answer) { + const definition = answer.definitions[0] ? `: *${answer.definitions[0].slice(0, 100)}${mash.answers[0].definitions[0].length > 100 ? '...*' : '*'}` : ''; context.sendMessage(mash.answers.length === 1 ? `**${word}** is the right answer${definition}, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! There were no other options for **${mash.anagram}**.` - : `**${word}** is the right answer${definition}, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! Other options for **${mash.anagram}**: ${mash.answers.filter((answer) => answer.word !== word).map((answer) => `*${answer.word}*`).join(', ')}`, context.room.id); + : `**${word}** is the right answer${definition}, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! Other options for **${mash.anagram}**: ${mash.answers.filter((answerX) => answerX.word !== word).map((answerX) => `*${answerX.word}*`).join(', ')}`, context.room.id); context.logger.info(`Mash '${mash.anagram}' guessed by '${context.user.username}' with '${word}'`); context.setPoints(context.user, 1); @@ -141,11 +143,11 @@ function hint(context) { } if (mash.anagram.length === 4) { - context.sendMessage(`Hints for **${mash.anagram}**, @${context.user.username}: ${mash.answers.map((answer) => `**${answer.word.slice(0, 1)} ${'_ '.repeat(answer.word.length - 1).trim()}**`).join(', ')}`, context.room.id); + context.sendMessage(`Hints for **${mash.anagram}**, @${context.user.username}: ${mash.answers.map((answer) => `**${answer.word.slice(0, 1)} ${'_ '.repeat(answer.word.length - 1).trim()}** (${answer.definitions[0]})`).join(', ')}`, context.room.id); return; } - context.sendMessage(`Hints for **${mash.anagram}**, @${context.user.username}: ${mash.answers.map((answer) => `**${answer.word.slice(0, 1)} ${'_ '.repeat(answer.word.length - 2)}${answer.word.slice(-1)}**`).join(', ')}`, context.room.id); + context.sendMessage(`Hints for **${mash.anagram}**, @${context.user.username}: ${mash.answers.map((answer) => `**${answer.word.slice(0, 1)} ${'_ '.repeat(answer.word.length - 2)}${answer.word.slice(-1)}** (${answer.definitions[0]})`).join(', ')}`, context.room.id); } function onCommand(args, context) { @@ -185,7 +187,14 @@ function onCommand(args, context) { play(args[0], context); } +function onMessage(message, context) { + if (mash && context.user.id !== config.user.id) { + play(message.body, context, true); + } +} + module.exports = { name: 'Mash', onCommand, + onMessage, }; diff --git a/src/games/trivia.js b/src/games/trivia.js index 4146a19..2590bea 100644 --- a/src/games/trivia.js +++ b/src/games/trivia.js @@ -69,7 +69,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}**`, context.room.id); game = null; return;