From 4984318fc41412872843e8deecc48ec32405f9a1 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Mon, 30 Oct 2017 18:13:00 +0000 Subject: [PATCH] randomRange util function --- commands/avatar-edit/card.js | 3 ++- commands/games/battle.js | 4 ++-- commands/games/fishy.js | 14 ++++++++------ commands/games/gunfight.js | 5 ++--- commands/random-res/guess-my-looks.js | 7 ++++--- util/Util.js | 4 ++++ 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/commands/avatar-edit/card.js b/commands/avatar-edit/card.js index dd5d991a..0b877f13 100644 --- a/commands/avatar-edit/card.js +++ b/commands/avatar-edit/card.js @@ -2,6 +2,7 @@ const { Command } = require('discord.js-commando'); const { createCanvas, loadImage, registerFont } = require('canvas'); const snekfetch = require('snekfetch'); const path = require('path'); +const { randomRange } = require('../../util/Util'); const { version } = require('../../package'); registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto.ttf'), { family: 'Noto' }); registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-CJK.otf'), { family: 'Noto' }); @@ -38,7 +39,7 @@ module.exports = class CardCommand extends Command { size: 512 }); try { - const cardID = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000; + const cardID = randomRange(1000, 9999); let rarity; if (cardID < 5000) rarity = 'C'; else if (cardID < 9000) rarity = 'U'; diff --git a/commands/games/battle.js b/commands/games/battle.js index 768bb712..03e4e0be 100644 --- a/commands/games/battle.js +++ b/commands/games/battle.js @@ -1,6 +1,6 @@ const { Command } = require('discord.js-commando'); const { stripIndents } = require('common-tags'); -const { verify } = require('../../util/Util'); +const { randomRange, verify } = require('../../util/Util'); module.exports = class BattleCommand extends Command { constructor(client) { @@ -88,7 +88,7 @@ module.exports = class BattleCommand extends Command { } else if (choice === 'special') { const hit = Math.floor(Math.random() * 4) + 1; if (hit === 1) { - const damage = Math.floor(Math.random() * ((guard ? 150 : 300) - 100 + 1)) + 100; + const damage = randomRange(100, guard ? 150 : 300); await msg.say(`${user} deals **${damage}** damage!`); dealDamage(damage); } else { diff --git a/commands/games/fishy.js b/commands/games/fishy.js index eda54a72..abfa0a33 100644 --- a/commands/games/fishy.js +++ b/commands/games/fishy.js @@ -1,4 +1,5 @@ const { Command } = require('discord.js-commando'); +const { randomRange } = require('../../util/Util'); const fishes = require('../../assets/json/fishy'); module.exports = class FishyCommand extends Command { @@ -13,13 +14,14 @@ module.exports = class FishyCommand extends Command { } run(msg) { - const fish = Math.floor(Math.random() * 10) + 1; + const fishID = Math.floor(Math.random() * 10) + 1; let rarity; - if (fish < 5) rarity = 'junk'; - else if (fish < 8) rarity = 'common'; - else if (fish < 10) rarity = 'uncommon'; + if (fishID < 5) rarity = 'junk'; + else if (fishID < 8) rarity = 'common'; + else if (fishID < 10) rarity = 'uncommon'; else rarity = 'rare'; - const worth = Math.floor(Math.random() * (fishes[rarity].max - fishes[rarity].min + 1)) + fishes[rarity].min; - return msg.say(`You caught a ${fishes[rarity].symbol}. I bet it'd sell for around $${worth}.`); + const fish = fishes[rarity]; + const worth = randomRange(fish.min, fish.max); + return msg.say(`You caught a ${fish.symbol}. I bet it'd sell for around $${worth}.`); } }; diff --git a/commands/games/gunfight.js b/commands/games/gunfight.js index 9451ff03..0f45bfb8 100644 --- a/commands/games/gunfight.js +++ b/commands/games/gunfight.js @@ -1,5 +1,5 @@ const { Command } = require('discord.js-commando'); -const { wait, verify } = require('../../util/Util'); +const { wait, randomRange, verify } = require('../../util/Util'); const words = ['fire', 'draw', 'shoot', 'bang', 'pull']; module.exports = class GunfightCommand extends Command { @@ -36,8 +36,7 @@ module.exports = class GunfightCommand extends Command { return msg.say('Looks like they declined...'); } await msg.say('Get Ready...'); - const length = Math.floor(Math.random() * (30000 - 1000 + 1)) + 1000; - await wait(length); + await wait(randomRange(1000, 30000)); const word = words[Math.floor(Math.random() * words.length)]; await msg.say(`TYPE \`${word.toUpperCase()}\` NOW!`); const filter = res => [opponent.id, msg.author.id].includes(res.author.id) && res.content.toLowerCase() === word; diff --git a/commands/random-res/guess-my-looks.js b/commands/random-res/guess-my-looks.js index e37a2c4d..d6781171 100644 --- a/commands/random-res/guess-my-looks.js +++ b/commands/random-res/guess-my-looks.js @@ -1,5 +1,6 @@ const { Command } = require('discord.js-commando'); const { oneLine } = require('common-tags'); +const { randomRange } = require('../../util/Util'); const genders = ['male', 'female']; const eyeColors = ['blue', 'brown', 'hazel', 'green', 'yellow']; const hairColors = ['blonde', 'brown', 'red', 'black', 'grey', 'white']; @@ -31,10 +32,10 @@ module.exports = class GuessMyLooksCommand extends Command { const eyeColor = eyeColors[Math.floor(Math.random() * eyeColors.length)]; const hairColor = hairColors[Math.floor(Math.random() * hairColors.length)]; const hairStyle = hairStyles[Math.floor(Math.random() * hairStyles.length)]; - const age = Math.floor(Math.random() * (100 - 10 + 1)) + 10; - const feet = Math.floor(Math.random() * (7 - 3 + 1)) + 3; + const age = randomRange(10, 100); + const feet = randomRange(3, 7); const inches = Math.floor(Math.random() * 12); - const weight = Math.floor(Math.random() * (300 - 50 + 1)) + 50; + const weight = randomRange(50, 300); const extra = extras[Math.floor(Math.random() * extras.length)]; return msg.say(oneLine` I think ${user.username} is a ${age} year old ${gender} with ${eyeColor} eyes and ${hairStyle} ${hairColor} diff --git a/util/Util.js b/util/Util.js index c6e41921..e0dfece4 100644 --- a/util/Util.js +++ b/util/Util.js @@ -48,6 +48,10 @@ class Util { return `${prefix.slice(text.length)}${text}`; } + static randomRange(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; + } + static promisifyAll(obj, suffix = 'Async') { for (const key of Object.keys(obj)) { if (typeof obj[key] !== 'function') continue;