diff --git a/anonkun.py b/anonkun.py index 43730fe..06c296f 100644 --- a/anonkun.py +++ b/anonkun.py @@ -84,6 +84,16 @@ def unix2string(unix): return time.strftime(form, t) +@app.template_filter("filter_rolls") +def filter_rolls(quest_rolls, post_id): + """ + Filters quest's rolls and returns only the rolls applicable to the + given post_id. + """ + quest_rolls = [roll for roll in quest_rolls if roll[2] == post_id] + return quest_rolls + + init() if __name__ == "__main__": app.run(host='0.0.0.0', port=5050) diff --git a/anonkun.sql b/anonkun.sql index 60067e4..d2f05cc 100644 --- a/anonkun.sql +++ b/anonkun.sql @@ -16,7 +16,7 @@ CREATE TABLE `quest_meta` ( ) ENGINE=InnoDB CHARSET utf8mb4; CREATE TABLE `quest_data` ( - `post_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, + `post_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, `quest_id` SMALLINT UNSIGNED NOT NULL, `post_type` ENUM('text', 'dice', 'poll') NOT NULL, `post` MEDIUMTEXT NOT NULL, @@ -24,10 +24,29 @@ CREATE TABLE `quest_data` ( PRIMARY KEY (`post_id`) ) ENGINE=InnoDB CHARSET utf8mb4; +CREATE TABLE `quest_dice_calls` ( + `post_id` MEDIUMINT UNSIGNED NOT NULL, + `dice_roll` TEXT NOT NULL, + `strict` BOOLEAN DEFAULT FALSE, + `dice_challenge` SMALLINT UNSIGNED, + `rolls_taken` TINYINT UNSIGNED, + PRIMARY KEY (`post_id`) +) ENGINE=InnoDB CHARSET utf8mb4; + +CREATE TABLE `quest_rolls` ( + `message_id` MEDIUMINT UNSIGNED NOT NULL, + `quest_id` SMALLINT UNSIGNED NOT NULL, + `post_id` MEDIUMINT UNSIGNED NOT NULL, + `roll_dice` TEXT NOT NULL, + `roll_results` TEXT NOT NULL, + PRIMARY KEY (`message_id`) +) ENGINE=InnoDB CHARSET utf8mb4; + CREATE TABLE `chat_messages` ( - `room_id` SMALLINT UNSIGNED NOT NULL, - `name` VARCHAR(20) NOT NULL, + `message_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, + `room_id` MEDIUMINT UNSIGNED NOT NULL, `name_id` SMALLINT UNSIGNED DEFAULT NULL, `date` INT UNSIGNED NOT NULL, - `message` TEXT NOT NULL + `message` TEXT NOT NULL, + PRIMARY KEY (`message_id`) ) ENGINE=InnoDB CHARSET utf8mb4; diff --git a/database.py b/database.py index ff712f2..117eedc 100644 --- a/database.py +++ b/database.py @@ -99,12 +99,18 @@ def log_chat_message(data): message = data["message"] date = data["date"] room_id = data["room"] - name = data["name"] user_id = data.get("user_id") _DB.execute( "INSERT INTO `chat_messages` (" \ - + "`message`, `room_id`, `date`, `name`, `name_id`) VALUES (" \ - + "%s, %s, %s, %s, %s)", (message, room_id, date, name, user_id)) + + "`message`, `room_id`, `date`, `name_id`) VALUES (" \ + + "%s, %s, %s, %s)", + (message, room_id, date, user_id)) + + post_id = _DB.execute( + "SELECT `message_id` FROM `chat_messages` WHERE `room_id` = %s " \ + + "ORDER BY `message_id` DESC", + (room_id,)).fetchone()[0] + return post_id def get_chat_messages(room_id): @@ -120,8 +126,10 @@ def insert_quest(canon_title, ident_title, owner_id): _DB.execute( "INSERT INTO `quest_meta` (`canon_title`, `ident_title`, `owner_id`) "\ + "VALUES (%s, %s, %s)", (canon_title, ident_title, owner_id)) + quest_id = _DB.execute( - "SELECT `quest_id` FROM `quest_meta` WHERE `ident_title` = %s", + "SELECT `quest_id` FROM `quest_meta` WHERE `ident_title` = %s" \ + + "ORDER BY `quest_id` DESC", (ident_title,)).fetchone()[0] return quest_id @@ -206,17 +214,27 @@ def update_quest_post(post_id, new_post): (new_post, post_id)) -def set_dice_call_open(quest_id, post_id): +def set_dice_call_open(post_id, quest_id, new_call=None): """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)) + if new_call: + dice_roll, strict, dice_challence, rolls_taken = new_call + _DB.execute( + "INSERT INTO `quest_dice_calls`" \ + + "(`post_id`, `dice_roll`, `strict`, " \ + + "`dice_challenge`, `rolls_taken`)" \ + + "VALUES (%s, %s, %s, %s, %s)", + (post_id, dice_roll, strict, dice_challence, rolls_taken)) + 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", + "UPDATE `quest_meta` SET `dice_call` = NULL, `dice_rolls` = NULL, " \ + "`dice_strct` = NULL WHERE `quest_id` = %s", (quest_id,)) @@ -226,3 +244,21 @@ def get_dice_call(quest_id): "SELECT `dice_call` FROM `quest_meta` WHERE `quest_id` = %s", (quest_id,)).fetchone() return data + + +def insert_quest_roll(message_id, quest_id, post_id, roll_dice, roll_results): + """Inserts a user roll into the `quest_rolls` table.""" + _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)) + + +def get_quest_rolls(quest_id): + """Gets all rolls for the given quest.""" + data = _DB.execute( + "SELECT * FROM `quest_rolls` WHERE `quest_id` = %s " \ + + "ORDER BY `message_id` ASC", + (quest_id,)).fetchall() + return data diff --git a/events.py b/events.py index b224ab3..85a1c9f 100644 --- a/events.py +++ b/events.py @@ -54,14 +54,31 @@ def message(data): message = "
".join(lines) message = tools.handle_img(message) data["message"] = message + + roll_msg = "" 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) + message_id = db.log_chat_message(data) emit("message", data, room=room) + if roll_msg: + dice_call = db.get_dice_call(data["room"])[0] + if dice_call: + dice_roll = re.search(r"(\d+d\d+(?:[+-]\d+)?)", message).group(1) + roll_results = re.search(r"Rolled (.+)", roll_msg).group(1) + db.insert_quest_roll( + message_id, room, dice_call, dice_roll, roll_results) + + room = data["room"] + data = {} + data["post"] = roll_msg + " (" + dice_roll + ")" + data["post_type"] = "dice" + data["post_id"] = dice_call + emit("update_post", data, room=room) + def handle_dice(data): """ @@ -85,22 +102,10 @@ def handle_dice(data): msg = f"Rolled {', '.join(map(str, dice))}" if diceMod: if diceMod > 0: - msg += " +" - msg += " " + str(diceMod) + msg += " +" + str(diceMod) + else: + msg += " - " + str(diceMod)[1:] 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 @@ -143,6 +148,7 @@ def update_post(data): if session.get("user_id") != res[3]: return + # TODO: enforce only update text posts post = data["post"] post = post.strip().replace("
", "
") post = tools.handle_img(post) @@ -153,6 +159,7 @@ def update_post(data): data = {} data["post"] = post data["post_id"] = post_id + data["post_type"] = "text" emit("update_post", data, room=room) @@ -183,12 +190,15 @@ def dice_post(data): assert 0 <= diceRolls < 100 except (ValueError, AssertionError): return + diceStrict = bool(data.get("diceStrict")) - post = f"

Roll {data['diceNum']}d{data['diceSides']}" + dice_roll = f"{data['diceNum']}d{data['diceSides']}" if diceMod: if diceMod > 0: - post += "+" - post += str(diceMod) + dice_roll += "+" + dice_roll += str(diceMod) + + post = "

Roll " + dice_roll if diceChal: post += " vs DC" + str(diceChal) post += "

" @@ -200,5 +210,6 @@ def dice_post(data): post_id = db.insert_quest_post(room, "dice", post, data["date"]) data["post_id"] = post_id - db.set_dice_call_open(room, post_id) + new_call = (dice_roll, diceStrict, diceChal, diceRolls) + db.set_dice_call_open(post_id, room, new_call) emit("new_post", data, room=room) diff --git a/static/anonkun.css b/static/anonkun.css index 98cde1f..adad0ac 100644 --- a/static/anonkun.css +++ b/static/anonkun.css @@ -4,7 +4,8 @@ img { } h3 { - margin: 0px; + margin-top: 0px; + margin-bottom: 0.5em; } .header { diff --git a/templates/quest.html b/templates/quest.html index 57ad415..b213e91 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -37,7 +37,13 @@ }); socket.on('update_post', function(data) { let post_id = data.post_id; - document.getElementById('questPostData-' + post_id).innerHTML = data.post; + let post = document.getElementById('questPostData-' + post_id); + if (data.post_type == 'text') { + post.innerHTML = data.post; + } + else if (data.post_type == 'dice') { + post.innerHTML += '' + data.post + '
'; + } }); let mtarea = document.getElementById('messageTextArea'); mtarea.addEventListener('keypress', function(event) { @@ -140,6 +146,13 @@ {% autoescape false %} {{ quest_post[3] }} {% endautoescape %} + {% if quest_post[2] == "dice" %} + {% for quest_roll in quest_rolls %} + {% if quest_roll[2] == quest_post[0] %} +
Rolled {{ quest_roll[4] }} ({{ quest_roll[3] }})
+ {% endif %} + {% endfor %} + {% endif %}
{% endfor %} @@ -189,7 +202,7 @@ {% for message in messages %}
- {{ message[1] }} {{ message[3] | strftime }} + Anonymous {{ message[3] | strftime }}
{{ message[4] }}
diff --git a/views.py b/views.py index 7d992a2..13c0925 100644 --- a/views.py +++ b/views.py @@ -55,11 +55,13 @@ def quest(quest_title): quest_title = data[1] owner_id = data[3] quest_posts = db.get_quest_data(quest_id) + quest_rolls = db.get_quest_rolls(quest_id) messages = db.get_chat_messages(quest_id) return render_template('quest.html', quest_title=quest_title, ident_title=ident_title, quest_posts=quest_posts, + quest_rolls=quest_rolls, owner_id=owner_id, room_id=quest_id, messages=messages) @@ -100,7 +102,7 @@ def create_quest(): timestamp = int(time.time()) quest_id = db.insert_quest(canon_title, ident_title, owner_id) - db.insert_quest_post(quest_id, quest_body, timestamp) + db.insert_quest_post(quest_id, "text", quest_body, timestamp) return redirect(url_for('.quest', quest_title=ident_title))