From 78820ac797476bbbe3fa5c45be18075547591d46 Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 21 Aug 2018 07:18:03 -0400 Subject: [PATCH] added dice rolling in messages --- quest/events.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/quest/events.py b/quest/events.py index eea96b3..77d9839 100644 --- a/quest/events.py +++ b/quest/events.py @@ -6,6 +6,7 @@ QuestConsumer object in consumers.py. import re import time import types +import random import bleach @@ -19,12 +20,14 @@ def message(socket, data): message = data.get('message') quest_id = data.get('quest_id') + # cleaning message = message.strip() if not message: return tags = ["b", "code", "i", "s"] message = bleach.clean(message, tags=tags) + # greentext lines = [] for line in message.splitlines(): if line.startswith(">") and not line.startswith(">>"): @@ -32,6 +35,7 @@ def message(socket, data): lines.append(line) message = "
".join(lines) + # quote links quotes = re.findall(r">>\d+", message) for quote in quotes: msg_id = quote.replace(">>", "") @@ -41,6 +45,32 @@ def message(socket, data): msg += 'onmouseout="clearPreview()">' + quote + '' message = message.replace(quote, msg) + # handle image + + # dice rolling + if any(map(message.startswith, ["/dice", "/roll"])): + reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", data["message"]) + if not reg: + return + try: + groups = [0 if d is None else int(d) for d in reg.groups()] + dice_num, dice_sides, dice_mod = groups + assert 0 < dice_num < 100 + assert 0 < dice_sides < 100 + assert -1000 < dice_mod < 1000 + except (ValueError, AssertionError): + return + dice = [random.randint(1, dice_sides) for _ in range(dice_num)] + total = sum(dice) + dice_mod + roll_msg = f"Rolled {', '.join(map(str, dice))}" + if dice_mod: + if dice_mod > 0: + roll_msg += " + " + str(dice_mod) + else: + roll_msg += " - " + str(dice_mod)[1:] + roll_msg += " = " + str(total) + message += '
' + roll_msg + "" + user = socket.scope['user'] m = Message(