fixed new posts not displaying correctly, dicecall frontend

This commit is contained in:
iou1name 2018-07-04 17:20:47 -04:00
parent fd91294698
commit ad4a9793bb
3 changed files with 43 additions and 13 deletions

View File

@ -136,7 +136,11 @@ def insert_quest_post(quest_id, post, timestamp):
""" """
_DB.execute( _DB.execute(
"INSERT INTO `quest_data` (`quest_id`, `post`, `timestamp`) " \ "INSERT INTO `quest_data` (`quest_id`, `post`, `timestamp`) " \
"VALUES (%s, %s, %s)", (quest_id, post, timestamp)) + "VALUES (%s, %s, %s)", (quest_id, post, timestamp))
post_id = _DB.execute(
"SELECT `post_id` FROM `quest_data` WHERE `quest_id` = %s " \
+ "ORDER BY `post_id` DESC", (quest_id,)).fetchone()[0]
return post_id
def get_quest_meta(quest_id=None, ident_title=None): def get_quest_meta(quest_id=None, ident_title=None):

View File

@ -69,7 +69,9 @@ def new_post(data):
post = post.replace("\n", "<br />") post = post.replace("\n", "<br />")
post = tools.handle_img(post) post = tools.handle_img(post)
data["post"] = [post] data["post"] = [post]
db.insert_quest_post(room, post, int(time.time())) data["date"] = int(time.time())
post_id = db.insert_quest_post(room, post, data["date"])
data["post_id"] = post_id
emit("new_post", data, room=room) emit("new_post", data, room=room)
@ -96,8 +98,8 @@ def update_post(data):
emit("update_post", data, room=room) emit("update_post", data, room=room)
@socketio.on('dice_post') @socketio.on("dice_post")
def dice_post(data): def dice_(data):
""" """
Called when the QM posts a new dice call. Called when the QM posts a new dice call.
""" """
@ -108,4 +110,9 @@ def dice_post(data):
return return
if session.get("user_id") != res[3]: if session.get("user_id") != res[3]:
return return
date = int(time.time())
data["date"] = date
data["post_id"] = 0
emit("dice_post", data, room=room) emit("dice_post", data, room=room)

View File

@ -15,13 +15,9 @@
socket.emit('joined', {room: {{ room_id }}}); socket.emit('joined', {room: {{ room_id }}});
}); });
socket.on('message', function(data) { socket.on('message', function(data) {
let date = new Date(data.date * 1000); let msg_str = '<div class="messageHeader"><span class="messageName">' + data.name + '</span> ';
let date_str = date.getFullYear() + '-' + padToTwo(date.getMonth()+1) + '-' + padToTwo(date.getDate()) + ' '; msg_str += '<span class="messageDate">' + strftime(data.date) + '</span></div>';
date_str += padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds()); msg_str += '<div class="message">' + data.message + '</div><hr>';
let msg_str = '<div class="messageHeader">\n<span class="messageName">' + data.name + '</span> ';
msg_str += '<span class="messageDate">' + date_str + '</span>\n</div>\n';
msg_str += '<div class="message">' + data.message + '</div>\n<hr>';
let mbox = document.getElementById('chatWindow'); let mbox = document.getElementById('chatWindow');
mbox.innerHTML = mbox.innerHTML + msg_str; mbox.innerHTML = mbox.innerHTML + msg_str;
@ -29,13 +25,30 @@
}); });
socket.on('new_post', function(data) { socket.on('new_post', function(data) {
let qposts = document.getElementById('questPosts'); let qposts = document.getElementById('questPosts');
qposts.innerHTML = qposts.innerHTML + '<div class="questPost">' + data.post + '</div>'; let post_str = '<div class="questPost"><div class="questPostMeta">' + strftime(data.date);
{% if session.get("user_id") == owner_id %}
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>';
{% endif %}
post_str += '</div><div class="questPostData" id="questPostData-' + data.post_id + '">' + data.post + '</div></div>';
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;
document.getElementById('questPostData-' + post_id).innerHTML = data.post; document.getElementById('questPostData-' + post_id).innerHTML = data.post;
}); });
socket.on('dice_post', function(data) { socket.on('dice_post', function(data) {
let qposts = document.getElementById('questPosts');
let post_str = '<div class="questPost"><div class="questPostMeta">' + strftime(data.date) + "</div>";
post_str += '<div class="questPostData" id="questPostData-' + data.post_id + '">'
post_str += "<h3>Roll " + data.diceNum + "d" + data.diceSides;
if (data.diceMod > 0) { post_str += "+"; }
if (data.diceMod != 0) { post_str += data.diceMod; }
if (data.diceChal) { post_str += " vs DC" + data.diceChal; }
post_str += "</h3><br />";
post_str += "</div></div>";
qposts.innerHTML = qposts.innerHTML + post_str;
}); });
let mtarea = document.getElementById('messageTextArea'); let mtarea = document.getElementById('messageTextArea');
mtarea.addEventListener('keypress', function(event) { mtarea.addEventListener('keypress', function(event) {
@ -53,6 +66,12 @@
if (number<=99) { number = ("0"+number).slice(-2); } if (number<=99) { number = ("0"+number).slice(-2); }
return number; 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;
}
</script> </script>
{% if session.get("user_id") == owner_id %} {% if session.get("user_id") == owner_id %}
<script> <script>
@ -78,11 +97,11 @@
} }
function dice_post() { function dice_post() {
let formData = new FormData(document.getElementById('QMDicePostForm')); let formData = new FormData(document.getElementById('QMDicePostForm'));
formData.append('room', {{ room_id }});
let obj = {}; let obj = {};
formData.forEach(function(value, key) { formData.forEach(function(value, key) {
obj[key] = value; obj[key] = value;
}); });
obj.room = {{ room_id }};
socket.emit('dice_post', obj); socket.emit('dice_post', obj);
document.getElementById('QMDicePostForm').reset(); document.getElementById('QMDicePostForm').reset();
} }