2021-10-31 21:33:18 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
|
|
|
const bhttp = require('bhttp');
|
|
|
|
const WebSocket = require('ws');
|
2021-11-01 02:45:04 +00:00
|
|
|
const fs = require('fs').promises;
|
2021-11-01 00:12:17 +00:00
|
|
|
const logger = require('simple-node-logger').createSimpleLogger();
|
|
|
|
|
2021-11-01 02:45:04 +00:00
|
|
|
const points = {};
|
2021-10-31 21:33:18 +00:00
|
|
|
|
|
|
|
async function auth() {
|
2021-11-01 15:06:48 +00:00
|
|
|
const httpSession = bhttp.session();
|
2021-10-31 21:33:18 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
const res = await httpSession.post(`${config.api}/session`, config.user, {
|
|
|
|
encodeJSON: true,
|
|
|
|
});
|
2021-10-31 21:33:18 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
if (res.statusCode !== 200) {
|
|
|
|
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
|
|
|
|
}
|
2021-10-31 21:33:18 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
logger.info(`Authenticated with ${config.api}`);
|
2021-11-01 00:12:17 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
return {
|
|
|
|
user: res.body,
|
|
|
|
httpSession,
|
|
|
|
sessionCookie: res.headers['set-cookie'][0],
|
|
|
|
};
|
2021-10-31 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getWsId(httpSession) {
|
2021-11-01 15:06:48 +00:00
|
|
|
const res = await httpSession.get(`${config.api}/socket`);
|
2021-10-31 21:33:18 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
if (res.statusCode !== 200) {
|
|
|
|
throw new Error(`Failed to retrieve WebSocket ID: ${res.body.toString()}`);
|
|
|
|
}
|
2021-10-31 21:33:18 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
return res.body;
|
2021-10-31 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function connect(wsCreds, sessionCookie) {
|
2021-11-01 15:06:48 +00:00
|
|
|
const ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
|
|
|
|
headers: {
|
|
|
|
cookie: sessionCookie,
|
|
|
|
},
|
|
|
|
});
|
2021-10-31 21:33:18 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
logger.info(`Connected to ${config.socket}`);
|
2021-11-01 00:12:17 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
return ws;
|
2021-10-31 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 02:45:04 +00:00
|
|
|
async function setPoints(gameKey, user, value, mode = 'add') {
|
2021-11-01 15:06:48 +00:00
|
|
|
const userKey = `${user.id}:${user.username}`;
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
if (!points[gameKey]) {
|
|
|
|
points[gameKey] = {};
|
|
|
|
}
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
if (mode === 'add') {
|
|
|
|
points[gameKey][userKey] = (points[gameKey][userKey] || 0) + value;
|
|
|
|
}
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
if (mode === 'set') {
|
|
|
|
points[gameKey][userKey] = value;
|
|
|
|
}
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
await fs.writeFile('./points.json', JSON.stringify(points, null, 4));
|
2021-11-01 02:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPoints(game, { user, room }) {
|
2021-11-01 15:06:48 +00:00
|
|
|
const userPoints = points[game.key]?.[`${user.id}:${user.username}`];
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
game.sendMessage(`You have scored **${userPoints || 0}** points in ${game.name}, @${user.username}`, room.id);
|
2021-11-01 02:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getLeaderboard(game, { user, room }) {
|
2021-11-01 15:06:48 +00:00
|
|
|
const leaderboard = points[game.key];
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
if (!leaderboard || Object.keys(leaderboard).length === 0) {
|
|
|
|
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
const curatedLeaderboard = Object.entries(leaderboard)
|
|
|
|
.sort(([, scoreA], [, scoreB]) => scoreB - scoreA)
|
|
|
|
.map(([userKey, score]) => {
|
|
|
|
const username = userKey.split(':')[1];
|
|
|
|
return `**${username === user.username ? '@' : ''}${username}** at **${score}** points`;
|
|
|
|
})
|
|
|
|
.slice(-10)
|
|
|
|
.join(', ');
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
game.sendMessage(`The top ${Math.min(Object.keys(curatedLeaderboard).length, 10)} ${game.name} players are ${curatedLeaderboard}`, room.id);
|
2021-11-01 02:45:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 00:12:17 +00:00
|
|
|
function onConnect(data, bot) {
|
2021-11-01 15:06:48 +00:00
|
|
|
bot.transmit('joinRooms', { rooms: config.channels });
|
2021-10-31 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 00:12:17 +00:00
|
|
|
function onRooms({ rooms, users }, bot) {
|
2021-11-01 15:06:48 +00:00
|
|
|
logger.info(`Joined ${rooms.map((room) => room.name).join(', ')}`);
|
|
|
|
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
bot.rooms = rooms;
|
|
|
|
bot.users = { ...bot.users, ...users };
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
|
|
|
|
rooms.forEach((room) => {
|
|
|
|
bot.transmit('message', {
|
|
|
|
roomId: room.id,
|
|
|
|
body: `Hi, I am ${config.user.username}, your game host!`,
|
|
|
|
style: config.style,
|
|
|
|
});
|
2021-10-31 21:33:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-01 02:45:04 +00:00
|
|
|
function onMessage(message, bot, games) {
|
2021-11-01 15:06:48 +00:00
|
|
|
const [, command, subcommand] = message.body?.match(new RegExp(`^${config.prefix}(\\w+)(?:\\:(\\w+))?`)) || [];
|
|
|
|
const user = bot.users[message.userId];
|
|
|
|
const room = bot.rooms.find((roomX) => roomX.id === message.roomId);
|
|
|
|
|
|
|
|
if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) {
|
|
|
|
getLeaderboard(games[command], { user, room });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['points', 'score'].includes(subcommand) && games[command]) {
|
|
|
|
getPoints(games[command], { user, room });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command) {
|
|
|
|
const args = message.body.split(/\s+/).slice(1);
|
|
|
|
const game = games[command];
|
|
|
|
|
|
|
|
if (game) {
|
|
|
|
if (user) {
|
|
|
|
user.points = points[game.key]?.[`${user.id}:${user.username}`] || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
games[command].onCommand(args, {
|
|
|
|
...game,
|
|
|
|
subcommand,
|
|
|
|
bot,
|
|
|
|
message,
|
|
|
|
user,
|
|
|
|
room,
|
|
|
|
points: points[game.key] || {},
|
|
|
|
logger,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.values(games).forEach((game) => game.onMessage?.(message, {
|
2021-11-01 02:45:04 +00:00
|
|
|
...game,
|
|
|
|
bot,
|
|
|
|
message,
|
|
|
|
user,
|
|
|
|
room,
|
|
|
|
logger,
|
2021-11-01 15:06:48 +00:00
|
|
|
}));
|
2021-11-01 00:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const messageHandlers = {
|
2021-11-01 15:06:48 +00:00
|
|
|
connect: onConnect,
|
|
|
|
rooms: onRooms,
|
|
|
|
message: onMessage,
|
2021-10-31 21:33:18 +00:00
|
|
|
};
|
|
|
|
|
2021-11-01 02:45:04 +00:00
|
|
|
async function initPoints() {
|
2021-11-01 15:06:48 +00:00
|
|
|
try {
|
|
|
|
const pointsFile = await fs.readFile('./points.json', 'utf-8');
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
Object.assign(points, JSON.parse(pointsFile));
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ENOENT') {
|
|
|
|
logger.info('Creating new points file');
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
await fs.writeFile('./points.json', '{}');
|
|
|
|
initPoints();
|
|
|
|
}
|
2021-11-01 02:45:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 21:33:18 +00:00
|
|
|
async function init() {
|
2021-11-01 15:06:48 +00:00
|
|
|
const { user, httpSession, sessionCookie } = await auth();
|
|
|
|
const wsCreds = await getWsId(httpSession);
|
|
|
|
const ws = connect(wsCreds, sessionCookie);
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
ws.transmit = (domain, data) => {
|
|
|
|
ws.send(JSON.stringify([domain, data]));
|
2021-11-01 02:45:04 +00:00
|
|
|
};
|
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
const bot = {
|
|
|
|
user,
|
|
|
|
ws,
|
|
|
|
httpSession,
|
|
|
|
rooms: [],
|
|
|
|
users: [],
|
|
|
|
transmit: ws.transmit,
|
|
|
|
};
|
2021-10-31 21:33:18 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
const games = config.games.reduce((acc, key) => {
|
|
|
|
const game = require(`./games/${key}`); // eslint-disable-line global-require, import/no-dynamic-require
|
|
|
|
|
|
|
|
const sendMessage = (body, roomId) => {
|
|
|
|
bot.transmit('message', {
|
|
|
|
roomId,
|
|
|
|
body: `[${game.name || key}] ${body}`,
|
|
|
|
style: config.style,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const setGamePoints = (userId, score, mode) => setPoints(key, userId, score, mode);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[key]: {
|
|
|
|
...game,
|
|
|
|
name: game.name || key,
|
|
|
|
key,
|
|
|
|
sendMessage,
|
|
|
|
setPoints: setGamePoints,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
ws.on('message', (msg) => {
|
|
|
|
const [domain, data] = JSON.parse(msg);
|
|
|
|
|
|
|
|
if (messageHandlers[domain]) {
|
|
|
|
messageHandlers[domain](data, bot, games);
|
|
|
|
}
|
|
|
|
});
|
2021-11-01 02:45:04 +00:00
|
|
|
|
2021-11-01 15:06:48 +00:00
|
|
|
await initPoints();
|
2021-10-31 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init();
|