Added command handling and a word mash game.
This commit is contained in:
parent
755411b9c1
commit
bcebf73f43
File diff suppressed because it is too large
Load Diff
|
@ -12,6 +12,10 @@ module.exports = {
|
||||||
},
|
},
|
||||||
socket: 'ws://127.0.0.1:3000/socket',
|
socket: 'ws://127.0.0.1:3000/socket',
|
||||||
api: 'http://127.0.0.1:3000/api',
|
api: 'http://127.0.0.1:3000/api',
|
||||||
encrypt: false,
|
prefix: '~',
|
||||||
|
style: {
|
||||||
|
color: 'var(--message-56)',
|
||||||
|
},
|
||||||
|
games: ['mash'],
|
||||||
channels: ['GamesNight'],
|
channels: ['GamesNight'],
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bhttp": "^1.2.8",
|
"bhttp": "^1.2.8",
|
||||||
"config": "^3.3.6",
|
"config": "^3.3.6",
|
||||||
|
"simple-node-logger": "^21.8.12",
|
||||||
"ws": "^8.2.3",
|
"ws": "^8.2.3",
|
||||||
"yargs": "^17.2.1"
|
"yargs": "^17.2.1"
|
||||||
}
|
}
|
||||||
|
@ -230,6 +231,11 @@
|
||||||
"node": ">=6"
|
"node": ">=6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/lodash": {
|
||||||
|
"version": "4.17.21",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||||
|
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
|
||||||
|
},
|
||||||
"node_modules/lodash.clonedeep": {
|
"node_modules/lodash.clonedeep": {
|
||||||
"version": "4.5.0",
|
"version": "4.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
|
||||||
|
@ -256,6 +262,14 @@
|
||||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
|
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
|
||||||
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
|
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
|
||||||
},
|
},
|
||||||
|
"node_modules/moment": {
|
||||||
|
"version": "2.29.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
|
||||||
|
"integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/ms": {
|
"node_modules/ms": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||||
|
@ -306,6 +320,15 @@
|
||||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||||
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
||||||
},
|
},
|
||||||
|
"node_modules/simple-node-logger": {
|
||||||
|
"version": "21.8.12",
|
||||||
|
"resolved": "https://registry.npmjs.org/simple-node-logger/-/simple-node-logger-21.8.12.tgz",
|
||||||
|
"integrity": "sha512-RPImnYDq3jdUjaTvYLghaF1n65Dd0LV8hdZtlT0X1NZBAkw+lx0ZJtFydcUyYKjg0Yxd27AW9IAIc3OLhTjBzA==",
|
||||||
|
"dependencies": {
|
||||||
|
"lodash": "^4.17.12",
|
||||||
|
"moment": "^2.20.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/stream-length": {
|
"node_modules/stream-length": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/stream-length/-/stream-length-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/stream-length/-/stream-length-1.0.2.tgz",
|
||||||
|
@ -680,6 +703,11 @@
|
||||||
"minimist": "^1.2.5"
|
"minimist": "^1.2.5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"lodash": {
|
||||||
|
"version": "4.17.21",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||||
|
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
|
||||||
|
},
|
||||||
"lodash.clonedeep": {
|
"lodash.clonedeep": {
|
||||||
"version": "4.5.0",
|
"version": "4.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
|
||||||
|
@ -700,6 +728,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
|
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
|
||||||
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
|
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
|
||||||
},
|
},
|
||||||
|
"moment": {
|
||||||
|
"version": "2.29.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
|
||||||
|
"integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ=="
|
||||||
|
},
|
||||||
"ms": {
|
"ms": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||||
|
@ -744,6 +777,15 @@
|
||||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||||
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
||||||
},
|
},
|
||||||
|
"simple-node-logger": {
|
||||||
|
"version": "21.8.12",
|
||||||
|
"resolved": "https://registry.npmjs.org/simple-node-logger/-/simple-node-logger-21.8.12.tgz",
|
||||||
|
"integrity": "sha512-RPImnYDq3jdUjaTvYLghaF1n65Dd0LV8hdZtlT0X1NZBAkw+lx0ZJtFydcUyYKjg0Yxd27AW9IAIc3OLhTjBzA==",
|
||||||
|
"requires": {
|
||||||
|
"lodash": "^4.17.12",
|
||||||
|
"moment": "^2.20.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"stream-length": {
|
"stream-length": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/stream-length/-/stream-length-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/stream-length/-/stream-length-1.0.2.tgz",
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bhttp": "^1.2.8",
|
"bhttp": "^1.2.8",
|
||||||
"config": "^3.3.6",
|
"config": "^3.3.6",
|
||||||
|
"simple-node-logger": "^21.8.12",
|
||||||
"ws": "^8.2.3",
|
"ws": "^8.2.3",
|
||||||
"yargs": "^17.2.1"
|
"yargs": "^17.2.1"
|
||||||
}
|
}
|
||||||
|
|
75
src/app.js
75
src/app.js
|
@ -3,6 +3,10 @@
|
||||||
const config = require('config');
|
const config = require('config');
|
||||||
const bhttp = require('bhttp');
|
const bhttp = require('bhttp');
|
||||||
const WebSocket = require('ws');
|
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() {
|
async function auth() {
|
||||||
const httpSession = bhttp.session();
|
const httpSession = bhttp.session();
|
||||||
|
@ -15,6 +19,8 @@ async function auth() {
|
||||||
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
|
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info(`Authenticated with ${config.api}`);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user: res.body,
|
user: res.body,
|
||||||
httpSession,
|
httpSession,
|
||||||
|
@ -39,25 +45,69 @@ function connect(wsCreds, sessionCookie) {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
logger.info(`Connected to ${config.socket}`);
|
||||||
|
|
||||||
return ws;
|
return ws;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onConnect(data, ws) {
|
function onConnect(data, bot) {
|
||||||
ws.transmit('joinRooms', { rooms: config.channels });
|
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) => {
|
rooms.forEach((room) => {
|
||||||
ws.transmit('message', {
|
bot.transmit('message', {
|
||||||
roomId: room.id,
|
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,
|
connect: onConnect,
|
||||||
rooms: onRooms,
|
rooms: onRooms,
|
||||||
|
message: onMessage,
|
||||||
};
|
};
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
|
@ -67,12 +117,21 @@ async function init() {
|
||||||
|
|
||||||
ws.transmit = (domain, data) => {
|
ws.transmit = (domain, data) => {
|
||||||
ws.send(JSON.stringify([domain, data]));
|
ws.send(JSON.stringify([domain, data]));
|
||||||
}
|
};
|
||||||
|
|
||||||
|
const bot = {
|
||||||
|
user,
|
||||||
|
ws,
|
||||||
|
httpSession,
|
||||||
|
rooms: [],
|
||||||
|
users: [],
|
||||||
|
transmit: ws.transmit,
|
||||||
|
};
|
||||||
|
|
||||||
ws.on('message', (msg) => {
|
ws.on('message', (msg) => {
|
||||||
const [domain, data] = JSON.parse(msg);
|
const [domain, data] = JSON.parse(msg);
|
||||||
|
|
||||||
handlers[domain]?.(data, ws);
|
messageHandlers[domain]?.(data, bot);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,
|
||||||
|
};
|
Loading…
Reference in New Issue