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.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
|
|
|
|
message = data["message"]
|
|
|
|
date = int(time.time())
|
|
|
|
data["date"] = date
|
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-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
|
|
|
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)
|
|
|
|
|
2018-06-20 13:07:11 -04:00
|
|
|
|
|
|
|
@socketio.on("new_post")
|
|
|
|
def new_post(data):
|
|
|
|
"""
|
|
|
|
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-06-20 13:07:11 -04:00
|
|
|
post = bleach.clean(post.strip())
|
|
|
|
post = post.replace("\n", "<br />")
|
2018-06-27 08:43:00 -04:00
|
|
|
post = tools.handle_img(post)
|
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-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
|
|
|
data["post"] = post
|
|
|
|
|
2018-06-21 20:44:43 -04:00
|
|
|
post_id = data["post_id"]
|
|
|
|
db.update_quest_post(post_id, post)
|
|
|
|
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")
|
|
|
|
def dice_(data):
|
2018-07-02 16:10:50 -04:00
|
|
|
"""
|
|
|
|
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:
|
|
|
|
return
|
|
|
|
if session.get("user_id") != res[3]:
|
|
|
|
return
|
2018-07-04 17:20:47 -04:00
|
|
|
|
|
|
|
date = int(time.time())
|
|
|
|
data["date"] = date
|
|
|
|
data["post_id"] = 0
|
|
|
|
|
2018-07-02 16:10:50 -04:00
|
|
|
emit("dice_post", data, room=room)
|