forked from reactxsw/Smilewinbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.py
424 lines (347 loc) · 19.7 KB
/
blackjack.py
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from code import interact
from locale import currency
import nextcord
from nextcord.ext import commands
import random
import settings
card_list = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
class Blackjack(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.group(invoke_without_command=True, aliases=["bj"])
async def blackjack(self, ctx: commands.Context, amount = None):
if amount is None:
await ctx.send(f"Please specify an amount of money to bet | `{settings.COMMAND_PREFIX} blackjack <amount>`")
return
try:
amount = int(amount)
except ValueError:
await ctx.send(f"Amount must be a integer.")
return
# Check status of economy system
guild = await settings.collection.find_one({"guild_id": ctx.guild.id})
if guild is not None:
status = guild["economy_system"]
if status == "YES":
user = await settings.collectionmoney.find_one({"guild_id": ctx.guild.id, "user_id": ctx.author.id})
if user is None:
embed = nextcord.Embed(
title=f"{ctx.author.name} ยังไม่มีบัญชี",
description=f"ใช้คําสั่ง {settings.COMMAND_PREFIX} openbal เพื่อเปิดใช้",
colour=0x983925,
)
embed.set_footer(text=f"┗Requested by {ctx.author}")
message = await ctx.send(embed=embed)
await message.add_reaction("💸")
else:
current = user["wallet"]
currency = guild["currency"]
if current <= amount:
await ctx.send("You don't have enough money.")
return
# Check if the user already has a game
game = await settings.collectionblackjack.find_one({"player_id": ctx.author.id, "game": "blackjack"})
if game is not None:
await ctx.send(f"You already have a game running! | Use {settings.COMMAND_PREFIX} blackjack stop")
return
# Initialize variables
while True:
player_hand = random.sample(card_list, 1)
dealer_hand = random.sample(card_list, 2)
player_score = await get_score(player_hand)
dealer_score = await get_score(dealer_hand)
if player_score < 21 and dealer_score < 21:
break
# Create the embed
embed = await Blackjack.embed_generator(self,player_hand, dealer_hand, first_turn=True)
# Send embed
msg = await ctx.send(embed=embed, view=Blackjack_Button(self.bot))
# Create data for the game
data = {
"game": "blackjack",
"player_id": ctx.author.id,
"player_hand": player_hand,
"dealer_hand": dealer_hand,
"channel_id": ctx.channel.id,
"amount": amount,
"msg_id": msg.id
}
# Insert data into database
await settings.collectionblackjack.insert_one(data)
async def embed_generator(self, player_hand, dealer_hand, first_turn=False, state=None, infotext=""): # state = 1 for win, 2 for lose, 3 for draw
player_hand_text = ""
for i in player_hand:
player_hand_text += f"{i} "
dealer_hand_text = ""
for i in dealer_hand:
dealer_hand_text += f"{i} "
embed = nextcord.Embed(title="Blackjack", color=0xFED000)
if state is None:
embed.add_field(
name="Your hand",
value=f"{player_hand_text}\n\n Value: {await get_score(player_hand)}",
)
if first_turn:
embed.add_field(
name="Dealer's hand",
value=f"{dealer_hand[0]} #\n\n Value: {await get_score([dealer_hand[0]])}",
)
else:
embed.add_field(
name="Dealer's hand",
value=f"{dealer_hand_text}\n\n Value: {await get_score(dealer_hand)}",
)
return embed
elif state == 1 :
embed.add_field(name="Game end!", value=f"You won! | {infotext}", inline=False)
embed.add_field(
name="Your hand",
value=f"{player_hand_text}\n\n Value: {await get_score(player_hand)}",
)
if first_turn:
embed.add_field(
name="Dealer's hand",
value=f"{dealer_hand[0]} #\n\n Value: {await get_score([dealer_hand[0]])}",
)
else:
embed.add_field(
name="Dealer's hand",
value=f"{dealer_hand_text}\n\n Value: {await get_score(dealer_hand)}",
)
return embed
elif state == 2 :
embed.add_field(name="Game end!", value=f"You lose! | {infotext}", inline=False)
embed.add_field(
name="Your hand",
value=f"{player_hand_text}\n\n Value: {await get_score(player_hand)}",
)
if first_turn:
embed.add_field(
name="Dealer's hand",
value=f"{dealer_hand[0]} #\n\n Value: {await get_score([dealer_hand[0]])}",
)
else:
embed.add_field(
name="Dealer's hand",
value=f"{dealer_hand_text}\n\n Value: {await get_score(dealer_hand)}",
)
return embed
elif state == 3 :
embed.add_field(name="Game end!", value="Draw!", inline=False)
embed.add_field(
name="Your hand",
value=f"{player_hand_text}\n\n Value: {await get_score(player_hand)}",
)
if first_turn:
embed.add_field(
name="Dealer's hand",
value=f"{dealer_hand[0]} #\n\n Value: {await get_score([dealer_hand[0]])}",
)
else:
embed.add_field(
name="Dealer's hand",
value=f"{dealer_hand_text}\n\n Value: {await get_score(dealer_hand)}",
)
return embed
@blackjack.command()
async def stop(self, ctx):
# Check if the user already has a game
game = await settings.collectionblackjack.find_one({"player_id": ctx.author.id, "game": "blackjack"})
if game is not None:
await settings.collectionblackjack.delete_one({"player_id": ctx.author.id, "game": "blackjack"})
await ctx.send(f"Game has stopped!")
else:
await ctx.send(f"You don't have a game running!")
return
@blackjack.command()
async def help(self, ctx):
embed = nextcord.Embed(title="Blackjack",color=0xFED000)
embed.add_field(name="Blackjack", value=f"Play blackjack with the bot! | {settings.COMMAND_PREFIX} blackjack", inline=False)
embed.add_field(name="Stop", value=f"Stop the current game | {settings.COMMAND_PREFIX} blackjack stop", inline=False)
await ctx.send(embed=embed)
# It will come from Blackjack_Button
async def handle_click(self, button: nextcord.ui.Button, interaction):
if button.custom_id == "hit":
game = await settings.collectionblackjack.find_one(
{"player_id": interaction.user.id, "game": "blackjack"}
)
if game is not None:
newcard = random.sample(card_list, 1)
game["player_hand"].append(newcard[0])
await settings.collectionblackjack.update_one(
{"player_id": game["player_id"]}, {"$set": game}
)
#Check player win lose or draw in this turn
#update message
player_score = await get_score(game["player_hand"])
dealer_score = await get_score(game["dealer_hand"])
win,lose,draw = await Blackjack.check_win_lose_draw(self,player_score,dealer_score)
if win:
data = await settings.collection.find_one({"guild_id": interaction.guild.id})
user = await settings.collectionmoney.find_one({"user_id": interaction.user.id})
current = user["wallet"]
currency = data["currency"]
infotext = f"You got {game['amount']*2}{currency} Current {current + game['amount']*2:,}{currency}"
# updata user wallet
await settings.collectionmoney.update_one({"guild_id": interaction.guild.id ,"user_id": interaction.user.id}, {"$set": {"wallet": current + game['amount']*2}})
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=1, infotext=infotext)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
elif lose:
data = await settings.collection.find_one({"guild_id": interaction.guild.id})
user = await settings.collectionmoney.find_one({"user_id": interaction.user.id})
current = user["wallet"]
currency = data["currency"]
infotext = f"You got {game['amount']*2}{currency} Current {current - game['amount']*2:,}{currency}"
# updata user wallet
await settings.collectionmoney.update_one({"guild_id": interaction.guild.id ,"user_id": interaction.user.id}, {"$set": {"wallet": current - (game['amount']*2)}})
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=1, infotext=infotext)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
elif draw:
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=3)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
else:
#if player not lose then call computer turn and check win lose draw again
# call computer dealer playing function
await Blackjack.computer_dealer_playing(self,game)
player_score = await get_score(game["player_hand"])
dealer_score = await get_score(game["dealer_hand"])
win,lose,draw = await Blackjack.check_win_lose_draw(self,player_score,dealer_score)
if win:
data = await settings.collection.find_one({"guild_id": interaction.guild.id})
user = await settings.collectionmoney.find_one({"user_id": interaction.user.id})
current = user["wallet"]
currency = data["currency"]
infotext = f"You got {game['amount']*2}{currency} Current {current + game['amount']*2:,}{currency}"
# updata user wallet
await settings.collectionmoney.update_one({"guild_id": interaction.guild.id ,"user_id": interaction.user.id}, {"$set": {"wallet": current + game['amount']*2}})
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=1, infotext=infotext)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
elif lose:
data = await settings.collection.find_one({"guild_id": interaction.guild.id})
user = await settings.collectionmoney.find_one({"user_id": interaction.user.id})
current = user["wallet"]
currency = data["currency"]
infotext = f"You got {game['amount']*2}{currency} Current {current - game['amount']*2:,}{currency}"
# updata user wallet
await settings.collectionmoney.update_one({"guild_id": interaction.guild.id ,"user_id": interaction.user.id}, {"$set": {"wallet": current - game['amount']*2}})
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=1, infotext=infotext)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
elif draw:
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=3)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
else:
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"])
await Blackjack.update_message(self, embed, interaction)
elif button.custom_id == "stand":
game = await settings.collectionblackjack.find_one(
{"player_id": interaction.user.id, "game": "blackjack"}
)
if game is not None:
# call computer dealer playing function
await Blackjack.computer_dealer_playing(self,game)
#update message
player_score = await get_score(game["player_hand"])
dealer_score = await get_score(game["dealer_hand"])
win,lose,draw = await Blackjack.check_win_lose_draw(self,player_score,dealer_score)
if win:
data = await settings.collection.find_one({"guild_id": interaction.guild.id})
user = await settings.collectionmoney.find_one({"user_id": interaction.user.id})
current = user["wallet"]
currency = data["currency"]
infotext = f"You got {game['amount']*2}{currency} Current {current + game['amount']*2:,}{currency}"
# updata user wallet
await settings.collectionmoney.update_one({"guild_id": interaction.guild.id ,"user_id": interaction.user.id}, {"$set": {"wallet": current + game['amount']*2}})
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=1, infotext=infotext)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
elif lose:
data = await settings.collection.find_one({"guild_id": interaction.guild.id})
user = await settings.collectionmoney.find_one({"user_id": interaction.user.id})
current = user["wallet"]
currency = data["currency"]
infotext = f"You got {game['amount']*2}{currency} Current {current - game['amount']*2:,}{currency}"
# updata user wallet
await settings.collectionmoney.update_one({"guild_id": interaction.guild.id ,"user_id": interaction.user.id}, {"$set": {"wallet": current - game['amount']*2}})
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=1, infotext=infotext)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
elif draw:
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"], state=3)
await Blackjack.update_message(self, embed, interaction, remove_view=True)
await settings.collectionblackjack.delete_one({"player_id": interaction.user.id, "game": "blackjack"})
else:
embed = await Blackjack.embed_generator(self,game["player_hand"], game["dealer_hand"])
await Blackjack.update_message(self, embed, interaction)
async def check_win_lose_draw(self,player_score, dealer_score):
#check draw
if player_score == 21 and dealer_score == 21:
return False,False,True #win,lose,draw
#check lose
elif player_score >= 21:
return False,True,False #win,lose,draw
#check win
elif dealer_score >= 21 or player_score == 21:
return True,False,False #win,lose,draw
return False,False,False
async def computer_dealer_playing(self, game):
action = random.randint(0, 1)
# If the dealer has a score of 17 or less, he will hit
if action == 0:
newcard = random.sample(card_list, 1)
game["dealer_hand"].append(newcard[0])
await settings.collectionblackjack.update_one(
{"player_id": game["player_id"]}, {"$set": game}
)
# If action = 0 then do nothing
else:
pass
async def update_message(self, embed, interaction, remove_view=False):
if remove_view:
await interaction.edit(embed=embed, view=None)
else:
await interaction.edit(embed=embed)
async def get_score(cards: list):
sum = 0
for _ in range(cards.count("A")):
if "A" in cards:
if "J" in cards or "Q" in cards or "K" in cards:
sum += 11
else:
sum += 1
for _ in range(cards.count("J") + cards.count("Q") + cards.count("K")):
if "J" in cards or "Q" in cards or "K" in cards:
sum += 10
for card in cards:
if card not in ["A", "J", "Q", "K"]:
sum += int(card)
return sum
class Blackjack_Button(nextcord.ui.View):
def __init__(self, bot):
self.bot = bot
super().__init__(timeout=None)
@nextcord.ui.button(
label=" Hit ",
style=nextcord.ButtonStyle.secondary,
custom_id="hit",
row=0,
)
async def double_down(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
):
await Blackjack.handle_click(self, button, interaction)
@nextcord.ui.button(
label=" Stand ", style=nextcord.ButtonStyle.secondary, custom_id="stand", row=0
)
async def stand(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
):
await Blackjack.handle_click(self, button, interaction)
def setup(bot):
bot.add_cog(Blackjack(bot))
bot.add_view(Blackjack_Button(bot))