added dice rolling in messages

This commit is contained in:
iou1name 2018-08-21 07:18:03 -04:00
parent 617f0b7a6d
commit 78820ac797

View File

@ -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 = "<br>".join(lines)
# quote links
quotes = re.findall(r"&gt;&gt;\d+", message)
for quote in quotes:
msg_id = quote.replace("&gt;&gt;", "")
@ -41,6 +45,32 @@ def message(socket, data):
msg += 'onmouseout="clearPreview()">' + quote + '</a>'
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 += '<hr class="msgSrvHr" /><b>' + roll_msg + "</b>"
user = socket.scope['user']
m = Message(