From 3a42f8dcc9c89b663457811fafa69ccffec9ea78 Mon Sep 17 00:00:00 2001 From: iou1name Date: Sun, 8 Jul 2018 17:50:54 -0400 Subject: [PATCH] qm can open and close dice calls --- anonkun.py | 10 -------- database.py | 9 +++---- events.py | 61 ++++++++++++++++++++++++++++++++++++-------- templates/quest.html | 51 +++++++++++++++++++++++++++++++++--- views.py | 38 ++++++++++++++------------- 5 files changed, 122 insertions(+), 47 deletions(-) diff --git a/anonkun.py b/anonkun.py index 06c296f..43730fe 100644 --- a/anonkun.py +++ b/anonkun.py @@ -84,16 +84,6 @@ 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/database.py b/database.py index 117eedc..4d239b4 100644 --- a/database.py +++ b/database.py @@ -233,16 +233,15 @@ def set_dice_call_open(post_id, quest_id, new_call=None): def set_dice_call_closed(quest_id): """Closes a quest's dice call.""" _DB.execute( - "UPDATE `quest_meta` SET `dice_call` = NULL, `dice_rolls` = NULL, " \ - "`dice_strct` = NULL WHERE `quest_id` = %s", + "UPDATE `quest_meta` SET `dice_call` = NULL WHERE `quest_id` = %s", (quest_id,)) -def get_dice_call(quest_id): +def get_dice_call(post_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() + "SELECT * FROM `quest_dice_calls` WHERE `post_id` = %s", + (post_id,)).fetchone() return data diff --git a/events.py b/events.py index 85a1c9f..651364e 100644 --- a/events.py +++ b/events.py @@ -59,24 +59,25 @@ def message(data): if message.startswith("/dice") or message.startswith("/roll"): roll_msg = handle_dice(data) if roll_msg: - data["message"] += '
' + roll_msg + data["message"] += '
' + roll_msg + "" 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_call_id = db.get_quest_meta(data["room"])[4] + if dice_call_id: + dice_call = db.get_dice_call(dice_call_id) dice_roll = re.search(r"(\d+d\d+(?:[+-]\d+)?)", message).group(1) - roll_results = re.search(r"Rolled (.+)", roll_msg).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) + message_id, room, dice_call_id, dice_roll, roll_results) room = data["room"] data = {} data["post"] = roll_msg + " (" + dice_roll + ")" data["post_type"] = "dice" - data["post_id"] = dice_call + data["post_id"] = dice_call_id emit("update_post", data, room=room) @@ -99,13 +100,13 @@ def handle_dice(data): total = sum(dice) if diceMod: total += diceMod - msg = f"Rolled {', '.join(map(str, dice))}" + msg = f"Rolled {', '.join(map(str, dice))}" if diceMod: if diceMod > 0: msg += " +" + str(diceMod) else: msg += " - " + str(diceMod)[1:] - msg += " = " + str(total) + "" + msg += " = " + str(total) return msg @@ -198,10 +199,9 @@ def dice_post(data): dice_roll += "+" dice_roll += str(diceMod) - post = "

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

" data = {} data["post"] = post @@ -213,3 +213,44 @@ def dice_post(data): new_call = (dice_roll, diceStrict, diceChal, diceRolls) db.set_dice_call_open(post_id, room, new_call) emit("new_post", data, room=room) + + +@socketio.on('close_dice_call') +def close_dice_call(data): + """ + Closes an active dice call. + """ + room = data["room"] + res = db.get_quest_meta(quest_id=room) + if not res: + return + if session.get("user_id") != res[3]: + return + + post_id = data.get("post_id") + db.set_dice_call_closed(room) + + data = {} + data["post_id"] = post_id + emit("close_dice_call", data, room=room) + + +@socketio.on("open_dice_call") +def open_dice_call(data): + """ + Opens a closed dice call. This is only permitted if the dice call is + the last post in the quest. + """ + room = data["room"] + res = db.get_quest_meta(quest_id=room) + if not res: + return + if session.get("user_id") != res[3]: + return + + post_id = data.get("post_id") + db.set_dice_call_open(post_id, room) + + data = {} + data["post_id"] = post_id + emit("open_dice_call", data, room=room) diff --git a/templates/quest.html b/templates/quest.html index b213e91..7c8ff45 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -31,8 +31,19 @@ post_str += '
Edit'; post_str += ''; } + else if (data.post_type == 'dice') { + post_str += '
Close'; + post_str += '' + } {% endif %} - post_str += '
' + data.post + '

'; + post_str += '
'; + if (data.post_type == 'text') { + post_str += data.post; + } + else if (data.post_type == 'dice') { + post_str += '

' + data.post + ' - Open

'; + } + post_str += '

'; qposts.innerHTML = qposts.innerHTML + post_str; }); socket.on('update_post', function(data) { @@ -45,6 +56,14 @@ post.innerHTML += '' + data.post + '
'; } }); + socket.on('close_dice_call', function(data) { + let post = document.getElementById('questPostData-' + data.post_id); + post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Open', 'Closed'); + }); + socket.on('open_dice_call', function(data) { + let post = document.getElementById('questPostData-' + data.post_id); + post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open'); + }); let mtarea = document.getElementById('messageTextArea'); mtarea.addEventListener('keypress', function(event) { if (event.key == 'Enter' && !event.shiftKey) { @@ -82,6 +101,7 @@ document.execCommand("defaultParagraphSeparator", false, "br"); document.getElementById('questPostData-' + post_id).style.border = '1px solid #ccc'; document.getElementById('savePost-' + post_id).style.display = 'initial'; + document.getElementById('editPost-' + post_id).style.display = 'none'; } function save_post(post_id) { document.getElementById('questPostData-' + post_id).contentEditable = 'false'; @@ -89,6 +109,7 @@ let text = document.getElementById('questPostData-' + post_id).innerHTML; socket.emit('update_post', {post_id: post_id, post: text, room: {{ room_id }}}); document.getElementById('savePost-' + post_id).style.display = 'none'; + document.getElementById('editPost-' + post_id).style.display = 'initial'; } function dice_post() { let formData = new FormData(document.getElementById('QMDicePostForm')); @@ -100,6 +121,16 @@ socket.emit('dice_post', obj); document.getElementById('QMDicePostForm').reset(); } + function close_dice_call(post_id) { + document.getElementById('close_dc-' + post_id).style.display = 'none'; + document.getElementById('open_dc-' + post_id).style.display = 'initial'; + socket.emit('close_dice_call', {post_id: post_id, room: {{ room_id }}}); + } + function open_dice_call(post_id) { + document.getElementById('close_dc-' + post_id).style.display = 'initial'; + document.getElementById('open_dc-' + post_id).style.display = 'none'; + socket.emit('open_dice_call', {post_id: post_id, room: {{ room_id }}}); + }