anonkun/events.py

309 lines
7.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
SocketIO events.
"""
2018-07-11 18:25:10 -04:00
# TODO: harden the fuck up
2018-07-05 13:27:48 -04:00
import re
import time
import random
2018-07-11 18:25:10 -04:00
import functools
import bleach
2018-06-21 20:44:43 -04:00
from flask import session
from flask_socketio import SocketIO, emit, join_room
2018-06-25 15:52:10 -04:00
import tools
import database as db
socketio = SocketIO()
2018-07-11 18:25:10 -04:00
def qm_only(msg=""):
"""
A decorator function to protect certain endpoints so that only the
QM can access them.
"""
# TODO: better docstring, test this more thoroughly
def actual_decorator(func):
@functools.wraps(func)
def _nop(*args, **kwargs):
data = args[0]
room = data.get("room")
res = db.get_quest_meta(quest_id=room)
if not res:
return msg
if session.get("user_id") != res[3]:
return msg
return func(*args, **kwargs)
return _nop
return actual_decorator
@socketio.on('joined')
def joined(data):
"""
Sent by clients when they enter a room.
"""
room = data["room"]
join_room(room)
@socketio.on('message')
def message(data):
"""
Sent by a client when the user entered a new message.
"""
room = int(data["room"])
message = data["message"]
name = data["name"]
user_id = data.get("user_id")
data = {}
date = int(time.time())
data["date"] = date
data["name"] = name
data["user_id"] = user_id
data["room"] = room
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)
data["message"] = message
roll_msg = ""
if message.startswith("/dice") or message.startswith("/roll"):
roll_msg = handle_dice(data)
if roll_msg:
2018-07-08 17:50:54 -04:00
data["message"] += '<hr class="msgSrvHr" /><b>' + roll_msg + "</b>"
2018-07-05 13:27:48 -04:00
message_id = db.log_chat_message(data)
emit("message", data, room=room)
if roll_msg:
2018-07-08 17:50:54 -04:00
dice_call_id = db.get_quest_meta(data["room"])[4]
if dice_call_id:
dice_call = db.get_dice_call(dice_call_id)
dice_roll = re.search(r"(\d+d\d+(?:[+-]\d+)?)", message).group(1)
if dice_call[2] and dice_roll != dice_call[1]:
return
roll_results = re.search(r"Rolled (.+) =", roll_msg).group(1)
roll_total = int(re.search(r"= (\d+)$", roll_msg).group(1))
roll_data = (dice_roll, roll_results, roll_total)
db.insert_quest_roll(message_id, room, dice_call_id, roll_data)
2018-07-11 18:25:10 -04:00
if len(db.get_dice_rolls(post_id=dice_call_id)) == dice_call[4]:
db.set_dice_call_closed(room)
emit("close_dice_call", {"post_id": dice_call_id}, room=room)
room = data["room"]
data = {}
data["post"] = roll_msg + " (" + dice_roll + ")"
if dice_call[3]:
if roll_total >= dice_call[3]:
data["post"] += " - Pass"
else:
data["post"] += " - Fail"
data["post_type"] = "dice"
2018-07-08 17:50:54 -04:00
data["post_id"] = dice_call_id
emit("update_post", data, room=room)
2018-07-05 13:27:48 -04:00
def handle_dice(data):
2018-07-05 13:27:48 -04:00
"""
Handle /dice or /roll messages.
"""
reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", data["message"])
2018-07-05 13:27:48 -04:00
if not reg:
return
try:
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
2018-07-08 17:50:54 -04:00
msg = f"Rolled {', '.join(map(str, dice))}"
2018-07-05 13:27:48 -04:00
if diceMod:
if diceMod > 0:
msg += " + " + str(diceMod)
else:
msg += " - " + str(diceMod)[1:]
2018-07-08 17:50:54 -04:00
msg += " = " + str(total)
return msg
2018-07-05 13:27:48 -04:00
@socketio.on("new_post")
2018-07-11 18:25:10 -04:00
@qm_only()
def new_post(data, internal=False):
"""
Called when the QM makes a new post.
"""
2018-07-11 18:25:10 -04:00
room = data.get("room")
post = data.get("post")
post = bleach.clean(post.strip())
post = post.replace("\n", "<br />")
post = tools.handle_img(post)
2018-07-11 18:25:10 -04:00
date = int(time.time())
post_id = db.insert_quest_post(room, "text", post, date)
db.set_post_closed(room)
data = {}
data["post"] = [post]
data["post_type"] = "text"
2018-07-11 18:25:10 -04:00
data["date"] = date
data["post_id"] = post_id
emit("new_post", data, room=room)
2018-06-21 20:44:43 -04:00
@socketio.on("update_post")
2018-07-11 18:25:10 -04:00
@qm_only()
2018-06-21 20:44:43 -04:00
def update_post(data):
"""
Called when the QM edits and saves a post.
"""
# TODO: enforce only update text posts
2018-07-11 18:25:10 -04:00
room = data["room"]
2018-06-21 20:44:43 -04:00
post = data["post"]
2018-06-21 22:49:25 -04:00
post = post.strip().replace("<br>", "<br />")
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)
data = {}
data["post"] = post
data["post_id"] = post_id
data["post_type"] = "text"
2018-06-21 20:44:43 -04:00
emit("update_post", data, room=room)
@socketio.on("dice_post")
2018-07-11 18:25:10 -04:00
@qm_only()
def dice_post(data):
"""
Called when the QM posts a new dice call.
"""
room = data["room"]
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))
2018-07-11 18:25:10 -04:00
diceRollsTaken = int(data.get("diceRollsTaken", 0))
assert 0 < diceNum < 100
assert 0 < diceSides < 100
assert -1000 < diceMod < 1000
assert 0 <= diceChal < 100
2018-07-11 18:25:10 -04:00
assert 0 <= diceRollsTaken < 100
except (ValueError, AssertionError):
return
diceStrict = bool(data.get("diceStrict"))
dice_roll = f"{data['diceNum']}d{data['diceSides']}"
if diceMod:
if diceMod > 0:
dice_roll += "+"
dice_roll += str(diceMod)
2018-07-08 17:50:54 -04:00
post = "Roll " + dice_roll
if diceChal:
post += " vs DC" + str(diceChal)
2018-07-11 18:25:10 -04:00
date = int(time.time())
post_id = db.insert_quest_post(room, "dice", post, date)
new_call = (dice_roll, diceStrict, diceChal, diceRollsTaken)
db.insert_dice_call(post_id, room, new_call)
db.set_post_open(post_id, room)
data = {}
data["post"] = post
data["post_type"] = "dice"
2018-07-11 18:25:10 -04:00
data["date"] = date
data["post_id"] = post_id
emit("new_post", data, room=room)
2018-07-08 17:50:54 -04:00
2018-07-11 18:25:10 -04:00
@socketio.on('close_post')
@qm_only()
2018-07-08 17:50:54 -04:00
def close_dice_call(data):
"""
2018-07-11 18:25:10 -04:00
Closes an active post.
2018-07-08 17:50:54 -04:00
"""
room = data["room"]
post_id = data.get("post_id")
2018-07-11 18:25:10 -04:00
db.set_post_closed(room)
2018-07-08 17:50:54 -04:00
data = {}
data["post_id"] = post_id
2018-07-11 18:25:10 -04:00
emit("close_post", data, room=room)
2018-07-08 17:50:54 -04:00
2018-07-11 18:25:10 -04:00
@socketio.on("open_post")
@qm_only()
2018-07-08 17:50:54 -04:00
def open_dice_call(data):
"""
2018-07-11 18:25:10 -04:00
Opens an active post. This is only permitted if the active post is
2018-07-08 17:50:54 -04:00
the last post in the quest.
"""
# TODO: enforce only open if last post
2018-07-11 18:25:10 -04:00
room = data["room"]
2018-07-08 17:50:54 -04:00
post_id = data.get("post_id")
2018-07-11 18:25:10 -04:00
db.set_post_open(post_id, room)
2018-07-08 17:50:54 -04:00
data = {}
data["post_id"] = post_id
2018-07-11 18:25:10 -04:00
emit("open_post", data, room=room)
@socketio.on("poll_post")
@qm_only()
def poll_post(data):
"""
Called when the QM posts a new dice call.
"""
room = data.pop("room", None)
multi_choice = bool(data.pop("pollAllowMultipleChoices", None))
allow_writein = bool(data.pop("pollAllowUserOptions", None))
options = []
for key, value in data.items():
if not value or not key.startswith("pollOption-"):
continue
if len(value) >= 200:
return
options.append(value)
post = "Poll"
date = int(time.time())
post_id = db.insert_quest_post(room, "poll", post, date)
db.insert_poll(post_id, multi_choice, allow_writein)
for option in options:
db.insert_poll_option(post_id, room, option)
db.set_post_open(post_id, room)
data = {}
data["post"] = post
data["post_id"] = post_id
data["post_type"] = "poll"
data["date"] = int(time.time())
data["options"] = options
emit("new_post", data, room=room)