Added leaderboard.

This commit is contained in:
ThePendulum 2021-11-01 03:45:04 +01:00
parent ad35d12810
commit 126113bc9b
7 changed files with 1860 additions and 29 deletions

18
.eslintrc Normal file
View File

@ -0,0 +1,18 @@
{
"root": true,
"extends": "airbnb-base",
"parser": "@babel/eslint-parser",
"parserOptions": {
"sourceType": "script"
},
"env": {
"es2020": true
},
"rules": {
"strict": 0,
"no-unused-vars": ["error", {"argsIgnorePattern": "^_"}],
"no-console": 0,
"indent": ["error", 4],
"max-len": [2, {"code": 400, "tabWidth": 4, "ignoreUrls": true}]
}
}

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules/
config/*
!config/default.js
points.json

View File

@ -569,7 +569,8 @@
"try"
],
"otw": [
"two"
"two",
"tow"
],
"aiv": [
"via"

1706
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -23,5 +23,8 @@
"simple-node-logger": "^21.8.12",
"ws": "^8.2.3",
"yargs": "^17.2.1"
},
"devDependencies": {
"eslint": "^8.1.0"
}
}

View File

@ -3,10 +3,10 @@
const config = require('config');
const bhttp = require('bhttp');
const WebSocket = require('ws');
const fs = require('fs').promises;
const logger = require('simple-node-logger').createSimpleLogger();
const games = config.games.reduce((acc, key) => ({ ...acc, [key]: { ...require(`./games/${key}`) }, key }), {});
const gamesWithListeners = Object.entries(games).filter(([key, game]) => game.onMessage).map(([key, game]) => ({ ...game, key }));
const points = {};
async function auth() {
const httpSession = bhttp.session();
@ -50,6 +50,51 @@ function connect(wsCreds, sessionCookie) {
return ws;
}
async function setPoints(gameKey, user, value, mode = 'add') {
const userKey = `${user.id}:${user.username}`;
if (!points[gameKey]) {
points[gameKey] = {};
}
if (mode === 'add') {
points[gameKey][userKey] = (points[gameKey][userKey] || 0) + value;
}
if (mode === 'set') {
points[gameKey][userKey] = value;
}
await fs.writeFile('./points.json', JSON.stringify(points, null, 4));
}
function getPoints(game, { user, room }) {
const userPoints = points[game.key]?.[`${user.id}:${user.username}`];
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];
if (!leaderboard || Object.keys(leaderboard).length === 0) {
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
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(', ');
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 });
}
@ -69,39 +114,51 @@ function onRooms({ rooms, users }, bot) {
});
}
function onMessage(message, bot) {
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);
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) {
const sendMessage = (body, roomId) => {
bot.transmit('message', {
roomId,
body: `[${game.name || command}] ${body}`,
style: config.style,
});
};
if (user) {
user.points = points[game.key]?.[`${user.id}:${user.username}`] || 0;
}
games[command].onCommand(args, { subcommand, bot, message, user, room, sendMessage, logger });
games[command].onCommand(args, {
...game,
subcommand,
bot,
message,
user,
room,
points: points[game.key] || {},
logger,
});
}
}
gamesWithListeners.forEach((game) => {
const sendMessage = (body, roomId) => {
bot.transmit('message', {
roomId,
body: `[${game.name || game.key}] ${body}`,
style: config.style,
});
};
game.onMessage(message, { bot, message, user, room, sendMessage, logger });
});
Object.values(games).forEach((game) => game.onMessage?.(message, {
...game,
bot,
message,
user,
room,
logger,
}));
}
const messageHandlers = {
@ -110,6 +167,21 @@ const messageHandlers = {
message: onMessage,
};
async function initPoints() {
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');
await fs.writeFile('./points.json', '{}');
initPoints();
}
}
}
async function init() {
const { user, httpSession, sessionCookie } = await auth();
const wsCreds = await getWsId(httpSession);
@ -128,11 +200,40 @@ async function init() {
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);
return {
...acc,
[key]: {
...game,
name: game.name || key,
key,
sendMessage,
setPoints: setGamePoints,
},
};
}, {});
const gamesWithListeners = Object.entries(games).filter(([key, game]) => game.onMessage).map(([key, game]) => ({ ...game, key }));
ws.on('message', (msg) => {
const [domain, data] = JSON.parse(msg);
messageHandlers[domain]?.(data, bot);
messageHandlers[domain]?.(data, bot, games);
});
await initPoints();
}
init();

View File

@ -44,28 +44,29 @@ function start(length, context, attempt = 0) {
function play(word, context) {
if (word.length !== mash.key.length) {
context.sendMessage(`Your answer needs to be ${mash.key.length} letters, ${context.user.username}`, context.room.id);
context.sendMessage(`Your answer needs to be ${mash.key.length} letters, @${context.user.username}`, context.room.id);
return;
}
const key = word.split('').sort().join('');
if (key !== mash.key) {
context.sendMessage(`You are not using the letters in **${mash.anagram}**, ${context.user.username}`, context.room.id);
context.sendMessage(`You are not using the letters in **${mash.anagram}**, @${context.user.username}`, context.room.id);
return;
}
if (word === mash.anagram) {
context.sendMessage(`${context.user.username}... :expressionless:`, context.room.id);
context.sendMessage(`@${context.user.username}... :expressionless:`, context.room.id);
return;
}
if (mash.answers.includes(word)) {
context.sendMessage(mash.answers.length === 1
? `**${word}** is the right answer, ${context.user.username}! There were no other options for **${mash.anagram}**.`
: `**${word}** is the right answer, ${context.user.username}! Other options for **${mash.anagram}**: ${mash.answers.filter((answer) => answer !== word).map((word) => `**${word}**`).join(', ')}`, context.room.id);
? `**${word}** is the right answer, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! There were no other options for **${mash.anagram}**.`
: `**${word}** is the right answer, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! Other options for **${mash.anagram}**: ${mash.answers.filter((answer) => answer !== word).map((word) => `**${word}**`).join(', ')}`, context.room.id);
context.logger.info(`Mash '${mash.anagram}' guessed by '${context.user.username}' with '${word}'`);
context.setPoints(context.user, 1);
mash = null;