From 741f770d272e2c89b31cab6e8925e05cbba629e0 Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Fri, 21 Oct 2022 22:44:31 +0200 Subject: [PATCH] Centralized array random pick. Made duck miss ratio configurable. Different response for missing Duck befriend and bang. --- config/default.js | 1 + src/games/duck.js | 55 +++++++++++++++++++++++++++++--------------- src/games/letters.js | 6 ++--- src/play.js | 10 +++++++- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/config/default.js b/config/default.js index 3976db3..1a59e1c 100644 --- a/config/default.js +++ b/config/default.js @@ -44,6 +44,7 @@ module.exports = { duck: { interval: [10, 3600], // seconds duck: ':duck:', + missRatio: 0.25, }, letters: { length: 9, diff --git a/src/games/duck.js b/src/games/duck.js index 8ba770e..0fc1571 100644 --- a/src/games/duck.js +++ b/src/games/duck.js @@ -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); } } diff --git a/src/games/letters.js b/src/games/letters.js index f0591db..31e8e31 100644 --- a/src/games/letters.js +++ b/src/games/letters.js @@ -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]])}`; }); } diff --git a/src/play.js b/src/play.js index a3704eb..262e046 100644 --- a/src/play.js +++ b/src/play.js @@ -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;