added user rolling support

This commit is contained in:
iou1name 2018-07-05 13:27:48 -04:00
parent ad4a9793bb
commit 8479ac8c3f

View File

@ -2,6 +2,7 @@
"""
SocketIO events.
"""
import re
import time
import random
@ -47,10 +48,45 @@ def message(data):
message = "<br />".join(lines)
message = tools.handle_img(message)
data["message"] = message
db.log_chat_message(data)
emit("message", data, room=room)
if message.startswith("/dice") or message.startswith("/roll"):
handle_dice(data)
def handle_dice(data):
"""
Handle /dice or /roll messages.
"""
room = data["room"]
reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", data['message'])
if not reg:
return
try:
diceNum, diceSides, diceMod = map(int, reg.groups())
assert 0 < diceNum < 100
assert 0 < diceSides < 100
assert -1000 < diceMod < 1000
except (ValueError, AssertionError):
return
dice = [random.randint(1, diceSides) for _ in range(diceNum)]
total = sum(dice)
if diceMod:
total += diceMod
msg = f"<b>Rolled {', '.join(map(str, dice))}"
if diceMod:
if diceMod > 0:
msg += " +"
msg += " " + str(diceMod)
msg += " = " + str(total) + "</b>"
date = int(time.time())
name = "Server"
data = {"message": msg, "name": name, "date": date, "room": room}
#emit("message", data, room=room)
message(data)
@socketio.on("new_post")
def new_post(data):