mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-15 15:57:47 +02:00
Playing Card Commands structure system
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user