Added rudimentary Trivia game.

This commit is contained in:
2021-11-06 01:33:52 +01:00
parent 14fb0f90bf
commit 09f1f5a14a
8 changed files with 231 additions and 5 deletions
+23 -2
View File
@@ -44,6 +44,11 @@ async function getWsId(httpSession) {
}
async function setPoints(gameKey, user, value, mode = 'add') {
if (!user) {
logger.warn(`Failed to set ${gameKey} points for missing user`);
return;
}
const userKey = `${user.id}:${user.username}`;
if (!points[gameKey]) {
@@ -85,7 +90,7 @@ function getLeaderboard(game, { user, room }) {
.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) {
@@ -109,6 +114,19 @@ function onRooms({ rooms, users }, bot) {
});
}
/* eslint-disable no-param-reassign */
function onJoin(data, bot) {
bot.users[data.user.id] = data.user;
if (!bot.rooms[data.roomId].includes(data.user.id)) {
bot.rooms[data.roomId].push(data.user.id);
}
}
function onLeave(data, bot) {
bot.rooms[data.roomId].users = bot.rooms[data.roomId].users.filter((userId) => userId !== data.userId);
}
function onMessage(message, bot, games) {
const [, command, subcommand] = message.body?.match(new RegExp(`^${config.prefix}(\\w+)(?:\\:(\\w+))?`)) || [];
const user = bot.users[message.userId];
@@ -160,6 +178,8 @@ const messageHandlers = {
connect: onConnect,
rooms: onRooms,
message: onMessage,
join: onJoin,
leave: onLeave,
};
async function initPoints() {
@@ -179,7 +199,7 @@ async function initPoints() {
function getGames(bot) {
const games = config.games.reduce((acc, key) => {
const game = require(`./games/${key}`); // eslint-disable-line global-require, import/no-dynamic-require
const game = require(`./games/${key.game || key}`); // eslint-disable-line global-require, import/no-dynamic-require
const sendMessage = (body, roomId) => {
bot.socket.transmit('message', {
@@ -195,6 +215,7 @@ function getGames(bot) {
...acc,
[key]: {
...game,
...(key.game && key),
name: game.name || key,
key,
sendMessage,