schat2-clive/src/play.js

271 lines
7.2 KiB
JavaScript
Raw Normal View History

2022-10-18 23:24:13 +00:00
'use strict';
const config = require('config');
const fs = require('fs').promises;
const logger = require('simple-node-logger').createSimpleLogger();
const { argv } = require('yargs');
// const timers = require('timers/promises');
2022-10-23 19:53:59 +00:00
const getLeaders = require('./utils/get-leaders');
2022-10-18 23:24:13 +00:00
const style = require('./utils/style');
logger.setLevel(argv.level || 'info');
const points = {};
async function initPoints(identifier) {
try {
const pointsFile = await fs.readFile(`./points-${identifier}.json`, 'utf-8');
Object.assign(points, JSON.parse(pointsFile));
} catch (error) {
if (error.code === 'ENOENT') {
logger.info('Creating new points file');
await fs.writeFile(`./points-${identifier}.json`, '{}');
2022-10-19 22:07:48 +00:00
await initPoints(identifier);
2022-10-18 23:24:13 +00:00
}
}
}
async function setPoints(identifier, 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-${identifier}.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}`];
2022-10-28 03:06:48 +00:00
game.sendMessage(`${username ? `${style.bold(username)} has` : 'You have'} scored ${style.bold(userPoints || 'no')} points in ${game.name}, ${config.usernamePrefix}${user.username}`, room.id);
2022-10-18 23:24:13 +00:00
}
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;
}
2023-04-10 03:54:27 +00:00
game.sendMessage(`The top ${Math.min(Object.keys(leaderboard).length, 10)} ${style.italic(game.name)} players are: ${getLeaders(leaderboard, user, { ping: false, limit: 10 })}`, room.id);
2022-10-18 23:24:13 +00:00
}
2022-10-21 23:23:11 +00:00
/* eslint-disable no-irregular-whitespace */
function styleCommands(rawBody) {
const styledCommands = rawBody.replaceAll(new RegExp(`${config.prefix}\\w+`, 'g'), (match) => (match.includes('game') ? `${style.cyan(config.prefix)}${style.italic(style.grey(match.slice(1)))}` : style.cyan(match)));
const styledSubcommands = styledCommands.replaceAll(/:\w+/g, (match) => style.green(match));
return styledSubcommands.replaceAll(/(?<!^)\[[\w\d|-]+\]/g, (match) => {
// using a zero-width space so numerical values don't mess up the color code
const value = match.slice(1, -1).replace(/[|-]/, (delimiter) => `${style.grey(delimiter)}`);
return `${style.grey('[')}${value}${style.grey(']')}`;
});
}
/* eslint-enable no-irregular-whitespace */
2022-11-06 02:11:39 +00:00
function capitalize(string) {
return `${string.slice(0, 1).toUpperCase()}${string.slice(1)}`;
}
2022-10-21 23:23:11 +00:00
function curateMessageBody(rawBody, game, key, options) {
const body = options?.styleCommands ? styleCommands(rawBody) : rawBody;
if (options?.label === false || config.labels === false) {
return body;
}
2022-11-06 15:14:14 +00:00
const label = typeof options?.label === 'string'
2022-11-06 02:11:39 +00:00
? capitalize(options.label)
: game.name || capitalize(key);
return `${style.grey(`[${label}]`)} ${body}`;
2022-10-21 23:23:11 +00:00
}
2022-10-18 23:24:13 +00:00
async function getGames(bot, identifier) {
await initPoints(identifier);
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) => {
2022-10-21 23:23:11 +00:00
const curatedBody = curateMessageBody(body, game, key, options);
2022-10-18 23:24:13 +00:00
if (config.platform === 'irc') {
2023-04-10 23:31:12 +00:00
bot.client.say(/^#/.test(roomId) ? roomId : recipient, curatedBody);
2022-10-18 23:24:13 +00:00
}
if (config.platform === 'schat') {
bot.socket.transmit('message', {
roomId,
2023-04-10 22:37:03 +00:00
recipient: options?.type === 'whisper' || !roomId ? recipient : null,
type: options?.type || 'message',
2022-10-18 23:24:13 +00:00
body: curatedBody,
2022-10-27 22:22:00 +00:00
style: { ...config.style, ...options?.style },
2022-10-18 23:24:13 +00:00
});
}
};
2022-11-06 02:11:39 +00:00
const setGamePoints = (user, score = 1, options) => setPoints(identifier, key, user, score, options);
2022-10-18 23:24:13 +00:00
const curatedGame = {
...game,
...(key.game && key),
name: game.name || key,
key,
sendMessage,
setPoints: setGamePoints,
};
if (game.onStart) {
2023-04-08 23:44:22 +00:00
game.onStart({ ...curatedGame, bot, logger });
2022-10-18 23:24:13 +00:00
}
return {
...acc,
[key]: curatedGame,
...game.commands?.reduce((commandAcc, command) => ({ ...commandAcc, [command]: curatedGame }), {}),
};
}, {});
return games;
}
function getMessageUser(message, bot) {
if (config.platform === 'irc') {
return {
username: message.from,
id: message.from,
prefixedUsername: `${config.usernamePrefix}${message.from}`,
2022-10-18 23:24:13 +00:00
};
}
if (config.platform === 'schat') {
const user = bot.users[message.userId] || message.user;
if (user) {
return {
...user,
prefixedUsername: `${config.usernamePrefix}${user.username}`,
};
}
2022-10-18 23:24:13 +00:00
}
return null;
}
function getMessageRoom(message, bot) {
if (config.platform === 'irc') {
return {
id: message.to,
name: message.to,
};
}
if (config.platform === 'schat') {
return bot.rooms[message.roomId];
}
return null;
}
function onMessage(message, bot, games) {
const body = message.originalBody || message.body;
2022-11-08 21:21:43 +00:00
const [, command, subcommand] = body?.match(new RegExp(`^${config.prefix}([\\w>]+)(?:\\:(\\w+))?`)) || [];
2022-10-18 23:24:13 +00:00
const user = getMessageUser(message, bot);
const room = getMessageRoom(message, bot);
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.help && ['help', 'commands'].includes(subcommand)) {
2022-10-21 23:23:11 +00:00
game.sendMessage(game.help, room.id, { styleCommands: true });
if (game.blockHelp !== false) {
return;
}
}
2022-10-18 23:24:13 +00:00
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] || {},
2022-10-21 23:23:11 +00:00
games,
2022-10-18 23:24:13 +00:00
logger,
});
}
}
if (message.type === 'message' && user?.username !== config.user.username) {
2022-10-24 20:57:08 +00:00
Object.values(Object.fromEntries(Object.values(games).map((game) => [game.key, game]))).forEach((game) => game.onMessage?.(message, {
...game,
bot,
message,
2023-04-10 22:37:03 +00:00
containsCommand: command,
containsSubcommand: subcommand,
2022-10-24 20:57:08 +00:00
user: user && {
...user,
points: points[game.key]?.[`${user.id}:${user.username}`] || 0,
},
room,
logger,
}));
}
2022-10-18 23:24:13 +00:00
}
module.exports = {
onMessage,
getGames,
getLeaderboard,
getPoints,
setPoints,
initPoints,
};