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>
remotes/1693045480750635534/spooky-22
BuckFromRdrama 2022-02-13 20:39:48 -05:00 committed by GitHub
parent 7f2d219427
commit e9f03cc8b9
6 changed files with 102 additions and 6 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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/<cid>")
@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()

View File

@ -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 %}
<div id="comment-{{c.id}}" class="anchor {% if c.unread %}unread{% endif %} comment {% if standalone and level==1 %} mt-0{% endif %} {% if c.collapse_for_user(v,request.path) %}collapsed{% endif %}" style="{% if isreply %}padding-left:0!important;{% elif not c.unread %}border-left: 2px solid {% if c.ghost %}var(--primary){% else %}#{{c.author.namecolor}};{% endif %}{% endif %} {% if c.unread %}padding: 10px 10px 10px !important;{% endif %}">
{% if not isreply %}
<span class="comment-collapse-desktop d-none d-md-block" {% if not c.unread %}style="border-left: 2px solid {% if c.ghost %}var(--primary){% else %}#{{c.author.namecolor}}{% endif %}"{% endif %} onclick="collapse_comment('{{c.id}}')"></span>
@ -279,6 +286,19 @@
<strong>Blackjack! Won {{(wager|int * 3/2)|round(0, 'floor')|int}} {{currency_kind}}.</strong>
{% endif %}
{% endif %}
{% if c.wordle_result %}
<em>{{wordle_guesses}}</em>
{% if wordle_status == 'active' and v.id == c.author_id %}
<input autocomplete="off" id="guess_box" type="text" name="guess" class="form-control" maxsize="4" style="width: 200px;
display: initial;" placeholder="5-letter guess"></input>
<button class="btn btn-success small" style="text-transform: uppercase; padding: 2px;" onclick="handle_wordle_action('{{c.id}}', document.getElementById('guess_box').value)">Guess</button>
{% elif wordle_status == 'won' %}
<strong>Correct!</strong>
{% elif wordle_status == 'lost' %}
<strong>Lost. The answer was: {{wordle_answer}}</strong>
{% endif %}
{% endif %}
</div>
{% if c.active_flags %}
<div id="flaggers-{{c.id}}" class="flaggers d-none">
@ -945,16 +965,16 @@
<script src="/static/assets/js/comments_v.js?a=246"></script>
{% endif %}
<script src="/static/assets/js/clipboard.js?a=240"></script>
<script src="/static/assets/js/clipboard.js"></script>
{% if v and v.admin_level > 1 %}
<script src="/static/assets/js/comments_admin.js?a=240"></script>
<script src="/static/assets/js/comments_admin.js"></script>
{% endif %}
{% include "expanded_image_modal.html" %}
<script src="/static/assets/js/comments+submission_listing.js?a=240"></script>
<script src="/static/assets/js/comments.js?a=242"></script>
<script src="/static/assets/js/comments+submission_listing.js"></script>
<script src="/static/assets/js/comments.js"></script>
<script>
{% if p and (not v or v.highlightcomments) %}

View File

@ -300,6 +300,7 @@ CREATE TABLE public.comments (
ghost boolean,
slots_result character varying(50),
blackjack_result character varying(3000),
wordle_result character varying(3000),
treasure_amount character varying(10)
);