Merge branch 'frost' of https://github.com/Aevann1/Drama into frost

This commit is contained in:
Aevann1 2022-02-07 13:39:29 +02:00
commit b8fe64e79b
3 changed files with 132 additions and 109 deletions

View file

@ -1,5 +1,6 @@
from functools import reduce from functools import reduce
from json.encoder import INFINITY from json.encoder import INFINITY
from locale import currency
import random import random
from math import floor from math import floor
@ -44,38 +45,47 @@ def format_cards(hand):
return map(lambda x: "".join(x), hand) return map(lambda x: "".join(x), hand)
def format_all(player_hand, dealer_hand, deck, status, wager): def format_all(player_hand, dealer_hand, deck, status, wager, kind):
formatted_player_hand = format_cards(player_hand) formatted_player_hand = format_cards(player_hand)
formatted_dealer_hand = format_cards(dealer_hand) formatted_dealer_hand = format_cards(dealer_hand)
formatted_deck = format_cards(deck) formatted_deck = format_cards(deck)
return f'{"/".join(formatted_player_hand)}_{"/".join(formatted_dealer_hand)}_{"/".join(formatted_deck)}_{status}_{wager}' return f'{"/".join(formatted_player_hand)}_{"/".join(formatted_dealer_hand)}_{"/".join(formatted_deck)}_{status}_{wager}_{kind}'
class Blackjack: class Blackjack:
command_word = "!blackjack" coins_command_word = "!blackjack"
casino_word = "!blackjackmb" marseybucks_command_word = "!blackjackmb"
minimum_bet = 100 minimum_bet = 100
maximum_bet = INFINITY maximum_bet = INFINITY
def __init__(self, g): def __init__(self, g):
self.db = g.db self.db = g.db
def check_for_blackjack_command(self, in_text, from_user, from_comment): def check_for_blackjack_commands(self, in_text, from_user, from_comment):
in_text = in_text.lower() for command_word in (self.coins_command_word, self.marseybucks_command_word):
if self.command_word in in_text: currency_prop = "coins" if command_word == self.coins_command_word else "procoins"
currency_value = getattr(from_user, currency_prop, 0)
if command_word in in_text:
for word in in_text.split(): for word in in_text.split():
if self.command_word in word: if command_word in word:
try: try:
wager = word[len(self.command_word):] wager = word[len(command_word):]
wager_value = int(wager) wager_value = int(wager)
except: break except:
break
if (wager_value < self.minimum_bet): break if (wager_value < self.minimum_bet):
elif (wager_value > self.maximum_bet): break break
elif (wager_value > from_user.coins): break elif (wager_value > self.maximum_bet):
break
elif (wager_value <= currency_value):
setattr(from_user, currency_prop,
currency_value - wager_value)
from_user.coins -= wager_value if currency_prop == "coins":
from_user.winnings -= wager_value from_user.winnings -= wager_value
player_hand, dealer_hand, rest_of_deck = deal_initial_cards() player_hand, dealer_hand, rest_of_deck = deal_initial_cards()
@ -85,16 +95,19 @@ class Blackjack:
if player_value == 21 and dealer_value == 21: if player_value == 21 and dealer_value == 21:
status = 'push' status = 'push'
self.apply_game_result(from_comment, wager, status) self.apply_game_result(
from_comment, wager, status, currency_prop)
elif player_value == 21: elif player_value == 21:
status = 'blackjack' status = 'blackjack'
self.apply_game_result(from_comment, wager, status) self.apply_game_result(
from_comment, wager, status, currency_prop)
elif dealer_value == 21: elif dealer_value == 21:
status = 'lost' status = 'lost'
self.apply_game_result(from_comment, wager, status) self.apply_game_result(
from_comment, wager, status, currency_prop)
from_comment.blackjack_result = format_all( from_comment.blackjack_result = format_all(
player_hand, dealer_hand, rest_of_deck, status, wager) player_hand, dealer_hand, rest_of_deck, status, wager, currency_prop)
def player_hit(self, from_comment): def player_hit(self, from_comment):
player_hand, dealer_hand, deck, status, wager = from_comment.blackjack_result.split( player_hand, dealer_hand, deck, status, wager = from_comment.blackjack_result.split(
@ -116,7 +129,7 @@ class Blackjack:
self.player_stayed(from_comment) self.player_stayed(from_comment)
def player_stayed(self, from_comment): def player_stayed(self, from_comment):
player_hand, dealer_hand, deck, status, wager = from_comment.blackjack_result.split( player_hand, dealer_hand, deck, status, wager, kind = from_comment.blackjack_result.split(
"_") "_")
player_hand = player_hand.split("/") player_hand = player_hand.split("/")
player_value = get_hand_value(player_hand) player_value = get_hand_value(player_hand)
@ -137,21 +150,29 @@ class Blackjack:
status = 'push' status = 'push'
from_comment.blackjack_result = format_all( from_comment.blackjack_result = format_all(
player_hand, dealer_hand, deck, status, wager) player_hand, dealer_hand, deck, status, wager, kind)
self.apply_game_result(from_comment, wager, status) self.apply_game_result(from_comment, wager, status, kind)
def apply_game_result(self, from_comment, wager, result): def apply_game_result(self, from_comment, wager, result, kind):
reward = 0 wager_value = int(wager)
user = from_comment.author
reward = -1
if result == 'push': if result == 'push':
reward = int(wager) reward = 0
elif result == 'won': elif result == 'won':
reward = int(wager) * 2 reward = wager_value
elif result == 'blackjack': elif result == 'blackjack':
reward = floor(int(wager) * (5/2)) reward = floor(wager_value * 3/2)
if (reward > 0): if (reward > -1):
user = from_comment.author currency_value = int(getattr(user, kind, 0))
user.coins += reward
setattr(user, kind, currency_value + wager_value + reward)
if kind == 'coins':
user.winnings += reward user.winnings += reward
self.db.add(user)
self.db.commit()

View file

@ -642,7 +642,7 @@ def api_comment(v):
slots.check_for_slots_command(body, v, c) slots.check_for_slots_command(body, v, c)
blackjack = Blackjack(g) blackjack = Blackjack(g)
blackjack.check_for_blackjack_command(body, v, c) blackjack.check_for_blackjack_commands(body, v, c)
treasure = Treasure(g) treasure = Treasure(g)
treasure.check_for_treasure(body, c) treasure.check_for_treasure(body, c)

View file

@ -185,6 +185,7 @@
{% 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 blackjack_status == 'active' else split_result[1] %}
{% set dealer_hand = dealer_hand.replace('X', '10') %} {% set dealer_hand = dealer_hand.replace('X', '10') %}
{% set wager = split_result[4] %} {% set wager = split_result[4] %}
{% set kind = split_result[5] %}
{% endif %} {% endif %}
<div id="comment-{{c.id}}" class="anchor {% if c.unread %}unread{% endif %} comment {% if standalone and level==1 %} mt-0{% endif %} {% if c.collapse_for_user(v,request.path) %}collapsed{% endif %}" style="{% if isreply %}padding-left:0!important;{% elif not c.unread %}border-left: 2px solid {% if c.ghost %}var(--primary){% else %}#{{c.author.namecolor}};{% endif %}{% endif %} {% if c.unread %}padding: 10px 10px 10px !important;{% endif %}"> <div id="comment-{{c.id}}" class="anchor {% if c.unread %}unread{% endif %} comment {% if standalone and level==1 %} mt-0{% endif %} {% if c.collapse_for_user(v,request.path) %}collapsed{% endif %}" style="{% if isreply %}padding-left:0!important;{% elif not c.unread %}border-left: 2px solid {% if c.ghost %}var(--primary){% else %}#{{c.author.namecolor}};{% endif %}{% endif %} {% if c.unread %}padding: 10px 10px 10px !important;{% endif %}">
@ -259,20 +260,21 @@
{% endif %} {% endif %}
{% if c.blackjack_result %} {% if c.blackjack_result %}
{% set currency_kind = "Coins" if kind == "coins" else "Marseybucks" %}
<em>{{player_hand}} vs. {{dealer_hand}}</em> <em>{{player_hand}} vs. {{dealer_hand}}</em>
{% if blackjack_status == 'active' and v.id == c.author_id %} {% if blackjack_status == 'active' 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-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-danger small" style="text-transform: uppercase; padding: 2px;" onclick="handle_blackjack_action('{{c.id}}', 'stay')">Stay</button>
{% elif blackjack_status == 'push' %} {% elif blackjack_status == 'push' %}
<strong>Pushed.</strong> <strong>Pushed. Refunded {{wager}} {{currency_kind}}.</strong>
{% elif blackjack_status == 'bust' %} {% elif blackjack_status == 'bust' %}
<strong>Bust. Lost {{wager}} Coins.</strong> <strong>Bust. Lost {{wager}} {{currency_kind}}.</strong>
{% elif blackjack_status == 'lost' %} {% elif blackjack_status == 'lost' %}
<strong>Lost {{wager}} Coins.</strong> <strong>Lost {{wager}} {{currency_kind}}.</strong>
{% elif blackjack_status == 'won' %} {% elif blackjack_status == 'won' %}
<strong>Won {{wager}} Coins.</strong> <strong>Won {{wager}} Coins.</strong>
{% elif blackjack_status == 'blackjack' %} {% 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>Blackjack! Won {{(wager|int * 3/2)|round(0, 'floor')|int}} {{currency_kind}}.</strong>
{% endif %} {% endif %}
{% endif %} {% endif %}
</div> </div>