remotes/1693045480750635534/spooky-22
Aevann1 2022-02-14 22:29:36 +02:00
parent 9cc78fc04b
commit 1e316595b3
10 changed files with 329 additions and 382 deletions

View File

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

View File

@ -1,150 +0,0 @@
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 deal_initial_cards():
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
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, kind):
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}_{kind}'
class Blackjack:
coins_command_word = "!blackjack"
marseybucks_command_word = "!blackjackmb"
minimum_bet = 100
maximum_bet = INFINITY
def __init__(self, g):
self.db = g.db
def check_for_blackjack_commands(self, in_text, from_user, from_comment):
for command_word in (self.coins_command_word, self.marseybucks_command_word):
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():
if command_word in word:
try:
wager = word[len(command_word):]
wager_value = int(wager)
except: break
if (wager_value < self.minimum_bet): break
elif (wager_value > self.maximum_bet): break
elif (wager_value <= currency_value):
setattr(from_user, currency_prop, currency_value - 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, currency_prop)
elif player_value == 21:
status = 'blackjack'
self.apply_game_result(from_comment, wager, status, currency_prop)
elif dealer_value == 21:
status = 'lost'
self.apply_game_result(from_comment, wager, status, currency_prop)
from_comment.blackjack_result = format_all(player_hand, dealer_hand, rest_of_deck, status, wager, currency_prop)
def player_hit(self, from_comment):
player_hand, dealer_hand, deck, status, wager, kind = 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, kind)
from_comment.blackjack_result = format_all(player_hand, dealer_hand, deck, status, wager, kind)
if (player_value == 21): self.player_stayed(from_comment)
def player_stayed(self, from_comment):
player_hand, dealer_hand, deck, status, wager, kind = 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, kind)
self.apply_game_result(from_comment, wager, status, kind)
def apply_game_result(self, from_comment, wager, result, kind):
wager_value = int(wager)
user = from_comment.author
reward = -1
if result == 'push': reward = 0
elif result == 'won': reward = wager_value
elif result == 'blackjack': reward = floor(wager_value * 3/2)
if (reward > -1):
currency_value = int(getattr(user, kind, 0))
setattr(user, kind, currency_value + wager_value + reward)
user.winnings += reward
self.db.add(user)
self.db.commit()

View File

@ -1,121 +0,0 @@
from json.encoder import INFINITY
import random
from files.helpers.const import *
def shuffle(stuff):
random.shuffle(stuff)
return stuff
class Slots:
command_word = "!slots"
casino_word = "!slotsmb"
if SITE_NAME == 'Drama': minimum_bet = 100
else: minimum_bet = 10
maximum_bet = INFINITY
payout_to_symbols = {
2: ["👣", "🍀", "🌈", "⭐️"],
3: ["🍎", "🔞", "⚛️", "☢️"],
5: ["✡️", "⚔️", "🍆", "🍒"],
12: ["🐱"]
}
def __init__(self, g):
self.db = g.db
def check_for_slots_command(self, in_text, from_user, from_comment):
in_text = in_text.lower()
if self.command_word in in_text:
for word in in_text.split():
if self.command_word in word:
try:
wager = word[len(self.command_word):]
wager_value = int(wager)
except: break
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
from_user.winnings -= wager_value
payout = self.determine_payout()
symbols = self.build_symbols(payout)
text = self.build_text(wager_value, payout, from_user, "Coins")
reward = wager_value * payout
from_user.coins += reward
from_user.winnings += reward
from_comment.slots_result = f'{symbols} {text}'
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
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
payout = self.determine_payout()
symbols = self.build_symbols(payout)
text = self.build_text(wager_value, payout, from_user, "Marseybux")
reward = wager_value * payout
from_user.procoins += reward
from_user.winnings += reward
from_comment.slots_result = f'{symbols} {text}'
def determine_payout(self):
value = random.randint(1, 100)
if value == 100: return 12
elif value >= 96: return 5
elif value >= 88: return 3
elif value >= 72: return 2
elif value >= 61: return 1
else: return 0
def build_symbols(self, for_payout):
all_symbols = []
for payout in self.payout_to_symbols:
for symbol in self.payout_to_symbols[payout]:
all_symbols.append(symbol)
shuffle(all_symbols)
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):
if result == 0: return f'Lost {wager_value} {currency}'
elif result == 1: return 'Broke Even'
elif result == 12: return f'Jackpot! Won {wager_value * (result-1)} {currency}'
else: return f'Won {wager_value * (result-1)} {currency}'

View File

@ -1,35 +0,0 @@
import random
class Treasure:
special_min = 100
special_max = 1000
standard_min = 10
standard_max = 100
def __init__(self, g):
self.db = g.db
def check_for_treasure(self, in_text, from_comment):
if '!slots' not in in_text and '!blackjack' not in in_text and '!wordle' not in in_text:
seed = random.randint(1, 1000)
is_special = seed == 1000
is_standard = seed >= 990
amount = 0
if is_special:
amount = random.randint(self.special_min, self.special_max)
elif is_standard:
amount = random.randint(self.standard_min, self.standard_max)
if random.randint(1, 100) > 75: amount = -amount
if amount != 0:
user = from_comment.author
user.coins += amount
if user.coins < 0: user.coins = 0
from_comment.treasure_amount = str(amount)
self.db.add(user)
self.db.add(from_comment)
self.db.commit()

View File

@ -1,52 +0,0 @@
import random
from os import environ
def format_guesses(guesses):
return " -> ".join(guesses)
def format_all(guesses, status, answer):
formatted_guesses = format_guesses(guesses)
return f'{formatted_guesses}_{status}_{answer}'
class Wordle:
def __init__(self, g):
self.word_list = environ.get('WORDLE').split(' ')
self.command_word = "!wordle"
self.db = g.db
def check_for_wordle_commands(self, in_text, from_user, from_comment):
word_list = self.word_list
command_word = self.command_word
if command_word in in_text:
answer = random.choice(word_list) # choose a random word from word list
guesses = []
status = 'active'
from_comment.wordle_result = format_all(guesses, status, answer)
def check_guess(self,from_comment, guess):
guesses, status, answer = from_comment.wordle_result.split("_")
guesses = guesses.split(" -> ")
if (guesses[0] == ""):
guesses = []
count = len(guesses)
if (guess != None and len(guess) == 5 and status == "active"):
result = ["🟥"]*5
pos = 0 # letter position
guess = guess.lower()
for i in guess:
result[pos] = i.upper()
if i == answer[pos]:
result[pos] = result[pos] + "🟩" # green
elif i in answer:
result[pos] = result[pos] + "🟨" # yellow
else:
result[pos] = result[pos] + "🟥" # red
pos += 1 # add 1 to the letter position
guesses.append("/".join(result))
if (guess.lower() == answer): status = "won"
elif (count == 5): status = "lost"
from_comment.wordle_result = format_all(guesses, status, answer)

View File

@ -0,0 +1,140 @@
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 = ["♠️", "♥️", "♣️", "♦️"]
coins_command_word = "!blackjack"
marseybucks_command_word = "!blackjackmb"
minimum_bet = 100
maximum_bet = INFINITY
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)])
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, kind):
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}_{kind}'
def check_for_blackjack_commands(in_text, from_user, from_comment):
for command_word in (coins_command_word, marseybucks_command_word):
currency_prop = "coins" if command_word == 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():
if command_word in word:
try:
wager = word[len(command_word):]
wager_value = int(wager)
except: break
if (wager_value < minimum_bet): break
elif (wager_value > maximum_bet): break
elif (wager_value <= currency_value):
setattr(from_user, currency_prop, currency_value - 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'
apply_game_result(from_comment, wager, status, currency_prop)
elif player_value == 21:
status = 'blackjack'
apply_game_result(from_comment, wager, status, currency_prop)
elif dealer_value == 21:
status = 'lost'
apply_game_result(from_comment, wager, status, currency_prop)
from_comment.blackjack_result = format_all(player_hand, dealer_hand, rest_of_deck, status, wager, currency_prop)
def player_hit(from_comment):
player_hand, dealer_hand, deck, status, wager, kind = 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'
apply_game_result(from_comment, wager, status, kind)
from_comment.blackjack_result = format_all(player_hand, dealer_hand, deck, status, wager, kind)
if (player_value == 21): player_stayed(from_comment)
def player_stayed(from_comment):
player_hand, dealer_hand, deck, status, wager, kind = 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, kind)
apply_game_result(from_comment, wager, status, kind)
def apply_game_result(from_comment, wager, result, kind):
wager_value = int(wager)
user = from_comment.author
reward = -1
if result == 'push': reward = 0
elif result == 'won': reward = wager_value
elif result == 'blackjack': reward = floor(wager_value * 3/2)
if (reward > -1):
currency_value = int(getattr(user, kind, 0))
setattr(user, kind, currency_value + wager_value + reward)
user.winnings += reward

View File

@ -0,0 +1,117 @@
from json.encoder import INFINITY
import random
from .const import *
command_word = "!slots"
casino_word = "!slotsmb"
if SITE_NAME == 'Drama': minimum_bet = 100
else: minimum_bet = 10
maximum_bet = INFINITY
payout_to_symbols = {
2: ["👣", "🍀", "🌈", "⭐️"],
3: ["🍎", "🔞", "⚛️", "☢️"],
5: ["✡️", "⚔️", "🍆", "🍒"],
12: ["🐱"]
}
def shuffle(stuff):
random.shuffle(stuff)
return stuff
def check_for_slots_command(in_text, from_user, from_comment):
in_text = in_text.lower()
if command_word in in_text:
for word in in_text.split():
if command_word in word:
try:
wager = word[len(command_word):]
wager_value = int(wager)
except: break
if (wager_value < minimum_bet): break
elif (wager_value > maximum_bet): break
elif (wager_value > from_user.coins): break
from_user.coins -= wager_value
from_user.winnings -= wager_value
payout = determine_payout()
symbols = build_symbols(payout)
text = build_text(wager_value, payout, from_user, "Coins")
reward = wager_value * payout
from_user.coins += reward
from_user.winnings += reward
from_comment.slots_result = f'{symbols} {text}'
if casino_word in in_text:
for word in in_text.split():
if casino_word in word:
try:
wager = word[len(casino_word):]
wager_value = int(wager)
except: break
if (wager_value < minimum_bet): break
elif (wager_value > maximum_bet): break
elif (wager_value > from_user.procoins): break
from_user.procoins -= wager_value
from_user.winnings -= wager_value
payout = determine_payout()
symbols = build_symbols(payout)
text = build_text(wager_value, payout, from_user, "Marseybux")
reward = wager_value * payout
from_user.procoins += reward
from_user.winnings += reward
from_comment.slots_result = f'{symbols} {text}'
def determine_payout():
value = random.randint(1, 100)
if value == 100: return 12
elif value >= 96: return 5
elif value >= 88: return 3
elif value >= 72: return 2
elif value >= 61: return 1
else: return 0
def build_symbols(for_payout):
all_symbols = []
for payout in payout_to_symbols:
for symbol in payout_to_symbols[payout]:
all_symbols.append(symbol)
shuffle(all_symbols)
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(payout_to_symbols[for_payout])
symbol = relevantSymbols[0]
return "".join([symbol, symbol, symbol])
def build_text(wager_value, result, user, currency):
if result == 0: return f'Lost {wager_value} {currency}'
elif result == 1: return 'Broke Even'
elif result == 12: return f'Jackpot! Won {wager_value * (result-1)} {currency}'
else: return f'Won {wager_value * (result-1)} {currency}'

View File

@ -0,0 +1,27 @@
import random
special_min = 100
special_max = 1000
standard_min = 10
standard_max = 100
def check_for_treasure(in_text, from_comment):
if '!slots' not in in_text and '!blackjack' not in in_text and '!wordle' not in in_text:
seed = random.randint(1, 1000)
is_special = seed == 1000
is_standard = seed >= 990
amount = 0
if is_special:
amount = random.randint(special_min, special_max)
elif is_standard:
amount = random.randint(standard_min, standard_max)
if random.randint(1, 100) > 90: amount = -amount
if amount != 0:
user = from_comment.author
user.coins += amount
if user.coins < 0: user.coins = 0
from_comment.treasure_amount = str(amount)

View File

@ -3,6 +3,9 @@ from files.helpers.filters import *
from files.helpers.alerts import *
from files.helpers.images import *
from files.helpers.const import *
from files.helpers.slots import *
from files.helpers.blackjack import *
from files.helpers.treasure import *
from files.classes import *
from files.routes.front import comment_idlist
from files.routes.static import marsey_list
@ -21,6 +24,7 @@ if PUSHER_ID: beams_client = PushNotifications(instance_id=PUSHER_ID, secret_key
CF_KEY = environ.get("CF_KEY", "").strip()
CF_ZONE = environ.get("CF_ZONE", "").strip()
CF_HEADERS = {"Authorization": f"Bearer {CF_KEY}", "Content-Type": "application/json"}
WORD_LIST = tuple(set(environ.get("WORDLE").split(" ")))
@app.get("/comment/<cid>")
@app.get("/post/<pid>/<anything>/<cid>")
@ -638,22 +642,20 @@ def api_comment(v):
g.db.add(c)
if not v.rehab:
slots = Slots(g)
slots.check_for_slots_command(body, v, c)
check_for_slots_command(body, v, c)
blackjack = Blackjack(g)
blackjack.check_for_blackjack_commands(body, v, c)
wordle = Wordle(g)
wordle.check_for_wordle_commands(body, v, c)
check_for_blackjack_commands(body, v, c)
treasure = Treasure(g)
treasure.check_for_treasure(body, c)
check_for_treasure(body, c)
if not c.slots_result and not c.blackjack_result and not c.wordle_result:
parent_post.comment_count += 1
g.db.add(parent_post)
if "!wordle" in body:
answer = random.choice(WORD_LIST)
c.wordle_result = f'_active_{answer}'
g.db.commit()
if request.headers.get("Authorization"): return c.json
@ -1035,10 +1037,9 @@ def unsave_comment(cid, v):
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)
if action == 'hit': player_hit(comment)
elif action == 'stay': player_stayed(comment)
g.db.add(comment)
g.db.add(v)
@ -1050,12 +1051,36 @@ def handle_blackjack_action(cid, v):
@auth_required
def handle_wordle_action(cid, v):
comment = get_comment(cid)
guess = request.values.get("guess", "")
wordle = Wordle(g)
wordle.check_guess(comment,guess)
guesses, status, answer = comment.wordle_result.split("_")
count = len(guesses.split(" -> "))
try: guess = request.values.get("guess").strip().lower()
except: abort(400)
if (len(guess) == 5 and status == "active"):
result = ""
not_finished = [x for x in answer]
pos = 0
for i in guess:
result += i.upper()
if i == answer[pos]:
result += "🟩/"
not_finished[pos] = " "
elif i in not_finished: result += "🟨/"
else: result += "🟥/"
pos += 1
guesses += result[:-1]
if (guess == answer): status = "won"
elif (count == 6): status = "lost"
else: guesses += ' -> '
comment.wordle_result = f'{guesses}_{status}_{answer}'
g.db.add(comment)
g.db.commit()
g.db.add(comment)
g.db.add(v)
g.db.commit()
return { "message" : "..." }

View File

@ -7,6 +7,7 @@ from files.helpers.filters import *
from files.helpers.alerts import *
from files.helpers.discord import send_discord_message
from files.helpers.const import *
from files.helpers.slots import *
from files.classes import *
from flask import *
from io import BytesIO
@ -1279,8 +1280,7 @@ def submit_post(v, sub=None):
g.db.add(n)
if body.startswith('!slots1000'):
slots = Slots(g)
slots.check_for_slots_command(body, snappy, c)
check_for_slots_command(body, snappy, c)
new_post.comment_count += 1