Change slots odds and display (#178)
This commit is contained in:
parent
5f34c6e9d2
commit
1f00eb9ed0
5 changed files with 96 additions and 128 deletions
|
@ -44,6 +44,7 @@ class Comment(Base):
|
||||||
body_html = Column(String)
|
body_html = Column(String)
|
||||||
ban_reason = Column(String)
|
ban_reason = Column(String)
|
||||||
slots_result = Column(String)
|
slots_result = Column(String)
|
||||||
|
slots_text = Column(String)
|
||||||
|
|
||||||
post = relationship("Submission", viewonly=True)
|
post = relationship("Submission", viewonly=True)
|
||||||
author = relationship("User", primaryjoin="User.id==Comment.author_id")
|
author = relationship("User", primaryjoin="User.id==Comment.author_id")
|
||||||
|
|
|
@ -1,140 +1,109 @@
|
||||||
from json.encoder import INFINITY
|
from json.encoder import INFINITY
|
||||||
import random
|
import random
|
||||||
from .comment import *
|
|
||||||
from files.helpers.const import *
|
def shuffle(stuff):
|
||||||
|
random.shuffle(stuff)
|
||||||
|
return stuff
|
||||||
|
|
||||||
class Slots:
|
class Slots:
|
||||||
commandWord = "!slots"
|
command_word = "!slots"
|
||||||
minimumBet = 5
|
minimum_bet = 5
|
||||||
maximumBet = INFINITY
|
maximum_bet = INFINITY
|
||||||
symbols = {"♦️", "♠️", "♥️", "♣️", "⚧", "🔞", "⚛️", "☢️", "✡️", "⚔️", "🐱"}
|
payout_to_symbols = {
|
||||||
|
2: ["👣", "🍀", "🌈", "⭐️"],
|
||||||
# Common...
|
3: ["🍎", "🔞", "⚛️", "☢️"],
|
||||||
commonRatio = 4
|
5: ["✡️", "⚔️", "🍆", "🍒"],
|
||||||
commonPayout = 2
|
12: ["🐱"]
|
||||||
|
}
|
||||||
# Uncommon.
|
|
||||||
uncommonIndex = 4
|
|
||||||
uncommonRatio = 3
|
|
||||||
uncommonPayout = 3
|
|
||||||
|
|
||||||
# Rare~
|
|
||||||
rareIndex = 8
|
|
||||||
rareRatio = 2
|
|
||||||
rarePayout = 5
|
|
||||||
|
|
||||||
# Jackpot!
|
|
||||||
jackpotIndex = 10
|
|
||||||
jackpotRatio = 1
|
|
||||||
jackpotPayout = 100
|
|
||||||
|
|
||||||
def __init__(self, g):
|
def __init__(self, g):
|
||||||
self.db = g.db
|
self.db = g.db
|
||||||
|
|
||||||
# Check for !slots<wager>
|
|
||||||
def check_for_slots_command(self, in_text, from_user, from_comment):
|
def check_for_slots_command(self, in_text, from_user, from_comment):
|
||||||
if self.commandWord in in_text:
|
if self.command_word in in_text:
|
||||||
for word in in_text.split():
|
for word in in_text.split():
|
||||||
if self.commandWord in word:
|
if self.command_word in word:
|
||||||
try:
|
try:
|
||||||
wager = word[len(self.commandWord):]
|
wager = word[len(self.command_word):]
|
||||||
wagerValue = int(wager, base=10)
|
wager_value = int(wager, base=10)
|
||||||
|
|
||||||
if self.wager_is_valid(from_user, wagerValue):
|
if self.wager_is_valid(from_user, wager_value):
|
||||||
result = self.pull_the_arm(from_user, wagerValue, from_comment)
|
from_user.coins -= wager_value
|
||||||
return { 'pulled': True, 'result': result }
|
|
||||||
|
|
||||||
|
payout = self.determine_payout()
|
||||||
|
symbols = self.build_symbols(payout)
|
||||||
|
text = self.build_text(wager_value, payout)
|
||||||
|
reward = wager_value * payout
|
||||||
|
|
||||||
|
from_user.coins += reward
|
||||||
|
self.db.add(from_user)
|
||||||
|
|
||||||
|
from_comment.slots_result = symbols
|
||||||
|
from_comment.slots_text = text
|
||||||
|
self.db.add(from_comment)
|
||||||
|
|
||||||
|
self.db.commit()
|
||||||
except: break
|
except: break
|
||||||
return { 'pulled': False, 'result': '' }
|
|
||||||
|
|
||||||
# Ensure user is capable of the wager
|
|
||||||
def wager_is_valid(self, from_user, wager):
|
def wager_is_valid(self, from_user, wager):
|
||||||
if (wager < self.minimumBet):
|
if (wager < self.minimum_bet):
|
||||||
return False
|
return False
|
||||||
elif (wager > self.maximumBet):
|
elif (wager > self.maximum_bet):
|
||||||
return False
|
return False
|
||||||
elif (wager > from_user.coins):
|
elif (wager > from_user.coins):
|
||||||
return False
|
return False
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
# Generate full set of symbols.
|
|
||||||
def count_out_symbols(self):
|
|
||||||
countedSymbols = []
|
|
||||||
payoutLookup = {}
|
|
||||||
index = 0
|
|
||||||
|
|
||||||
for item in self.symbols:
|
|
||||||
count = 0
|
|
||||||
|
|
||||||
if index == self.jackpotIndex:
|
|
||||||
count = self.jackpotRatio
|
|
||||||
payoutLookup[item] = self.jackpotPayout
|
|
||||||
elif index >= self.rareIndex:
|
|
||||||
count = self.rareRatio
|
|
||||||
payoutLookup[item] = self.rarePayout
|
|
||||||
elif index >= self.uncommonIndex:
|
|
||||||
count = self.uncommonRatio
|
|
||||||
payoutLookup[item] = self.uncommonPayout
|
|
||||||
else:
|
else:
|
||||||
count = self.commonRatio
|
|
||||||
payoutLookup[item] = self.commonPayout
|
|
||||||
|
|
||||||
while count > 0:
|
|
||||||
countedSymbols.append(item)
|
|
||||||
count -= 1
|
|
||||||
|
|
||||||
index += 1
|
|
||||||
|
|
||||||
random.shuffle(countedSymbols)
|
|
||||||
|
|
||||||
return { 'symbols': countedSymbols, 'payout': payoutLookup }
|
|
||||||
|
|
||||||
# Consolation prizes return the user's wager.
|
|
||||||
def check_for_consolation(self, symbols):
|
|
||||||
# 1. Any 2 matching.
|
|
||||||
if symbols[0] == symbols[1] or symbols[0] == symbols[2] or symbols[1] == symbols[2]:
|
|
||||||
return True
|
|
||||||
# 2. Any instance of jackpot.
|
|
||||||
for symbol in symbols:
|
|
||||||
if symbol == "🐱":
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
def determine_payout(self):
|
||||||
|
value = random.randint(0, 100)
|
||||||
|
|
||||||
# Actually make the relevant calls
|
if value == 100:
|
||||||
def pull_the_arm(self, from_user, amount, from_comment):
|
return 12
|
||||||
# Charge user for the bet
|
elif value >= 96:
|
||||||
self.charge_user(from_user, amount)
|
return 5
|
||||||
|
elif value >= 88:
|
||||||
|
return 3
|
||||||
|
elif value >= 72:
|
||||||
|
return 2
|
||||||
|
elif value >= 61:
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
# Determine the outcome
|
def build_symbols(self, for_payout):
|
||||||
result1 = self.count_out_symbols()
|
all_symbols = []
|
||||||
result2 = self.count_out_symbols()
|
|
||||||
result3 = self.count_out_symbols()
|
|
||||||
symbol1 = result1['symbols'][0]
|
|
||||||
symbol2 = result2['symbols'][0]
|
|
||||||
symbol3 = result3['symbols'][0]
|
|
||||||
payout = result1['payout'][symbol1]
|
|
||||||
isMatch = symbol1 == symbol2 and symbol2 == symbol3
|
|
||||||
resultSymbols = [symbol1, symbol2, symbol3]
|
|
||||||
isConsolation = self.check_for_consolation(resultSymbols)
|
|
||||||
|
|
||||||
if isMatch:
|
for payout in self.payout_to_symbols:
|
||||||
# Pay out
|
for symbol in self.payout_to_symbols[payout]:
|
||||||
reward = amount * payout
|
all_symbols.append(symbol)
|
||||||
self.credit_user(from_user, reward)
|
|
||||||
elif isConsolation:
|
|
||||||
# Refund wager
|
|
||||||
self.credit_user(from_user, amount)
|
|
||||||
|
|
||||||
return "".join(resultSymbols)
|
shuffle(all_symbols)
|
||||||
|
|
||||||
# Credit the user's account
|
if for_payout == 0:
|
||||||
def credit_user(self, from_user, amount):
|
return "".join([all_symbols[0], all_symbols[1], all_symbols[2]])
|
||||||
from_user.coins += amount
|
elif for_payout == 1:
|
||||||
self.db.add(from_user)
|
indices = shuffle([0, 1, 2])
|
||||||
|
symbol_set = ["", "", ""]
|
||||||
|
[match_a, match_b, nonmatch] = indices
|
||||||
|
[matching_symbol, other_symbol] = all_symbols
|
||||||
|
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):
|
||||||
|
if result == 0:
|
||||||
|
return f'Lost {wager_value} Coins'
|
||||||
|
elif result == 1:
|
||||||
|
return 'Broke Even'
|
||||||
|
elif result == 12:
|
||||||
|
return f'Jackpot! Won {wager_value} Coins'
|
||||||
|
else:
|
||||||
|
return f'Won {wager_value} Coins'
|
||||||
|
|
||||||
# Charge the user's account
|
|
||||||
def charge_user(self, from_user, amount):
|
|
||||||
from_user.coins -= amount
|
|
||||||
self.db.add(from_user)
|
|
|
@ -563,13 +563,10 @@ def api_comment(v):
|
||||||
g.db.add(c)
|
g.db.add(c)
|
||||||
|
|
||||||
slots = Slots(g)
|
slots = Slots(g)
|
||||||
slots_check = slots.check_for_slots_command(body, v, c)
|
slots.check_for_slots_command(body, v, c)
|
||||||
|
|
||||||
if (slots_check['pulled'] == True):
|
|
||||||
c.slots_result = slots_check['result']
|
|
||||||
g.db.add(c)
|
|
||||||
|
|
||||||
g.db.commit()
|
g.db.commit()
|
||||||
|
|
||||||
if request.headers.get("Authorization"): return c.json
|
if request.headers.get("Authorization"): return c.json
|
||||||
return render_template("comments.html", v=v, comments=[c])
|
return render_template("comments.html", v=v, comments=[c])
|
||||||
|
|
||||||
|
|
|
@ -231,7 +231,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if c.slots_result %}
|
{% if c.slots_result %}
|
||||||
<span style="position: relative; top: 2px; margin-left: 0.5rem">{{c.slots_result}}</span>
|
<span style="position: relative; top: 2px; margin-left: 0.5rem">{{c.slots_result}} <em>{{c.slots_text}}</em></span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% if c.active_flags %}
|
{% if c.active_flags %}
|
||||||
|
|
|
@ -299,7 +299,8 @@ CREATE TABLE public.comments (
|
||||||
top_comment_id integer,
|
top_comment_id integer,
|
||||||
is_pinned_utc integer,
|
is_pinned_utc integer,
|
||||||
ghost boolean,
|
ghost boolean,
|
||||||
slots_result character varying(30)
|
slots_result character varying(30),
|
||||||
|
slots_text character varying(100)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue