From e55f3edf882a470742b4dde7d92c3000afe0b84d Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 10 May 2020 20:47:03 -0400 Subject: [PATCH] Playing Card Commands structure system --- commands/games-sp/blackjack.js | 45 ++++++------------------------ commands/random-res/draw-cards.js | 25 +++-------------- package.json | 3 +- structures/cards/Card.js | 46 +++++++++++++++++++++++++++++++ structures/cards/Deck.js | 44 +++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 58 deletions(-) create mode 100644 structures/cards/Card.js create mode 100644 structures/cards/Deck.js diff --git a/commands/games-sp/blackjack.js b/commands/games-sp/blackjack.js index 04ea2826..6dfc7663 100644 --- a/commands/games-sp/blackjack.js +++ b/commands/games-sp/blackjack.js @@ -1,8 +1,7 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); -const { shuffle, verify } = require('../../util/Util'); -const suits = ['♣', '♥', '♦', '♠']; -const faces = ['Jack', 'Queen', 'King']; +const { verify } = require('../../util/Util'); +const Deck = require('../../structures/cards/Deck'); const hitWords = ['hit', 'hit me']; const standWords = ['stand']; @@ -32,7 +31,7 @@ module.exports = class BlackjackCommand extends Command { const current = this.client.games.get(msg.channel.id); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); try { - this.client.games.set(msg.channel.id, { name: this.name, data: this.generateDeck(deckCount) }); + this.client.games.set(msg.channel.id, { name: this.name, data: new Deck({ deckCount }) }); const dealerHand = []; this.draw(msg.channel, dealerHand); this.draw(msg.channel, dealerHand); @@ -114,44 +113,18 @@ module.exports = class BlackjackCommand extends Command { } } - generateDeck(deckCount) { - const deck = []; - for (let i = 0; i < deckCount; i++) { - for (const suit of suits) { - deck.push({ - value: 11, - display: `${suit} Ace` - }); - for (let j = 2; j <= 10; j++) { - deck.push({ - value: j, - display: `${suit} ${j}` - }); - } - for (const face of faces) { - deck.push({ - value: 10, - display: `${suit} ${face}` - }); - } - } - } - return shuffle(deck); - } - draw(channel, hand) { - const deck = this.client.games.get(channel.id).data; - const card = deck[0]; - deck.shift(); + const { deck } = this.client.games.get(channel.id).data; + const card = deck.draw(); hand.push(card); return card; } calculate(hand) { - return hand.sort((a, b) => a.value - b.value).reduce((a, b) => { - let { value } = b; - if (value === 11 && a + value > 21) value = 1; - return a + value; + return hand.sort((a, b) => a.blackjackValue - b.blackjackValue).reduce((a, b) => { + let { blackjackValue } = b; + if (blackjackValue === 11 && a + blackjackValue > 21) blackjackValue = 1; + return a + blackjackValue; }, 0); } }; diff --git a/commands/random-res/draw-cards.js b/commands/random-res/draw-cards.js index 377ae653..e9d66eb8 100644 --- a/commands/random-res/draw-cards.js +++ b/commands/random-res/draw-cards.js @@ -1,7 +1,5 @@ const Command = require('../../structures/Command'); -const { shuffle } = require('../../util/Util'); -const suits = ['♣', '♥', '♦', '♠']; -const faces = ['Jack', 'Queen', 'King']; +const Deck = require('../../structures/cards/Deck'); module.exports = class DrawCardsCommand extends Command { constructor(client) { @@ -28,26 +26,11 @@ module.exports = class DrawCardsCommand extends Command { } ] }); - - this.deck = null; } run(msg, { amount, jokers }) { - if (!this.deck) this.deck = this.generateDeck(); - let cards = this.deck; - if (!jokers) cards = cards.filter(card => !card.includes('Joker')); - return msg.reply(`${amount === 1 ? '' : '\n'}${shuffle(cards).slice(0, amount).join('\n')}`); - } - - generateDeck() { - const deck = []; - for (const suit of suits) { - deck.push(`${suit} Ace`); - for (let i = 2; i <= 10; i++) deck.push(`${suit} ${i}`); - for (const face of faces) deck.push(`${suit} ${face}`); - } - deck.push('⭐ Joker'); - deck.push('⭐ Joker'); - return deck; + const deck = new Deck({ includeJokers: jokers }); + const cards = deck.draw(amount); + return msg.reply(`${amount === 1 ? '' : '\n'}${Array.isArray(cards) ? cards.join('\n') : cards}`); } }; diff --git a/package.json b/package.json index 2d6af875..716cb966 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "114.8.7", + "version": "114.8.8", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { @@ -49,6 +49,7 @@ "moment-duration-format": "^2.3.2", "moment-timezone": "^0.5.28", "node-superfetch": "^0.1.10", + "pokersolver": "^2.1.3", "random-js": "^2.1.0", "rss-parser": "^3.8.0", "soap": "^0.31.0", diff --git a/structures/cards/Card.js b/structures/cards/Card.js new file mode 100644 index 00000000..836ae749 --- /dev/null +++ b/structures/cards/Card.js @@ -0,0 +1,46 @@ +const displaySuits = { + spades: '♠', + diamonds: '♦', + hearts: '♥', + clubs: '♣', + joker: '⭐' +}; + +module.exports = class Card { + constructor(value, suit) { + this.value = value; + this.suit = suit; + } + + get blackjackValue() { + if (this.value === 'Joker') return 0; + if (this.value === 'King' || this.value === 'Queen' || this.value === 'Jack') return 10; + if (this.value === 'Ace') return 11; + return this.value; + } + + get display() { + return `${displaySuits[this.suit]} ${this.value}`; + } + + get pokersolverKey() { + if (this.value === 'Joker') return null; + let suitLetter; + switch (this.suit) { + case 'clubs': suitLetter = 'c'; break; + case 'hearts': suitLetter = 'h'; break; + case 'diamonds': suitLetter = 'd'; break; + case 'spades': suitLetter = 's'; break; + } + let value; + switch (this.value) { + case 'King': value = 'K'; break; + case 'Queen': value = 'Q'; break; + case 'Jack': value = 'J'; break; + case 'Ace': value = 'A'; break; + case 10: value = 'T'; break; + default: value = this.value; break; + } + return `${value}${suitLetter}`; + } +}; diff --git a/structures/cards/Deck.js b/structures/cards/Deck.js new file mode 100644 index 00000000..8ff06527 --- /dev/null +++ b/structures/cards/Deck.js @@ -0,0 +1,44 @@ +const Card = require('./Card'); +const suits = ['spades', 'hearts', 'diamonds', 'clubs']; +const faces = ['Jack', 'Queen', 'King']; +const { shuffle } = require('../../util/Util'); + +module.exports = class Deck { + constructor(options) { + this.deckCount = options.deckCount || 1; + this.includeJokers = options.includeJokers || false; + this.deck = []; + this.makeCards(this.deckCount); + } + + makeCards(deckCount) { + for (let i = 0; i < deckCount; i++) { + for (const suit of suits) { + this.deck.push(new Card('Ace', suit)); + for (let j = 2; j <= 10; j++) this.deck.push(new Card(j, suit)); + for (const face of faces) this.deck.push(new Card(face, suit)); + } + if (this.includeJokers) { + this.deck.push(new Card('Joker', 'joker')); + this.deck.push(new Card('Joker', 'joker')); + } + } + this.deck = shuffle(this.deck); + return this.deck; + } + + draw(amount = 1) { + const cards = []; + for (let i = 0; i < amount; i++) { + const card = this.deck[0]; + this.deck.shift(); + cards.push(card); + } + return amount === 1 ? cards[0] : cards; + } + + reset() { + this.deck = this.makeCards(this.deckCount); + return this; + } +};