-
Notifications
You must be signed in to change notification settings - Fork 58
/
blackjack.wip
315 lines (271 loc) 路 10.8 KB
/
blackjack.wip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
const { MessageEmbed } = require('discord.js')
const colors = require('../lib/colors.json')
const random = require('random');
const cooldowns = new Map();
const humanizeDuration = require('humanize-duration');
exports.run = async (client, message, args) => {
try {
if (!args[0]) return message.channel.send('You need to specify an amount to bet.')
if (isNaN(args[0])) return message.channel.send('Invalid amount.')
const cooldown = cooldowns.get(message.author.id);
if (cooldown) {
const remaining = humanizeDuration(cooldown - Date.now());
return message.channel.send(`You have to wait ${remaining} before you can gamble again`)
.catch(console.error);
}
cooldowns.set(message.author.id, Date.now() + 5000);
setTimeout(() => cooldowns.delete(message.author.id), 5000);
const member = message.mentions.members.first() ? message.mentions.members.first() : message.member
const key = `${message.author.id}`
client.money.ensure(key, {
member: key,
money: 0
})
const money = client.money.get(member.id, 'money');
if (money < args[0]) return message.channel.send('You do not have enough money to gamble.')
if (args[0] < 50) return message.channel.send('You cannot bet less than $50 and cannot bet more than $50000.')
if (args[0] > 50000) return message.channel.send('You cannot bet less than $50 and cannot bet more than $50000.')
// Blackjack code
let gameStarted = false;
let gameOver = false;
let playerWon = false;
let dealerCards = [];
let playerCards = [];
let dealerScore = 0;
let playerScore = 0;
let deck = [];
let dealerCardString;
let playerCardString;
let msgID;
let embed = new MessageEmbed()
.setAuthor('馃儚 Blackjacker')
.setColor(colors.default)
.addField('Dealer\'s cards', dealerCardString + '\n(Score: ' + dealerScore + ')', false)
.addField('Your cards', playerCardString + '\n(Score: ' + playerScore + ')', false)
.setFooter(`${lang.RespondingTo} ${message.author.tag}`, message.author.avatarURL())
.setTimestamp()
function startGame() {
// Start game variables
gameStarted = true;
gameOver = false;
playerWon = false;
// Gives dealer and player 2 cards when starting
deck = createDeck();
shuffleDeck(deck);
dealerCards = [getNextCard(), getNextCard()];
playerCards = [getNextCard(), getNextCard()];
showStatus();
embed = new MessageEmbed()
.setAuthor('馃儚 Blackjack')
.setColor(colors.default)
.addField('Dealer\'s cards', dealerCardString + '\n(Score: ' + dealerScore + ')', false)
.addField('Your cards', playerCardString + '\n(Score: ' + playerScore + ')', false)
.setFooter(`${lang.RespondingTo} ${message.author.tag}`, message.author.avatarURL())
.setTimestamp()
message.channel.send({
embeds: [embed]
}).then(newMsg => {
msgID = newMsg.id
newMsg.react('馃憡').then(() => newMsg.react('馃洃'));
const filter = (reaction, user) => {
return ['馃憡', '馃洃'].includes(reaction.emoji.name) && user.id === message.author.id;
};
newMsg.awaitReactions(filter, {
max: 1,
time: 60000,
errors: ['time']
})
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '馃憡') {
message.reply('you hit');
hit();
} else {
message.reply('you stay');
stay();
}
})
.catch(collected => {
message.reply('Your Blackjack game has been cancelled due to inactivity.');
});
});
}
startGame();
async function hit() {
playerCards.push(getNextCard());
checkForEndOfGame();
showStatus()
const newEmbed = new MessageEmbed()
.setAuthor('馃儚 Blackjack')
.setColor(colors.default)
.addField('Dealer\'s cards', dealerCardString + '\n(Score: ' + dealerScore + ')', false)
.addField('Your cards', playerCardString + '\n(Score: ' + playerScore + ')', false)
.setFooter(`${lang.RespondingTo} ${message.author.tag}`, message.author.avatarURL())
.setTimestamp()
message.channel.fetch(msgID)
.then(found => {
console.log("found one")
console.log(found)
found.edit(newEmbed);
});
}
async function stay() {
gameOver = true;
checkForEndOfGame();
showStatus();
const newEmbed = new MessageEmbed()
.setAuthor('馃儚 Blackjack')
.setColor(colors.default)
.addField('Dealer\'s cards', dealerCardString + '\n(Score: ' + dealerScore + ')', false)
.addField('Your cards', playerCardString + '\n(Score: ' + playerScore + ')', false)
.setFooter(`${lang.RespondingTo} ${message.author.tag}`, message.author.avatarURL())
.setTimestamp()
const msg = guild.channels.cache.find(c => c.name == settings.modLogChannel)
message.channel.fetch(msgID)
.then(found => {
found.edit(newEmbed);
});
}
function createDeck() {
// Card variables
let suits = ["鈾ワ笍", "鈾o笍", "鈾︼笍", "鈾狅笍"];
let values = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"];
// For loop creates a deck with suits and values
for (let i = 0; i < suits.length; i++) {
for (let x = 0; x < values.length; x++) {
let card = {
suit: suits[i],
value: values[x]
};
deck.push(card);
}
}
return deck;
}
// Algorithm Durstenfeld https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffleDeck(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function getCardString(card) {
return card.suit + ' ' + card.value;
}
function getNextCard() {
return deck.shift();
}
function getCardNumericValue(card) {
switch (card.value) {
case 'Ace':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return 10;
}
}
function getScore(cardArray) {
let score = 0;
let hasAce = false;
// Adds the card value to the score
for (let i = 0; i < cardArray.length; i++) {
let card = cardArray[i];
score += getCardNumericValue(card);
// If the card Ace is there
if (card.value === 'Ace') {
hasAce = true;
}
}
// And if the Ace card is below or equal to 21 when adding 10 extra points, do that instead of +1 points
if (hasAce && score + 10 <= 21) {
return score + 10;
}
return score;
}
function updateScores() {
dealerScore = getScore(dealerCards);
playerScore = getScore(playerCards);
}
function checkForEndOfGame() {
updateScores();
if (gameOver) {
// let dealer take cards
while (dealerScore < playerScore && playerScore <= 21 && dealerScore <= 21) {
dealerCards.push(getNextCard());
updateScores();
}
}
if (playerScore > 21) {
playerWon = false;
gameOver = true;
} else if (playerScore === 21) {
playerWon = true;
gameOver = true;
} else if (dealerScore === 21) {
playerWon = false;
gameOver = true;
} else if (dealerScore > 21) {
playerWon = true;
gameOver = true;
} else if (gameOver) {
if (playerScore > dealerScore) {
playerWon = true;
} else {
playerWon = false;
}
}
}
function showStatus() {
dealerCardString = "";
playerCardString = "";
for (let i = 0; i < dealerCards.length; i++) {
dealerCardString += getCardString(dealerCards[i]) + '\n';
}
for (let i = 0; i < playerCards.length; i++) {
playerCardString += getCardString(playerCards[i]) + '\n';
}
updateScores();
if (gameOver) {
if (playerWon) {
message.channel.send("You won!");
} else if (dealerScore === playerScore) {
message.channel.send("It's a draw.");
} else {
message.channel.send("Dealer won!");
}
}
}
} catch (err) {
const errors = require('../modules/errors.js')
errors.embedError(err, lang, message)
}
};
exports.conf = {
enabled: false,
aliases: ["bj"],
guildOnly: true,
permLevel: 'User'
}
exports.help = {
name: 'blackjack',
category: 'Economy',
description: 'Try and get as close to 21 as possible, however, if you go over 21, you lose.',
usage: 'blackjack <amount>'
}