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 7e9b7e8aa..02438f884 100644 --- a/src/locales/en-US/commands.json +++ b/src/locales/en-US/commands.json @@ -1957,5 +1957,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 balance hasn't changed!", + "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 d7314d701..0e87258e3 100644 --- a/src/utils/Constants.js +++ b/src/utils/Constants.js @@ -43,6 +43,9 @@ module.exports = { MERRIAM_WEBSTER_COLOR: '#375c71', FIVEM_COLOR: '#ff8637', + GENERIC_SUCCESS: '#16f747', + GENERIC_FAILURE: '#e03d19', + // Emojis // Common