diff --git a/anonkun.py b/anonkun.py index 43730fe..c12d072 100644 --- a/anonkun.py +++ b/anonkun.py @@ -10,8 +10,9 @@ from flask_paranoid import Paranoid from flask_session import Session from flask_socketio import SocketIO -from events import socketio +import database as db from views import views +from events import socketio class ReverseProxied(object): """ @@ -84,6 +85,14 @@ def unix2string(unix): return time.strftime(form, t) +@app.template_filter("dice_chal") +def get_dice_challenge(post_id): + """ + Returns the dice challenge for the given post_id. + """ + return db.get_dice_call(post_id)[3] + + init() if __name__ == "__main__": app.run(host='0.0.0.0', port=5050) diff --git a/anonkun.sql b/anonkun.sql index d2f05cc..e5d89f9 100644 --- a/anonkun.sql +++ b/anonkun.sql @@ -39,6 +39,7 @@ CREATE TABLE `quest_rolls` ( `post_id` MEDIUMINT UNSIGNED NOT NULL, `roll_dice` TEXT NOT NULL, `roll_results` TEXT NOT NULL, + `roll_total` SMALLINT UNSIGNED NOT NULL PRIMARY KEY (`message_id`) ) ENGINE=InnoDB CHARSET utf8mb4; diff --git a/database.py b/database.py index 63ca736..5368ca5 100644 --- a/database.py +++ b/database.py @@ -245,13 +245,15 @@ def get_dice_call(post_id): return data -def insert_quest_roll(message_id, quest_id, post_id, roll_dice, roll_results): +def insert_quest_roll(message_id, quest_id, post_id, roll_data): """Inserts a user roll into the `quest_rolls` table.""" + ins = (message_id, quest_id, post_id) + roll_data _DB.execute( "INSERT INTO `quest_rolls`" \ - + "(`message_id`, `quest_id`, `post_id`, `roll_dice`, `roll_results`)"\ - + "VALUES (%s, %s, %s, %s, %s)", - (message_id, quest_id, post_id, roll_dice, roll_results)) + + "(`message_id`, `quest_id`, `post_id`, " \ + + "`roll_dice`, `roll_results`, `roll_total`)" \ + + "VALUES (%s, %s, %s, %s, %s, %s)", + (ins)) def get_quest_rolls(quest_id=None, post_id=None): diff --git a/events.py b/events.py index 60d1161..2fb7231 100644 --- a/events.py +++ b/events.py @@ -71,16 +71,23 @@ def message(data): dice_roll = re.search(r"(\d+d\d+(?:[+-]\d+)?)", message).group(1) if dice_call[2] and dice_roll != dice_call[1]: return - roll_results = re.search(r"Rolled (.+)$", roll_msg).group(1) - db.insert_quest_roll( - message_id, room, dice_call_id, dice_roll, roll_results) + roll_results = re.search(r"Rolled (.+) =", roll_msg).group(1) + roll_total = int(re.search(r"= (\d+)$", roll_msg).group(1)) + roll_data = (dice_roll, roll_results, roll_total) + db.insert_quest_roll(message_id, room, dice_call_id, roll_data) if len(db.get_quest_rolls(post_id=dice_call_id)) == dice_call[4]: + db.set_dice_call_closed(room) emit("close_dice_call", {"post_id": dice_call_id}, room=room) room = data["room"] data = {} data["post"] = roll_msg + " (" + dice_roll + ")" + if dice_call[3]: + if roll_total >= dice_call[3]: + data["post"] += " - Pass" + else: + data["post"] += " - Fail" data["post_type"] = "dice" data["post_id"] = dice_call_id emit("update_post", data, room=room) diff --git a/templates/quest.html b/templates/quest.html index 9481c16..a3981d4 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -207,11 +207,11 @@ {{ quest_post[3] }} {% endautoescape %} {% elif quest_post[2] == "dice" %} -

{{ quest_post[3] }} - {% if quest_post[0] == dice_call %}Open{% else %}Closed{% endif %}

+

{{ quest_post[3] }} - {% if quest_post[0] == dice_call_id %}Open{% else %}Closed{% endif %}

{% for quest_roll in quest_rolls %} {% if quest_roll[2] == quest_post[0] %}
- Rolled {{ quest_roll[4] }} ({{ quest_roll[3] }}) + Rolled {{ quest_roll[4] }} = {{ quest_roll[5] }} ({{ quest_roll[3] }}){% if quest_post[0]|dice_chal != 0 %} - {% if quest_roll[5] >= quest_post[0]|dice_chal %}Pass{% else %} Fail{% endif %}{% endif %}
{% endif %} {% endfor %} diff --git a/views.py b/views.py index f292217..9e6c2df 100644 --- a/views.py +++ b/views.py @@ -54,7 +54,7 @@ def quest(quest_title): quest_id = data[0] quest_title = data[1] owner_id = data[3] - dice_call = data[4] + dice_call_id = data[4] quest_posts = db.get_quest_data(quest_id) quest_rolls = db.get_quest_rolls(quest_id) messages = db.get_chat_messages(quest_id) @@ -64,7 +64,7 @@ def quest(quest_title): quest_posts=quest_posts, quest_rolls=quest_rolls, owner_id=owner_id, - dice_call=dice_call, + dice_call_id=dice_call_id, room_id=quest_id, messages=messages)