From 416e0e4c49a3ae85624cd9160724dde91e79f680 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 4 Feb 2022 09:58:20 +0200 Subject: [PATCH] f --- files/classes/blackjack.py | 102 ++++++++++++++++++++-------------- files/classes/slots.py | 2 +- files/classes/treasure.py | 2 +- files/routes/comments.py | 11 ++-- files/templates/comments.html | 4 +- 5 files changed, 70 insertions(+), 51 deletions(-) diff --git a/files/classes/blackjack.py b/files/classes/blackjack.py index ffa8d6b58..1f18c62d9 100644 --- a/files/classes/blackjack.py +++ b/files/classes/blackjack.py @@ -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 \ No newline at end of file diff --git a/files/classes/slots.py b/files/classes/slots.py index ee1e45b34..a1e5f389d 100644 --- a/files/classes/slots.py +++ b/files/classes/slots.py @@ -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 diff --git a/files/classes/treasure.py b/files/classes/treasure.py index 295cc5bed..0a808650a 100644 --- a/files/classes/treasure.py +++ b/files/classes/treasure.py @@ -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) diff --git a/files/routes/comments.py b/files/routes/comments.py index 2c728753c..f306f8104 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -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) diff --git a/files/templates/comments.html b/files/templates/comments.html index ec5d98e5e..595c9635f 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -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 %} {{player_hand}} vs. {{dealer_hand}} - {% if blackjack_status == 'active' and v.id == c.author_id %} + {% if 'active' in blackjack_status and v.id == c.author_id %} {% elif blackjack_status == 'push' %}