master
Aevann1 2022-02-04 10:50:57 +02:00
parent 416e0e4c49
commit 52a50796a0
3 changed files with 45 additions and 33 deletions

View File

@ -1,6 +1,6 @@
from functools import reduce
from json.encoder import INFINITY
from random import shuffle
import random
from math import floor
deck_count = 4
@ -8,6 +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 deal_initial_cards():
deck = shuffle([rank + suit for rank in ranks for suit in suits for _ in range(deck_count)])
@ -86,7 +89,7 @@ class Blackjack:
self.apply_game_result(from_comment, wager, status, 1)
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:
@ -103,7 +106,7 @@ class Blackjack:
from_user.winnings -= wager_value
player_hand, dealer_hand, rest_of_deck = deal_initial_cards()
status = 'activemb'
status = 'mbactive'
player_value = get_hand_value(player_hand)
dealer_value = get_hand_value(dealer_hand)
@ -111,15 +114,14 @@ class Blackjack:
status = 'push'
self.apply_game_result(from_comment, wager, status, 2)
elif player_value == 21:
status = 'blackjack'
status = 'mbblackjack'
self.apply_game_result(from_comment, wager, status, 2)
elif dealer_value == 21:
status = 'lost'
status = 'mblost'
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("/")
@ -129,13 +131,13 @@ class Blackjack:
player_value = get_hand_value(player_hand)
if player_value == -1:
status = 'bust'
if currency == 1: status = 'bust'
else: status = 'mbbust'
self.apply_game_result(from_comment, wager, status, currency)
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, currency)
def player_stayed(self, from_comment, currency):
player_hand, dealer_hand, deck, status, wager = from_comment.blackjack_result.split("_")
@ -150,21 +152,25 @@ 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:
if currency == 1: status = 'won'
else: status = 'mbwon'
elif dealer_value > player_value:
if currency == 1: status = 'lost'
else: status = 'mblost'
else: status += 'push'
from_comment.blackjack_result = format_all(player_hand, dealer_hand, deck, status, wager)
self.apply_game_result(from_comment, wager, status, currency)
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.endswith('push'): reward = int(wager)
elif result.endswith('won'): reward = int(wager) * 2
elif result.endswith('blackjack'): reward = floor(int(wager) * (5/2))
if (reward > 0):
user = from_comment.author

View File

@ -282,7 +282,7 @@ def api_comment(v):
is_bot = bool(request.headers.get("Authorization"))
if parent_post.id not in (37696,37697,37749,37833,37838) and not is_bot and not v.marseyawarded and AGENDAPOSTER_PHRASE not in body.lower() and len(body) > 10:
if '!slots' not in body.lower() and '!blackjack' not in body.lower() and parent_post.id not in (37696,37697,37749,37833,37838) and not is_bot and not v.marseyawarded and AGENDAPOSTER_PHRASE not in body.lower() and len(body) > 10:
now = int(time.time())
cutoff = now - 60 * 60 * 24
@ -700,7 +700,7 @@ def edit_comment(cid, v):
if ban.reason: reason += f" {ban.reason}"
return {'error': reason}, 400
if AGENDAPOSTER_PHRASE not in body.lower():
if '!slots' not in body.lower() and '!blackjack' not in body.lower() and AGENDAPOSTER_PHRASE not in body.lower():
now = int(time.time())
cutoff = now - 60 * 60 * 24
@ -964,8 +964,9 @@ def handle_blackjack_action(cid, v):
blackjack = Blackjack(g)
blackjack_status = comment.blackjack_result.split('_')[3]
if blackjack_status == 'activemb': currency = 2
else: currency = 1
if blackjack_status == 'mbactive': currency = 2
elif blackjack_status == 'active': currency = 1
else: abort(400)
if action == 'hit': blackjack.player_hit(comment, currency)
elif action == 'stay': blackjack.player_stayed(comment, currency)

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 'active' in blackjack_status else split_result[1] %}
{% set dealer_hand = split_result[1].split('/')[0] if blackjack_status.endswith('active') else split_result[1] %}
{% set dealer_hand = dealer_hand.replace('X', '10') %}
{% set wager = split_result[4] %}
{% endif %}
@ -255,20 +255,25 @@
{% endif %}
{% if c.blackjack_result %}
<em>{{player_hand}} vs. {{dealer_hand}}</em>
<em>{{player_hand}} vs. {{dealer_hand}}</em>
{% if blackjack_status.startswith('mb') %}
{% set currency = 'Marseybux' %}
{% else %}
{% set currency = 'Coins' %}
{% endif %}
{% 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>
<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' %}
<strong>Pushed.</strong>
{% elif blackjack_status == 'bust' %}
<strong>Bust. Lost {{wager}} Coins.</strong>
{% elif blackjack_status == 'lost' %}
<strong>Lost {{wager}} Coins.</strong>
{% elif blackjack_status == 'won' %}
<strong>Won {{wager}} Coins.</strong>
{% elif blackjack_status == 'blackjack' %}
<strong>Blackjack! Won <span id="blackjack-result-{{c.id}}">{{(wager|int * 3/2)|round(0, 'floor')|int}}</span> Coins.</strong>
<strong>Pushed.</strong>
{% elif blackjack_status.endswith('bust') %}
<strong>Bust. Lost {{wager}} {{currency}}.</strong>
{% elif blackjack_status.endswith('lost') %}
<strong>Lost {{wager}} {{currency}}.</strong>
{% elif blackjack_status.endswith('won') %}
<strong>Won {{wager}} {{currency}}.</strong>
{% elif blackjack_status.endswith('blackjack') %}
<strong>Blackjack! Won <span id="blackjack-result-{{c.id}}">{{(wager|int * 3/2)|round(0, 'floor')|int}}</span> {{currency}}.</strong>
{% endif %}
{% endif %}
</div>