Centralized array random pick. Made duck miss ratio configurable. Different response for missing Duck befriend and bang.

This commit is contained in:
ThePendulum 2022-10-21 22:44:31 +02:00
parent 0deea0e016
commit 741f770d27
4 changed files with 50 additions and 22 deletions

View File

@ -44,6 +44,7 @@ module.exports = {
duck: {
interval: [10, 3600], // seconds
duck: ':duck:',
missRatio: 0.25,
},
letters: {
length: 9,

View File

@ -3,6 +3,7 @@
const config = require('config');
const style = require('../utils/style');
const pickRandom = require('../utils/pick-random');
const ducks = new Map();
let shots = new Map();
@ -31,17 +32,35 @@ function launchDuck(context) {
function onCommand(args, context) {
const duck = ducks.get(context.room.id);
if (!duck) {
context.sendMessage(`There is no duck, what are you shooting at, ${config.usernamePrefix}${context.user.username}?!`, context.room.id);
if (!duck && context.command === 'bang') {
const message = pickRandom([
`There is no duck, what are you shooting at, ${context.user.prefixedUsername}?!`,
`You're wasting bullets, ${context.user.prefixedUsername}, there is no duck!`,
`The only quack is you, ${context.user.prefixedUsername}...`,
]);
context.sendMessage(message, context.room.id);
return;
}
const hit = Math.random() > 0.3;
if (!duck && ['befriend', 'bef'].includes(context.command)) {
const message = pickRandom([
`You're trying to befriend a non-existent duck, ${context.user.prefixedUsername}...`,
`There aren't any ducks to befriend, ${context.user.prefixedUsername}...`,
`That's no duck, ${context.user.prefixedUsername} ✈️`,
]);
context.sendMessage(message, context.room.id);
return;
}
const hit = Math.random() > config.duck.missRatio;
const time = ((new Date().getTime() - duck.getTime()) / 1000).toFixed(3);
if (context.command === 'bang') {
if (hit) {
context.sendMessage(`You shot a duck in ${style.bold(`${time} seconds`)}, ${config.usernamePrefix}${context.user.username}`, context.room.id);
context.sendMessage(`You shot a duck in ${style.bold(`${time} seconds`)}, ${context.user.prefixedUsername}`, context.room.id);
launchDuck(context);
context.setPoints(context.user, 1, { key: 'bang' });
@ -49,22 +68,22 @@ function onCommand(args, context) {
return;
}
const messages = [
`How could you miss ${style.italic('that')}, ${config.usernamePrefix}${context.user.username}...?!`,
`That's a miss! Better luck next time, ${config.usernamePrefix}${context.user.username}.`,
`The duck outsmarted you, ${config.usernamePrefix}${context.user.username}`,
`Channeling Gareth Southgate, ${config.usernamePrefix}${context.user.username}? You missed!`,
];
const message = pickRandom([
`How could you miss ${style.italic('that')}, ${context.user.prefixedUsername}...?!`,
`That's a miss! Better luck next time, ${context.user.prefixedUsername}.`,
`The duck outsmarted you, ${context.user.prefixedUsername}`,
`Channeling Gareth Southgate, ${context.user.prefixedUsername}? You missed!`,
]);
shots.set(context.user.id, new Date());
context.sendMessage(messages[Math.floor(Math.random() * messages.length)], context.room.id);
context.sendMessage(message, context.room.id);
return;
}
if (['bef', 'befriend'].includes(context.command)) {
if (hit) {
context.sendMessage(`You befriended a duck in ${style.bold(`${time} seconds`)}, ${config.usernamePrefix}${context.user.username}`, context.room.id);
context.sendMessage(`You befriended a duck in ${style.bold(`${time} seconds`)}, ${context.user.prefixedUsername}`, context.room.id);
launchDuck(context);
context.setPoints(context.user, 1, { key: 'befriend' });
@ -72,14 +91,14 @@ function onCommand(args, context) {
return;
}
const messages = [
`The duck does not want to be your friend right now, ${config.usernamePrefix}${context.user.username}`,
`The duck would like some time for itself, ${config.usernamePrefix}${context.user.username}`,
`The duck isn't in the mood right now, ${config.usernamePrefix}${context.user.username}`,
];
const message = pickRandom([
`The duck does not want to be your friend right now, ${context.user.prefixedUsername}`,
`The duck would like some time for itself, ${context.user.prefixedUsername}`,
`The duck isn't in the mood right now, ${context.user.prefixedUsername}`,
]);
shots.set(context.user.id, new Date());
context.sendMessage(messages[Math.floor(Math.random() * messages.length)], context.room.id);
context.sendMessage(message, context.room.id);
}
}

View File

@ -3,10 +3,10 @@
const config = require('config');
const timers = require('timers/promises');
const crypto = require('crypto');
const style = require('../utils/style');
const getLeaders = require('../utils/get-leaders');
const getWordKey = require('../utils/get-word-key');
const pickRandom = require('../utils/pick-random');
const words = require('../../assets/mash-words.json');
const availableLetters = {
@ -106,10 +106,10 @@ function pickLetters(type, context) {
}
if (type === 'consonant' || type === 'vowel') {
game.word = `${game.word}${availableLetters[type][crypto.randomInt(0, availableLetters[type].length)]}`;
game.word = `${game.word}${pickRandom(availableLetters[type])}`;
} else {
type.toLowerCase().slice(0, config.letters.length - game.word.length).split('').forEach((typeKey) => {
game.word = `${game.word}${availableLetters[types[typeKey]][crypto.randomInt(0, availableLetters[types[typeKey]].length)]}`;
game.word = `${game.word}${pickRandom(availableLetters[types[typeKey]])}`;
});
}

View File

@ -138,11 +138,19 @@ function getMessageUser(message, bot) {
return {
username: message.from,
id: message.from,
prefixedUsername: `${config.usernamePrefix}${message.from}`,
};
}
if (config.platform === 'schat') {
return bot.users[message.userId] || message.user;
const user = bot.users[message.userId] || message.user;
if (user) {
return {
...user,
prefixedUsername: `${config.usernamePrefix}${user.username}`,
};
}
}
return null;