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",
"parser": "@babel/eslint-parser",
"parserOptions": {
"sourceType": "script"
"sourceType": "script",
"requireConfigFile": false
},
"env": {
"es2020": true
@ -12,7 +13,8 @@
"strict": 0,
"no-unused-vars": ["error", {"argsIgnorePattern": "^_"}],
"no-console": 0,
"indent": ["error", 4],
"indent": ["error", "tab"],
"no-tabs": 0,
"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"
},
"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 = {};
async function auth() {
const httpSession = bhttp.session();
const httpSession = bhttp.session();
const res = await httpSession.post(`${config.api}/session`, config.user, {
encodeJSON: true,
});
const res = await httpSession.post(`${config.api}/session`, config.user, {
encodeJSON: true,
});
if (res.statusCode !== 200) {
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
}
if (res.statusCode !== 200) {
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
}
logger.info(`Authenticated with ${config.api}`);
logger.info(`Authenticated with ${config.api}`);
return {
user: res.body,
httpSession,
sessionCookie: res.headers['set-cookie'][0],
};
return {
user: res.body,
httpSession,
sessionCookie: res.headers['set-cookie'][0],
};
}
async function getWsId(httpSession) {
const res = await httpSession.get(`${config.api}/socket`);
const res = await httpSession.get(`${config.api}/socket`);
if (res.statusCode !== 200) {
throw new Error(`Failed to retrieve WebSocket ID: ${res.body.toString()}`);
}
if (res.statusCode !== 200) {
throw new Error(`Failed to retrieve WebSocket ID: ${res.body.toString()}`);
}
return res.body;
return res.body;
}
function connect(wsCreds, sessionCookie) {
const ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
headers: {
cookie: sessionCookie,
},
});
const ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
headers: {
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') {
const userKey = `${user.id}:${user.username}`;
const userKey = `${user.id}:${user.username}`;
if (!points[gameKey]) {
points[gameKey] = {};
}
if (!points[gameKey]) {
points[gameKey] = {};
}
if (mode === 'add') {
points[gameKey][userKey] = (points[gameKey][userKey] || 0) + value;
}
if (mode === 'add') {
points[gameKey][userKey] = (points[gameKey][userKey] || 0) + value;
}
if (mode === 'set') {
points[gameKey][userKey] = value;
}
if (mode === 'set') {
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 }) {
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 }) {
const leaderboard = points[game.key];
const leaderboard = points[game.key];
if (!leaderboard || Object.keys(leaderboard).length === 0) {
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
if (!leaderboard || Object.keys(leaderboard).length === 0) {
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
return;
}
return;
}
const curatedLeaderboard = Object.entries(leaderboard)
.sort(([userKey, scoreA], [userKeyB, scoreB]) => scoreB - scoreA)
.map(([userKey, score]) => {
const username = userKey.split(':')[1];
return `**${username === user.username ? '@' : ''}${username}** at **${score}** points`
})
.slice(-10)
.join(', ');
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(', ');
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) {
bot.transmit('joinRooms', { rooms: config.channels });
bot.transmit('joinRooms', { rooms: config.channels });
}
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;
bot.users = { ...bot.users, ...users };
/* 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,
rooms.forEach((room) => {
bot.transmit('message', {
roomId: room.id,
body: `Hi, I am ${config.user.username}, your game host!`,
style: config.style,
});
});
});
}
function onMessage(message, bot, games) {
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);
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 (['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 (['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 (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;
}
if (game) {
if (user) {
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,
subcommand,
bot,
message,
user,
room,
points: points[game.key] || {},
logger,
});
}
}
Object.values(games).forEach((game) => game.onMessage?.(message, {
...game,
bot,
message,
user,
room,
logger,
}));
}));
}
const messageHandlers = {
connect: onConnect,
rooms: onRooms,
message: onMessage,
connect: onConnect,
rooms: onRooms,
message: onMessage,
};
async function initPoints() {
try {
const pointsFile = await fs.readFile('./points.json', 'utf-8');
try {
const pointsFile = await fs.readFile('./points.json', 'utf-8');
Object.assign(points, JSON.parse(pointsFile));
} catch (error) {
if (error.code === 'ENOENT') {
logger.info('Creating new points file');
Object.assign(points, JSON.parse(pointsFile));
} catch (error) {
if (error.code === 'ENOENT') {
logger.info('Creating new points file');
await fs.writeFile('./points.json', '{}');
initPoints();
await fs.writeFile('./points.json', '{}');
initPoints();
}
}
}
}
async function init() {
const { user, httpSession, sessionCookie } = await auth();
const wsCreds = await getWsId(httpSession);
const ws = connect(wsCreds, sessionCookie);
const { user, httpSession, sessionCookie } = await auth();
const wsCreds = await getWsId(httpSession);
const ws = connect(wsCreds, sessionCookie);
ws.transmit = (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,
});
ws.transmit = (domain, data) => {
ws.send(JSON.stringify([domain, data]));
};
const setGamePoints = (userId, score, mode) => setPoints(key, userId, score, mode);
return {
...acc,
[key]: {
...game,
name: game.name || key,
key,
sendMessage,
setPoints: setGamePoints,
},
const bot = {
user,
ws,
httpSession,
rooms: [],
users: [],
transmit: ws.transmit,
};
}, {});
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 [domain, data] = JSON.parse(msg);
const sendMessage = (body, roomId) => {
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();