qm dice call updates page properly, fix rolling bug

This commit is contained in:
iou1name 2018-07-06 02:09:55 -04:00
parent 75a62ea7e8
commit da36e2ac1e
3 changed files with 55 additions and 37 deletions

View File

@ -98,7 +98,7 @@ def log_chat_message(data):
"""
message = data["message"]
date = data["date"]
room_id = int(data["room"])
room_id = data["room"]
name = data["name"]
user_id = data.get("user_id")
_DB.execute(

View File

@ -29,11 +29,17 @@ def message(data):
"""
Sent by a client when the user entered a new message.
"""
room = data["room"]
room = int(data["room"])
message = data["message"]
name = data["name"]
user_id = data.get("user_id")
data = {}
date = int(time.time())
data["date"] = date
data["name"] = name
data["user_id"] = user_id
data["room"] = room
message = message.strip()
if not message:
@ -48,7 +54,8 @@ def message(data):
message = "<br />".join(lines)
message = tools.handle_img(message)
if message.startswith("/dice") or message.startswith("/roll"):
roll_msg = handle_dice(data)
roll_msg = handle_dice(message)
if roll_msg:
message += '<hr class="msgSrvHr" />' + roll_msg
data["message"] = message
@ -57,18 +64,16 @@ def message(data):
emit("message", data, room=room)
def handle_dice(message):
"""
Handle /dice or /roll messages.
"""
room = data["room"]
reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", message)
if not reg:
return
try:
diceNum, diceSides, diceMod = map(int, reg.groups())
groups = [0 if d is None else int(d) for d in reg.groups()]
diceNum, diceSides, diceMod = groups
assert 0 < diceNum < 100
assert 0 < diceSides < 100
assert -1000 < diceMod < 1000
@ -88,7 +93,7 @@ def handle_dice(message):
@socketio.on("new_post")
def new_post(data):
def new_post(data, internal=False):
"""
Called when the QM makes a new post.
"""
@ -100,13 +105,18 @@ def new_post(data):
return
post = data["post"]
if not internal:
post = bleach.clean(post.strip())
post = post.replace("\n", "<br />")
post = tools.handle_img(post)
editable = data.get("editable", True)
data = {}
data["post"] = [post]
data["date"] = int(time.time())
post_id = db.insert_quest_post(room, post, data["date"])
data["post_id"] = post_id
data["editable"] = editable # TODO: enforce this on server-side
emit("new_post", data, room=room)
@ -126,19 +136,21 @@ def update_post(data):
post = data["post"]
post = post.strip().replace("<br>", "<br />")
post = tools.handle_img(post)
data["post"] = post
post_id = data["post_id"]
db.update_quest_post(post_id, post)
data = {}
data["post"] = post
data["post_id"] = post_id
emit("update_post", data, room=room)
@socketio.on("dice_post")
def dice_(data):
def dice_post(data):
"""
Called when the QM posts a new dice call.
"""
print(data)
room = data["room"]
res = db.get_quest_meta(quest_id=room)
if not res:
@ -146,8 +158,28 @@ def dice_(data):
if session.get("user_id") != res[3]:
return
date = int(time.time())
data["date"] = date
data["post_id"] = 0
data = {k: v for k, v in data.items() if v}
try:
diceNum = int(data.get("diceNum", 0))
diceSides = int(data.get("diceSides", 0))
diceMod = int(data.get("diceMod", 0))
diceChal = int(data.get("diceChal", 0))
diceRolls = int(data.get("diceRolls", 0))
emit("dice_post", data, room=room)
assert 0 < diceNum < 100
assert 0 < diceSides < 100
assert -1000 < diceMod < 1000
assert 0 <= diceChal < 100
assert 0 <= diceRolls < 100
except (ValueError, AssertionError):
return
post = f"<h3>Roll {data['diceNum']}d{data['diceSides']}"
if diceMod:
if diceMod > 0:
post += "+"
post += str(diceMod)
if diceChal:
post += " vs DC" + str(diceChal)
new_post({"post": post, "room": room, "editable": False}, internal=True)

View File

@ -27,29 +27,18 @@
let qposts = document.getElementById('questPosts');
let post_str = '<div class="questPost"><div class="questPostMeta">' + strftime(data.date);
{% if session.get("user_id") == owner_id %}
if (data.editable) {
post_str += '<br /><a href="javascript:void(0);" onclick="edit_post(\'' + data.post_id + '\')">Edit</a>';
post_str += '<a href="javascript:void(0);" id="savePost-' + data.post_id + '" onclick="save_post(\'' + data.post_id + '\')" style="display:none;">Save</a>';
}
{% endif %}
post_str += '</div><div class="questPostData" id="questPostData-' + data.post_id + '">' + data.post + '</div></div>';
post_str += '</div><div class="questPostData" id="questPostData-' + data.post_id + '">' + data.post + '</div></div><br />';
qposts.innerHTML = qposts.innerHTML + post_str;
});
socket.on('update_post', function(data) {
let post_id = data.post_id;
document.getElementById('questPostData-' + post_id).innerHTML = data.post;
});
socket.on('dice_post', function(data) {
let qposts = document.getElementById('questPosts');
let post_str = '<div class="questPost"><div class="questPostMeta">' + strftime(data.date) + "</div>";
post_str += '<div class="questPostData" id="questPostData-' + data.post_id + '">'
post_str += "<h3>Roll " + data.diceNum + "d" + data.diceSides;
if (data.diceMod > 0) { post_str += "+"; }
if (data.diceMod != 0) { post_str += data.diceMod; }
if (data.diceChal) { post_str += " vs DC" + data.diceChal; }
post_str += "</h3><br />";
post_str += "</div></div>";
qposts.innerHTML = qposts.innerHTML + post_str;
});
let mtarea = document.getElementById('messageTextArea');
mtarea.addEventListener('keypress', function(event) {
if (event.key == 'Enter' && !event.shiftKey) {
@ -169,8 +158,8 @@
<div id="QMPostDice" class="QMPostTabContent" style="display:none;">
Dice for the dice god.<br />
<form id="QMDicePostForm" action="javascript:void(0);" onsubmit="dice_post();">
Dice: <input type="number" name="diceNum" min="1" max="99"/>
d <input type="number" name="diceSides" min="1" max="99"/>
Dice: <input type="number" name="diceNum" min="1" max="99" required/>
d <input type="number" name="diceSides" min="1" max="99" required/>
±<input type="number" name="diceMod" min="-999" max="999"/>
<input type="checkbox" name="diceStrict"/>
<span class="tooltip" title="Only take matching rolls.">Strict</span><br />
@ -180,9 +169,6 @@
<input type="checkbox" onclick="document.getElementById('diceRolls').disabled=!this.checked;"/>
<span class="tooltip" title="Automatically close the dice call after this many rolls have been made.">Rolls Taken:</span>
<input type="number" name="diceRolls" id="diceRolls" min="1" max="99" disabled/><br />
<input type="checkbox" onclick="document.getElementById('diceBest').disabled=!this.checked;"/>
<span class="tooltip" title="Pick out the N highest rolls after the dice call has been closed. This should be lower than the number of rolls taken.">Best:</span>
<input type="number" name="diceBest" id="diceBest" min="1" max="99" disabled/><br />
<input type="submit" name="submit" value="Roll 'em"/>
</form>
</div>