diff --git a/main.py b/main.py index c7dfd51..5403782 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ from PIL import Image from jina import get_grass_touching_similarity from ocr_check import generate_challenge, check_text_similarity -import os, flask_login, uuid, base64, sqlite3, bcrypt, secrets, hashlib, time +import os, flask_login, uuid, base64, sqlite3, bcrypt, secrets, hashlib, time, threading if os.path.exists(".env"): load_dotenv(".env") @@ -52,6 +52,20 @@ def close_connection(exception): if db is not None: db.close() +def check_grass_touching_bans(): + while True: + cur = get_db().cursor() + + cur.execute("SELECT username, last_grass_touch_time FROM Users") + for user in cur.fetchall(): + if time.time() - user[1] >= (24 * 3600): + cur.execute("UPDATE users SET banned = ? WHERE username = ?", (True, user[0])) + cur.close() + + time.sleep(60) + +threading.Thread(target=check_grass_touching_bans, daemon=True).start() + class User(flask_login.UserMixin): pass @@ -61,6 +75,39 @@ def user_loader(user_id): user.id = user_id return user +@login_manager.unauthorized_handler +def unauthorized_handler(): + return redirect(url_for("login")) + +def resize_image_file(path, max_side=256, fmt="JPEG"): + img = Image.open(path) + scale = max_side / max(img.size) + if scale < 1: + img = img.resize((int(img.width * scale), int(img.height * scale)), Image.LANCZOS) + img.save(path, format=fmt) + +@app.route("/submit_grass_touching", methods=["POST"]) +@flask_login.login_required +def submit_grass_touching(): + username = flask_login.current_user.id + + if not challenges.get(username): + return Response("Start and finish a challenge before submitting the grass touching.", 401) + + if not challenges[username]["completed"]: + return Response("Finish a challenge before submitting the grass touching.", 401) + + cur = get_db().cursor() + + cur.execute("UPDATE Users grass_touching_count = grass_touching_count + 1 WHERE username = ?", (username,)) + cur.execute("UPDATE Users last_grass_touch_time = ? WHERE username = ?", (time.time(), username)) + + get_db().commit() + + cur.close() + + return redirect("/") + @app.route("/generate_challenge", methods=["POST"]) def generate_challenge_route(): username = request.json["username"] @@ -70,13 +117,6 @@ def generate_challenge_route(): return challenges[username]["text"] -def resize_image_file(path, max_side=256, fmt="JPEG"): - img = Image.open(path) - scale = max_side / max(img.size) - if scale < 1: - img = img.resize((int(img.width * scale), int(img.height * scale)), Image.LANCZOS) - img.save(path, format=fmt) - @app.route("/submit_challenge", methods=["POST"]) def submit_challenge(): try: @@ -228,8 +268,4 @@ def logout(): flask_login.logout_user() return redirect(url_for("/")) -@login_manager.unauthorized_handler -def unauthorized_handler(): - return redirect(url_for("login")) - app.run(port=os.environ.get("PORT"), host=os.environ.get("HOST", "0.0.0.0")) \ No newline at end of file diff --git a/templates/home.jinja2 b/templates/home.jinja2 index dc7f35e..67260d0 100644 --- a/templates/home.jinja2 +++ b/templates/home.jinja2 @@ -29,6 +29,9 @@ Login {% else %} + diff --git a/templates/leaderboard.jinja2 b/templates/leaderboard.jinja2 index cf51581..3d7c0ba 100644 --- a/templates/leaderboard.jinja2 +++ b/templates/leaderboard.jinja2 @@ -29,6 +29,9 @@ Login {% else %} + diff --git a/templates/register.jinja2 b/templates/register.jinja2 index 3992fbe..5143fe5 100644 --- a/templates/register.jinja2 +++ b/templates/register.jinja2 @@ -50,10 +50,12 @@
To complete this challenge, you need to submit a picture of you touching grass next to a paper containing the following text: Loading...
+ Dont worry! We wont tell your friends. + @@ -64,6 +66,8 @@ function upload_success(image_url) { document.getElementById("submit").disabled = false; document.getElementById("submit").innerHTML = "Submit" document.getElementById("submit").type = "submit" + document.getElementById("error-label").textContent = "" + document.getElementById("error-label").hidden = true; document.getElementById("grass-touching-form").innerHTML += ``; } @@ -76,15 +80,15 @@ function upload_error(error_message) { document.getElementById("submit").innerHTML = "Submit" document.getElementById("submit").disabled = false; - document.getElementById("grass-touching-form").innerHTML += `
${error_message}
`; - + document.getElementById("error-label").textContent = error_message; + document.getElementById("error-label").hidden = false; } function upload_file(file_type, file_content) { - username_input = document.getElementById("usernameinput").value + username_input = document.getElementById("usernameinput") fetch('{{ url_for("submit_challenge") }}', { method: "POST", - body: JSON.stringify({image_data: file_content, image_type: file_type, username: username_input}), + body: JSON.stringify({image_data: file_content, image_type: file_type, username: username_input.value}), headers: { "Content-type": "application/json" } @@ -131,7 +135,8 @@ function read_file () { file_type = "png"; } else { - document.getElementById("grass-touching-form").textContent += `
Only JPEG and PNG is supported.
`; + document.getElementById("error-label").textContent = "Only JPEG and PNG is supported." + document.getElementById("error-label").hidden = false; return; } diff --git a/templates/submit_grass_touching.jinja2 b/templates/submit_grass_touching.jinja2 new file mode 100644 index 0000000..5d969c9 --- /dev/null +++ b/templates/submit_grass_touching.jinja2 @@ -0,0 +1,177 @@ +{% extends "base.jinja2" %} +{% block title %}Grass Touching Register{% endblock %} +{% block body %} + + +
+
+
+

Grass touching submit

+
+ + + Dont worry! We wont tell your friends. +
+ +
+
+
+ + + +{% endblock %} \ No newline at end of file