dicecalls get saved to database

This commit is contained in:
iou1name 2018-08-23 11:57:37 -04:00
parent 3d0f1beadf
commit 1c1ed68d7b
5 changed files with 95 additions and 19 deletions

View File

@ -11,7 +11,7 @@ import random
import bleach
from django.utils.timezone import localtime
from quest.models import Message, Quest, Post
from quest.models import Message, Quest, Post, DiceCall
from quest.forms import DiceCallForm
def message(socket, data):
@ -129,23 +129,40 @@ def dice_post(socket, data):
form = DiceCallForm(data)
if not form.is_valid():
return # error message?
form = form.cleaned_data
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 = str(form['diceNum']) + "d"
dice_roll += str(form['diceSides'])
if form['diceMod']:
if form['diceMod'] > 0:
dice_roll += "+"
dice_roll += str(form.cleaned_data['diceMod'])
dice_roll += str(form['diceMod'])
post_text = "Roll " + dice_roll
if form.cleaned_data['diceChal']:
post_text += " vs DC" + str(form.cleaned_data['diceChal'])
if form['diceChal']:
post_text += " vs DC" + str(form['diceChal'])
p = Post(
quest=Quest.objects.get(id=quest_id),
page_num=page_num,
post_type='dice',
post_text=post_text
)
p.save()
d = DiceCall(
post=p,
dice_roll=dice_roll,
strict=form['diceStrict'],
dice_challenge=form['diceChal'],
rolls_taken=form['diceRollsTaken']
)
d.save()
data = {}
data['post_text'] = post_text
data['post_type'] = 'dice'
data['date'] = int(time.time())
data['post_id'] = 1
data['date'] = localtime(p.timestamp).strftime('%Y-%m-%d %H:%M')
data['post_id'] = p.id
socket.send('new_post', data)

View File

@ -10,10 +10,10 @@ class DiceCallForm(forms.Form):
"""
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)
diceMod = forms.IntegerField(
min_value=-999, max_value=999, required=False)
diceChal = forms.IntegerField(
min_value=1, max_value=999, required=False)
diceRollsTaken = forms.IntegerField(
min_value=1, max_value=99, required=False)
diceStrict = forms.BooleanField(required=False)

View File

@ -62,7 +62,8 @@
<b>Rolled {{ dice_roll[4] }} = {{ dice_roll[5] }} ({{ dice_roll[3] }}){% if post.id|dice_chal != 0 %} - {% if dice_roll[5] >= post.id|dice_chal %}Pass{% else %}Fail{% endif %}{% endif %}</b>
</div>
{% endfor #}
<h3>{{ post.post_test }} - {% if post.id == quest.open_post_id %}Open{% else %}Closed{% endif %}</h3>
{% elif post.post_type == "poll" %}
<h3>{{ post.post_text }} - {% if post.id == quest.open_post_id %}Open{% else %}Closed{% endif %}</h3>
<table class="poll" id="poll-{{ post.id }}">
{# for option in options.get(post.id, []) %}
<tr id="optionRow-{{ option[0] }}">
@ -109,7 +110,7 @@
<span class="tooltip" title="Only take matching rolls.">Strict</span><br />
<input type="checkbox" onclick="document.getElementById('diceChal').disabled=!this.checked;"/>
<span class="tooltip" title="Dice challenge">DC:</span>
<input type="number" name="diceChal" id="diceChal" min="1" max="99" disabled/><br />
<input type="number" name="diceChal" id="diceChal" min="1" max="999" disabled/><br />
<input type="checkbox" onclick="document.getElementById('diceRollsTaken').disabled=!this.checked;"/>
<span class="tooltip" title="Automatically close the dice call after this many rolls have been made.">Rolls Taken:</span>
<input type="number" name="diceRollsTaken" id="diceRollsTaken" min="1" max="99" disabled/><br />

View File

@ -0,0 +1,25 @@
# Generated by Django 2.1 on 2018-08-23 15:46
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('quest', '0006_auto_20180817_0921'),
]
operations = [
migrations.CreateModel(
name='DiceCall',
fields=[
('post', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='quest.Post')),
('dice_roll', models.CharField(max_length=9)),
('strict', models.BooleanField()),
('dice_challenge', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(999), django.core.validators.MinValueValidator(1)])),
('rolls_taken', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(99), django.core.validators.MinValueValidator(1)])),
],
),
]

View File

@ -4,6 +4,7 @@ The main quest models.
"""
from django.db import models
from django.conf import settings
from django.core.validators import MaxValueValidator, MinValueValidator
class Quest(models.Model):
"""
@ -25,11 +26,42 @@ class Post(models.Model):
POST_TYPES = (
('text', 'Text'),
('dice', 'Dice'),
('poll', 'Poll'))
('poll', 'Poll')
)
post_type = models.CharField(max_length=4, choices=POST_TYPES)
post_text = models.TextField()
timestamp = models.DateTimeField(auto_now=True)
class DiceCall(models.Model):
"""
An object representing dice calls made by the QM.
"""
post = models.OneToOneField(
Post,
on_delete=models.CASCADE,
primary_key=True,
)
dice_roll = models.CharField(max_length=9)
strict = models.BooleanField()
dice_challenge = models.IntegerField(
null=True,
blank=True,
validators=[
MaxValueValidator(999),
MinValueValidator(1)
]
)
rolls_taken = models.IntegerField(
null=True,
blank=True,
validators=[
MaxValueValidator(99),
MinValueValidator(1)
]
)
class PageTitle(models.Model):
"""
Represents the title of a quest page.
@ -41,6 +73,7 @@ class PageTitle(models.Model):
def __str__(self):
return self.title
class Message(models.Model):
"""
Represents a chat message.