Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rock paper scissors game #1333

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/commands/games/rockpaperscissors.js
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What the dog doing here

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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch case could be used here, but meh

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could? since It's checking both choices. Also, it's more lines and ugly!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch case would look better to read imho

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only that, but it also performs better

if (choice1 === 'r') return choice2 === 'p' ? 'lose' : 'win'
if (choice1 === 'p') return choice2 === 's' ? 'lose' : 'win'
if (choice1 === 's') return choice2 === 'r' ? 'lose' : 'win'
}
}
12 changes: 12 additions & 0 deletions src/controllers/EconomyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } })
}
}
14 changes: 14 additions & 0 deletions src/locales/en-US/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<rock, paper or scissors> [money to bet]",
"noChoice": "You have to give me your choice, either rock, paper or scissors",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"noChoice": "You have to give me your choice, either rock, paper or scissors",
"noChoice": "You have to choose 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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"notEnoughMoney": "You don't have this enough money to bet",
"notEnoughMoney": "You don't have enough money to bet.",

"bet": {
"draw": "You balance hasn't changed!",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"draw": "You balance hasn't changed!",
"draw": "Your balance hasn't changed!",

"win": "Nice! You have won **$t(commons:currencyWithCount, { 'count': {{count}} })**",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"win": "Nice! You have won **$t(commons:currencyWithCount, { 'count': {{count}} })**",
"win": "Nice! You won **$t(commons:currencyWithCount, { 'count': {{count}} })**",

"lose": "Whoops! You lose **$t(commons:currencyWithCount, { 'count': {{count}} })**"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"lose": "Whoops! You lose **$t(commons:currencyWithCount, { 'count': {{count}} })**"
"lose": "Whoops! You lost **$t(commons:currencyWithCount, { 'count': {{count}} })**"

}
}
}
3 changes: 3 additions & 0 deletions src/utils/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ module.exports = {
STEAM_COLOR: '#1b2838',
MERRIAM_WEBSTER_COLOR: '#375c71',

GENERIC_SUCCESS: '#16f747',
GENERIC_FAILURE: '#e03d19',

// Emojis

// Common
Expand Down