diff --git a/database.py b/database.py index e06e985..f063225 100644 --- a/database.py +++ b/database.py @@ -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( diff --git a/events.py b/events.py index 97f635f..d3df23e 100644 --- a/events.py +++ b/events.py @@ -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,27 +54,26 @@ def message(data): message = "
".join(lines) message = tools.handle_img(message) if message.startswith("/dice") or message.startswith("/roll"): - roll_msg = handle_dice(data) - message += '
' + roll_msg + roll_msg = handle_dice(message) + if roll_msg: + message += '
' + roll_msg data["message"] = message db.log_chat_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"] - post = bleach.clean(post.strip()) + if not internal: + post = bleach.clean(post.strip()) post = post.replace("\n", "
") 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("
", "
") 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"

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) diff --git a/templates/quest.html b/templates/quest.html index 05b8dea..aae691d 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -27,29 +27,18 @@ let qposts = document.getElementById('questPosts'); let post_str = '
' + strftime(data.date); {% if session.get("user_id") == owner_id %} + if (data.editable) { post_str += '
Edit'; post_str += ''; + } {% endif %} - post_str += '
' + data.post + '
'; + post_str += '
' + data.post + '

'; 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 = '
' + strftime(data.date) + "
"; - post_str += '
' - post_str += "

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 += "


"; - post_str += "
"; - - 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 @@