From 52370eabfc8102c02f3e780928a73b984fa7e387 Mon Sep 17 00:00:00 2001 From: iou1name Date: Mon, 9 Jul 2018 07:45:35 -0400 Subject: [PATCH] strict rolls are enforced and dice call will close after rolls_taken is met --- database.py | 16 +++++++++++----- events.py | 7 ++++++- templates/quest.html | 8 ++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/database.py b/database.py index 4d239b4..63ca736 100644 --- a/database.py +++ b/database.py @@ -254,10 +254,16 @@ def insert_quest_roll(message_id, quest_id, post_id, roll_dice, roll_results): (message_id, quest_id, post_id, roll_dice, roll_results)) -def get_quest_rolls(quest_id): +def get_quest_rolls(quest_id=None, post_id=None): """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() + if quest_id: + sql = "SELECT * FROM `quest_rolls` WHERE `quest_id` = %s " + ins = quest_id + elif post_id: + sql = "SELECT * FROM `quest_rolls` WHERE `post_id` = %s " + ins = post_id + else: + return + sql += "ORDER BY `message_id` ASC" + data = _DB.execute(sql, (ins,)).fetchall() return data diff --git a/events.py b/events.py index 8879417..60d1161 100644 --- a/events.py +++ b/events.py @@ -69,10 +69,15 @@ def message(data): 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) + 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) + if len(db.get_quest_rolls(post_id=dice_call_id)) == dice_call[4]: + emit("close_dice_call", {"post_id": dice_call_id}, room=room) + room = data["room"] data = {} data["post"] = roll_msg + " (" + dice_roll + ")" @@ -103,7 +108,7 @@ def handle_dice(data): msg = f"Rolled {', '.join(map(str, dice))}" if diceMod: if diceMod > 0: - msg += " +" + str(diceMod) + msg += " + " + str(diceMod) else: msg += " - " + str(diceMod)[1:] msg += " = " + str(total) diff --git a/templates/quest.html b/templates/quest.html index d312029..9481c16 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -64,10 +64,14 @@ socket.on('close_dice_call', function(data) { let post = document.getElementById('questPostData-' + data.post_id); post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed'); + document.getElementById('close_dc-' + data.post_id).style.display = 'none'; + document.getElementById('open_dc-' + data.post_id).style.display = 'initial'; }); socket.on('open_dice_call', function(data) { let post = document.getElementById('questPostData-' + data.post_id); post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open'); + document.getElementById('close_dc-' + data.post_id).style.display = 'initial'; + document.getElementById('open_dc-' + data.post_id).style.display = 'none'; }); let mtarea = document.getElementById('messageTextArea'); mtarea.addEventListener('keypress', function(event) { @@ -127,13 +131,9 @@ 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 }}}); } function deactivate_post() {