2018-06-16 18:52:46 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
SocketIO events.
|
|
|
|
"""
|
2018-07-05 13:27:48 -04:00
|
|
|
import re
|
2018-06-16 18:52:46 -04:00
|
|
|
import time
|
2018-07-02 16:10:50 -04:00
|
|
|
import random
|
2018-06-16 18:52:46 -04:00
|
|
|
|
2018-06-17 17:49:49 -04:00
|
|
|
import bleach
|
2018-06-21 20:44:43 -04:00
|
|
|
from flask import session
|
2018-06-16 18:52:46 -04:00
|
|
|
from flask_socketio import SocketIO, emit, join_room
|
|
|
|
|
2018-06-25 15:52:10 -04:00
|
|
|
import tools
|
2018-07-02 16:10:50 -04:00
|
|
|
import database as db
|
2018-06-17 17:49:49 -04:00
|
|
|
|
2018-06-16 18:52:46 -04:00
|
|
|
socketio = SocketIO()
|
|
|
|
|
2018-06-20 13:07:11 -04:00
|
|
|
@socketio.on('joined')
|
2018-06-16 18:52:46 -04:00
|
|
|
def joined(data):
|
|
|
|
"""
|
|
|
|
Sent by clients when they enter a room.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
join_room(room)
|
|
|
|
|
|
|
|
|
2018-06-20 13:07:11 -04:00
|
|
|
@socketio.on('message')
|
|
|
|
def message(data):
|
2018-06-16 18:52:46 -04:00
|
|
|
"""
|
|
|
|
Sent by a client when the user entered a new message.
|
|
|
|
"""
|
2018-07-06 02:09:55 -04:00
|
|
|
room = int(data["room"])
|
2018-06-16 18:52:46 -04:00
|
|
|
message = data["message"]
|
2018-07-06 02:09:55 -04:00
|
|
|
name = data["name"]
|
|
|
|
user_id = data.get("user_id")
|
|
|
|
|
|
|
|
data = {}
|
2018-06-16 18:52:46 -04:00
|
|
|
date = int(time.time())
|
|
|
|
data["date"] = date
|
2018-07-06 02:09:55 -04:00
|
|
|
data["name"] = name
|
|
|
|
data["user_id"] = user_id
|
|
|
|
data["room"] = room
|
2018-06-17 17:49:49 -04:00
|
|
|
|
|
|
|
message = message.strip()
|
|
|
|
if not message:
|
|
|
|
return
|
|
|
|
tags = ["b", "code", "i", "s"]
|
|
|
|
message = bleach.clean(message, tags=tags)
|
|
|
|
lines = []
|
|
|
|
for line in message.splitlines():
|
|
|
|
if line.startswith(">"):
|
|
|
|
line = '<span class="greenText">' + line + '</span>'
|
|
|
|
lines.append(line)
|
|
|
|
message = "<br />".join(lines)
|
2018-06-25 15:52:10 -04:00
|
|
|
message = tools.handle_img(message)
|
2018-07-05 18:17:36 -04:00
|
|
|
if message.startswith("/dice") or message.startswith("/roll"):
|
2018-07-06 02:09:55 -04:00
|
|
|
roll_msg = handle_dice(message)
|
|
|
|
if roll_msg:
|
|
|
|
message += '<hr class="msgSrvHr" />' + roll_msg
|
2018-07-05 18:17:36 -04:00
|
|
|
|
2018-06-17 17:49:49 -04:00
|
|
|
data["message"] = message
|
2018-07-05 13:27:48 -04:00
|
|
|
|
2018-06-17 21:10:41 -04:00
|
|
|
db.log_chat_message(data)
|
2018-06-17 17:49:49 -04:00
|
|
|
emit("message", data, room=room)
|
2018-06-20 13:07:11 -04:00
|
|
|
|
2018-07-05 13:27:48 -04:00
|
|
|
|
2018-07-05 18:17:36 -04:00
|
|
|
def handle_dice(message):
|
2018-07-05 13:27:48 -04:00
|
|
|
"""
|
|
|
|
Handle /dice or /roll messages.
|
|
|
|
"""
|
2018-07-05 18:17:36 -04:00
|
|
|
reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", message)
|
2018-07-05 13:27:48 -04:00
|
|
|
if not reg:
|
|
|
|
return
|
|
|
|
try:
|
2018-07-06 02:09:55 -04:00
|
|
|
groups = [0 if d is None else int(d) for d in reg.groups()]
|
|
|
|
diceNum, diceSides, diceMod = groups
|
2018-07-05 13:27:48 -04:00
|
|
|
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>"
|
2018-07-05 18:17:36 -04:00
|
|
|
return msg
|
2018-07-05 13:27:48 -04:00
|
|
|
|
2018-06-20 13:07:11 -04:00
|
|
|
|
|
|
|
@socketio.on("new_post")
|
2018-07-06 02:09:55 -04:00
|
|
|
def new_post(data, internal=False):
|
2018-06-20 13:07:11 -04:00
|
|
|
"""
|
|
|
|
Called when the QM makes a new post.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
2018-06-21 20:44:43 -04:00
|
|
|
res = db.get_quest_meta(quest_id=room)
|
|
|
|
if not res:
|
|
|
|
return
|
|
|
|
if session.get("user_id") != res[3]:
|
|
|
|
return
|
2018-06-20 13:07:11 -04:00
|
|
|
|
2018-06-21 20:44:43 -04:00
|
|
|
post = data["post"]
|
2018-07-06 02:09:55 -04:00
|
|
|
if not internal:
|
|
|
|
post = bleach.clean(post.strip())
|
2018-06-20 13:07:11 -04:00
|
|
|
post = post.replace("\n", "<br />")
|
2018-06-27 08:43:00 -04:00
|
|
|
post = tools.handle_img(post)
|
2018-07-06 02:09:55 -04:00
|
|
|
editable = data.get("editable", True)
|
|
|
|
|
|
|
|
data = {}
|
2018-06-20 13:07:11 -04:00
|
|
|
data["post"] = [post]
|
2018-07-04 17:20:47 -04:00
|
|
|
data["date"] = int(time.time())
|
|
|
|
post_id = db.insert_quest_post(room, post, data["date"])
|
|
|
|
data["post_id"] = post_id
|
2018-07-06 02:09:55 -04:00
|
|
|
data["editable"] = editable # TODO: enforce this on server-side
|
2018-06-20 13:07:11 -04:00
|
|
|
|
|
|
|
emit("new_post", data, room=room)
|
2018-06-21 20:44:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.on("update_post")
|
|
|
|
def update_post(data):
|
|
|
|
"""
|
|
|
|
Called when the QM edits and saves a post.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
res = db.get_quest_meta(quest_id=room)
|
|
|
|
if not res:
|
|
|
|
return
|
|
|
|
if session.get("user_id") != res[3]:
|
|
|
|
return
|
|
|
|
|
|
|
|
post = data["post"]
|
2018-06-21 22:49:25 -04:00
|
|
|
post = post.strip().replace("<br>", "<br />")
|
2018-06-27 08:43:00 -04:00
|
|
|
post = tools.handle_img(post)
|
2018-06-21 22:49:25 -04:00
|
|
|
|
2018-06-21 20:44:43 -04:00
|
|
|
post_id = data["post_id"]
|
|
|
|
db.update_quest_post(post_id, post)
|
2018-07-06 02:09:55 -04:00
|
|
|
|
|
|
|
data = {}
|
|
|
|
data["post"] = post
|
|
|
|
data["post_id"] = post_id
|
2018-06-21 20:44:43 -04:00
|
|
|
emit("update_post", data, room=room)
|
2018-07-02 16:10:50 -04:00
|
|
|
|
|
|
|
|
2018-07-04 17:20:47 -04:00
|
|
|
@socketio.on("dice_post")
|
2018-07-06 02:09:55 -04:00
|
|
|
def dice_post(data):
|
2018-07-02 16:10:50 -04:00
|
|
|
"""
|
|
|
|
Called when the QM posts a new dice call.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
res = db.get_quest_meta(quest_id=room)
|
|
|
|
if not res:
|
|
|
|
return
|
|
|
|
if session.get("user_id") != res[3]:
|
|
|
|
return
|
2018-07-04 17:20:47 -04:00
|
|
|
|
2018-07-06 02:09:55 -04:00
|
|
|
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))
|
|
|
|
|
|
|
|
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)
|
2018-07-04 17:20:47 -04:00
|
|
|
|
2018-07-06 02:09:55 -04:00
|
|
|
new_post({"post": post, "room": room, "editable": False}, internal=True)
|