From 3d0f1beadf555272e6d62fb67d2cfce7fb42cdf2 Mon Sep 17 00:00:00 2001 From: iou1name Date: Thu, 23 Aug 2018 07:40:28 -0400 Subject: [PATCH] added limited dice call posting --- quest/events.py | 41 ++++++++++++++++++++++++++++++++++++----- quest/forms.py | 19 +++++++++++++++++++ quest/static/quest.css | 3 +-- quest/static/quest.js | 6 +++--- quest/static/questQM.js | 17 ++++++++++++++--- 5 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 quest/forms.py diff --git a/quest/events.py b/quest/events.py index ac18b23..ea92787 100644 --- a/quest/events.py +++ b/quest/events.py @@ -12,6 +12,7 @@ import bleach from django.utils.timezone import localtime from quest.models import Message, Quest, Post +from quest.forms import DiceCallForm def message(socket, data): """ @@ -93,13 +94,14 @@ def text_post(socket, data): """ Called when the QM creates a new text post. """ + # TODO: security quest_id = data.get('quest_id') - text = data.get('text') + post_text = data.get('text') page_num = data.get('page_num') # cleaning - text = bleach.clean(text.strip()) - text = text.replace("\n", "
") + post_text = bleach.clean(post_text.strip()) + post_text = text.replace("\n", "
") # handle image @@ -107,17 +109,46 @@ def text_post(socket, data): quest=Quest.objects.get(id=quest_id), page_num=page_num, post_type='text', - post_text=text) + post_text=post_text) p.save() data = {} - data['text'] = text + data['post_text'] = post_text 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) +def dice_post(socket, data): + """ + Called when the QM makes a new dice post. + """ + quest_id = data.get('quest_id') + page_num = data.get('page_num') + form = DiceCallForm(data) + if not form.is_valid(): + return # error message? + + dice_roll = str(form.cleaned_data['diceNum']) + "d" + dice_roll += str(form.cleaned_data['diceSides']) + if form.cleaned_data['diceMod']: + if form.cleaned_data['diceMod'] > 0: + dice_roll += "+" + dice_roll += str(form.cleaned_data['diceMod']) + + post_text = "Roll " + dice_roll + if form.cleaned_data['diceChal']: + post_text += " vs DC" + str(form.cleaned_data['diceChal']) + + data = {} + data['post_text'] = post_text + data['post_type'] = 'dice' + data['date'] = int(time.time()) + data['post_id'] = 1 + socket.send('new_post', data) + + events = {} for obj in dir(): if type(locals()[obj]) == types.FunctionType: diff --git a/quest/forms.py b/quest/forms.py new file mode 100644 index 0000000..53f8455 --- /dev/null +++ b/quest/forms.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +""" +Form(s) for the quest page. +""" +from django import forms + +class DiceCallForm(forms.Form): + """ + The form for the QM making dice calls. + """ + diceNum = forms.IntegerField(min_value=1, max_value=99) + diceSides = forms.IntegerField(min_value=1, max_value=99) + diceMod = forms.IntegerField(min_value=-999, max_value=999, + required=False) + diceChal = forms.IntegerField(min_value=1, max_value=99, + required=False) + diceRollsTaken = forms.IntegerField(min_value=1, max_value=99, + required=False) + diceStrict = forms.BooleanField(required=False) diff --git a/quest/static/quest.css b/quest/static/quest.css index 9b44446..768a5d7 100644 --- a/quest/static/quest.css +++ b/quest/static/quest.css @@ -15,8 +15,7 @@ h3 { #questPane { padding-left: 5%; - padding-right: 35%; - width: 100%; + width: 65%; } .questPost { diff --git a/quest/static/quest.js b/quest/static/quest.js index 01bd9ca..e551b38 100644 --- a/quest/static/quest.js +++ b/quest/static/quest.js @@ -55,11 +55,11 @@ socket.events['new_post'] = function(data) { post_str += '
' + data.date; post_str += '
'; if (data.post_type == 'text') { - post_str += data.text; + post_str += data.post_text; } else if (data.post_type == 'dice') { - post_str += '

' + data.text + ' - Open

'; + post_str += '

' + data.post_text + ' - Open

'; } else if (data.post_type == 'poll') { - post_str += '

' + data.text + ' - Open

'; + post_str += '

' + data.post_text + ' - Open

'; post_str += ''; post_str += ''; for (i = 0; i < data.options.length; i++) { diff --git a/quest/static/questQM.js b/quest/static/questQM.js index 435efd8..4667e17 100644 --- a/quest/static/questQM.js +++ b/quest/static/questQM.js @@ -21,11 +21,11 @@ socket.events['new_post'] = function(data) { /* end QM only */ post_str += '
'; if (data.post_type == 'text') { - post_str += data.text; + post_str += data.post_text; } else if (data.post_type == 'dice') { - post_str += '

' + data.text + ' - Open

'; + post_str += '

' + data.post_text + ' - Open

'; } else if (data.post_type == 'poll') { - post_str += '

' + data.text + ' - Open

'; + post_str += '

' + data.post_text + ' - Open

'; post_str += '
'; post_str += ''; for (i = 0; i < data.options.length; i++) { @@ -66,3 +66,14 @@ function openPostTab(event, modeName) { document.getElementById(modeName).style.display = "block"; event.currentTarget.className += " active"; } +function form_post(form_id, emit_msg) { + let formData = new FormData(document.getElementById(form_id)); + let obj = {}; + formData.forEach(function(value, key) { + obj[key] = value; + }); + obj.quest_id = quest_id; + obj.page_num = page_num; + socket.send(emit_msg, obj); + document.getElementById(form_id).reset(); +}