Blackjack (#187)

* Filter out slots from the body. (#181)

* Slots changes (#182)

* Switch slots result to title and fix snappy

* These two guys again

* These two guys again

* Fix breaking-even

* ...??

* Initial commit

* Finalize initial blackjack attempt

* Remove obsolete helper file
remotes/1693045480750635534/spooky-22
outruncolors 2022-01-28 02:00:15 -06:00 committed by GitHub
parent b3b92b7492
commit 557c368abc
7 changed files with 241 additions and 6 deletions

View File

@ -10,6 +10,7 @@ from .submission import *
from .votes import *
from .domains import *
from .slots import *
from .blackjack import *
from .subscriptions import *
from files.__main__ import app
from .mod_logs import *

View File

@ -0,0 +1,166 @@
from functools import reduce
from json.encoder import INFINITY
import random
from math import floor
deck_count = 4
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()
p1, d1, p2, d2, *rest_of_deck = deck
return [p1, p2], [d1, d2], rest_of_deck
def get_card_value(card):
rank = card[0]
return 0 if rank == "A" else min(ranks.index(rank) + 2, 10)
def get_hand_value(hand):
without_aces = sum(map(get_card_value, hand))
ace_count = sum("A" in c for c in hand)
possibilities = []
for i in range(ace_count + 1):
value = without_aces + (ace_count - i) + i * 11
possibilities.append(-1 if value > 21 else value)
return max(possibilities)
def format_cards(hand):
return map(lambda x: "".join(x), hand)
def format_all(player_hand, dealer_hand, deck, status, wager):
formatted_player_hand = format_cards(player_hand)
formatted_dealer_hand = format_cards(dealer_hand)
formatted_deck = format_cards(deck)
return f'{"/".join(formatted_player_hand)}_{"/".join(formatted_dealer_hand)}_{"/".join(formatted_deck)}_{status}_{wager}'
class Blackjack:
command_word = "!blackjack"
minimum_bet = 100
maximum_bet = INFINITY
def __init__(self, g):
self.db = g.db
def check_for_blackjack_command(self, in_text, from_user, from_comment):
if self.command_word in in_text:
for word in in_text.split():
if self.command_word in word:
wager = word[len(self.command_word):]
wager_value = int(wager)
if (wager_value < self.minimum_bet):
break
elif (wager_value > self.maximum_bet):
break
elif (wager_value > from_user.coins):
break
from_user.coins -= wager_value
player_hand, dealer_hand, rest_of_deck = deal_initial_cards()
status = 'active'
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)
elif player_value == 21:
status = 'blackjack'
self.apply_game_result(from_comment, wager, status)
elif dealer_value == 21:
status = 'lost'
self.apply_game_result(from_comment, wager, status)
from_comment.blackjack_result = format_all(
player_hand, dealer_hand, rest_of_deck, status, wager)
self.db.add(from_comment)
self.db.commit()
def player_hit(self, from_comment):
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("/")
player_hand.append(deck.pop(0))
player_value = get_hand_value(player_hand)
if player_value == -1:
status = 'bust'
self.apply_game_result(from_comment, wager, status)
from_comment.blackjack_result = format_all(
player_hand, dealer_hand, deck, status, wager)
self.db.add(from_comment)
self.db.commit()
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(
"_")
player_hand = player_hand.split("/")
player_value = get_hand_value(player_hand)
dealer_hand = dealer_hand.split("/")
dealer_value = get_hand_value(dealer_hand)
deck = deck.split("/")
while dealer_value < 17 and dealer_value != -1:
next = deck.pop(0)
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'
from_comment.blackjack_result = format_all(
player_hand, dealer_hand, deck, status, wager)
self.db.add(from_comment)
self.db.commit()
self.apply_game_result(from_comment, wager, status)
def apply_game_result(self, from_comment, wager, result):
reward = 0
if result == 'push':
reward = int(wager)
elif result == 'won':
reward = int(wager) * 2
elif result == 'blackjack':
reward = floor(int(wager) * (3/2))
if (reward > 0):
user = from_comment.author
user.coins += reward
self.db.add(user)
self.db.commit()

View File

@ -44,6 +44,7 @@ class Comment(Base):
body_html = Column(String)
ban_reason = Column(String)
slots_result = Column(String)
blackjack_result = Column(String)
post = relationship("Submission", viewonly=True)
author = relationship("User", primaryjoin="User.id==Comment.author_id")

View File

@ -92,11 +92,25 @@ class Slots:
shuffle(all_symbols)
if for_payout == 0: return "".join([all_symbols[0], all_symbols[1], all_symbols[2]])
elif for_payout == 1: return "".join([all_symbols[0], all_symbols[0], all_symbols[2]])
if for_payout == 0:
return "".join([all_symbols[0], all_symbols[1], all_symbols[2]])
elif for_payout == 1:
indices = shuffle([0, 1, 2])
symbol_set = ["", "", ""]
match_a = indices[0]
match_b = indices[1]
nonmatch = indices[2]
matching_symbol = all_symbols[0]
other_symbol = all_symbols[1]
symbol_set[match_a] = matching_symbol
symbol_set[match_b] = matching_symbol
symbol_set[nonmatch] = other_symbol
return "".join(symbol_set)
else:
relevantSymbols = shuffle(self.payout_to_symbols[for_payout])
symbol = relevantSymbols[0]
return "".join([symbol, symbol, symbol])
def build_text(self, wager_value, result, user, currency):

View File

@ -160,7 +160,7 @@ def api_comment(v):
else: abort(400)
body = request.values.get("body", "").strip()[:10000]
if v.admin_level == 3 and parent_post.id == 37749:
with open(f"snappy_{SITE_NAME}.txt", "a") as f:
f.write('\n{[para]}\n' + body)
@ -587,6 +587,9 @@ def api_comment(v):
slots = Slots(g)
slots.check_for_slots_command(body, v, c)
blackjack = Blackjack(g)
blackjack.check_for_blackjack_command(body, v, c)
g.db.commit()
if request.headers.get("Authorization"): return c.json
@ -888,4 +891,20 @@ def unsave_comment(cid, v):
g.db.delete(save)
g.db.commit()
return {"message": "Comment unsaved!"}
return {"message": "Comment unsaved!"}
@app.post("/blackjack/<cid>")
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def handle_blackjack_action(cid, v):
comment = get_comment(cid)
action = request.values.get("action", "")
blackjack = Blackjack(g)
if action == 'hit':
blackjack.player_hit(comment)
elif action == 'stay':
blackjack.player_stayed(comment)
return { "message" : "..." }

View File

@ -176,6 +176,14 @@
{% set isreply = False %}
{% endif %}
{% if c.blackjack_result %}
{% 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 wager = split_result[4] %}
{% 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) and request.path != '/admin/removed/comments' or (standalone and c.over_18 and not (v and v.over_18)) %} 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 %}">
{% if not isreply %}
<span class="comment-collapse-desktop d-none d-md-block" {% if not c.unread %}style="border-left: 2px solid {% if c.ghost %}var(--primary){% else %}#{{c.author.namecolor}}{% endif %}"{% endif %} onclick="collapse_comment('{{c.id}}')"></span>
@ -231,7 +239,32 @@
{% endif %}
{% if c.slots_result %}
<em style="position: relative; top: 2px; margin-left: 0.5rem">{{c.slots_result}}</em>
<em style="position: relative; top: 2px; margin-left: 0.5rem">{{c.slots_result}}</em>
{% endif %}
{% if c.blackjack_result %}
<em>{{player_hand}} vs. {{dealer_hand}}</em>
{% 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-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}}</span> Coins.</strong>
<script>
var blackjackResult = document.getElementById('blackjack-result-{{c.id}}');
if (blackjackResult) {
blackjackResult.innerText = Math.floor(parseInt(blackjackResult.innerText) * (3/2))
}
</script>
{% endif %}
{% endif %}
</div>
{% if c.active_flags %}

View File

@ -299,7 +299,8 @@ CREATE TABLE public.comments (
top_comment_id integer,
is_pinned_utc integer,
ghost boolean,
slots_result character varying(50)
slots_result character varying(50),
blackjack_result character varying(3000)
);