Added command handling and a word mash game.
This commit is contained in:
75
src/app.js
75
src/app.js
@@ -3,6 +3,10 @@
|
||||
const config = require('config');
|
||||
const bhttp = require('bhttp');
|
||||
const WebSocket = require('ws');
|
||||
const logger = require('simple-node-logger').createSimpleLogger();
|
||||
|
||||
const games = config.games.reduce((acc, key) => ({ ...acc, [key]: { ...require(`./games/${key}`) }, key }), {});
|
||||
const gamesWithListeners = Object.entries(games).filter(([key, game]) => game.onMessage).map(([key, game]) => ({ ...game, key }));
|
||||
|
||||
async function auth() {
|
||||
const httpSession = bhttp.session();
|
||||
@@ -15,6 +19,8 @@ async function auth() {
|
||||
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
|
||||
}
|
||||
|
||||
logger.info(`Authenticated with ${config.api}`);
|
||||
|
||||
return {
|
||||
user: res.body,
|
||||
httpSession,
|
||||
@@ -39,25 +45,69 @@ function connect(wsCreds, sessionCookie) {
|
||||
},
|
||||
});
|
||||
|
||||
logger.info(`Connected to ${config.socket}`);
|
||||
|
||||
return ws;
|
||||
}
|
||||
|
||||
function onConnect(data, ws) {
|
||||
ws.transmit('joinRooms', { rooms: config.channels });
|
||||
function onConnect(data, bot) {
|
||||
bot.transmit('joinRooms', { rooms: config.channels });
|
||||
}
|
||||
|
||||
function onRooms({ rooms }, ws) {
|
||||
function onRooms({ rooms, users }, bot) {
|
||||
logger.info(`Joined ${rooms.map((room) => room.name).join(', ')}`);
|
||||
|
||||
bot.rooms = rooms;
|
||||
bot.users = { ...bot.users, ...users };
|
||||
|
||||
rooms.forEach((room) => {
|
||||
ws.transmit('message', {
|
||||
bot.transmit('message', {
|
||||
roomId: room.id,
|
||||
body: `Hi, I am ${config.user.username}, your game host!`
|
||||
body: `Hi, I am ${config.user.username}, your game host!`,
|
||||
style: config.style,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const handlers = {
|
||||
function onMessage(message, bot) {
|
||||
const [, command, subcommand] = message.body?.match(new RegExp(`^${config.prefix}(\\w+)(?:\\:(\\w+))?`)) || [];
|
||||
const user = bot.users[message.userId];
|
||||
const room = bot.rooms.find((room) => room.id === message.roomId);
|
||||
|
||||
if (command) {
|
||||
const args = message.body.split(/\s+/).slice(1);
|
||||
const game = games[command];
|
||||
|
||||
if (game) {
|
||||
const sendMessage = (body, roomId) => {
|
||||
bot.transmit('message', {
|
||||
roomId,
|
||||
body: `[${game.name || command}] ${body}`,
|
||||
style: config.style,
|
||||
});
|
||||
};
|
||||
|
||||
games[command].onCommand(args, { subcommand, bot, message, user, room, sendMessage, logger });
|
||||
}
|
||||
}
|
||||
|
||||
gamesWithListeners.forEach((game) => {
|
||||
const sendMessage = (body, roomId) => {
|
||||
bot.transmit('message', {
|
||||
roomId,
|
||||
body: `[${game.name || game.key}] ${body}`,
|
||||
style: config.style,
|
||||
});
|
||||
};
|
||||
|
||||
game.onMessage(message, { bot, message, user, room, sendMessage, logger });
|
||||
});
|
||||
}
|
||||
|
||||
const messageHandlers = {
|
||||
connect: onConnect,
|
||||
rooms: onRooms,
|
||||
message: onMessage,
|
||||
};
|
||||
|
||||
async function init() {
|
||||
@@ -67,12 +117,21 @@ async function init() {
|
||||
|
||||
ws.transmit = (domain, data) => {
|
||||
ws.send(JSON.stringify([domain, data]));
|
||||
}
|
||||
};
|
||||
|
||||
const bot = {
|
||||
user,
|
||||
ws,
|
||||
httpSession,
|
||||
rooms: [],
|
||||
users: [],
|
||||
transmit: ws.transmit,
|
||||
};
|
||||
|
||||
ws.on('message', (msg) => {
|
||||
const [domain, data] = JSON.parse(msg);
|
||||
|
||||
handlers[domain]?.(data, ws);
|
||||
messageHandlers[domain]?.(data, bot);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
100
src/games/mash.js
Normal file
100
src/games/mash.js
Normal file
@@ -0,0 +1,100 @@
|
||||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
const words = require('../../assets/mash-words.json');
|
||||
let mash = null;
|
||||
|
||||
function start(length, context, attempt = 0) {
|
||||
const lengthWords = words[length];
|
||||
|
||||
if (!lengthWords) {
|
||||
context.sendMessage(`No words with ${length} letters available`, context.room.id);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (mash) {
|
||||
context.sendMessage(`The mash **${mash.anagram}** was not guessed, possible answers: ${mash.answers.map((answer) => `**${answer}**`).join(', ')}`, context.room.id);
|
||||
context.logger.info(`Mash '${mash.anagram}' discarded`);
|
||||
|
||||
mash = null;
|
||||
}
|
||||
|
||||
const wordEntries = Object.entries(lengthWords);
|
||||
const [key, answers] = wordEntries[Math.floor(Math.random() * wordEntries.length)];
|
||||
|
||||
const anagram = key.split('').sort(() => Math.random() > .5 ? 1 : -1).join('');
|
||||
|
||||
if (answers.includes(anagram)) {
|
||||
if (attempt >= 10) {
|
||||
context.sendMessage(`Sorry, I did not find a mashable ${length}-letter word`);
|
||||
return;
|
||||
}
|
||||
|
||||
start(length, context, attempt + 1);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
mash = { key, anagram, answers };
|
||||
|
||||
context.sendMessage(`Stomp stomp, here's your mash: **${mash.anagram}**`, context.room.id);
|
||||
context.logger.info(`Mash started, '${anagram}' with answers ${answers.map((answer) => `'${answer}'`).join(', ')}`);
|
||||
}
|
||||
|
||||
function play(word, context) {
|
||||
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 = word.split('').sort().join('');
|
||||
|
||||
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 (mash.answers.includes(word)) {
|
||||
context.sendMessage(mash.answers.length === 1
|
||||
? `**${word}** is the right answer, ${context.user.username}! There were no other options for **${mash.anagram}**.`
|
||||
: `**${word}** is the right answer, ${context.user.username}! Other options for **${mash.anagram}**: ${mash.answers.filter((answer) => answer !== word).map((word) => `**${word}**`).join(', ')}`, context.room.id);
|
||||
|
||||
context.logger.info(`Mash '${mash.anagram}' guessed by '${context.user.username}' with '${word}'`);
|
||||
|
||||
mash = null;
|
||||
|
||||
setTimeout(() => start(word.length, context), 2000);
|
||||
}
|
||||
}
|
||||
|
||||
function onCommand(args, context) {
|
||||
const length = Number(args[0]);
|
||||
|
||||
if (!Number.isNaN(length)) {
|
||||
start(length, context);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!args[0] && !mash) {
|
||||
context.sendMessage(`Start a mash with ${config.prefix}mash {length}`, context.room.id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!args[0] && mash) {
|
||||
context.sendMessage(`The current mash is: **${mash.anagram}**`, context.room.id);
|
||||
return;
|
||||
}
|
||||
|
||||
play(args[0], context);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
name: 'Mash',
|
||||
onCommand,
|
||||
};
|
||||
Reference in New Issue
Block a user