qm can open and close dice calls

This commit is contained in:
iou1name 2018-07-08 17:50:54 -04:00
parent 513841bd89
commit 3a42f8dcc9
5 changed files with 122 additions and 47 deletions

View File

@ -84,16 +84,6 @@ def unix2string(unix):
return time.strftime(form, t)
@app.template_filter("filter_rolls")
def filter_rolls(quest_rolls, post_id):
"""
Filters quest's rolls and returns only the rolls applicable to the
given post_id.
"""
quest_rolls = [roll for roll in quest_rolls if roll[2] == post_id]
return quest_rolls
init()
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5050)

View File

@ -233,16 +233,15 @@ def set_dice_call_open(post_id, quest_id, new_call=None):
def set_dice_call_closed(quest_id):
"""Closes a quest's dice call."""
_DB.execute(
"UPDATE `quest_meta` SET `dice_call` = NULL, `dice_rolls` = NULL, " \
"`dice_strct` = NULL WHERE `quest_id` = %s",
"UPDATE `quest_meta` SET `dice_call` = NULL WHERE `quest_id` = %s",
(quest_id,))
def get_dice_call(quest_id):
def get_dice_call(post_id):
"""Retrives the currently open dice call, if there is one."""
data = _DB.execute(
"SELECT `dice_call` FROM `quest_meta` WHERE `quest_id` = %s",
(quest_id,)).fetchone()
"SELECT * FROM `quest_dice_calls` WHERE `post_id` = %s",
(post_id,)).fetchone()
return data

View File

@ -59,24 +59,25 @@ def message(data):
if message.startswith("/dice") or message.startswith("/roll"):
roll_msg = handle_dice(data)
if roll_msg:
data["message"] += '<hr class="msgSrvHr" />' + roll_msg
data["message"] += '<hr class="msgSrvHr" /><b>' + roll_msg + "</b>"
message_id = db.log_chat_message(data)
emit("message", data, room=room)
if roll_msg:
dice_call = db.get_dice_call(data["room"])[0]
if dice_call:
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)
roll_results = re.search(r"Rolled (.+)</b>", roll_msg).group(1)
roll_results = re.search(r"Rolled (.+)$", roll_msg).group(1)
db.insert_quest_roll(
message_id, room, dice_call, dice_roll, roll_results)
message_id, room, dice_call_id, dice_roll, roll_results)
room = data["room"]
data = {}
data["post"] = roll_msg + " (" + dice_roll + ")"
data["post_type"] = "dice"
data["post_id"] = dice_call
data["post_id"] = dice_call_id
emit("update_post", data, room=room)
@ -99,13 +100,13 @@ def handle_dice(data):
total = sum(dice)
if diceMod:
total += diceMod
msg = f"<b>Rolled {', '.join(map(str, dice))}"
msg = f"Rolled {', '.join(map(str, dice))}"
if diceMod:
if diceMod > 0:
msg += " +" + str(diceMod)
else:
msg += " - " + str(diceMod)[1:]
msg += " = " + str(total) + "</b>"
msg += " = " + str(total)
return msg
@ -198,10 +199,9 @@ def dice_post(data):
dice_roll += "+"
dice_roll += str(diceMod)
post = "<h3>Roll " + dice_roll
post = "Roll " + dice_roll
if diceChal:
post += " vs DC" + str(diceChal)
post += "</h3>"
data = {}
data["post"] = post
@ -213,3 +213,44 @@ def dice_post(data):
new_call = (dice_roll, diceStrict, diceChal, diceRolls)
db.set_dice_call_open(post_id, room, new_call)
emit("new_post", data, room=room)
@socketio.on('close_dice_call')
def close_dice_call(data):
"""
Closes an active dice call.
"""
room = data["room"]
res = db.get_quest_meta(quest_id=room)
if not res:
return
if session.get("user_id") != res[3]:
return
post_id = data.get("post_id")
db.set_dice_call_closed(room)
data = {}
data["post_id"] = post_id
emit("close_dice_call", data, room=room)
@socketio.on("open_dice_call")
def open_dice_call(data):
"""
Opens a closed dice call. This is only permitted if the dice call is
the last post in the quest.
"""
room = data["room"]
res = db.get_quest_meta(quest_id=room)
if not res:
return
if session.get("user_id") != res[3]:
return
post_id = data.get("post_id")
db.set_dice_call_open(post_id, room)
data = {}
data["post_id"] = post_id
emit("open_dice_call", data, room=room)

View File

@ -31,8 +31,19 @@
post_str += '<br /><a href="javascript:void(0);" onclick="edit_post(\'' + data.post_id + '\')">Edit</a>';
post_str += '<a href="javascript:void(0);" id="savePost-' + data.post_id + '" onclick="save_post(\'' + data.post_id + '\')" style="display:none;">Save</a>';
}
else if (data.post_type == 'dice') {
post_str += '<br /><a href="javascript:void(0);" id="close_dc-' + data.post_id + '" onclick="close_dice_call(' + data.post_id + ')">Close</a>';
post_str += '<a href="javascript:void(0);" id="open_dc-' + data.post_id + '" onclick="open_dice_call(' + data.post_id + ')" style="display:none;">Open</a>'
}
{% endif %}
post_str += '</div><div class="questPostData" id="questPostData-' + data.post_id + '">' + data.post + '</div></div><br />';
post_str += '</div><div class="questPostData" id="questPostData-' + data.post_id + '">';
if (data.post_type == 'text') {
post_str += data.post;
}
else if (data.post_type == 'dice') {
post_str += '<h3>' + data.post + ' - Open</h3>';
}
post_str += '</div></div><br />';
qposts.innerHTML = qposts.innerHTML + post_str;
});
socket.on('update_post', function(data) {
@ -45,6 +56,14 @@
post.innerHTML += '<b>' + data.post + '</b><br />';
}
});
socket.on('close_dice_call', function(data) {
let post = document.getElementById('questPostData-' + data.post_id);
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Open', 'Closed');
});
socket.on('open_dice_call', function(data) {
let post = document.getElementById('questPostData-' + data.post_id);
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
});
let mtarea = document.getElementById('messageTextArea');
mtarea.addEventListener('keypress', function(event) {
if (event.key == 'Enter' && !event.shiftKey) {
@ -82,6 +101,7 @@
document.execCommand("defaultParagraphSeparator", false, "br");
document.getElementById('questPostData-' + post_id).style.border = '1px solid #ccc';
document.getElementById('savePost-' + post_id).style.display = 'initial';
document.getElementById('editPost-' + post_id).style.display = 'none';
}
function save_post(post_id) {
document.getElementById('questPostData-' + post_id).contentEditable = 'false';
@ -89,6 +109,7 @@
let text = document.getElementById('questPostData-' + post_id).innerHTML;
socket.emit('update_post', {post_id: post_id, post: text, room: {{ room_id }}});
document.getElementById('savePost-' + post_id).style.display = 'none';
document.getElementById('editPost-' + post_id).style.display = 'initial';
}
function dice_post() {
let formData = new FormData(document.getElementById('QMDicePostForm'));
@ -100,6 +121,16 @@
socket.emit('dice_post', obj);
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 }}});
}
</script>
<script>
function openPostTab(event, modeName) {
@ -137,19 +168,31 @@
{{ quest_post[4] | strftime }}
{% if session.get("user_id") == owner_id %}
{% if quest_post[2] == "text" %}
<br /><a href="javascript:void(0);" onclick="edit_post('{{ quest_post[0] }}')">Edit</a>
<br /><a href="javascript:void(0);" id="editPost-{{ quest_post[0] }}" onclick="edit_post({{ quest_post[0] }})">Edit</a>
<a href="javascript:void(0);" id="savePost-{{ quest_post[0] }}" onclick="save_post('{{ quest_post[0] }}')" style="display:none;">Save</a>
{% elif quest_post[2] == "dice" and quest_post == quest_posts|last %}
{% if quest_post[0] == dice_call %}
<br /><a href="javascript:void(0);" id="close_dc-{{ quest_post[0] }}" onclick="close_dice_call({{ quest_post[0] }})">Close</a>
<a href="javascript:void(0);" id="open_dc-{{ quest_post[0] }}" onclick="open_dice_call({{ quest_post[0] }})" style="display:none;">Open</a>
{% else %}
<br /><a href="javascript:void(0);" id="close_dc-{{ quest_post[0] }}" onclick="close_dice_call({{ quest_post[0] }})" style="display:none;">Close</a>
<a href="javascript:void(0);" id="open_dc-{{ quest_post[0] }}" onclick="open_dice_call({{ quest_post[0] }})">Open</a>
{% endif %}
{% endif %}
{% endif %}
</div>
<div class="questPostData" id="questPostData-{{ quest_post[0] }}">
{% if quest_post[2] == "text" %}
{% autoescape false %}
{{ quest_post[3] }}
{% endautoescape %}
{% if quest_post[2] == "dice" %}
{% elif quest_post[2] == "dice" %}
<h3>{{ quest_post[3] }} - {% if quest_post[0] == dice_call %}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></div>
<div id="questRollId-{{ quest_roll[0] }}">
<b>Rolled {{ quest_roll[4] }} ({{ quest_roll[3] }})</b>
</div>
{% endif %}
{% endfor %}
{% endif %}

View File

@ -54,6 +54,7 @@ def quest(quest_title):
quest_id = data[0]
quest_title = data[1]
owner_id = data[3]
dice_call = 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)
@ -63,10 +64,29 @@ def quest(quest_title):
quest_posts=quest_posts,
quest_rolls=quest_rolls,
owner_id=owner_id,
dice_call=dice_call,
room_id=quest_id,
messages=messages)
@views.route("/quest/<path:quest_title>/edit_quest", methods=["GET", "POST"])
def edit_quest(quest_title):
"""
Allows the quest owner to edit the quest.
"""
ident_title = quest_title
data = db.get_quest_meta(ident_title=ident_title)
if not data:
abort(404)
quest_id = data[0]
quest_title = data[1]
if request.method == "GET":
return render_template("edit_quest.html",
quest_title=quest_title,
ident_title=ident_title)
return redirect(url_for(".quest", quest_title=ident_title))
@views.route("/profile/<path:username>")
def profile(username):
"""
@ -107,24 +127,6 @@ def create_quest():
return redirect(url_for('.quest', quest_title=ident_title))
@views.route("/quest/<path:quest_title>/edit_quest", methods=["GET", "POST"])
def edit_quest(quest_title):
"""
Allows the quest owner to edit the quest.
"""
ident_title = quest_title
data = db.get_quest_meta(ident_title=ident_title)
if not data:
abort(404)
quest_id = data[0]
quest_title = data[1]
if request.method == "GET":
return render_template("edit_quest.html",
quest_title=quest_title,
ident_title=ident_title)
return redirect(url_for(".quest", quest_title=ident_title))
@views.route("/set_session")
def set_session():
"""