2018-08-15 17:41:51 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Individual functions for handling WebSocket events. Gets called by the
|
|
|
|
QuestConsumer object in consumers.py.
|
|
|
|
"""
|
2018-08-20 07:23:33 -04:00
|
|
|
import re
|
2018-08-16 15:43:43 -04:00
|
|
|
import time
|
2018-08-15 17:41:51 -04:00
|
|
|
import types
|
2018-08-21 07:18:03 -04:00
|
|
|
import random
|
2018-08-15 17:41:51 -04:00
|
|
|
|
2018-08-16 15:43:43 -04:00
|
|
|
import bleach
|
2018-10-08 10:22:36 -04:00
|
|
|
from django.conf import settings
|
2018-09-04 08:30:20 -04:00
|
|
|
from django.db import IntegrityError
|
2018-08-21 09:01:16 -04:00
|
|
|
from django.utils.timezone import localtime
|
2018-09-24 13:46:46 -04:00
|
|
|
from django.urls import reverse
|
2018-08-16 15:43:43 -04:00
|
|
|
|
2018-08-29 14:01:31 -04:00
|
|
|
from quest.models import *
|
2018-09-18 11:45:12 -04:00
|
|
|
from quest.tools import handle_img
|
2018-08-29 14:01:31 -04:00
|
|
|
from quest.forms import DiceCallForm, PollForm
|
2018-10-08 10:22:36 -04:00
|
|
|
from user.models import User
|
2018-08-20 06:57:36 -04:00
|
|
|
|
2018-08-15 17:41:51 -04:00
|
|
|
def message(socket, data):
|
|
|
|
"""
|
|
|
|
Gets called when the server receives a 'message' event.
|
|
|
|
"""
|
|
|
|
message = data.get('message')
|
2018-08-16 15:43:43 -04:00
|
|
|
|
2018-08-21 07:18:03 -04:00
|
|
|
# cleaning
|
2018-09-24 08:02:53 -04:00
|
|
|
message = message[:512]
|
2018-08-16 15:43:43 -04:00
|
|
|
message = message.strip()
|
2018-10-08 11:52:08 -04:00
|
|
|
message = re.sub(r'\n\n+', '\n\n', message)
|
2018-08-16 15:43:43 -04:00
|
|
|
if not message:
|
|
|
|
return
|
|
|
|
tags = ["b", "code", "i", "s"]
|
|
|
|
message = bleach.clean(message, tags=tags)
|
2018-08-20 07:23:33 -04:00
|
|
|
|
2018-08-21 07:18:03 -04:00
|
|
|
# greentext
|
2018-08-20 07:23:33 -04:00
|
|
|
lines = []
|
|
|
|
for line in message.splitlines():
|
|
|
|
if line.startswith(">") and not line.startswith(">>"):
|
|
|
|
line = '<span class="greenText">' + line + '</span>'
|
|
|
|
lines.append(line)
|
|
|
|
message = "<br>".join(lines)
|
|
|
|
|
2018-08-21 07:18:03 -04:00
|
|
|
# quote links
|
2018-08-20 07:23:33 -04:00
|
|
|
quotes = re.findall(r">>\d+", message)
|
|
|
|
for quote in quotes:
|
|
|
|
msg_id = quote.replace(">>", "")
|
|
|
|
msg = '<a class="quotelink" '
|
2018-09-03 17:59:41 -04:00
|
|
|
msg += 'href="javascript:scroll_to_msg(\'' + msg_id + '\')" '
|
|
|
|
msg += 'onmousemove="show_preview(event, \'' + msg_id + '\')" '
|
|
|
|
msg += 'onmouseout="clear_preview()">' + quote + '</a>'
|
2018-08-20 07:23:33 -04:00
|
|
|
message = message.replace(quote, msg)
|
|
|
|
|
2018-08-21 07:18:03 -04:00
|
|
|
# handle image
|
2018-09-18 11:45:12 -04:00
|
|
|
message = handle_img(message)
|
2018-08-21 07:18:03 -04:00
|
|
|
|
|
|
|
# dice rolling
|
|
|
|
if any(map(message.startswith, ["/dice", "/roll"])):
|
|
|
|
reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", data["message"])
|
|
|
|
if not reg:
|
|
|
|
return
|
|
|
|
try:
|
2018-10-09 14:05:03 -04:00
|
|
|
# TODO: form validation?
|
2018-08-21 07:18:03 -04:00
|
|
|
groups = [0 if d is None else int(d) for d in reg.groups()]
|
|
|
|
dice_num, dice_sides, dice_mod = groups
|
2018-09-24 09:59:11 -04:00
|
|
|
assert 1 <= dice_num <= 256
|
|
|
|
assert 1 <= dice_sides <= 256
|
|
|
|
assert -65536 <= dice_mod <= 65536
|
2018-08-21 07:18:03 -04:00
|
|
|
except (ValueError, AssertionError):
|
|
|
|
return
|
2018-08-24 21:51:03 -04:00
|
|
|
dice_results = [random.randint(1, dice_sides) for _ in range(dice_num)]
|
|
|
|
total = sum(dice_results) + dice_mod
|
|
|
|
roll_msg = f"Rolled {', '.join(map(str, dice_results))}"
|
2018-08-21 07:18:03 -04:00
|
|
|
if dice_mod:
|
|
|
|
if dice_mod > 0:
|
|
|
|
roll_msg += " + " + str(dice_mod)
|
|
|
|
else:
|
|
|
|
roll_msg += " - " + str(dice_mod)[1:]
|
|
|
|
roll_msg += " = " + str(total)
|
2018-08-24 21:51:03 -04:00
|
|
|
message += '<hr class="msgSrvHr"><b>' + roll_msg + "</b>"
|
|
|
|
|
2018-08-20 06:57:36 -04:00
|
|
|
user = socket.scope['user']
|
2018-10-04 14:44:07 -04:00
|
|
|
quest = Quest.objects.get(id=socket.quest_id)
|
2018-08-20 06:57:36 -04:00
|
|
|
|
|
|
|
m = Message(
|
2018-10-04 14:44:07 -04:00
|
|
|
quest=quest,
|
2018-08-20 06:57:36 -04:00
|
|
|
message=message)
|
2018-10-12 08:07:48 -04:00
|
|
|
if user.is_authenticated:
|
2018-08-20 06:57:36 -04:00
|
|
|
m.user = user
|
|
|
|
m.save()
|
2018-08-15 17:41:51 -04:00
|
|
|
|
2019-04-13 17:08:32 -04:00
|
|
|
if user.is_authenticated:
|
|
|
|
anonymize = (user.anonymize or quest.anonymize) and user != quest.owner
|
|
|
|
else:
|
|
|
|
anonymize = True
|
2018-10-04 14:44:07 -04:00
|
|
|
|
2018-08-15 17:41:51 -04:00
|
|
|
data = {}
|
2018-08-20 06:57:36 -04:00
|
|
|
data['message_id'] = m.id
|
2018-08-16 15:43:43 -04:00
|
|
|
data['message'] = message
|
2018-08-20 06:57:36 -04:00
|
|
|
data['date'] = int(time.time())
|
|
|
|
data['name'] = user.username
|
2018-10-04 14:44:07 -04:00
|
|
|
data['anonymize'] = anonymize
|
2018-08-15 17:41:51 -04:00
|
|
|
socket.send('message', data)
|
|
|
|
|
2018-08-24 21:51:03 -04:00
|
|
|
# append rolls to dicecall
|
|
|
|
if any(map(message.startswith, ["/dice", "/roll"])):
|
|
|
|
try:
|
2018-09-24 08:02:53 -04:00
|
|
|
dc = DiceCall.objects.get(
|
|
|
|
post__quest__id=socket.quest_id,
|
|
|
|
open=True
|
|
|
|
)
|
2018-08-24 21:51:03 -04:00
|
|
|
except DiceCall.DoesNotExist:
|
|
|
|
return
|
|
|
|
dice_roll = f"{dice_num}d{dice_sides}"
|
|
|
|
if dice_mod:
|
|
|
|
if dice_mod > 0:
|
|
|
|
dice_roll += "+"
|
|
|
|
dice_roll += str(dice_mod)
|
|
|
|
|
|
|
|
if dc.strict and dc.dice_roll != dice_roll:
|
|
|
|
return
|
|
|
|
|
|
|
|
dr = DiceRoll(
|
|
|
|
dicecall=dc,
|
|
|
|
message=m,
|
|
|
|
roll=dice_roll,
|
|
|
|
results=re.search(r"Rolled (.+) =", roll_msg).group(1),
|
|
|
|
total=total,
|
|
|
|
)
|
|
|
|
dr.save()
|
|
|
|
|
|
|
|
if DiceRoll.objects.filter(dicecall=dc).count() == dc.rolls_taken:
|
|
|
|
dc.open = False
|
|
|
|
dc.save()
|
|
|
|
socket.send('close_post', {'post_id': dc.post_id})
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['post_text'] = roll_msg + " (" + dice_roll + ")"
|
|
|
|
if dc.dice_challenge:
|
|
|
|
if total >= dc.dice_challenge:
|
|
|
|
data["post_text"] += " - Pass"
|
|
|
|
else:
|
|
|
|
data["post_text"] += " - Fail"
|
|
|
|
data['post_type'] = 'dice'
|
|
|
|
data['post_id'] = dc.post_id
|
|
|
|
socket.send('update_post', data)
|
|
|
|
|
2018-08-15 17:41:51 -04:00
|
|
|
|
2018-08-21 09:01:16 -04:00
|
|
|
def text_post(socket, data):
|
|
|
|
"""
|
|
|
|
Called when the QM creates a new text post.
|
|
|
|
"""
|
2018-10-09 14:05:03 -04:00
|
|
|
quest = Quest.objects.get(id=socket.quest_id)
|
|
|
|
user = socket.scope['user']
|
|
|
|
if quest.owner != user:
|
|
|
|
return # error message?
|
|
|
|
|
2018-08-23 07:40:28 -04:00
|
|
|
post_text = data.get('text')
|
2018-08-21 09:01:16 -04:00
|
|
|
page_num = data.get('page_num')
|
|
|
|
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
page = Page.objects.get(quest=quest, page_num=page_num)
|
|
|
|
except Page.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
2018-08-21 09:01:16 -04:00
|
|
|
# cleaning
|
2018-08-23 07:40:28 -04:00
|
|
|
post_text = bleach.clean(post_text.strip())
|
2018-08-24 22:54:27 -04:00
|
|
|
post_text = post_text.replace("\n", "<br>")
|
2018-08-21 09:01:16 -04:00
|
|
|
|
|
|
|
# handle image
|
2018-10-11 12:37:29 -04:00
|
|
|
post_text = handle_img(post_text)
|
2018-08-21 09:01:16 -04:00
|
|
|
|
|
|
|
p = Post(
|
2018-09-25 11:58:07 -04:00
|
|
|
quest=quest,
|
2018-10-09 14:05:03 -04:00
|
|
|
page=page,
|
2018-08-21 09:01:16 -04:00
|
|
|
post_type='text',
|
2018-08-23 07:40:28 -04:00
|
|
|
post_text=post_text)
|
2018-08-21 09:01:16 -04:00
|
|
|
p.save()
|
|
|
|
|
|
|
|
data = {}
|
2018-08-23 07:40:28 -04:00
|
|
|
data['post_text'] = post_text
|
2018-08-21 09:01:16 -04:00
|
|
|
data['post_type'] = 'text'
|
|
|
|
data['date'] = localtime(p.timestamp).strftime('%Y-%m-%d %H:%M')
|
|
|
|
data['post_id'] = p.id
|
|
|
|
socket.send('new_post', data)
|
|
|
|
|
2018-10-08 10:22:36 -04:00
|
|
|
server = User.objects.get(id=settings.SERVER_USER_ID)
|
|
|
|
m = Message(
|
|
|
|
quest=quest,
|
|
|
|
user=server,
|
|
|
|
message=f"{quest.owner.username} made a new post!",
|
|
|
|
)
|
|
|
|
m.save()
|
|
|
|
data = {}
|
|
|
|
data['message_id'] = m.id
|
|
|
|
data['message'] = m.message
|
|
|
|
data['date'] = int(time.time())
|
|
|
|
data['name'] = server.username
|
|
|
|
data['anonymize'] = False
|
|
|
|
socket.send('message', data)
|
|
|
|
|
2018-08-21 09:01:16 -04:00
|
|
|
|
2018-08-23 07:40:28 -04:00
|
|
|
def dice_post(socket, data):
|
|
|
|
"""
|
|
|
|
Called when the QM makes a new dice post.
|
|
|
|
"""
|
2018-10-09 14:05:03 -04:00
|
|
|
quest = Quest.objects.get(id=socket.quest_id)
|
|
|
|
user = socket.scope['user']
|
|
|
|
if quest.owner != user:
|
|
|
|
return # error message?
|
|
|
|
|
2018-08-23 07:40:28 -04:00
|
|
|
page_num = data.get('page_num')
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
page = Page.objects.get(quest=quest, page_num=page_num)
|
|
|
|
except Page.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
2018-08-23 07:40:28 -04:00
|
|
|
form = DiceCallForm(data)
|
|
|
|
if not form.is_valid():
|
|
|
|
return # error message?
|
2018-08-23 11:57:37 -04:00
|
|
|
form = form.cleaned_data
|
2018-08-23 07:40:28 -04:00
|
|
|
|
2018-08-24 21:51:03 -04:00
|
|
|
posts = Post.objects.filter(
|
2018-10-09 14:05:03 -04:00
|
|
|
quest=quest,
|
|
|
|
post_type='dice',
|
|
|
|
dicecall__open=True
|
|
|
|
)
|
2018-08-24 21:51:03 -04:00
|
|
|
for post in posts:
|
|
|
|
post.dicecall.open = False
|
|
|
|
post.dicecall.save()
|
2018-10-09 14:05:03 -04:00
|
|
|
socket.send('close_all_posts', {'post_type': 'dice'})
|
2018-08-24 21:51:03 -04:00
|
|
|
|
2018-08-23 11:57:37 -04:00
|
|
|
dice_roll = str(form['diceNum']) + "d"
|
|
|
|
dice_roll += str(form['diceSides'])
|
|
|
|
if form['diceMod']:
|
|
|
|
if form['diceMod'] > 0:
|
2018-08-23 07:40:28 -04:00
|
|
|
dice_roll += "+"
|
2018-08-23 11:57:37 -04:00
|
|
|
dice_roll += str(form['diceMod'])
|
2018-08-23 07:40:28 -04:00
|
|
|
|
|
|
|
post_text = "Roll " + dice_roll
|
2018-08-23 11:57:37 -04:00
|
|
|
if form['diceChal']:
|
|
|
|
post_text += " vs DC" + str(form['diceChal'])
|
2018-09-25 11:58:07 -04:00
|
|
|
|
2018-08-23 11:57:37 -04:00
|
|
|
p = Post(
|
2018-09-25 11:58:07 -04:00
|
|
|
quest=quest,
|
2018-10-09 14:05:03 -04:00
|
|
|
page=page,
|
2018-08-23 11:57:37 -04:00
|
|
|
post_type='dice',
|
|
|
|
post_text=post_text
|
|
|
|
)
|
|
|
|
p.save()
|
2018-08-24 21:51:03 -04:00
|
|
|
dc = DiceCall(
|
2018-08-23 11:57:37 -04:00
|
|
|
post=p,
|
|
|
|
dice_roll=dice_roll,
|
|
|
|
strict=form['diceStrict'],
|
|
|
|
dice_challenge=form['diceChal'],
|
2018-08-24 17:45:18 -04:00
|
|
|
rolls_taken=form['diceRollsTaken'],
|
|
|
|
open=True,
|
2018-08-23 11:57:37 -04:00
|
|
|
)
|
2018-08-24 21:51:03 -04:00
|
|
|
dc.save()
|
2018-08-24 17:45:18 -04:00
|
|
|
|
2018-08-23 07:40:28 -04:00
|
|
|
data = {}
|
2018-08-29 14:01:31 -04:00
|
|
|
data['post_id'] = p.id
|
2018-08-23 07:40:28 -04:00
|
|
|
data['post_type'] = 'dice'
|
2018-08-29 14:01:31 -04:00
|
|
|
data['post_text'] = post_text
|
2018-08-23 11:57:37 -04:00
|
|
|
data['date'] = localtime(p.timestamp).strftime('%Y-%m-%d %H:%M')
|
2018-08-29 14:01:31 -04:00
|
|
|
socket.send('new_post', data)
|
|
|
|
|
2018-10-08 10:22:36 -04:00
|
|
|
server = User.objects.get(id=settings.SERVER_USER_ID)
|
|
|
|
m = Message(
|
|
|
|
quest=quest,
|
|
|
|
user=server,
|
|
|
|
message=f"{quest.owner.username} made a new dice call!",
|
|
|
|
)
|
|
|
|
m.save()
|
|
|
|
data = {}
|
|
|
|
data['message_id'] = m.id
|
|
|
|
data['message'] = m.message
|
|
|
|
data['date'] = int(time.time())
|
|
|
|
data['name'] = server.username
|
|
|
|
data['anonymize'] = False
|
|
|
|
socket.send('message', data)
|
|
|
|
|
2018-08-29 14:01:31 -04:00
|
|
|
|
|
|
|
def poll_post(socket, data):
|
|
|
|
"""
|
|
|
|
Called when the QM makes a new dice post.
|
|
|
|
"""
|
2018-10-09 14:05:03 -04:00
|
|
|
quest = Quest.objects.get(id=socket.quest_id)
|
|
|
|
user = socket.scope['user']
|
|
|
|
if quest.owner != user:
|
|
|
|
return # error message?
|
|
|
|
|
2019-04-13 17:08:32 -04:00
|
|
|
page_num = data.get('page_num')
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
page = Page.objects.get(quest=quest, page_num=page_num)
|
|
|
|
except Page.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
2018-08-29 14:01:31 -04:00
|
|
|
form = PollForm(data)
|
|
|
|
if not form.is_valid():
|
|
|
|
return # error message?
|
|
|
|
form = form.cleaned_data
|
2019-04-13 17:08:32 -04:00
|
|
|
multi_choice = form.pop('multi_choice')
|
|
|
|
allow_writein = form.pop('allow_writein')
|
2018-08-29 14:01:31 -04:00
|
|
|
|
|
|
|
p = Post(
|
2018-09-25 11:58:07 -04:00
|
|
|
quest=quest,
|
2018-10-09 14:05:03 -04:00
|
|
|
page=page,
|
2018-08-29 14:01:31 -04:00
|
|
|
post_type='poll',
|
|
|
|
post_text="Poll"
|
|
|
|
)
|
|
|
|
p.save()
|
|
|
|
pl = Poll(
|
|
|
|
post=p,
|
2019-04-13 17:08:32 -04:00
|
|
|
multi_choice=multi_choice,
|
|
|
|
allow_writein=allow_writein,
|
2018-08-29 14:01:31 -04:00
|
|
|
open=True
|
|
|
|
)
|
|
|
|
pl.save()
|
|
|
|
options = []
|
|
|
|
for key, option in form.items():
|
|
|
|
o = PollOption(
|
|
|
|
poll=pl,
|
|
|
|
text=option
|
|
|
|
)
|
|
|
|
o.save()
|
|
|
|
options.append(o)
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['post_type'] = 'poll'
|
2018-08-23 11:57:37 -04:00
|
|
|
data['post_id'] = p.id
|
2018-08-29 14:01:31 -04:00
|
|
|
data['post_text'] = "Poll"
|
|
|
|
data['date'] = localtime(p.timestamp).strftime('%Y-%m-%d %H:%M')
|
2018-09-03 17:59:41 -04:00
|
|
|
data['options'] = [(o.id, o.text) for o in options]
|
2019-04-13 17:08:32 -04:00
|
|
|
data['allow_writein'] = allow_writein
|
2018-08-23 07:40:28 -04:00
|
|
|
socket.send('new_post', data)
|
|
|
|
|
2018-10-08 10:22:36 -04:00
|
|
|
server = User.objects.get(id=settings.SERVER_USER_ID)
|
|
|
|
m = Message(
|
|
|
|
quest=quest,
|
|
|
|
user=server,
|
|
|
|
message=f"{quest.owner.username} made a new poll!",
|
|
|
|
)
|
|
|
|
m.save()
|
|
|
|
data = {}
|
|
|
|
data['message_id'] = m.id
|
|
|
|
data['message'] = m.message
|
|
|
|
data['date'] = int(time.time())
|
|
|
|
data['name'] = server.username
|
|
|
|
data['anonymize'] = False
|
|
|
|
socket.send('message', data)
|
|
|
|
|
2018-08-23 07:40:28 -04:00
|
|
|
|
2018-09-07 12:56:13 -04:00
|
|
|
def edit_post(socket, data):
|
|
|
|
"""
|
|
|
|
Called when the QM saves an edited post.
|
|
|
|
"""
|
2018-10-09 14:05:03 -04:00
|
|
|
quest = Quest.objects.get(id=socket.quest_id)
|
|
|
|
user = socket.scope['user']
|
|
|
|
if quest.owner != user:
|
|
|
|
return # error message?
|
|
|
|
|
2018-09-07 12:56:13 -04:00
|
|
|
post_id = data.get('post_id')
|
|
|
|
post_text = data.get('post_text')
|
|
|
|
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
p = Post.objects.get(id=post_id)
|
|
|
|
except Post.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
2018-09-07 12:56:13 -04:00
|
|
|
# cleaning
|
|
|
|
post_text = bleach.clean(post_text.strip())
|
|
|
|
post_text = post_text.replace("\n", "<br>")
|
|
|
|
|
|
|
|
# handle image
|
2018-10-11 12:37:29 -04:00
|
|
|
post_text = handle_img(post_text)
|
2018-09-07 12:56:13 -04:00
|
|
|
|
|
|
|
p.post_text = post_text
|
|
|
|
p.save()
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['post_text'] = post_text
|
|
|
|
data['post_type'] = 'text'
|
|
|
|
data['post_id'] = p.id
|
|
|
|
socket.send('update_post', data)
|
|
|
|
|
|
|
|
|
2018-08-24 17:45:18 -04:00
|
|
|
def close_post(socket, data):
|
|
|
|
"""
|
|
|
|
Called when the QM closes an open post.
|
|
|
|
"""
|
2018-10-09 14:05:03 -04:00
|
|
|
quest = Quest.objects.get(id=socket.quest_id)
|
|
|
|
user = socket.scope['user']
|
|
|
|
if quest.owner != user:
|
|
|
|
return # error message?
|
|
|
|
|
2018-08-24 17:45:18 -04:00
|
|
|
post_id = data.get('post_id')
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
p = Post.objects.get(id=post_id)
|
|
|
|
except Post.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
2018-08-24 17:45:18 -04:00
|
|
|
if data.get('post_type') == 'dice':
|
|
|
|
p.dicecall.open = False
|
|
|
|
p.dicecall.save()
|
|
|
|
elif data.get('post_type') == 'poll':
|
|
|
|
p.poll.open = False
|
|
|
|
p.poll.save()
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['post_id'] = post_id
|
|
|
|
socket.send('close_post', data)
|
|
|
|
|
|
|
|
|
|
|
|
def open_post(socket, data):
|
|
|
|
"""
|
|
|
|
Called when the QM opens a closed post.
|
|
|
|
"""
|
2018-10-09 14:05:03 -04:00
|
|
|
quest = Quest.objects.get(id=socket.quest_id)
|
|
|
|
user = socket.scope['user']
|
|
|
|
if quest.owner != user:
|
|
|
|
return # error message?
|
|
|
|
|
2018-08-24 17:45:18 -04:00
|
|
|
post_id = data.get('post_id')
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
p = Post.objects.get(id=post_id)
|
|
|
|
except Post.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
2018-08-24 17:45:18 -04:00
|
|
|
if data.get('post_type') == 'dice':
|
2018-08-24 21:51:03 -04:00
|
|
|
posts = Post.objects.filter(
|
2018-10-09 14:05:03 -04:00
|
|
|
quest=quest,
|
|
|
|
post_type='dice',
|
|
|
|
dicecall__open=True
|
|
|
|
)
|
2018-08-24 18:24:25 -04:00
|
|
|
for post in posts:
|
|
|
|
post.dicecall.open = False
|
|
|
|
post.dicecall.save()
|
|
|
|
socket.send('close_all_posts', {'post_type': 'dice'})
|
|
|
|
|
2018-08-24 17:45:18 -04:00
|
|
|
p.dicecall.open = True
|
|
|
|
p.dicecall.save()
|
|
|
|
elif data.get('post_type') == 'poll':
|
|
|
|
p.poll.open = True
|
|
|
|
p.poll.save()
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['post_id'] = post_id
|
|
|
|
socket.send('open_post', data)
|
|
|
|
|
|
|
|
|
2018-10-09 14:05:03 -04:00
|
|
|
def new_page(socket, data):
|
|
|
|
"""
|
|
|
|
Called when the QM creates a new page.
|
|
|
|
"""
|
|
|
|
quest = Quest.objects.get(id=socket.quest_id)
|
|
|
|
user = socket.scope['user']
|
|
|
|
if quest.owner != user:
|
|
|
|
return # error message?
|
|
|
|
|
|
|
|
title = data.get('page_title')
|
|
|
|
appendix = bool(data.get('appendix'))
|
|
|
|
|
|
|
|
if appendix:
|
|
|
|
page = Page.objects.filter(
|
|
|
|
quest=quest,
|
|
|
|
appendix=True
|
|
|
|
).order_by('page_num').last()
|
|
|
|
if page:
|
|
|
|
page_num = chr(ord(page.page_num)+1)
|
|
|
|
else:
|
|
|
|
page_num = 'a'
|
|
|
|
else:
|
|
|
|
last_page = Page.objects.filter(
|
|
|
|
quest=quest,
|
|
|
|
appendix=False
|
|
|
|
).order_by('page_num').last()
|
|
|
|
page_num = int(last_page.page_num) + 1
|
|
|
|
p = Page(
|
|
|
|
quest=quest,
|
|
|
|
page_num=page_num,
|
|
|
|
title=title,
|
|
|
|
appendix=appendix,
|
|
|
|
)
|
|
|
|
p.save()
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['page_num'] = p.page_num
|
|
|
|
data['title'] = title
|
|
|
|
if appendix:
|
|
|
|
data['appendix'] = True
|
|
|
|
else:
|
|
|
|
data['appendix'] = False
|
|
|
|
data['url'] = reverse('quest:quest', args=[socket.quest_id, p.page_num])
|
|
|
|
socket.send('new_page', data)
|
|
|
|
|
|
|
|
server = User.objects.get(id=settings.SERVER_USER_ID)
|
|
|
|
m = Message(
|
|
|
|
quest=quest,
|
|
|
|
user=server,
|
|
|
|
message=f"{quest.owner.username} started a new page!",
|
|
|
|
)
|
|
|
|
m.save()
|
|
|
|
data = {}
|
|
|
|
data['message_id'] = m.id
|
|
|
|
data['message'] = m.message
|
|
|
|
data['date'] = int(time.time())
|
|
|
|
data['name'] = server.username
|
|
|
|
data['anonymize'] = False
|
|
|
|
socket.send('message', data)
|
|
|
|
|
|
|
|
|
2018-09-03 17:59:41 -04:00
|
|
|
def vote(socket, data):
|
|
|
|
"""
|
|
|
|
Called when a player votes in a poll.
|
|
|
|
"""
|
|
|
|
post_id = data.get('post_id')
|
|
|
|
option_id = data.get('option_id')
|
|
|
|
polarity = data.get('polarity')
|
|
|
|
ip_address = socket.scope['client'][0]
|
|
|
|
user = socket.scope['user']
|
|
|
|
|
|
|
|
if polarity == False:
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
v = PollVote.objects.get(
|
|
|
|
ip_address=ip_address,
|
|
|
|
option__id=option_id
|
|
|
|
)
|
|
|
|
except PollVote.DoesNotExist:
|
|
|
|
return
|
2018-09-03 17:59:41 -04:00
|
|
|
v.delete()
|
|
|
|
else:
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
p = Poll.objects.get(post_id=post_id)
|
|
|
|
option = PollOption.objects.get(id=option_id)
|
|
|
|
except (Poll.DoesNotExist, PollOption.DoesNotExist):
|
|
|
|
return
|
|
|
|
pvs = PollVote.objects.filter(option=option, ip_address=ip_address)
|
|
|
|
if pvs.count() != 0:
|
|
|
|
return
|
|
|
|
|
2018-09-03 17:59:41 -04:00
|
|
|
if p.multi_choice == False:
|
|
|
|
votes = PollVote.objects.filter(
|
|
|
|
ip_address=ip_address,
|
|
|
|
option__poll=p
|
|
|
|
)
|
|
|
|
for vote in votes:
|
|
|
|
vote.delete()
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['option_id'] = vote.option.id
|
|
|
|
data['polarity'] = False
|
|
|
|
socket.send('vote', data)
|
|
|
|
socket.self_send('set_option_box', data)
|
2018-10-09 14:05:03 -04:00
|
|
|
pv = PollVote(
|
|
|
|
option=option,
|
2018-09-03 17:59:41 -04:00
|
|
|
ip_address=ip_address
|
|
|
|
)
|
2018-10-12 08:07:48 -04:00
|
|
|
if user.is_authenticated:
|
2018-10-09 14:05:03 -04:00
|
|
|
pv.user = user
|
|
|
|
pv.save()
|
2018-09-03 17:59:41 -04:00
|
|
|
|
2018-09-04 08:30:20 -04:00
|
|
|
data = {}
|
|
|
|
data['option_id'] = option_id
|
|
|
|
data['polarity'] = polarity
|
|
|
|
socket.send('vote', data)
|
2018-09-03 17:59:41 -04:00
|
|
|
|
|
|
|
|
2018-09-05 08:41:53 -04:00
|
|
|
def write_in(socket, data):
|
|
|
|
"""
|
|
|
|
Called when a player creates a new write-in.
|
|
|
|
"""
|
|
|
|
post_id = data.get('post_id')
|
|
|
|
option_text = data.get('option_text', '')
|
|
|
|
user = socket.scope['user']
|
|
|
|
|
2018-10-09 14:05:03 -04:00
|
|
|
try:
|
|
|
|
p = Poll.objects.get(post_id=post_id)
|
|
|
|
except Poll.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
2018-09-05 08:41:53 -04:00
|
|
|
option_text = option_text.strip()
|
|
|
|
if not option_text:
|
|
|
|
return
|
2019-04-13 17:08:32 -04:00
|
|
|
option_text = "Write-in: " + bleach.clean(option_text)
|
2018-09-05 08:41:53 -04:00
|
|
|
if len(option_text) > 200:
|
|
|
|
# error message?
|
|
|
|
return
|
|
|
|
o = PollOption(
|
|
|
|
poll=p,
|
|
|
|
text=option_text
|
|
|
|
)
|
|
|
|
o.save()
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['post_id'] = post_id
|
|
|
|
data['post_type'] = 'poll'
|
|
|
|
data['option_id'] = o.id
|
|
|
|
data['option_text'] = option_text
|
|
|
|
socket.send('update_post', data)
|
|
|
|
|
|
|
|
|
2018-08-15 17:41:51 -04:00
|
|
|
events = {}
|
|
|
|
for obj in dir():
|
|
|
|
if type(locals()[obj]) == types.FunctionType:
|
|
|
|
events[locals()[obj].__name__] = locals()[obj]
|