From 23fbca22237d26de64c15fcd7956e61ca8a997e3 Mon Sep 17 00:00:00 2001 From: BuckFromRdrama <98723184+BuckFromRdrama@users.noreply.github.com> Date: Sun, 13 Feb 2022 20:39:48 -0500 Subject: [PATCH] Wordle game (#206) * Wordle * added words * removed print * fixed invisible guess bug; made input look better * sneed Co-authored-by: Aevann1 <59999695+Aevann1@users.noreply.github.com> --- files/assets/js/comments_v.js | 16 ++++++++++ files/classes/__init__.py | 1 + files/classes/comment.py | 1 + files/classes/wordle.py | 55 +++++++++++++++++++++++++++++++++++ files/routes/comments.py | 22 ++++++++++++-- files/templates/comments.html | 28 +++++++++++++++--- schema.sql | 1 + 7 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 files/classes/wordle.py diff --git a/files/assets/js/comments_v.js b/files/assets/js/comments_v.js index 6c6b948a7..69b15fb99 100644 --- a/files/assets/js/comments_v.js +++ b/files/assets/js/comments_v.js @@ -322,6 +322,22 @@ function handle_blackjack_action(cid, action) { xhr.open("post", `/blackjack/${cid}`); xhr.setRequestHeader('xhr', 'xhr'); + xhr.onload = function() { + if (xhr.status == 200) location.reload(); + } + xhr.send(form); +} + +function handle_wordle_action(cid, guess) { + const form = new FormData(); + form.append('formkey', formkey()); + form.append('comment_id', cid); + form.append('guess', guess); + + const xhr = new XMLHttpRequest(); + xhr.open("post", `/wordle/${cid}`); + xhr.setRequestHeader('xhr', 'xhr'); + xhr.onload = function() { if (xhr.status == 200) location.reload(); } diff --git a/files/classes/__init__.py b/files/classes/__init__.py index ee4ae58d9..1ccd67762 100644 --- a/files/classes/__init__.py +++ b/files/classes/__init__.py @@ -11,6 +11,7 @@ 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 diff --git a/files/classes/comment.py b/files/classes/comment.py index 05b0b505c..e62eaaa86 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -45,6 +45,7 @@ class Comment(Base): ban_reason = Column(String) slots_result = Column(String) blackjack_result = Column(String) + wordle_result = Column(String) treasure_amount = Column(String) oauth_app = relationship("OauthApp", viewonly=True) diff --git a/files/classes/wordle.py b/files/classes/wordle.py new file mode 100644 index 000000000..1ca9a726a --- /dev/null +++ b/files/classes/wordle.py @@ -0,0 +1,55 @@ +import random + +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 = ['tariq', 'sneed', 'drama', 'chuck', 'bussy', 'which', 'their', 'would', 'there', 'could', 'other', 'about', 'great', 'these', 'after', 'first', 'never', 'where', 'those', 'shall', 'being', 'might', 'every', 'think', 'under', 'found', 'still', 'while', 'again', 'place', 'young', 'years', 'three', 'right', 'house', 'whole', 'world', 'thing', 'night', 'going', 'heard', 'heart', 'among', 'asked', 'small', 'woman', 'whose', 'quite', 'words', 'given', 'taken', 'hands', 'until', 'since', 'light'] + 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)) + else: + return + + if (guess.lower() in answer): + status = "won" + elif (count == 6): + status = "lost" + + from_comment.wordle_result = format_all(guesses, status, answer) + \ No newline at end of file diff --git a/files/routes/comments.py b/files/routes/comments.py index d694665a4..8b06cca07 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -643,11 +643,14 @@ def api_comment(v): blackjack = Blackjack(g) blackjack.check_for_blackjack_commands(body, v, c) + + wordle = Wordle(g) + wordle.check_for_wordle_commands(body, v, c) treasure = Treasure(g) treasure.check_for_treasure(body, c) - if not c.slots_result and not c.blackjack_result: + 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) @@ -734,7 +737,7 @@ def edit_comment(cid, v): if ban.reason: reason += f" {ban.reason}" return {'error': reason}, 400 - if '!slots' not in body.lower() and '!blackjack' not in body.lower() and AGENDAPOSTER_PHRASE not in body.lower(): + if '!slots' not in body.lower() and '!blackjack' not in body.lower() and '!wordle' not in body.lower() and AGENDAPOSTER_PHRASE not in body.lower(): now = int(time.time()) cutoff = now - 60 * 60 * 24 @@ -1037,6 +1040,21 @@ def handle_blackjack_action(cid, v): if action == 'hit': blackjack.player_hit(comment) elif action == 'stay': blackjack.player_stayed(comment) + g.db.add(comment) + g.db.add(v) + g.db.commit() + return { "message" : "..." } + +@app.post("/wordle/") +@limiter.limit("1/second;30/minute;200/hour;1000/day") +@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) + g.db.add(comment) g.db.add(v) g.db.commit() diff --git a/files/templates/comments.html b/files/templates/comments.html index c2c688aa8..edf79924d 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -188,6 +188,13 @@ {% 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 %} @@ -279,6 +286,19 @@ Blackjack! Won {{(wager|int * 3/2)|round(0, 'floor')|int}} {{currency_kind}}. {% endif %} {% 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 %} + {% endif %}
{% if c.active_flags %}
@@ -945,16 +965,16 @@ {% endif %} - + {% if v and v.admin_level > 1 %} - + {% endif %} {% include "expanded_image_modal.html" %} - - + +