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

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

8819
assets/mash-words_short.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,10 +11,11 @@ const points = {};
async function auth() {
const httpSession = bhttp.session();
const username = config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username;
const res = await httpSession.post(`${config.api}/session`, {
...config.user,
username: config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username,
username,
}, {
encodeJSON: true,
});
@ -23,7 +24,7 @@ async function auth() {
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
}
logger.info(`Authenticated with ${config.api}`);
logger.info(`Authenticated as '${username}' with ${config.api}`);
return {
user: res.body,
@ -205,6 +206,18 @@ function getGames(bot) {
return games;
}
function handleError(error, socket, domain, data) {
logger.error(`${domain} '${JSON.stringify(data)}' triggered error: ${error.message}`);
if (data?.roomId) {
socket.transmit('message', {
body: ':zap::robot::zap: Many fragments! Some large, some small.',
type: 'message',
roomId: data.roomId,
});
}
}
async function connect(wsCreds, sessionCookie, bot, games) {
const socket = { ws: { readyState: 0 } };
@ -217,11 +230,15 @@ async function connect(wsCreds, sessionCookie, bot, games) {
},
});
socket.ws.on('message', (msg) => {
socket.ws.on('message', async (msg) => {
const [domain, data] = JSON.parse(msg);
if (messageHandlers[domain]) {
messageHandlers[domain](data, bot, games);
try {
await messageHandlers[domain](data, bot, games);
} catch (error) {
handleError(error, socket, domain, data);
}
}
});

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;
}