-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
base: dev
Are you sure you want to change the base?
Rock paper scissors game #1333
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. switch case could be used here, but meh There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. switch case would look better to read imho There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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": "<rock, paper or scissors> [money to bet]", | ||||||
"noChoice": "You have to give me your choice, either rock, paper or scissors", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"draw": "It's a draw!", | ||||||
"win": "You win!", | ||||||
"lose": "You lose!", | ||||||
"notEnoughMoney": "You don't have this enough money to bet", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"bet": { | ||||||
"draw": "You balance hasn't changed!", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"win": "Nice! You have won **$t(commons:currencyWithCount, { 'count': {{count}} })**", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"lose": "Whoops! You lose **$t(commons:currencyWithCount, { 'count': {{count}} })**" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
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