Compare commits

..

2 Commits

6 changed files with 52 additions and 22 deletions

View File

@ -10,8 +10,9 @@ from flask_paranoid import Paranoid
from flask_session import Session
from flask_socketio import SocketIO
from events import socketio
import database as db
from views import views
from events import socketio
class ReverseProxied(object):
"""
@ -84,6 +85,14 @@ def unix2string(unix):
return time.strftime(form, t)
@app.template_filter("dice_chal")
def get_dice_challenge(post_id):
"""
Returns the dice challenge for the given post_id.
"""
return db.get_dice_call(post_id)[3]
init()
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5050)

View File

@ -39,6 +39,7 @@ CREATE TABLE `quest_rolls` (
`post_id` MEDIUMINT UNSIGNED NOT NULL,
`roll_dice` TEXT NOT NULL,
`roll_results` TEXT NOT NULL,
`roll_total` SMALLINT UNSIGNED NOT NULL
PRIMARY KEY (`message_id`)
) ENGINE=InnoDB CHARSET utf8mb4;

View File

@ -245,19 +245,27 @@ def get_dice_call(post_id):
return data
def insert_quest_roll(message_id, quest_id, post_id, roll_dice, roll_results):
def insert_quest_roll(message_id, quest_id, post_id, roll_data):
"""Inserts a user roll into the `quest_rolls` table."""
ins = (message_id, quest_id, post_id) + roll_data
_DB.execute(
"INSERT INTO `quest_rolls`" \
+ "(`message_id`, `quest_id`, `post_id`, `roll_dice`, `roll_results`)"\
+ "VALUES (%s, %s, %s, %s, %s)",
(message_id, quest_id, post_id, roll_dice, roll_results))
+ "(`message_id`, `quest_id`, `post_id`, " \
+ "`roll_dice`, `roll_results`, `roll_total`)" \
+ "VALUES (%s, %s, %s, %s, %s, %s)",
(ins))
def get_quest_rolls(quest_id):
def get_quest_rolls(quest_id=None, post_id=None):
"""Gets all rolls for the given quest."""
data = _DB.execute(
"SELECT * FROM `quest_rolls` WHERE `quest_id` = %s " \
+ "ORDER BY `message_id` ASC",
(quest_id,)).fetchall()
if quest_id:
sql = "SELECT * FROM `quest_rolls` WHERE `quest_id` = %s "
ins = quest_id
elif post_id:
sql = "SELECT * FROM `quest_rolls` WHERE `post_id` = %s "
ins = post_id
else:
return
sql += "ORDER BY `message_id` ASC"
data = _DB.execute(sql, (ins,)).fetchall()
return data

View File

@ -69,13 +69,25 @@ def message(data):
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)
roll_results = re.search(r"Rolled (.+)$", roll_msg).group(1)
db.insert_quest_roll(
message_id, room, dice_call_id, dice_roll, roll_results)
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)
if len(db.get_quest_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"
data["post_id"] = dice_call_id
emit("update_post", data, room=room)

View File

@ -64,10 +64,14 @@
socket.on('close_dice_call', function(data) {
let post = document.getElementById('questPostData-' + data.post_id);
post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed');
document.getElementById('close_dc-' + data.post_id).style.display = 'none';
document.getElementById('open_dc-' + data.post_id).style.display = 'initial';
});
socket.on('open_dice_call', function(data) {
let post = document.getElementById('questPostData-' + data.post_id);
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
document.getElementById('close_dc-' + data.post_id).style.display = 'initial';
document.getElementById('open_dc-' + data.post_id).style.display = 'none';
});
let mtarea = document.getElementById('messageTextArea');
mtarea.addEventListener('keypress', function(event) {
@ -127,13 +131,9 @@
document.getElementById('QMDicePostForm').reset();
}
function close_dice_call(post_id) {
document.getElementById('close_dc-' + post_id).style.display = 'none';
document.getElementById('open_dc-' + post_id).style.display = 'initial';
socket.emit('close_dice_call', {post_id: post_id, room: {{ room_id }}});
}
function open_dice_call(post_id) {
document.getElementById('close_dc-' + post_id).style.display = 'initial';
document.getElementById('open_dc-' + post_id).style.display = 'none';
socket.emit('open_dice_call', {post_id: post_id, room: {{ room_id }}});
}
function deactivate_post() {
@ -207,11 +207,11 @@
{{ quest_post[3] }}
{% endautoescape %}
{% elif quest_post[2] == "dice" %}
<h3>{{ quest_post[3] }} - {% if quest_post[0] == dice_call %}Open{% else %}Closed{% endif %}</h3>
<h3>{{ quest_post[3] }} - {% if quest_post[0] == dice_call_id %}Open{% else %}Closed{% endif %}</h3>
{% for quest_roll in quest_rolls %}
{% if quest_roll[2] == quest_post[0] %}
<div id="questRollId-{{ quest_roll[0] }}">
<b>Rolled {{ quest_roll[4] }} ({{ quest_roll[3] }})</b>
<b>Rolled {{ quest_roll[4] }} = {{ quest_roll[5] }} ({{ quest_roll[3] }}){% if quest_post[0]|dice_chal != 0 %} - {% if quest_roll[5] >= quest_post[0]|dice_chal %}Pass{% else %} Fail{% endif %}{% endif %}</b>
</div>
{% endif %}
{% endfor %}

View File

@ -54,7 +54,7 @@ def quest(quest_title):
quest_id = data[0]
quest_title = data[1]
owner_id = data[3]
dice_call = data[4]
dice_call_id = data[4]
quest_posts = db.get_quest_data(quest_id)
quest_rolls = db.get_quest_rolls(quest_id)
messages = db.get_chat_messages(quest_id)
@ -64,7 +64,7 @@ def quest(quest_title):
quest_posts=quest_posts,
quest_rolls=quest_rolls,
owner_id=owner_id,
dice_call=dice_call,
dice_call_id=dice_call_id,
room_id=quest_id,
messages=messages)