diff --git a/.gitignore b/.gitignore index f84c98ea3..ab8486374 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ under_attack venv/ .vscode/ .sass-cache/ -flask_session/ \ No newline at end of file +flask_session/ +marsey_count.json +.DS_Store \ No newline at end of file diff --git a/files/classes/__init__.py b/files/classes/__init__.py index 15fb4cb2c..a236af5a8 100644 --- a/files/classes/__init__.py +++ b/files/classes/__init__.py @@ -9,6 +9,7 @@ from .userblock import * from .submission import * from .votes import * from .domains import * +from .slots import * from .subscriptions import * from files.__main__ import app from .mod_logs import * diff --git a/files/classes/comment.py b/files/classes/comment.py index 5c27257d4..cc8dc0612 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -43,6 +43,7 @@ class Comment(Base): body = Column(String) body_html = Column(String) ban_reason = Column(String) + slots_result = Column(String, default="") post = relationship("Submission", viewonly=True) author = relationship("User", primaryjoin="User.id==Comment.author_id") diff --git a/files/classes/slots.py b/files/classes/slots.py new file mode 100644 index 000000000..f77ebc86a --- /dev/null +++ b/files/classes/slots.py @@ -0,0 +1,145 @@ +from json.encoder import INFINITY +import random +from .comment import * +from files.helpers.const import * + +class Slots: + commandWord = "!slots" + minimumBet = 5 + maximumBet = INFINITY + symbols = {"♦️", "♠️", "♥️", "♣️", "⚧", "🔞", "⚛️", "☢️", "✡️", "⚔️", "🐱"} + + # Common... + commonRatio = 4 + commonPayout = 2 + + # 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): + self.db = g.db + + # Check for !slots + def check_for_slots_command(self, in_text, from_user, from_comment): + if self.commandWord in in_text: + for word in in_text.split(): + if self.commandWord in word: + try: + wager = word[len(self.commandWord):] + wagerValue = int(wager, base=10) + + if self.wager_is_valid(from_user, wagerValue): + result = self.pull_the_arm(from_user, wagerValue, from_comment) + return { 'pulled': True, 'result': result } + + except ValueError: + break + return { 'pulled': False, 'result': '' } + + # Ensure user is capable of the wager + def wager_is_valid(self, from_user, wager): + if (wager < self.minimumBet): + return False + elif (wager > self.maximumBet): + return False + elif (wager > from_user.coins): + 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: + 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 False + + # Actually make the relevant calls + def pull_the_arm(self, from_user, amount, from_comment): + # Charge user for the bet + self.charge_user(from_user, amount) + + # Determine the outcome + result1 = self.count_out_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: + # Pay out + reward = amount * payout + self.credit_user(from_user, reward) + elif isConsolation: + # Refund wager + self.credit_user(from_user, amount) + + return "".join(resultSymbols) + + # Credit the user's account + def credit_user(self, from_user, amount): + from_user.coins += amount + + self.db.add(from_user) + self.db.commit() + + # Charge the user's account + def charge_user(self, from_user, amount): + from_user.coins -= amount + + self.db.add(from_user) + self.db.commit() diff --git a/files/routes/comments.py b/files/routes/comments.py index d098b8236..61e94bd65 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -123,7 +123,6 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None): else: template = "submission.html" return render_template(template, v=v, p=post, sort=sort, comment_info=comment_info, render_replies=True) - @app.post("/comment") @limiter.limit("1/second;6/minute;200/hour;1000/day") @auth_required @@ -561,6 +560,15 @@ def api_comment(v): g.db.commit() + # Slots + slots = Slots(g) + slots_check = 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() + if request.headers.get("Authorization"): return c.json return render_template("comments.html", v=v, comments=[c]) diff --git a/files/templates/comments.html b/files/templates/comments.html index 808a7b2c5..b2d26684e 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -86,8 +86,6 @@ {% if (c.is_banned or c.deleted_utc or c.is_blocking) and not (v and v.admin_level > 1) and not (v and v.id==c.author_id) %}
- -
@@ -231,6 +229,10 @@ {% if c.edited_utc %} · Edited {{c.edited_string}} {% endif %} + + {% if c.slots_result %} + {{c.slots_result}} + {% endif %}
{% if c.active_flags %}
diff --git a/schema.sql b/schema.sql index 49f26854f..d45137349 100644 --- a/schema.sql +++ b/schema.sql @@ -264,6 +264,7 @@ CREATE TABLE public.comments ( body character varying(10000), body_html character varying(40000), ban_reason character varying(25), + slots_result character varying(30), realupvotes integer, top_comment_id integer, is_pinned_utc integer,