Retrying on connection failure.

This commit is contained in:
ThePendulum 2022-09-19 16:17:23 +02:00
parent 19fb233d8a
commit fb88ded822
3 changed files with 66 additions and 31 deletions

28
assets/mash-average.js Normal file
View File

@ -0,0 +1,28 @@
'use strict';
const fs = require('fs').promises;
async function init() {
const rawDictionary = await fs.readFile('./mash-words.json', 'utf8');
const dictionary = JSON.parse(rawDictionary);
const averages = Object.entries(dictionary)
.reduce((accAverages, [length, anagramsMap]) => {
const anagramsList = Object.values(anagramsMap);
const total = anagramsList.reduce((acc, words) => acc + words.length, 0);
const average = total / anagramsList.length;
return {
...accAverages,
[length]: average,
};
}, {});
console.log(averages);
// CSV
console.log(Object.keys(averages).join(','));
console.log(Object.values(averages).join(','));
}
init();

View File

@ -269,46 +269,53 @@ async function connect(bot, games) {
const socket = { ws: { readyState: 0 } }; const socket = { ws: { readyState: 0 } };
socket.connect = async () => { socket.connect = async () => {
const { user, httpSession, sessionCookie } = await auth(); try {
const wsCreds = await getWsId(httpSession); const { user, httpSession, sessionCookie } = await auth();
const wsCreds = await getWsId(httpSession);
bot.user = user; bot.user = user;
bot.httpSession = httpSession; bot.httpSession = httpSession;
logger.info(`Attempting to connect to ${config.socket}`); logger.info(`Attempting to connect to ${config.socket}`);
socket.ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], { socket.ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
headers: { headers: {
cookie: sessionCookie, cookie: sessionCookie,
}, },
}); });
socket.ws.on('message', async (msg) => { socket.ws.on('message', async (msg) => {
const [domain, data] = JSON.parse(msg); const [domain, data] = JSON.parse(msg);
logger.debug(`Received ${domain}: ${JSON.stringify(data)}`); logger.debug(`Received ${domain}: ${JSON.stringify(data)}`);
if (messageHandlers[domain]) { if (messageHandlers[domain]) {
try { try {
await messageHandlers[domain](data, bot, games); await messageHandlers[domain](data, bot, games);
} catch (error) { } catch (error) {
handleError(error, socket, domain, data); handleError(error, socket, domain, data);
}
} }
} });
});
socket.ws.on('close', async (info) => { socket.ws.on('close', async (info) => {
logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`); logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`);
await delay(config.reconnectDelay * 1000);
socket.connect();
});
socket.ws.on('error', async (error) => {
logger.error(`WebSocket error: ${error.message}`);
});
logger.info(`Connected to ${config.socket}`);
} catch (error) {
logger.error(`Failed to connect, retrying in ${config.reconnectDelay} seconds: ${error.message}`);
await delay(config.reconnectDelay * 1000); await delay(config.reconnectDelay * 1000);
socket.connect(); socket.connect();
}); }
socket.ws.on('error', async (error) => {
logger.error(`WebSocket error: ${error.message}`);
});
logger.info(`Connected to ${config.socket}`);
}; };
socket.transmit = (domain, data) => { socket.transmit = (domain, data) => {

View File

@ -49,8 +49,8 @@ function onCommand(args, context) {
const messages = [ const messages = [
`How could you miss *that*, @${context.user.username}...?!`, `How could you miss *that*, @${context.user.username}...?!`,
`Better luck next time, @${context.user.username}.`, `That's a miss! Better luck next time, @${context.user.username}.`,
`The duck got away, @${context.user.username}`, `The duck outsmarted you, @${context.user.username}`,
`Channeling Gareth Southgate, @${context.user.username}? You missed!`, `Channeling Gareth Southgate, @${context.user.username}? You missed!`,
]; ];