Added anagram solver and dictionary lookup to mash game. Added error handling to controller.

This commit is contained in:
2021-11-04 03:53:25 +01:00
parent 74fe566403
commit 2be37f96b5
3 changed files with 8904 additions and 8 deletions
+21 -4
View File
@@ -11,10 +11,11 @@ const points = {};
async function auth() {
const httpSession = bhttp.session();
const username = config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username;
const res = await httpSession.post(`${config.api}/session`, {
...config.user,
username: config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username,
username,
}, {
encodeJSON: true,
});
@@ -23,7 +24,7 @@ async function auth() {
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
}
logger.info(`Authenticated with ${config.api}`);
logger.info(`Authenticated as '${username}' with ${config.api}`);
return {
user: res.body,
@@ -205,6 +206,18 @@ function getGames(bot) {
return games;
}
function handleError(error, socket, domain, data) {
logger.error(`${domain} '${JSON.stringify(data)}' triggered error: ${error.message}`);
if (data?.roomId) {
socket.transmit('message', {
body: ':zap::robot::zap: Many fragments! Some large, some small.',
type: 'message',
roomId: data.roomId,
});
}
}
async function connect(wsCreds, sessionCookie, bot, games) {
const socket = { ws: { readyState: 0 } };
@@ -217,11 +230,15 @@ async function connect(wsCreds, sessionCookie, bot, games) {
},
});
socket.ws.on('message', (msg) => {
socket.ws.on('message', async (msg) => {
const [domain, data] = JSON.parse(msg);
if (messageHandlers[domain]) {
messageHandlers[domain](data, bot, games);
try {
await messageHandlers[domain](data, bot, games);
} catch (error) {
handleError(error, socket, domain, data);
}
}
});