From 051e7b920905bd805949b5f8198fc940fa2f0420 Mon Sep 17 00:00:00 2001 From: iou1name Date: Sat, 7 Jul 2018 13:43:58 -0400 Subject: [PATCH] dice rolls displayed on the dice call post --- anonkun.sql | 2 ++ database.py | 80 ++++++++++++++++++++++++++++++-------------- events.py | 45 ++++++++++++++++++------- templates/quest.html | 8 +++-- views.py | 7 ++-- 5 files changed, 99 insertions(+), 43 deletions(-) diff --git a/anonkun.sql b/anonkun.sql index 613b23a..60067e4 100644 --- a/anonkun.sql +++ b/anonkun.sql @@ -11,12 +11,14 @@ CREATE TABLE `quest_meta` ( `canon_title` VARCHAR(300) DEFAULT NULL, `ident_title` VARCHAR(300) DEFAULT NULL, `owner_id` SMALLINT UNSIGNED DEFAULT NULL, + `dice_call` SMALLINT UNSIGNED DEFAULT NULL, PRIMARY KEY (`quest_id`) ) ENGINE=InnoDB CHARSET utf8mb4; CREATE TABLE `quest_data` ( `post_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, `quest_id` SMALLINT UNSIGNED NOT NULL, + `post_type` ENUM('text', 'dice', 'poll') NOT NULL, `post` MEDIUMTEXT NOT NULL, `timestamp` INT UNSIGNED NOT NULL, PRIMARY KEY (`post_id`) diff --git a/database.py b/database.py index f063225..ff712f2 100644 --- a/database.py +++ b/database.py @@ -108,9 +108,7 @@ def log_chat_message(data): def get_chat_messages(room_id): - """ - Retrieves all chat messages for the provided room_id. - """ + """Retrieves all chat messages for the provided room_id.""" res = _DB.execute( "SELECT * FROM `chat_messages` WHERE `room_id` = %s " \ + "ORDER BY `date` ASC", (room_id,)).fetchall() @@ -118,9 +116,7 @@ def get_chat_messages(room_id): def insert_quest(canon_title, ident_title, owner_id): - """ - Creates a new quest entry. - """ + """Creates a new quest entry.""" _DB.execute( "INSERT INTO `quest_meta` (`canon_title`, `ident_title`, `owner_id`) "\ + "VALUES (%s, %s, %s)", (canon_title, ident_title, owner_id)) @@ -130,13 +126,12 @@ def insert_quest(canon_title, ident_title, owner_id): return quest_id -def insert_quest_post(quest_id, post, timestamp): - """ - Insers a new quest post. - """ +def insert_quest_post(quest_id, post_type, post, timestamp): + """Insers a new quest post.""" _DB.execute( - "INSERT INTO `quest_data` (`quest_id`, `post`, `timestamp`) " \ - + "VALUES (%s, %s, %s)", (quest_id, post, timestamp)) + "INSERT INTO `quest_data`" \ + + "(`quest_id`, `post_type`, `post`, `timestamp`) " \ + + "VALUES (%s, %s, %s, %s)", (quest_id, post_type, post, timestamp)) post_id = _DB.execute( "SELECT `post_id` FROM `quest_data` WHERE `quest_id` = %s " \ + "ORDER BY `post_id` DESC", (quest_id,)).fetchone()[0] @@ -161,18 +156,35 @@ def get_quest_meta(quest_id=None, ident_title=None): def get_quest_data(quest_id): - """ - Retrieves all quest posts. - """ - data = _DB.execute("SELECT * FROM `quest_data` WHERE `quest_id` = %s", + """Retrieves all quest posts.""" + data = _DB.execute( + "SELECT * FROM `quest_data` WHERE `quest_id` = %s " \ + + "ORDER BY `post_id` ASC", (quest_id,)).fetchall() return data +def get_quest_post(post_id=None, quest_id=None): + """ + Retrieves the post data for the given post_id. If no post_id is given, + it returns the most recent post. + """ + if post_id: + data = _DB.execute( + "SELECT * FROM `quest_data` WHERE `post_id` = %s", + (post_id,)).fetchone() + elif quest_id: + data = _DB.execute( + "SELECT * FROM `quest_data` WHERE `quest_id` = %s "\ + + "ORDER BY `post_id` DESC", + (quest_id,)).fetchone() + else: + return + return data + + def get_user_info(username): - """ - Retrives relevant user data. - """ + """Retrives relevant user data.""" data = _DB.execute( "SELECT `user_id`, `signup_date` FROM `users` WHERE `username` = %s", (username,)).fetchone() @@ -180,9 +192,7 @@ def get_user_info(username): def get_user_quests(user_id): - """ - Retrieves all quests ran by a particular user_id. - """ + """Retrieves all quests ran by a particular user_id.""" data = _DB.execute( "SELECT * FROM `quest_meta` WHERE `owner_id` = %s", (user_id,)).fetchall() @@ -190,9 +200,29 @@ def get_user_quests(user_id): def update_quest_post(post_id, new_post): - """ - Updates a quest post. - """ + """Updates a quest post.""" _DB.execute( "UPDATE `quest_data` SET `post` = %s WHERE `post_id` = %s", (new_post, post_id)) + + +def set_dice_call_open(quest_id, post_id): + """Sets an open dice call for the given quest.""" + _DB.execute( + "UPDATE `quest_meta` SET `dice_call` = %s WHERE `quest_id` = %s", + (post_id, quest_id)) + + +def set_dice_call_closed(quest_id): + """Closes a quest's dice call.""" + _DB.execute( + "UPDATE `quest_meta` SET `dice_call` = NULL WHERE `quest_id` = %s", + (quest_id,)) + + +def get_dice_call(quest_id): + """Retrives the currently open dice call, if there is one.""" + data = _DB.execute( + "SELECT `dice_call` FROM `quest_meta` WHERE `quest_id` = %s", + (quest_id,)).fetchone() + return data diff --git a/events.py b/events.py index d3df23e..b224ab3 100644 --- a/events.py +++ b/events.py @@ -53,22 +53,21 @@ def message(data): lines.append(line) message = "
".join(lines) message = tools.handle_img(message) - if message.startswith("/dice") or message.startswith("/roll"): - roll_msg = handle_dice(message) - if roll_msg: - message += '
' + roll_msg - data["message"] = message + if message.startswith("/dice") or message.startswith("/roll"): + roll_msg = handle_dice(data) + if roll_msg: + data["message"] += '
' + roll_msg db.log_chat_message(data) emit("message", data, room=room) -def handle_dice(message): +def handle_dice(data): """ Handle /dice or /roll messages. """ - reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", message) + reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", data["message"]) if not reg: return try: @@ -89,6 +88,19 @@ def handle_dice(message): msg += " +" msg += " " + str(diceMod) msg += " = " + str(total) + "" + + dice_call = db.get_dice_call(data["room"]) + if dice_call: + post = db.get_quest_post(post_id=dice_call)[3] + post += "
" + msg + db.update_quest_post(dice_call, post) + + room = data["room"] + data = {} + data["post"] = post + data["post_id"] = dice_call + emit("update_post", data, room=room) + return msg @@ -105,18 +117,16 @@ def new_post(data, internal=False): return post = data["post"] - if not internal: - post = bleach.clean(post.strip()) + post = bleach.clean(post.strip()) post = post.replace("\n", "
") post = tools.handle_img(post) - editable = data.get("editable", True) data = {} data["post"] = [post] + data["post_type"] = "text" data["date"] = int(time.time()) - post_id = db.insert_quest_post(room, post, data["date"]) + post_id = db.insert_quest_post(room, "text", post, data["date"]) data["post_id"] = post_id - data["editable"] = editable # TODO: enforce this on server-side emit("new_post", data, room=room) @@ -181,5 +191,14 @@ def dice_post(data): post += str(diceMod) if diceChal: post += " vs DC" + str(diceChal) + post += "" - new_post({"post": post, "room": room, "editable": False}, internal=True) + data = {} + data["post"] = post + data["post_type"] = "dice" + data["date"] = int(time.time()) + post_id = db.insert_quest_post(room, "dice", post, data["date"]) + data["post_id"] = post_id + + db.set_dice_call_open(room, post_id) + emit("new_post", data, room=room) diff --git a/templates/quest.html b/templates/quest.html index aae691d..57ad415 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -27,7 +27,7 @@ let qposts = document.getElementById('questPosts'); let post_str = '
' + strftime(data.date); {% if session.get("user_id") == owner_id %} - if (data.editable) { + if (data.post_type == 'text') { post_str += '
Edit'; post_str += ''; } @@ -128,15 +128,17 @@ {% for quest_post in quest_posts %}
- {{ quest_post[3] | strftime }} + {{ quest_post[4] | strftime }} {% if session.get("user_id") == owner_id %} + {% if quest_post[2] == "text" %}
Edit {% endif %} + {% endif %}
{% autoescape false %} - {{ quest_post[2] }} + {{ quest_post[3] }} {% endautoescape %}

diff --git a/views.py b/views.py index ebeae4b..7d992a2 100644 --- a/views.py +++ b/views.py @@ -51,7 +51,9 @@ def quest(quest_title): data = db.get_quest_meta(ident_title=ident_title) if not data: abort(404) - quest_id, quest_title, _, owner_id = data + quest_id = data[0] + quest_title = data[1] + owner_id = data[3] quest_posts = db.get_quest_data(quest_id) messages = db.get_chat_messages(quest_id) return render_template('quest.html', @@ -112,7 +114,8 @@ def edit_quest(quest_title): data = db.get_quest_meta(ident_title=ident_title) if not data: abort(404) - quest_id, quest_title, _, owner_id = data + quest_id = data[0] + quest_title = data[1] if request.method == "GET": return render_template("edit_quest.html", quest_title=quest_title,