Added linting.

This commit is contained in:
ThePendulum 2021-11-01 16:06:48 +01:00
parent 126113bc9b
commit d36fe65612
5 changed files with 3439 additions and 489 deletions

14
.editorconfig Normal file
View File

@ -0,0 +1,14 @@
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_size = 4
# Matches multiple files with brace expansion notation
# Set default charset
[*.js]
charset = utf-8

View File

@ -3,7 +3,8 @@
"extends": "airbnb-base", "extends": "airbnb-base",
"parser": "@babel/eslint-parser", "parser": "@babel/eslint-parser",
"parserOptions": { "parserOptions": {
"sourceType": "script" "sourceType": "script",
"requireConfigFile": false
}, },
"env": { "env": {
"es2020": true "es2020": true
@ -12,7 +13,8 @@
"strict": 0, "strict": 0,
"no-unused-vars": ["error", {"argsIgnorePattern": "^_"}], "no-unused-vars": ["error", {"argsIgnorePattern": "^_"}],
"no-console": 0, "no-console": 0,
"indent": ["error", 4], "indent": ["error", "tab"],
"no-tabs": 0,
"max-len": [2, {"code": 400, "tabWidth": 4, "ignoreUrls": true}] "max-len": [2, {"code": 400, "tabWidth": 4, "ignoreUrls": true}]
} }
} }

3602
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -25,6 +25,8 @@
"yargs": "^17.2.1" "yargs": "^17.2.1"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^8.1.0" "@babel/eslint-parser": "^7.16.0",
"eslint": "^7.2.0",
"eslint-config-airbnb-base": "^14.2.1"
} }
} }

View File

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