schat2-clive/src/games/duck.js

90 lines
2.4 KiB
JavaScript

'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}...?!`,
`That's a miss! Better luck next time, @${context.user.username}.`,
`The duck outsmarted you, @${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,
};