Aevann1 2022-02-04 09:58:20 +02:00
parent 1896d80d82
commit 416e0e4c49
5 changed files with 70 additions and 51 deletions

View File

@ -1,6 +1,6 @@
from functools import reduce
from json.encoder import INFINITY
import random
from random import shuffle
from math import floor
deck_count = 4
@ -8,17 +8,9 @@ ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "X", "J", "Q", "K", "A"]
suits = ["♠️", "♥️", "♣️", "♦️"]
def shuffle(x):
random.shuffle(x)
return x
def get_shuffled_deck():
return shuffle([rank + suit for rank in ranks for suit in suits for _ in range(deck_count)])
def deal_initial_cards():
deck = get_shuffled_deck()
deck = shuffle([rank + suit for rank in ranks for suit in suits for _ in range(deck_count)])
p1, d1, p2, d2, *rest_of_deck = deck
return [p1, p2], [d1, d2], rest_of_deck
@ -85,20 +77,51 @@ class Blackjack:
if player_value == 21 and dealer_value == 21:
status = 'push'
self.apply_game_result(from_comment, wager, status)
self.apply_game_result(from_comment, wager, status, 1)
elif player_value == 21:
status = 'blackjack'
self.apply_game_result(from_comment, wager, status)
self.apply_game_result(from_comment, wager, status, 1)
elif dealer_value == 21:
status = 'lost'
self.apply_game_result(from_comment, wager, status)
self.apply_game_result(from_comment, wager, status, 1)
from_comment.blackjack_result = format_all(
player_hand, dealer_hand, rest_of_deck, status, wager)
from_comment.blackjack_result = format_all(player_hand, dealer_hand, rest_of_deck, status, wager)
if self.casino_word in in_text:
for word in in_text.split():
if self.casino_word in word:
try:
wager = word[len(self.casino_word):]
wager_value = int(wager)
except: break
def player_hit(self, from_comment):
player_hand, dealer_hand, deck, status, wager = from_comment.blackjack_result.split(
"_")
if (wager_value < self.minimum_bet): break
elif (wager_value > self.maximum_bet): break
elif (wager_value > from_user.procoins): break
from_user.procoins -= wager_value
from_user.winnings -= wager_value
player_hand, dealer_hand, rest_of_deck = deal_initial_cards()
status = 'activemb'
player_value = get_hand_value(player_hand)
dealer_value = get_hand_value(dealer_hand)
if player_value == 21 and dealer_value == 21:
status = 'push'
self.apply_game_result(from_comment, wager, status, 2)
elif player_value == 21:
status = 'blackjack'
self.apply_game_result(from_comment, wager, status, 2)
elif dealer_value == 21:
status = 'lost'
self.apply_game_result(from_comment, wager, status, 2)
from_comment.blackjack_result = format_all(player_hand, dealer_hand, rest_of_deck, status, wager)
def player_hit(self, from_comment, currency):
player_hand, dealer_hand, deck, status, wager = from_comment.blackjack_result.split("_")
player_hand = player_hand.split("/")
dealer_hand = dealer_hand.split("/")
deck = deck.split("/")
@ -107,17 +130,15 @@ class Blackjack:
if player_value == -1:
status = 'bust'
self.apply_game_result(from_comment, wager, status)
self.apply_game_result(from_comment, wager, status, currency)
from_comment.blackjack_result = format_all(
player_hand, dealer_hand, deck, status, wager)
from_comment.blackjack_result = format_all(player_hand, dealer_hand, deck, status, wager)
if (player_value == 21):
self.player_stayed(from_comment)
if (player_value == 21): self.player_stayed(from_comment)
def player_stayed(self, from_comment):
player_hand, dealer_hand, deck, status, wager = from_comment.blackjack_result.split(
"_")
def player_stayed(self, from_comment, currency):
player_hand, dealer_hand, deck, status, wager = from_comment.blackjack_result.split("_")
player_hand = player_hand.split("/")
player_value = get_hand_value(player_hand)
dealer_hand = dealer_hand.split("/")
@ -129,29 +150,24 @@ class Blackjack:
dealer_hand.append(next)
dealer_value = get_hand_value(dealer_hand)
if player_value > dealer_value or dealer_value == -1:
status = 'won'
elif dealer_value > player_value:
status = 'lost'
else:
status = 'push'
if player_value > dealer_value or dealer_value == -1: status = 'won'
elif dealer_value > player_value: status = 'lost'
else: status = 'push'
from_comment.blackjack_result = format_all(
player_hand, dealer_hand, deck, status, wager)
from_comment.blackjack_result = format_all(player_hand, dealer_hand, deck, status, wager)
self.apply_game_result(from_comment, wager, status)
self.apply_game_result(from_comment, wager, status, currency)
def apply_game_result(self, from_comment, wager, result):
def apply_game_result(self, from_comment, wager, result, currency):
reward = 0
if result == 'push':
reward = int(wager)
elif result == 'won':
reward = int(wager) * 2
elif result == 'blackjack':
reward = floor(int(wager) * (5/2))
if result == 'push': reward = int(wager)
elif result == 'won': reward = int(wager) * 2
elif result == 'blackjack': reward = floor(int(wager) * (5/2))
if (reward > 0):
user = from_comment.author
user.coins += reward
if currency == 1: user.coins += reward
else: user.procoins += reward
user.winnings += reward

View File

@ -8,7 +8,7 @@ def shuffle(stuff):
class Slots:
command_word = "!slots"
casino_word = "!casino"
casino_word = "!slotsmb"
if SITE == 'rdrama.net': minimum_bet = 100
else: minimum_bet = 10
maximum_bet = INFINITY

View File

@ -11,7 +11,7 @@ class Treasure:
self.db = g.db
def check_for_treasure(self, in_text, from_comment):
has_gamble_command = '!slots' in in_text or '!casino' in in_text or '!blackjack' in in_text
has_gamble_command = '!slots' in in_text or '!blackjack' in in_text
if not has_gamble_command:
seed = random.randint(1, 1000)

View File

@ -963,10 +963,13 @@ def handle_blackjack_action(cid, v):
action = request.values.get("action", "")
blackjack = Blackjack(g)
if action == 'hit':
blackjack.player_hit(comment)
elif action == 'stay':
blackjack.player_stayed(comment)
blackjack_status = comment.blackjack_result.split('_')[3]
if blackjack_status == 'activemb': currency = 2
else: currency = 1
if action == 'hit': blackjack.player_hit(comment, currency)
elif action == 'stay': blackjack.player_stayed(comment, currency)
else: abort(400)
g.db.add(comment)
g.db.add(v)

View File

@ -182,7 +182,7 @@
{% set split_result = c.blackjack_result.split('_') %}
{% set blackjack_status = split_result[3] %}
{% set player_hand = split_result[0].replace('X', '10') %}
{% set dealer_hand = split_result[1].split('/')[0] if blackjack_status == 'active' else split_result[1] %}
{% set dealer_hand = split_result[1].split('/')[0] if 'active' in blackjack_status else split_result[1] %}
{% set dealer_hand = dealer_hand.replace('X', '10') %}
{% set wager = split_result[4] %}
{% endif %}
@ -256,7 +256,7 @@
{% if c.blackjack_result %}
<em>{{player_hand}} vs. {{dealer_hand}}</em>
{% if blackjack_status == 'active' and v.id == c.author_id %}
{% if 'active' in blackjack_status and v.id == c.author_id %}
<button class="btn btn-success small" style="text-transform: uppercase; padding: 2px;" onclick="handle_blackjack_action('{{c.id}}', 'hit')">Hit</button>
<button class="btn btn-danger small" style="text-transform: uppercase; padding: 2px;" onclick="handle_blackjack_action('{{c.id}}', 'stay')">Stay</button>
{% elif blackjack_status == 'push' %}