dice rolls displayed on the dice call post

This commit is contained in:
iou1name 2018-07-07 13:43:58 -04:00
parent da36e2ac1e
commit 051e7b9209
5 changed files with 99 additions and 43 deletions

View File

@ -11,12 +11,14 @@ CREATE TABLE `quest_meta` (
`canon_title` VARCHAR(300) DEFAULT NULL, `canon_title` VARCHAR(300) DEFAULT NULL,
`ident_title` VARCHAR(300) DEFAULT NULL, `ident_title` VARCHAR(300) DEFAULT NULL,
`owner_id` SMALLINT UNSIGNED DEFAULT NULL, `owner_id` SMALLINT UNSIGNED DEFAULT NULL,
`dice_call` SMALLINT UNSIGNED DEFAULT NULL,
PRIMARY KEY (`quest_id`) PRIMARY KEY (`quest_id`)
) ENGINE=InnoDB CHARSET utf8mb4; ) ENGINE=InnoDB CHARSET utf8mb4;
CREATE TABLE `quest_data` ( CREATE TABLE `quest_data` (
`post_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, `post_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`quest_id` SMALLINT UNSIGNED NOT NULL, `quest_id` SMALLINT UNSIGNED NOT NULL,
`post_type` ENUM('text', 'dice', 'poll') NOT NULL,
`post` MEDIUMTEXT NOT NULL, `post` MEDIUMTEXT NOT NULL,
`timestamp` INT UNSIGNED NOT NULL, `timestamp` INT UNSIGNED NOT NULL,
PRIMARY KEY (`post_id`) PRIMARY KEY (`post_id`)

View File

@ -108,9 +108,7 @@ def log_chat_message(data):
def get_chat_messages(room_id): def get_chat_messages(room_id):
""" """Retrieves all chat messages for the provided room_id."""
Retrieves all chat messages for the provided room_id.
"""
res = _DB.execute( res = _DB.execute(
"SELECT * FROM `chat_messages` WHERE `room_id` = %s " \ "SELECT * FROM `chat_messages` WHERE `room_id` = %s " \
+ "ORDER BY `date` ASC", (room_id,)).fetchall() + "ORDER BY `date` ASC", (room_id,)).fetchall()
@ -118,9 +116,7 @@ def get_chat_messages(room_id):
def insert_quest(canon_title, ident_title, owner_id): def insert_quest(canon_title, ident_title, owner_id):
""" """Creates a new quest entry."""
Creates a new quest entry.
"""
_DB.execute( _DB.execute(
"INSERT INTO `quest_meta` (`canon_title`, `ident_title`, `owner_id`) "\ "INSERT INTO `quest_meta` (`canon_title`, `ident_title`, `owner_id`) "\
+ "VALUES (%s, %s, %s)", (canon_title, ident_title, owner_id)) + "VALUES (%s, %s, %s)", (canon_title, ident_title, owner_id))
@ -130,13 +126,12 @@ def insert_quest(canon_title, ident_title, owner_id):
return quest_id return quest_id
def insert_quest_post(quest_id, post, timestamp): def insert_quest_post(quest_id, post_type, post, timestamp):
""" """Insers a new quest post."""
Insers a new quest post.
"""
_DB.execute( _DB.execute(
"INSERT INTO `quest_data` (`quest_id`, `post`, `timestamp`) " \ "INSERT INTO `quest_data`" \
+ "VALUES (%s, %s, %s)", (quest_id, post, timestamp)) + "(`quest_id`, `post_type`, `post`, `timestamp`) " \
+ "VALUES (%s, %s, %s, %s)", (quest_id, post_type, post, timestamp))
post_id = _DB.execute( post_id = _DB.execute(
"SELECT `post_id` FROM `quest_data` WHERE `quest_id` = %s " \ "SELECT `post_id` FROM `quest_data` WHERE `quest_id` = %s " \
+ "ORDER BY `post_id` DESC", (quest_id,)).fetchone()[0] + "ORDER BY `post_id` DESC", (quest_id,)).fetchone()[0]
@ -161,18 +156,35 @@ def get_quest_meta(quest_id=None, ident_title=None):
def get_quest_data(quest_id): def get_quest_data(quest_id):
""" """Retrieves all quest posts."""
Retrieves all quest posts. data = _DB.execute(
""" "SELECT * FROM `quest_data` WHERE `quest_id` = %s " \
data = _DB.execute("SELECT * FROM `quest_data` WHERE `quest_id` = %s", + "ORDER BY `post_id` ASC",
(quest_id,)).fetchall() (quest_id,)).fetchall()
return data return data
def get_quest_post(post_id=None, quest_id=None):
"""
Retrieves the post data for the given post_id. If no post_id is given,
it returns the most recent post.
"""
if post_id:
data = _DB.execute(
"SELECT * FROM `quest_data` WHERE `post_id` = %s",
(post_id,)).fetchone()
elif quest_id:
data = _DB.execute(
"SELECT * FROM `quest_data` WHERE `quest_id` = %s "\
+ "ORDER BY `post_id` DESC",
(quest_id,)).fetchone()
else:
return
return data
def get_user_info(username): def get_user_info(username):
""" """Retrives relevant user data."""
Retrives relevant user data.
"""
data = _DB.execute( data = _DB.execute(
"SELECT `user_id`, `signup_date` FROM `users` WHERE `username` = %s", "SELECT `user_id`, `signup_date` FROM `users` WHERE `username` = %s",
(username,)).fetchone() (username,)).fetchone()
@ -180,9 +192,7 @@ def get_user_info(username):
def get_user_quests(user_id): def get_user_quests(user_id):
""" """Retrieves all quests ran by a particular user_id."""
Retrieves all quests ran by a particular user_id.
"""
data = _DB.execute( data = _DB.execute(
"SELECT * FROM `quest_meta` WHERE `owner_id` = %s", "SELECT * FROM `quest_meta` WHERE `owner_id` = %s",
(user_id,)).fetchall() (user_id,)).fetchall()
@ -190,9 +200,29 @@ def get_user_quests(user_id):
def update_quest_post(post_id, new_post): def update_quest_post(post_id, new_post):
""" """Updates a quest post."""
Updates a quest post.
"""
_DB.execute( _DB.execute(
"UPDATE `quest_data` SET `post` = %s WHERE `post_id` = %s", "UPDATE `quest_data` SET `post` = %s WHERE `post_id` = %s",
(new_post, post_id)) (new_post, post_id))
def set_dice_call_open(quest_id, post_id):
"""Sets an open dice call for the given quest."""
_DB.execute(
"UPDATE `quest_meta` SET `dice_call` = %s WHERE `quest_id` = %s",
(post_id, quest_id))
def set_dice_call_closed(quest_id):
"""Closes a quest's dice call."""
_DB.execute(
"UPDATE `quest_meta` SET `dice_call` = NULL WHERE `quest_id` = %s",
(quest_id,))
def get_dice_call(quest_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()
return data

View File

@ -53,22 +53,21 @@ def message(data):
lines.append(line) lines.append(line)
message = "<br />".join(lines) message = "<br />".join(lines)
message = tools.handle_img(message) message = tools.handle_img(message)
if message.startswith("/dice") or message.startswith("/roll"):
roll_msg = handle_dice(message)
if roll_msg:
message += '<hr class="msgSrvHr" />' + roll_msg
data["message"] = message data["message"] = message
if message.startswith("/dice") or message.startswith("/roll"):
roll_msg = handle_dice(data)
if roll_msg:
data["message"] += '<hr class="msgSrvHr" />' + roll_msg
db.log_chat_message(data) db.log_chat_message(data)
emit("message", data, room=room) emit("message", data, room=room)
def handle_dice(message): def handle_dice(data):
""" """
Handle /dice or /roll messages. Handle /dice or /roll messages.
""" """
reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", message) reg = re.search(r"(\d+)d(\d+)([+-]\d+)?", data["message"])
if not reg: if not reg:
return return
try: try:
@ -89,6 +88,19 @@ def handle_dice(message):
msg += " +" msg += " +"
msg += " " + str(diceMod) msg += " " + str(diceMod)
msg += " = " + str(total) + "</b>" msg += " = " + str(total) + "</b>"
dice_call = db.get_dice_call(data["room"])
if dice_call:
post = db.get_quest_post(post_id=dice_call)[3]
post += "<br />" + msg
db.update_quest_post(dice_call, post)
room = data["room"]
data = {}
data["post"] = post
data["post_id"] = dice_call
emit("update_post", data, room=room)
return msg return msg
@ -105,18 +117,16 @@ def new_post(data, internal=False):
return return
post = data["post"] post = data["post"]
if not internal:
post = bleach.clean(post.strip()) post = bleach.clean(post.strip())
post = post.replace("\n", "<br />") post = post.replace("\n", "<br />")
post = tools.handle_img(post) post = tools.handle_img(post)
editable = data.get("editable", True)
data = {} data = {}
data["post"] = [post] data["post"] = [post]
data["post_type"] = "text"
data["date"] = int(time.time()) data["date"] = int(time.time())
post_id = db.insert_quest_post(room, post, data["date"]) post_id = db.insert_quest_post(room, "text", post, data["date"])
data["post_id"] = post_id data["post_id"] = post_id
data["editable"] = editable # TODO: enforce this on server-side
emit("new_post", data, room=room) emit("new_post", data, room=room)
@ -181,5 +191,14 @@ def dice_post(data):
post += str(diceMod) post += str(diceMod)
if diceChal: if diceChal:
post += " vs DC" + str(diceChal) post += " vs DC" + str(diceChal)
post += "</h3>"
new_post({"post": post, "room": room, "editable": False}, internal=True) data = {}
data["post"] = post
data["post_type"] = "dice"
data["date"] = int(time.time())
post_id = db.insert_quest_post(room, "dice", post, data["date"])
data["post_id"] = post_id
db.set_dice_call_open(room, post_id)
emit("new_post", data, room=room)

View File

@ -27,7 +27,7 @@
let qposts = document.getElementById('questPosts'); let qposts = document.getElementById('questPosts');
let post_str = '<div class="questPost"><div class="questPostMeta">' + strftime(data.date); let post_str = '<div class="questPost"><div class="questPostMeta">' + strftime(data.date);
{% if session.get("user_id") == owner_id %} {% if session.get("user_id") == owner_id %}
if (data.editable) { if (data.post_type == 'text') {
post_str += '<br /><a href="javascript:void(0);" onclick="edit_post(\'' + data.post_id + '\')">Edit</a>'; 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>'; post_str += '<a href="javascript:void(0);" id="savePost-' + data.post_id + '" onclick="save_post(\'' + data.post_id + '\')" style="display:none;">Save</a>';
} }
@ -128,15 +128,17 @@
{% for quest_post in quest_posts %} {% for quest_post in quest_posts %}
<div class="questPost"> <div class="questPost">
<div class="questPostMeta"> <div class="questPostMeta">
{{ quest_post[3] | strftime }} {{ quest_post[4] | strftime }}
{% if session.get("user_id") == owner_id %} {% 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);" 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> <a href="javascript:void(0);" id="savePost-{{ quest_post[0] }}" onclick="save_post('{{ quest_post[0] }}')" style="display:none;">Save</a>
{% endif %} {% endif %}
{% endif %}
</div> </div>
<div class="questPostData" id="questPostData-{{ quest_post[0] }}"> <div class="questPostData" id="questPostData-{{ quest_post[0] }}">
{% autoescape false %} {% autoescape false %}
{{ quest_post[2] }} {{ quest_post[3] }}
{% endautoescape %} {% endautoescape %}
</div> </div>
</div><br /> </div><br />

View File

@ -51,7 +51,9 @@ def quest(quest_title):
data = db.get_quest_meta(ident_title=ident_title) data = db.get_quest_meta(ident_title=ident_title)
if not data: if not data:
abort(404) abort(404)
quest_id, quest_title, _, owner_id = data quest_id = data[0]
quest_title = data[1]
owner_id = data[3]
quest_posts = db.get_quest_data(quest_id) quest_posts = db.get_quest_data(quest_id)
messages = db.get_chat_messages(quest_id) messages = db.get_chat_messages(quest_id)
return render_template('quest.html', return render_template('quest.html',
@ -112,7 +114,8 @@ def edit_quest(quest_title):
data = db.get_quest_meta(ident_title=ident_title) data = db.get_quest_meta(ident_title=ident_title)
if not data: if not data:
abort(404) abort(404)
quest_id, quest_title, _, owner_id = data quest_id = data[0]
quest_title = data[1]
if request.method == "GET": if request.method == "GET":
return render_template("edit_quest.html", return render_template("edit_quest.html",
quest_title=quest_title, quest_title=quest_title,