diff --git a/files/classes/comment.py b/files/classes/comment.py index a3c03e2c8..03b088491 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -436,4 +436,57 @@ class Comment(Base): @property @lazy - def active_flags(self): return self.flags.count() \ No newline at end of file + def active_flags(self): return self.flags.count() + + @lazy + def wordle_html(self, v): + if not self.wordle_result: return '' + + split_wordle_result = self.wordle_result.split('_') + wordle_guesses = split_wordle_result[0] + wordle_status = split_wordle_result[1] + wordle_answer = split_wordle_result[2] + + body = f"{wordle_guesses}" + + if wordle_status == 'active' and v and v.id == self.author_id: + body += f'''''' + elif wordle_status == 'won': + body += "Correct!" + elif wordle_status == 'lost': + body += f"Lost. The answer was: {wordle_answer}" + + body += '' + return body + + @lazy + def blackjack_html(self, v): + if not self.blackjack_result: return '' + + split_result = self.blackjack_result.split('_') + blackjack_status = split_result[3] + player_hand = split_result[0].replace('X', '10') + dealer_hand = split_result[1].split('/')[0] if blackjack_status == 'active' else split_result[1] + dealer_hand = dealer_hand.replace('X', '10') + wager = split_result[4] + kind = split_result[5] + currency_kind = "Coins" if kind == "coins" else "Marseybucks" + + body = f"{player_hand} vs. {dealer_hand}" + + if blackjack_status == 'active' and v.id == self.author_id: + body += f''' + ''' + elif blackjack_status == 'push': + body += f"Pushed. Refunded {wager} {currency_kind}." + elif blackjack_status == 'bust': + body += f"Bust. Lost {wager} {currency_kind}." + elif blackjack_status == 'lost': + body += f"Lost {wager} {currency_kind}." + elif blackjack_status == 'won': + body += f"Won {wager} {currency_kind}." + elif blackjack_status == 'blackjack': + body += f"Blackjack! Won {floor(wager_value * 3/2)} {currency_kind}." + + body += '' + return body \ No newline at end of file diff --git a/files/routes/comments.py b/files/routes/comments.py index d571d7d3a..2f42a8d8c 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -1048,7 +1048,8 @@ def unsave_comment(cid, v): def handle_blackjack_action(cid, v): comment = get_comment(cid) if 'active' in comment.blackjack_result: - action = request.values.get("action", "") + try: action = request.values.get("thing").strip().lower() + except: abort(400) if action == 'hit': player_hit(comment) elif action == 'stay': player_stayed(comment) @@ -1056,7 +1057,7 @@ def handle_blackjack_action(cid, v): g.db.add(comment) g.db.add(v) g.db.commit() - return { "message" : "..." } + return {"response" : comment.blackjack_html(v)} def diff_words(answer, guess): @@ -1078,6 +1079,7 @@ def diff_words(answer, guess): diffs[i] = 0 return diffs + @app.post("/wordle/") @limiter.limit("1/second;30/minute;200/hour;1000/day") @auth_required @@ -1088,7 +1090,7 @@ def handle_wordle_action(cid, v): guesses, status, answer = comment.wordle_result.split("_") count = len(guesses.split(" -> ")) - try: guess = request.values.get("guess").strip().lower() + try: guess = request.values.get("thing").strip().lower() except: abort(400) if len(guess) != 5 or not d.check(guess) and guess not in WORDLE_LIST: @@ -1106,4 +1108,4 @@ def handle_wordle_action(cid, v): g.db.add(comment) g.db.commit() - return {"message" : "."} \ No newline at end of file + return {"response" : comment.wordle_html(v)} \ No newline at end of file diff --git a/files/templates/comments.html b/files/templates/comments.html index 708ddb998..fb57d4dda 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -157,23 +157,6 @@ {% 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 dealer_hand = dealer_hand.replace('X', '10') %} - {% set wager = split_result[4] %} - {% set kind = split_result[5] %} -{% endif %} - -{% if c.wordle_result %} - {% set split_wordle_result = c.wordle_result.split('_') %} - {% set wordle_guesses = split_wordle_result[0] %} - {% set wordle_status = split_wordle_result[1] %} - {% set wordle_answer = split_wordle_result[2] %} -{% endif %} -
{% if not isreply %} @@ -255,35 +238,11 @@ {% endif %} {% if c.blackjack_result %} - {% set currency_kind = "Coins" if kind == "coins" else "Marseybucks" %} - {{player_hand}} vs. {{dealer_hand}} - {% if blackjack_status == 'active' and v.id == c.author_id %} - - - {% elif blackjack_status == 'push' %} - Pushed. Refunded {{wager}} {{currency_kind}}. - {% elif blackjack_status == 'bust' %} - Bust. Lost {{wager}} {{currency_kind}}. - {% elif blackjack_status == 'lost' %} - Lost {{wager}} {{currency_kind}}. - {% elif blackjack_status == 'won' %} - Won {{wager}} {{currency_kind}}. - {% elif blackjack_status == 'blackjack' %} - Blackjack! Won {{(wager|int * 3/2)|round(0, 'floor')|int}} {{currency_kind}}. - {% endif %} + {{c.blackjack_html(v) | safe}} {% endif %} {% if c.wordle_result %} - {{wordle_guesses}} - {% if wordle_status == 'active' and v.id == c.author_id %} - - - {% elif wordle_status == 'won' %} - Correct! - {% elif wordle_status == 'lost' %} - Lost. The answer was: {{wordle_answer}} - {% endif %} + {{c.wordle_html(v) | safe}} {% endif %}
{% if c.active_flags %} @@ -874,7 +833,7 @@ {% if v %} - + {% endif %}