Added ping and duck, showing category in trivia.

This commit is contained in:
2021-11-12 16:43:20 +01:00
parent 39341178ff
commit 6ac189235c
5 changed files with 133 additions and 21 deletions
+28 -18
View File
@@ -47,7 +47,9 @@ async function getWsId(httpSession) {
return res.body;
}
async function setPoints(gameKey, user, value, mode = 'add') {
async function setPoints(defaultKey, user, value, { mode = 'add', key }) {
const gameKey = key || defaultKey;
if (!user) {
logger.warn(`Failed to set ${gameKey} points for missing user`);
return;
@@ -70,14 +72,14 @@ async function setPoints(gameKey, user, value, mode = 'add') {
await fs.writeFile(`./points-${instance}.json`, JSON.stringify(points, null, 4));
}
function getPoints(game, { user, room }) {
const userPoints = points[game.key]?.[`${user.id}:${user.username}`];
function getPoints(game, { user, room, command }) {
const userPoints = (points[command] || 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];
function getLeaderboard(game, { user, room, command }) {
const leaderboard = points[command] || points[game.key];
if (!leaderboard || Object.keys(leaderboard).length === 0) {
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
@@ -138,12 +140,12 @@ function onMessage(message, bot, games) {
const room = bot.rooms[message.roomId];
if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) {
getLeaderboard(games[command], { user, room });
getLeaderboard(games[command], { user, room, command });
return;
}
if (['points', 'score'].includes(subcommand) && games[command]) {
getPoints(games[command], { user, room });
getPoints(games[command], { user, room, command });
return;
}
@@ -151,13 +153,14 @@ function onMessage(message, bot, games) {
const args = message.body.split(/\s+/).slice(1);
const game = games[command];
if (game) {
if (game && game.onCommand) {
if (user) {
user.points = points[game.key]?.[`${user.id}:${user.username}`] || 0;
}
games[command].onCommand(args, {
game.onCommand(args, {
...game,
command,
subcommand,
bot,
message,
@@ -214,18 +217,25 @@ function getGames(bot) {
});
};
const setGamePoints = (userId, score, mode) => setPoints(key, userId, score, mode);
const setGamePoints = (userId, score, options) => setPoints(key, userId, score, options);
const curatedGame = {
...game,
...(key.game && key),
name: game.name || key,
key,
sendMessage,
setPoints: setGamePoints,
};
if (game.onStart) {
game.onStart({ ...curatedGame, bot });
}
return {
...acc,
[key]: {
...game,
...(key.game && key),
name: game.name || key,
key,
sendMessage,
setPoints: setGamePoints,
},
[key]: curatedGame,
...game.commands?.reduce((commandAcc, command) => ({ ...commandAcc, [command]: curatedGame }), {}),
};
}, {});