Added anagram solver and dictionary lookup to mash game. Added error handling to controller.

This commit is contained in:
2021-11-04 03:53:25 +01:00
parent 74fe566403
commit 2be37f96b5
3 changed files with 8904 additions and 8 deletions

View File

@@ -6,6 +6,10 @@ const words = require('../../assets/mash-words.json');
let mash = null;
function getWordKey(word) {
return word.split('').sort().join('');
}
function start(length, context, attempt = 0) {
const lengthWords = words[length];
@@ -50,7 +54,7 @@ function play(word, context) {
return;
}
const key = word.split('').sort().join('');
const key = getWordKey(word);
if (key !== mash.key) {
context.sendMessage(`You are not using the letters in **${mash.anagram}**, @${context.user.username}`, context.room.id);
@@ -78,20 +82,76 @@ function play(word, context) {
}
}
function resolve(word, context) {
if (!word) {
context.sendMessage(`Please specify an anagram you would like to resolve, @${context.user.username}`, context.room.id);
return;
}
const anagram = getWordKey(word);
const answers = words[word.length]?.[anagram];
if (answers?.length > 1 && answers.some((answer) => answer.word === word)) {
context.sendMessage(`**${word}** is a valid word in itself, and has the following anagrams, @${context.user.username}: ${answers.filter((answer) => answer.word !== word).map((answer) => `*${answer.word}*`).join(', ')}`, context.room.id);
return;
}
if (answers?.length === 1 && answers[0].word === word) {
context.sendMessage(`**${word}** is a valid word in itself, but has no anagrams, @${context.user.username}`, context.room.id);
return;
}
if (answers?.length > 0) {
context.sendMessage(`Anagrams of **${word}**, @${context.user.username}: ${answers.map((answer) => `*${answer.word}*`).join(', ')}`, context.room.id);
return;
}
context.sendMessage(`No anagrams found for **${word}**, @${context.user.username}`, context.room.id);
}
function define(word, context) {
if (!word) {
context.sendMessage(`Please specify word you would like to define, @${context.user.username}`, context.room.id);
return;
}
const anagram = getWordKey(word);
const answers = words[word.length]?.[anagram];
const answer = answers?.find((answerX) => answerX.word === word);
if (answer && answer.definitions?.length > 0) {
context.sendMessage(`${word} can be defined as follows, @${context.user.username}: *${answer.definitions[0]}*`, context.room.id);
return;
}
context.sendMessage(`No definition available for **${word}**, @${context.user.username}`, context.room.id);
}
function onCommand(args, context) {
const length = Number(args[0]);
const word = args[0];
const length = Number(word);
if (['solve', 'resolve', 'lookup'].includes(context.subcommand)) {
resolve(word, context);
return;
}
if (['define', 'dict', 'dictionary'].includes(context.subcommand)) {
define(word, context);
return;
}
if (!Number.isNaN(length)) {
start(length, context);
return;
}
if (!args[0] && !mash) {
if (!word || !mash) {
context.sendMessage(`Start a mash with ${config.prefix}mash {length}`, context.room.id);
return;
}
if (!args[0] && mash) {
if (!word && mash) {
context.sendMessage(`The current mash is: **${mash.anagram}**`, context.room.id);
return;
}