merged User.js and QM.js and various bugfixes

This commit is contained in:
iou1name 2018-07-26 20:37:29 -04:00
parent 41dae13d44
commit 209d759dcd
5 changed files with 225 additions and 351 deletions

View File

@ -85,12 +85,10 @@ def message(data):
if message.startswith("/dice") or message.startswith("/roll"): if message.startswith("/dice") or message.startswith("/roll"):
roll_msg = handle_dice(data) roll_msg = handle_dice(data)
if roll_msg: if roll_msg:
roll_msg += '<hr class="msgSrvHr" /><b>' + roll_msg + "</b>" message += '<hr class="msgSrvHr" /><b>' + roll_msg + "</b>"
date = int(time.time()) date = int(time.time())
message_id = db.log_chat_message(message, date, room, user_id) message_id = db.log_chat_message(message, date, room, user_id)
if roll_msg:
message += roll_msg
data = {} data = {}
data["date"] = date data["date"] = date
@ -101,22 +99,21 @@ def message(data):
emit("message", data, room=room) emit("message", data, room=room)
if roll_msg: if roll_msg:
dice_call_id = db.get_quest_meta(data["room"])[4] open_post_id = db.get_quest_meta(room)[3]
if dice_call_id: if open_post_id:
dice_call = db.get_dice_call(dice_call_id) dice_call = db.get_dice_call(open_post_id)
dice_roll = re.search(r"(\d+d\d+(?:[+-]\d+)?)", message).group(1) dice_roll = re.search(r"(\d+d\d+(?:[+-]\d+)?)", message).group(1)
if dice_call[2] and dice_roll != dice_call[1]: if dice_call[2] and dice_roll != dice_call[1]:
return return
roll_results = re.search(r"Rolled (.+) =", roll_msg).group(1) roll_results = re.search(r"Rolled (.+) =", roll_msg).group(1)
roll_total = int(re.search(r"= (\d+)$", roll_msg).group(1)) roll_total = int(re.search(r"= (\d+)", roll_msg).group(1))
roll_data = (dice_roll, roll_results, roll_total) roll_data = (dice_roll, roll_results, roll_total)
db.insert_quest_roll(message_id, room, dice_call_id, roll_data) db.insert_quest_roll(message_id, room, open_post_id, roll_data)
if len(db.get_dice_rolls(post_id=dice_call_id)) == dice_call[4]: if len(db.get_dice_rolls(post_id=open_post_id)) == dice_call[4]:
db.set_post_closed(room) db.set_post_closed(room)
emit("close_dice_call", {"post_id": dice_call_id}, room=room) emit("close_post", {"post_id": open_post_id}, room=room)
room = data["room"]
data = {} data = {}
data["post"] = roll_msg + " (" + dice_roll + ")" data["post"] = roll_msg + " (" + dice_roll + ")"
if dice_call[3]: if dice_call[3]:
@ -125,7 +122,7 @@ def message(data):
else: else:
data["post"] += " - Fail" data["post"] += " - Fail"
data["post_type"] = "dice" data["post_type"] = "dice"
data["post_id"] = dice_call_id data["post_id"] = open_post_id
emit("update_post", data, room=room) emit("update_post", data, room=room)

View File

@ -1,28 +1,5 @@
document.execCommand("defaultParagraphSeparator", false, "br"); socket.removeListener('new_post');
var socket; socket.on('new_post', function(data) {
var tid = setInterval( function () {
if ( document.readyState !== 'complete' ) return;
clearInterval( tid );
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
socket = io.connect('https://' + document.domain + location.port);
socket.on('connect', function() {
socket.emit('joined', {room: room_id});
});
socket.on('message', function(data) {
let msg_str = '<div id="msg-' + data.message_id + '" class="message">';
msg_str = '<div class="messageHeader"><span class="messageName">' + data.name + '</span> ';
msg_str += '<span class="messageDate">' + strftime(data.date) + '</span> ';
msg_str += '<span class="messageID">No.<a href="javascript:quote(' + data.message_id + ')">' + data.message_id + '</a></span></div>';
msg_str += '<div class="messageContent">' + data.message + '</div></div><hr>';
let mbox = document.getElementById('chatWindow');
mbox.innerHTML = mbox.innerHTML + msg_str;
if ((mbox.scrollTop + mbox.offsetHeight) >= (mbox.scrollHeight - mbox.clientHeight / 2)) {
mbox.scrollTop = mbox.scrollHeight;
}
});
socket.on('new_post', function(data) {
deactivate_post(); deactivate_post();
let qposts = document.getElementById('questPosts'); let qposts = document.getElementById('questPosts');
let post_str = '<div class="questPost '; let post_str = '<div class="questPost ';
@ -50,7 +27,7 @@ var tid = setInterval( function () {
post_str += '<h3>' + data.post + ' - Open</h3>'; post_str += '<h3>' + data.post + ' - Open</h3>';
} else if (data.post_type == 'poll') { } else if (data.post_type == 'poll') {
post_str += '<h3>' + data.post + ' - Open</h3>'; post_str += '<h3>' + data.post + ' - Open</h3>';
post_str += '<table class="poll" id="post-' + data.post_id + '">'; post_str += '<table class="poll" id="poll-' + data.post_id + '">';
post_str += '<col/><col/><col/>'; post_str += '<col/><col/><col/>';
for (i = 0; i < data.options.length; i++) { for (i = 0; i < data.options.length; i++) {
post_str += '<tr id="optionRow-' + data.options[i][0] + '">'; post_str += '<tr id="optionRow-' + data.options[i][0] + '">';
@ -68,35 +45,15 @@ var tid = setInterval( function () {
} }
post_str += '</div></div><br />'; post_str += '</div></div><br />';
qposts.innerHTML = qposts.innerHTML + post_str; qposts.innerHTML = qposts.innerHTML + post_str;
}); });
socket.on('update_post', function(data) { socket.removeListener('close_post');
let post_id = data.post_id; socket.on('close_post', function(data) {
let post = document.getElementById('questPostData-' + post_id);
if (data.post_type === 'text') {
post.innerHTML = data.post;
} else if (data.post_type === 'dice') {
post.innerHTML += '<b>' + data.post + '</b><br />';
} else if (data.post_type === 'poll') {
let row = post.children[1].insertRow(-1);
let cell = row.insertCell(0);
cell.className = 'pollCheckBox';
cell.innerHTML = '<input type="checkbox" id="pollInput-' + data.options_id + '" onchange="pollVote(' + data.post_id + ',' + data.options_id + ')"/>';
cell.innerHTML += '<label for="pollInput-' + data.options_id + '"></label>';
cell = row.insertCell(1);
cell.className = 'option_text';
cell.innerHTML = data.option_text;
cell = row.insertCell(2);
cell.className = "optionVotes";
cell.innerHTML = "0";
}
});
socket.on('close_post', function(data) {
let post = document.getElementById('questPostData-' + data.post_id); let post = document.getElementById('questPostData-' + data.post_id);
post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed'); post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed');
/* QM only */
document.getElementById('close_post_id-' + data.post_id).style.display = 'none'; document.getElementById('close_post_id-' + data.post_id).style.display = 'none';
document.getElementById('open_post_id-' + data.post_id).style.display = 'initial'; document.getElementById('open_post_id-' + data.post_id).style.display = 'initial';
/* end QM only */
if (post.parentElement.classList.contains('pollPost')) { if (post.parentElement.classList.contains('pollPost')) {
let table = document.getElementById('poll-' + data.post_id); let table = document.getElementById('poll-' + data.post_id);
table.getElementsByTagName("col")[0].style.visibility = 'collapse'; table.getElementsByTagName("col")[0].style.visibility = 'collapse';
@ -105,12 +62,15 @@ var tid = setInterval( function () {
writein.style.display = 'none'; writein.style.display = 'none';
} }
} }
}); });
socket.on('open_post', function(data) { socket.removeListener('open_post');
socket.on('open_post', function(data) {
let post = document.getElementById('questPostData-' + data.post_id); let post = document.getElementById('questPostData-' + data.post_id);
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open'); post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
/* QM only */
document.getElementById('close_post_id-' + data.post_id).style.display = 'initial'; document.getElementById('close_post_id-' + data.post_id).style.display = 'initial';
document.getElementById('open_post_id-' + data.post_id).style.display = 'none'; document.getElementById('open_post_id-' + data.post_id).style.display = 'none';
/* end QM only */
if (post.parentElement.classList.contains('pollPost')) { if (post.parentElement.classList.contains('pollPost')) {
table = document.getElementById('poll-' + data.post_id); table = document.getElementById('poll-' + data.post_id);
table.getElementsByTagName("col")[0].style.visibility = ''; table.getElementsByTagName("col")[0].style.visibility = '';
@ -119,89 +79,8 @@ var tid = setInterval( function () {
writein.style.display = 'initial'; writein.style.display = 'initial';
} }
} }
}); });
socket.on('vote', function(data) {
let row = document.getElementById('optionRow-' + data.option_id);
if (data.polarity) {
row.cells[2].textContent = Number(row.cells[2].textContent) + 1;
} else {
row.cells[2].textContent = Number(row.cells[2].textContent) - 1;
}
let table = row.parentElement.parentElement;
arr = Array.prototype.slice.call(table.rows);
arr.sort(sort_by_votes);
let new_tbody = document.createElement('tbody');
for (let i = 0; i < arr.length; i++) {
new_tbody.appendChild(arr[i]);
}
table.replaceChild(new_tbody, table.children[1]);
});
socket.on('toggle_option_box', function(data) {
document.getElementById('pollInput-' + data.option_id).checked = data.polarity;
});
let mtarea = document.getElementById('messageTextArea');
mtarea.addEventListener('keypress', function(event) {
if (event.key == 'Enter' && !event.shiftKey) {
let text = mtarea.value.trim();
mtarea.value = '';
if (text == '') { return; }
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
socket.emit('message', {message: text, name: 'Anonymous', room: room_id});
}
});
}, 100 );
function padToTwo(number) {
if (number<=99) { number = ("0"+number).slice(-2); }
return number;
}
function strftime(date) {
date = new Date(date * 1000);
let date_str = date.getFullYear() + '-' + padToTwo(date.getMonth()+1) + '-' + padToTwo(date.getDate()) + ' ';
date_str += padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds());
return date_str;
}
function sort_by_votes(a, b) {
return b.cells[2].textContent.localeCompare(a.cells[2].textContent);
}
function pollVote(post_id, option_id) {
let polarity = document.getElementById('pollInput-' + option_id).checked;
socket.emit('vote', {post_id: post_id, option_id: option_id, polarity: polarity, room: room_id});
}
function submitWritein(post_id) {
let writeinInput = document.getElementById('writeinInput');
if (!writeinInput) { return; }
let option_text = writeinInput.value;
writeinInput.value = '';
if (!option_text) { return; }
socket.emit('write_in', {option_text: option_text, post_id: post_id, room: room_id});
}
function quote(message_id) {
let textbox = document.getElementById('messageTextArea');
textbox.value += '>>' + message_id + '\n';
textbox.focus();
}
function scrollToMsg(message_id) {
let elem = document.getElementById('msg-' + message_id);
if (!elem) { return; }
elem.scrollIntoView();
}
function showPreview(event, message_id) {
let elem = document.getElementById('msg-' + message_id);
if (!elem) { return; }
let preview = document.getElementById('preview');
preview.innerHTML = elem.innerHTML;
preview.style.display = '';
let x = event.clientX + 20 + 'px';
let y = event.clientY + 20 + 'px';
let maxWidth = window.innerWidth - event.clientX - 80 + 'px';
preview.style.top = y;
preview.style.left = x;
preview.style.maxWidth = maxWidth;
}
function clearPreview() {
document.getElementById('preview').innerHTML = '';
document.getElementById('preview').style.display = 'none';
}
function deactivate_post() { function deactivate_post() {
let post = document.getElementsByClassName('active_post'); let post = document.getElementsByClassName('active_post');
if (post.length == 0) { return; } if (post.length == 0) { return; }

View File

@ -1,15 +1,10 @@
document.execCommand("defaultParagraphSeparator", false, "br"); document.execCommand("defaultParagraphSeparator", false, "br");
var socket; var socket;
var tid = setInterval( function () { socket = io.connect('https://' + document.domain + ':' + location.port);
if ( document.readyState !== 'complete' ) return; socket.on('connect', function() {
clearInterval( tid );
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
socket = io.connect('https://' + document.domain + ':' + location.port);
socket.on('connect', function() {
socket.emit('joined', {room: room_id}); socket.emit('joined', {room: room_id});
}); });
socket.on('message', function(data) { socket.on('message', function(data) {
let msg_str = '<div id="msg-' + data.message_id + '" class="message">'; let msg_str = '<div id="msg-' + data.message_id + '" class="message">';
msg_str = '<div class="messageHeader"><span class="messageName">' + data.name + '</span> '; msg_str = '<div class="messageHeader"><span class="messageName">' + data.name + '</span> ';
msg_str += '<span class="messageDate">' + strftime(data.date) + '</span> '; msg_str += '<span class="messageDate">' + strftime(data.date) + '</span> ';
@ -21,8 +16,8 @@ var tid = setInterval( function () {
if ((mbox.scrollTop + mbox.offsetHeight) >= (mbox.scrollHeight - mbox.clientHeight / 2)) { if ((mbox.scrollTop + mbox.offsetHeight) >= (mbox.scrollHeight - mbox.clientHeight / 2)) {
mbox.scrollTop = mbox.scrollHeight; mbox.scrollTop = mbox.scrollHeight;
} }
}); });
socket.on('new_post', function(data) { socket.on('new_post', function(data) {
deactivate_post(); deactivate_post();
let qposts = document.getElementById('questPosts'); let qposts = document.getElementById('questPosts');
let post_str = '<div class="questPost '; let post_str = '<div class="questPost ';
@ -41,7 +36,7 @@ var tid = setInterval( function () {
post_str += '<h3>' + data.post + ' - Open</h3>'; post_str += '<h3>' + data.post + ' - Open</h3>';
} else if (data.post_type == 'poll') { } else if (data.post_type == 'poll') {
post_str += '<h3>' + data.post + ' - Open</h3>'; post_str += '<h3>' + data.post + ' - Open</h3>';
post_str += '<table class="poll" id="post-' + data.post_id + '">'; post_str += '<table class="poll" id="poll-' + data.post_id + '">';
post_str += '<col/><col/><col/>'; post_str += '<col/><col/><col/>';
for (i = 0; i < data.options.length; i++) { for (i = 0; i < data.options.length; i++) {
post_str += '<tr id="optionRow-' + data.options[i][0] + '">'; post_str += '<tr id="optionRow-' + data.options[i][0] + '">';
@ -51,11 +46,16 @@ var tid = setInterval( function () {
post_str += '<td class="optionVotes">0</td></tr>'; post_str += '<td class="optionVotes">0</td></tr>';
} }
post_str += '</table>'; post_str += '</table>';
if (data.allow_writein) {
post_str += '<div id="writeinContainer">';
post_str += 'Write-in: <input type="text" id="writeinInput" placeholder="Custom choice..." maxlength="200" /><br />';
post_str += '<input type="submit" id="writeinSubmit" value="Submit" onclick="submitWritein({{ quest_post[0] }});"/></div>';
}
} }
post_str += '</div></div><br />'; post_str += '</div></div><br />';
qposts.innerHTML = qposts.innerHTML + post_str; qposts.innerHTML = qposts.innerHTML + post_str;
}); });
socket.on('update_post', function(data) { socket.on('update_post', function(data) {
let post_id = data.post_id; let post_id = data.post_id;
let post = document.getElementById('questPostData-' + post_id); let post = document.getElementById('questPostData-' + post_id);
if (data.post_type === 'text') { if (data.post_type === 'text') {
@ -77,12 +77,10 @@ var tid = setInterval( function () {
cell.className = "optionVotes"; cell.className = "optionVotes";
cell.innerHTML = "0"; cell.innerHTML = "0";
} }
}); });
socket.on('close_post', function(data) { socket.on('close_post', function(data) {
let post = document.getElementById('questPostData-' + data.post_id); let post = document.getElementById('questPostData-' + data.post_id);
post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed'); post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed');
document.getElementById('close_post_id-' + data.post_id).style.display = 'none';
document.getElementById('open_post_id-' + data.post_id).style.display = 'initial';
if (post.parentElement.classList.contains('pollPost')) { if (post.parentElement.classList.contains('pollPost')) {
let table = document.getElementById('poll-' + data.post_id); let table = document.getElementById('poll-' + data.post_id);
table.getElementsByTagName("col")[0].style.visibility = 'collapse'; table.getElementsByTagName("col")[0].style.visibility = 'collapse';
@ -91,12 +89,10 @@ var tid = setInterval( function () {
writein.style.display = 'none'; writein.style.display = 'none';
} }
} }
}); });
socket.on('open_post', function(data) { socket.on('open_post', function(data) {
let post = document.getElementById('questPostData-' + data.post_id); let post = document.getElementById('questPostData-' + data.post_id);
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open'); post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
document.getElementById('close_post_id-' + data.post_id).style.display = 'initial';
document.getElementById('open_post_id-' + data.post_id).style.display = 'none';
if (post.parentElement.classList.contains('pollPost')) { if (post.parentElement.classList.contains('pollPost')) {
table = document.getElementById('poll-' + data.post_id); table = document.getElementById('poll-' + data.post_id);
table.getElementsByTagName("col")[0].style.visibility = ''; table.getElementsByTagName("col")[0].style.visibility = '';
@ -105,8 +101,8 @@ var tid = setInterval( function () {
writein.style.display = 'initial'; writein.style.display = 'initial';
} }
} }
}); });
socket.on('vote', function(data) { socket.on('vote', function(data) {
let row = document.getElementById('optionRow-' + data.option_id); let row = document.getElementById('optionRow-' + data.option_id);
if (data.polarity) { if (data.polarity) {
row.cells[2].textContent = Number(row.cells[2].textContent) + 1; row.cells[2].textContent = Number(row.cells[2].textContent) + 1;
@ -121,21 +117,10 @@ var tid = setInterval( function () {
new_tbody.appendChild(arr[i]); new_tbody.appendChild(arr[i]);
} }
table.replaceChild(new_tbody, table.children[1]); table.replaceChild(new_tbody, table.children[1]);
}); });
socket.on('toggle_option_box', function(data) { socket.on('toggle_option_box', function(data) {
document.getElementById('pollInput-' + data.option_id).checked = data.polarity; document.getElementById('pollInput-' + data.option_id).checked = data.polarity;
}); });
let mtarea = document.getElementById('messageTextArea');
mtarea.addEventListener('keypress', function(event) {
if (event.key == 'Enter' && !event.shiftKey) {
let text = mtarea.value.trim();
mtarea.value = '';
if (text == '') { return; }
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
socket.emit('message', {message: text, name: 'Anonymous', room: room_id});
}
});
}, 100 );
function padToTwo(number) { function padToTwo(number) {
if (number<=99) { number = ("0"+number).slice(-2); } if (number<=99) { number = ("0"+number).slice(-2); }
return number; return number;
@ -199,3 +184,16 @@ function deactivate_post() {
writein.outerHTML = ''; writein.outerHTML = '';
} }
} }
function load() {
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
let mtarea = document.getElementById('messageTextArea');
mtarea.addEventListener('keypress', function(event) {
if (event.key == 'Enter' && !event.shiftKey) {
let text = mtarea.value.trim();
mtarea.value = '';
if (text == '') { return; }
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
socket.emit('message', {message: text, name: 'Anonymous', room: room_id});
}
});
}

View File

@ -3,15 +3,16 @@
{% block head %} {% block head %}
<script type="text/javascript" src="/static/socket.io.slim.js"></script> <script type="text/javascript" src="/static/socket.io.slim.js"></script>
<script>var room_id = {{ room_id }}; var page_num = {{ page_num }};</script> <script>var room_id = {{ room_id }}; var page_num = {{ page_num }};</script>
<script type="text/javascript" src="/static/anonkunUser.js"></script>
{% if session.get("user_id") == owner_id %} {% if session.get("user_id") == owner_id %}
<script type="text/javascript" src="/static/anonkunQM.js"></script> <script type="text/javascript" src="/static/anonkunQM.js"></script>
{% else %}
<script type="text/javascript" src="/static/anonkunUser.js"></script>
{% endif %} {% endif %}
<script>window.onload = load;</script>
{% endblock %} {% endblock %}
{% block header %} {% block header %}
{% if session.get("user_id") == owner_id %} {% if session.get("user_id") == owner_id %}
<li><a href="{{ url_for('.edit_quest', quest_id=quest_id) }}">Edit Quest</a></li> <li><a href="{{ url_for('.edit_quest', quest_id=quest_id) }}">Edit Quest</a></li>
{% endif %}
<li> <li>
<select onChange="window.location.href=this.value"> <select onChange="window.location.href=this.value">
{% for page in pages %} {% for page in pages %}
@ -19,7 +20,6 @@
{% endfor %} {% endfor %}
</select> </select>
</li> </li>
{% endif %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<div id="questContainer"> <div id="questContainer">
@ -58,7 +58,7 @@
{% endautoescape %} {% endautoescape %}
{% elif quest_post[3] == "dice" %} {% elif quest_post[3] == "dice" %}
<h3>{{ quest_post[4] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}</h3> <h3>{{ quest_post[4] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}</h3>
{% for dice_roll in dice_rolls[quest_post[0]] %} {% for dice_roll in dice_rolls.get(quest_post[0], []) %}
<div id="questRollId-{{ dice_roll[0] }}"> <div id="questRollId-{{ dice_roll[0] }}">
<b>Rolled {{ dice_roll[4] }} = {{ dice_roll[5] }} ({{ dice_roll[3] }}){% if quest_post[0]|dice_chal != 0 %} - {% if dice_roll[5] >= quest_post[0]|dice_chal %}Pass{% else %} Fail{% endif %}{% endif %}</b> <b>Rolled {{ dice_roll[4] }} = {{ dice_roll[5] }} ({{ dice_roll[3] }}){% if quest_post[0]|dice_chal != 0 %} - {% if dice_roll[5] >= quest_post[0]|dice_chal %}Pass{% else %} Fail{% endif %}{% endif %}</b>
</div> </div>

View File

@ -55,7 +55,7 @@ def quest(quest_id, page_num=1):
quest_id = room_id = data[0] quest_id = room_id = data[0]
quest_title = data[1] quest_title = data[1]
owner_id = data[2] owner_id = data[2]
open_post_id = data[2] open_post_id = data[3]
ip_address = request.remote_addr ip_address = request.remote_addr
pages = db.get_quest_pages(quest_id) pages = db.get_quest_pages(quest_id)