Merged SChat and IRC support.
This commit is contained in:
+9
-151
@@ -1,18 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
const fs = require('fs').promises;
|
||||
const irc = require('irc-upd');
|
||||
const logger = require('simple-node-logger').createSimpleLogger();
|
||||
const { argv } = require('yargs');
|
||||
// const timers = require('timers/promises');
|
||||
|
||||
const style = require('./utils/style');
|
||||
const {
|
||||
onMessage,
|
||||
getGames,
|
||||
} = require('./play');
|
||||
|
||||
logger.setLevel(argv.level || 'info');
|
||||
|
||||
const points = {};
|
||||
|
||||
const client = new irc.Client(config.server, config.user.nick, {
|
||||
userName: config.user.username,
|
||||
realName: config.user.realName,
|
||||
@@ -21,159 +21,16 @@ const client = new irc.Client(config.server, config.user.nick, {
|
||||
secure: true,
|
||||
});
|
||||
|
||||
async function setPoints(defaultKey, user, value, { mode = 'add', key } = {}) {
|
||||
const gameKey = key || defaultKey;
|
||||
|
||||
if (!user) {
|
||||
logger.warn(`Failed to set ${gameKey} points for missing user`);
|
||||
return;
|
||||
}
|
||||
|
||||
const userKey = `${user.id}:${user.username}`;
|
||||
|
||||
if (!points[gameKey]) {
|
||||
points[gameKey] = {};
|
||||
}
|
||||
|
||||
if (mode === 'add') {
|
||||
points[gameKey][userKey] = (points[gameKey][userKey] || 0) + value;
|
||||
}
|
||||
|
||||
if (mode === 'set') {
|
||||
points[gameKey][userKey] = value;
|
||||
}
|
||||
|
||||
await fs.writeFile(`./points-${config.user.nick}.json`, JSON.stringify(points, null, 4));
|
||||
}
|
||||
|
||||
function getPoints(game, rawUsername, { user, room, command }) {
|
||||
const username = rawUsername?.replace(new RegExp(`^${config.usernamePrefix}`), '');
|
||||
const gamePoints = points[command] || points[game.key];
|
||||
|
||||
const userPoints = username
|
||||
? Object.entries(gamePoints || {}).find(([identifier]) => identifier.split(':')[1] === username)?.[1]
|
||||
: gamePoints?.[`${user?.id}:${user?.username}`];
|
||||
|
||||
game.sendMessage(`${username ? `${style.bold(username)} has` : 'You have'} scored ${style.bold(userPoints || 0)} points in ${game.name}, ${config.usernamePrefix}${user.username}`, room.id);
|
||||
}
|
||||
|
||||
function getLeaderboard(game, { user, room, command }) {
|
||||
const leaderboard = points[command] || points[game.key];
|
||||
|
||||
if (!leaderboard || Object.keys(leaderboard).length === 0) {
|
||||
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const curatedLeaderboard = Object.entries(leaderboard)
|
||||
.sort(([, scoreA], [, scoreB]) => scoreB - scoreA)
|
||||
.map(([userKey, score]) => {
|
||||
const username = userKey.split(':')[1];
|
||||
return `${style.bold(`${username === user.username ? config.usernamePrefix : ''}${username}`)} at ${style.bold(score)} points`;
|
||||
})
|
||||
.slice(0, 10)
|
||||
.join(', ');
|
||||
|
||||
game.sendMessage(`The top ${Math.min(Object.keys(leaderboard).length, 10)} ${style.italic(game.name)} players are: ${curatedLeaderboard}`, room.id);
|
||||
}
|
||||
|
||||
function getGames(bot) {
|
||||
const games = config.games.reduce((acc, key) => {
|
||||
const game = require(`./games/${key.game || key}`); // eslint-disable-line global-require, import/no-dynamic-require
|
||||
|
||||
const sendMessage = (body, roomId, options, recipient) => {
|
||||
console.log(roomId || recipient, body);
|
||||
client.say(roomId || recipient, body);
|
||||
};
|
||||
|
||||
const setGamePoints = (userId, score, options) => setPoints(key, userId, score, options);
|
||||
|
||||
const curatedGame = {
|
||||
...game,
|
||||
...(key.game && key),
|
||||
name: game.name || key,
|
||||
key,
|
||||
sendMessage,
|
||||
setPoints: setGamePoints,
|
||||
};
|
||||
|
||||
if (game.onStart) {
|
||||
game.onStart({ ...curatedGame, bot });
|
||||
}
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[key]: curatedGame,
|
||||
...game.commands?.reduce((commandAcc, command) => ({ ...commandAcc, [command]: curatedGame }), {}),
|
||||
};
|
||||
}, {});
|
||||
|
||||
return games;
|
||||
}
|
||||
|
||||
function onMessage(message, bot, games) {
|
||||
const body = message.originalBody || message.body;
|
||||
const [, command, subcommand] = body?.match(new RegExp(`^${config.prefix}(\\w+)(?:\\:(\\w+))?`)) || [];
|
||||
// const user = bot.users[message.userId] || message.user;
|
||||
const user = { username: message.from, id: message.from };
|
||||
const room = { id: message.to, name: message.to };
|
||||
|
||||
if (command) {
|
||||
const args = body.split(/\s+/).slice(1);
|
||||
const game = games[command];
|
||||
|
||||
if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) {
|
||||
getLeaderboard(games[command], { user, room, command });
|
||||
return;
|
||||
}
|
||||
|
||||
if (['points', 'score'].includes(subcommand) && games[command]) {
|
||||
getPoints(games[command], args[0], { user, room, command });
|
||||
return;
|
||||
}
|
||||
|
||||
if (game && game.onCommand) {
|
||||
if (user) {
|
||||
user.points = points[game.key]?.[`${user.id}:${user.username}`] || 0;
|
||||
}
|
||||
|
||||
game.onCommand(args, {
|
||||
...game,
|
||||
command,
|
||||
subcommand,
|
||||
bot,
|
||||
message,
|
||||
user,
|
||||
room,
|
||||
points: points[game.key] || {},
|
||||
logger,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Object.values(games).forEach((game) => game.onMessage?.(message, {
|
||||
...game,
|
||||
bot,
|
||||
message,
|
||||
user: user && {
|
||||
...user,
|
||||
points: points[game.key]?.[`${user.id}:${user.username}`] || 0,
|
||||
},
|
||||
room,
|
||||
logger,
|
||||
}));
|
||||
}
|
||||
|
||||
async function init() {
|
||||
const bot = {
|
||||
client,
|
||||
rooms: config.channels.map((channel) => ({
|
||||
id: channel,
|
||||
name: channel,
|
||||
})),
|
||||
};
|
||||
|
||||
const games = getGames(bot);
|
||||
const games = await getGames(bot, config.user.nick);
|
||||
|
||||
client.addListener('registered', () => {
|
||||
logger.info('Connected!');
|
||||
@@ -195,12 +52,13 @@ async function init() {
|
||||
from,
|
||||
to,
|
||||
body,
|
||||
type: 'message',
|
||||
}, bot, games));
|
||||
|
||||
client.addListener('error', (error) => {
|
||||
console.error(error);
|
||||
logger.error(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
init();
|
||||
module.exports = init;
|
||||
|
||||
Reference in New Issue
Block a user