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

89
src/games/duck.js Normal file
View File

@@ -0,0 +1,89 @@
'use strict';
const config = require('config');
const ducks = new Map();
let shots = new Map();
function launchDuck(context) {
ducks.delete(context.room?.id);
shots = new Map();
const interval = Array.isArray(config.duck.interval)
? ((Math.random() * (config.duck.interval[1] - config.duck.interval[0])) + config.duck.interval[0]) * 1000
: config.duck.interval * 1000;
setTimeout(() => {
if (context.bot.rooms.length === 0) {
return;
}
const rooms = Object.values(context.bot.rooms);
const room = rooms[Math.floor(Math.random() * rooms.length)];
ducks.set(room.id, new Date());
context.sendMessage('Quack! :duck:', room.id);
}, interval);
}
function onCommand(args, context) {
const duck = ducks.get(context.room.id);
if (!duck) {
context.sendMessage(`There is no duck, what are you shooting at, @${context.user.username}?!`, context.room.id);
return;
}
const hit = Math.random() > 0.3;
const time = ((new Date().getTime() - duck.getTime()) / 1000).toFixed(3);
if (context.command === 'bang') {
if (hit) {
context.sendMessage(`You shot a duck in **${time} seconds**, @${context.user.username}`, context.room.id);
launchDuck(context);
context.setPoints(context.user, 1, { key: 'bang' });
return;
}
const messages = [
`How could you miss *that*, @${context.user.username}...?!`,
`Better luck next time, @${context.user.username}.`,
`The duck got away, @${context.user.username}`,
`Channeling Gareth Southgate, @${context.user.username}? You missed!`,
];
shots.set(context.user.id, new Date());
context.sendMessage(messages[Math.floor(Math.random() * messages.length)], context.room.id);
return;
}
if (['bef', 'befriend'].includes(context.command)) {
if (hit) {
context.sendMessage(`You befriended a duck in **${time} seconds**, @${context.user.username}`, context.room.id);
launchDuck(context);
context.setPoints(context.user, 1, { key: 'befriend' });
return;
}
const messages = [
`The duck does not want to be your friend right now, @${context.user.username}`,
`The duck would like some time for itself, @${context.user.username}`,
`The duck isn't in the mood right now, @${context.user.username}`,
];
shots.set(context.user.id, new Date());
context.sendMessage(messages[Math.floor(Math.random() * messages.length)], context.room.id);
}
}
module.exports = {
name: 'Duck',
commands: ['bang', 'bef', 'befriend'],
onStart: launchDuck,
onCommand,
};

10
src/games/ping.js Normal file
View File

@@ -0,0 +1,10 @@
'use strict';
function onCommand(args, context) {
context.sendMessage(`Pong, @${context.user.username}!`, context.room.id);
}
module.exports = {
name: 'Ping',
onCommand,
};

View File

@@ -50,7 +50,7 @@ async function playRound(context, round = 0) {
const question = game.questions[round];
context.sendMessage(`**Question ${round + 1}/${game.questions.length}**: ${question.question}`, context.room.id);
context.sendMessage(`**Question ${round + 1}/${game.questions.length}** (${question.category}): ${question.question}`, context.room.id);
context.logger.info(`Trivia asked "${question.question}" with answer: ${question.answer}`);
try {
@@ -163,7 +163,7 @@ function onCommand(args, context) {
}
async function onMessage(message, context) {
if (!game) {
if (!game || context.user.id === config.user.id) {
return;
}