From 2ac0ae76a5f3a44013c1f56ccb62586f0c47b96e Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 18 Feb 2022 11:39:12 +0200 Subject: [PATCH] vbn --- files/routes/comments.py | 38 ++++++++++------ files/routes/reporting.py | 60 ++++++++++++++++--------- files/templates/comments.html | 4 +- files/templates/submission.html | 2 +- files/templates/submission_listing.html | 2 +- 5 files changed, 68 insertions(+), 38 deletions(-) diff --git a/files/routes/comments.py b/files/routes/comments.py index 1a0b700d9..b16a60254 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -16,12 +16,14 @@ from files.helpers.sanitize import filter_emojis_only import requests from shutil import copyfile from json import loads +from collections import Counter IMGUR_KEY = environ.get("IMGUR_KEY").strip() if PUSHER_ID: beams_client = PushNotifications(instance_id=PUSHER_ID, secret_key=PUSHER_KEY) WORD_LIST = tuple(set(environ.get("WORDLE").split(" "))) +WORDLE_COLOR_MAPPINGS = {-1: "🟥", 0: "🟨", 1: "🟩"} @app.get("/comment/") @app.get("/post///") @@ -1050,10 +1052,31 @@ def handle_blackjack_action(cid, v): g.db.commit() return { "message" : "..." } + +def diff_words(answer, guess): + """ + Return a list of numbers corresponding to the char's relevance. + -1 means char is not in solution or the character appears too many times in the guess + 0 means char is in solution but in the wrong spot + 1 means char is in the correct spot + """ + diffs = [ + 1 if cs == cg else -1 for cs, cg in zip(answer, guess) + ] + char_freq = Counter( + c_guess for c_guess, diff, in zip(answer, diffs) if diff == -1 + ) + for i, cg in enumerate(guess): + if diffs[i] == -1 and cg in char_freq and char_freq[cg] > 0: + char_freq[cg] -= 1 + diffs[i] = 0 + return diffs + @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) guesses, status, answer = comment.wordle_result.split("_") @@ -1063,26 +1086,13 @@ def handle_wordle_action(cid, v): except: abort(400) if (len(guess) == 5 and status == "active"): - result = "" - not_finished = [x for x in answer] - pos = 0 - for i in guess: - result += i.upper() - if i == answer[pos]: - result += "🟩 " - not_finished[pos] = " " - elif i in not_finished: result += "🟨 " - else: result += "🟥 " - pos += 1 - - guesses += result[:-1] + guesses += "".join(cg + WORDLE_COLOR_MAPPINGS[diff] for cg, diff in zip(guess, diff_words(answer, guess))) if (guess == answer): status = "won" elif (count == 6): status = "lost" else: guesses += ' -> ' comment.wordle_result = f'{guesses}_{status}_{answer}' - g.db.add(comment) g.db.commit() diff --git a/files/routes/reporting.py b/files/routes/reporting.py index 0cf01d581..6a789abda 100644 --- a/files/routes/reporting.py +++ b/files/routes/reporting.py @@ -59,31 +59,51 @@ def api_flag_comment(cid, v): return {"message": "Comment reported!"} -@app.post('/del_report/') +@app.post('/del_report/post//') @limiter.limit("1/second;30/minute;200/hour;1000/day") @admin_level_required(2) -def remove_report(report_fn, v): - - if report_fn.startswith('c'): - report = g.db.query(CommentFlag).filter_by(id=int(report_fn.lstrip('c'))).one_or_none() - ma=ModAction( - kind="delete_report", - user_id=v.id, - target_comment_id=report.comment_id - ) - elif report_fn.startswith('p'): - report = g.db.query(Flag).filter_by(id=int(report_fn.lstrip('p'))).one_or_none() - ma=ModAction( - kind="delete_report", - user_id=v.id, - target_submission_id=report.post_id - ) - else: return {"error": "Invalid report ID"}, 400 - +def remove_report_post(v, pid, uid): + + pid = int(pid) + uid = int(uid) + + report = g.db.query(Flag).filter_by(post_id=pid, user_id=uid).one() + g.db.delete(report) + ma=ModAction( + kind="delete_report", + user_id=v.id, + target_submission_id=pid + ) + g.db.add(ma) g.db.commit() - return {"message": "Removed report"} \ No newline at end of file + return {"message": "Removed report!"} + + +@app.post('/del_report/comment//') +@limiter.limit("1/second;30/minute;200/hour;1000/day") +@admin_level_required(2) +def remove_report_comment(v, cid, uid): + + cid = int(cid) + uid = int(uid) + + report = g.db.query(CommentFlag).filter_by(comment_id=cid, user_id=uid).one() + + g.db.delete(report) + + ma=ModAction( + kind="delete_report", + user_id=v.id, + target_comment_id=cid + ) + + g.db.add(ma) + + g.db.commit() + + return {"message": "Removed report!"} \ No newline at end of file diff --git a/files/templates/comments.html b/files/templates/comments.html index 254fafbf7..92fed283d 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -313,7 +313,7 @@

 				
    {% for f in c.flags %} -
  • {{f.user.username}}{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}[remove]{% endif %}
  • +
  • {{f.user.username}}{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}[remove]{% endif %}
  • {% endfor %}
@@ -1015,7 +1015,7 @@ {% if v %} - + {% endif %} diff --git a/files/templates/submission.html b/files/templates/submission.html index 100e9f3d1..c22852d69 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -752,7 +752,7 @@

 						
    {% for f in p.flags %} -
  • {{f.user.username}}{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}[remove]{% endif %}
  • +
  • {{f.user.username}}{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}[remove]{% endif %}
  • {% endfor %}
diff --git a/files/templates/submission_listing.html b/files/templates/submission_listing.html index 751333418..2ed25cef8 100644 --- a/files/templates/submission_listing.html +++ b/files/templates/submission_listing.html @@ -74,7 +74,7 @@

 		
    {% for f in p.flags %} -
  • {{f.user.username}}{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}[remove]{% endif %}
  • +
  • {{f.user.username}}{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}[remove]{% endif %}
  • {% endfor %}