From 79133198175bc7790ccae2c414eb9c576cd5f2f5 Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Tue, 5 Oct 2021 09:49:32 -0300 Subject: [PATCH 1/2] Rock paper scissors game Closes #495 --- src/commands/games/rockpaperscissors.js | 75 +++++++++++++++++++++++++ src/controllers/EconomyController.js | 12 ++++ src/locales/en-US/commands.json | 14 +++++ src/utils/Constants.js | 3 + 4 files changed, 104 insertions(+) create mode 100644 src/commands/games/rockpaperscissors.js diff --git a/src/commands/games/rockpaperscissors.js b/src/commands/games/rockpaperscissors.js new file mode 100644 index 000000000..01f34afea --- /dev/null +++ b/src/commands/games/rockpaperscissors.js @@ -0,0 +1,75 @@ +const { Command, CommandError, SwitchbladeEmbed, Constants } = require('../../') + +const handEmojis = ['✊', '✋', '✌'] + +const objectEmojis = ['🪨', '📄', '✂️'] + +module.exports = class RockPaperScissorsCommand extends Command { + constructor (client) { + super({ + name: 'rockpaperscissors', + aliases: ['rps'], + category: 'games', + parameters: [{ + type: 'string', + clean: true, + required: true, + whitelist: ['rock', 'r', 'paper', 'p', 'scissors', 's'], + missingError: 'commands:rockpaperscissors.noChoice' + }, + { + type: 'number', min: 1, required: false + }, + [{ + type: 'booleanFlag', name: 'hand', aliases: ['h', 'hands'] + }]] + }, client) + } + + async run ({ t, author, channel, flags }, choice, betValue) { + console.log(betValue) + const embed = new SwitchbladeEmbed(author) + const emojis = flags.hand ? handEmojis : objectEmojis + const botSelected = ['r', 'p', 's'][Math.floor(Math.random() * 3)] + const botSelectedEmoji = this.getEmoji(botSelected, emojis) + + const result = this.calculateResult(choice, botSelected) + + if (betValue) { + const balance = await this.client.controllers.economy.balance(author.id) + if (betValue > balance) throw new CommandError(t('commands:rockpaperscissors.notEnoughMoney')) + embed.setDescription(t('commands:rockpaperscissors.bet.' + result, { count: betValue })) + if (result === 'win') { + await this.client.controllers.economy.give(author.id, betValue) + } else if (result === 'lose') { + await this.client.controllers.economy.take(author.id, betValue) + } + } + + embed.setTitle(`${this.getEmoji(choice, emojis)} 🆚 ${botSelectedEmoji}`) + embed.setAuthor(t('commands:rockpaperscissors.' + result)) + if (result !== 'draw') { + embed.setColor( + result === 'win' ? Constants.GENERIC_SUCCESS : Constants.GENERIC_FAILURE + ) + } + + channel.send(embed) + } + + getEmoji (choice, emojis) { + const /* valve */index = ['r', 'p', 's'].indexOf(choice[0].toLowerCase()) + return emojis[index] + } + + // Calculates the result on the choice1's perspective + calculateResult (_choice1, _choice2) { + const choice1 = _choice1[0].toLowerCase() + const choice2 = _choice2[0].toLowerCase() + + if (choice1 === choice2) return 'draw' + if (choice1 === 'r') return choice2 === 'p' ? 'lose' : 'win' + if (choice1 === 'p') return choice2 === 's' ? 'lose' : 'win' + if (choice1 === 's') return choice2 === 'r' ? 'lose' : 'win' + } +} diff --git a/src/controllers/EconomyController.js b/src/controllers/EconomyController.js index 439d8428e..89285713d 100644 --- a/src/controllers/EconomyController.js +++ b/src/controllers/EconomyController.js @@ -96,4 +96,16 @@ module.exports = class EconomyController extends Controller { return { won, chosenSide } } + + give (_user, amount) { + return this._users.update(_user, { $inc: { money: amount } }) + } + + async take (_user, amount) { + const balance = await this.balance(_user) + + if (balance < amount) throw new Error('NOT_ENOUGH_MONEY') + + return this._users.update(_user, { $inc: { money: -amount } }) + } } diff --git a/src/locales/en-US/commands.json b/src/locales/en-US/commands.json index 634981c37..e781d3597 100644 --- a/src/locales/en-US/commands.json +++ b/src/locales/en-US/commands.json @@ -1960,5 +1960,19 @@ "categoryObese1": "Obese Class I (Moderately obese)", "categoryObese2": "Obese Class II (Severely obese)", "categoryObese3": "Obese Class III (Very severely obese)" + }, + "rockpaperscissors": { + "commandDescription": "Plays Rock Paper Scissors with the bot", + "commandUsage": " [money to bet]", + "noChoice": "You have to give me your choice, either rock, paper or scissors", + "draw": "It's a draw!", + "win": "You win!", + "lose": "You lose!", + "notEnoughMoney": "You don't have this enough money to bet", + "bet": { + "draw": "You haven't lost or won anything!", + "win": "Nice! You have won **$t(commons:currencyWithCount, { 'count': {{count}} })**", + "lose": "Whoops! You lose **$t(commons:currencyWithCount, { 'count': {{count}} })**" + } } } diff --git a/src/utils/Constants.js b/src/utils/Constants.js index 662c1cef1..ab4ca652c 100644 --- a/src/utils/Constants.js +++ b/src/utils/Constants.js @@ -42,6 +42,9 @@ module.exports = { STEAM_COLOR: '#1b2838', MERRIAM_WEBSTER_COLOR: '#375c71', + GENERIC_SUCCESS: '#16f747', + GENERIC_FAILURE: '#e03d19', + // Emojis // Common From 3a8ee5488c9f1ebb4fe80c8b33c7a0702664cd48 Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Tue, 5 Oct 2021 09:55:03 -0300 Subject: [PATCH 2/2] typo fix --- src/locales/en-US/commands.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/locales/en-US/commands.json b/src/locales/en-US/commands.json index e781d3597..e7c363e97 100644 --- a/src/locales/en-US/commands.json +++ b/src/locales/en-US/commands.json @@ -1970,7 +1970,7 @@ "lose": "You lose!", "notEnoughMoney": "You don't have this enough money to bet", "bet": { - "draw": "You haven't lost or won anything!", + "draw": "You balance hasn't changed!", "win": "Nice! You have won **$t(commons:currencyWithCount, { 'count': {{count}} })**", "lose": "Whoops! You lose **$t(commons:currencyWithCount, { 'count': {{count}} })**" }