diff --git a/anonkun.sql b/anonkun.sql
index 613b23a..60067e4 100644
--- a/anonkun.sql
+++ b/anonkun.sql
@@ -11,12 +11,14 @@ CREATE TABLE `quest_meta` (
`canon_title` VARCHAR(300) DEFAULT NULL,
`ident_title` VARCHAR(300) DEFAULT NULL,
`owner_id` SMALLINT UNSIGNED DEFAULT NULL,
+ `dice_call` SMALLINT UNSIGNED DEFAULT NULL,
PRIMARY KEY (`quest_id`)
) ENGINE=InnoDB CHARSET utf8mb4;
CREATE TABLE `quest_data` (
`post_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`quest_id` SMALLINT UNSIGNED NOT NULL,
+ `post_type` ENUM('text', 'dice', 'poll') NOT NULL,
`post` MEDIUMTEXT NOT NULL,
`timestamp` INT UNSIGNED NOT NULL,
PRIMARY KEY (`post_id`)
diff --git a/database.py b/database.py
index f063225..ff712f2 100644
--- a/database.py
+++ b/database.py
@@ -108,9 +108,7 @@ def log_chat_message(data):
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(
"SELECT * FROM `chat_messages` WHERE `room_id` = %s " \
+ "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):
- """
- Creates a new quest entry.
- """
+ """Creates a new quest entry."""
_DB.execute(
"INSERT INTO `quest_meta` (`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
-def insert_quest_post(quest_id, post, timestamp):
- """
- Insers a new quest post.
- """
+def insert_quest_post(quest_id, post_type, post, timestamp):
+ """Insers a new quest post."""
_DB.execute(
- "INSERT INTO `quest_data` (`quest_id`, `post`, `timestamp`) " \
- + "VALUES (%s, %s, %s)", (quest_id, post, timestamp))
+ "INSERT INTO `quest_data`" \
+ + "(`quest_id`, `post_type`, `post`, `timestamp`) " \
+ + "VALUES (%s, %s, %s, %s)", (quest_id, post_type, 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]
@@ -161,18 +156,35 @@ def get_quest_meta(quest_id=None, ident_title=None):
def get_quest_data(quest_id):
- """
- Retrieves all quest posts.
- """
- data = _DB.execute("SELECT * FROM `quest_data` WHERE `quest_id` = %s",
+ """Retrieves all quest posts."""
+ data = _DB.execute(
+ "SELECT * FROM `quest_data` WHERE `quest_id` = %s " \
+ + "ORDER BY `post_id` ASC",
(quest_id,)).fetchall()
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):
- """
- Retrives relevant user data.
- """
+ """Retrives relevant user data."""
data = _DB.execute(
"SELECT `user_id`, `signup_date` FROM `users` WHERE `username` = %s",
(username,)).fetchone()
@@ -180,9 +192,7 @@ def get_user_info(username):
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(
"SELECT * FROM `quest_meta` WHERE `owner_id` = %s",
(user_id,)).fetchall()
@@ -190,9 +200,29 @@ def get_user_quests(user_id):
def update_quest_post(post_id, new_post):
- """
- Updates a quest post.
- """
+ """Updates a quest post."""
_DB.execute(
"UPDATE `quest_data` SET `post` = %s WHERE `post_id` = %s",
(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
diff --git a/events.py b/events.py
index d3df23e..b224ab3 100644
--- a/events.py
+++ b/events.py
@@ -53,22 +53,21 @@ def message(data):
lines.append(line)
message = "
".join(lines)
message = tools.handle_img(message)
- if message.startswith("/dice") or message.startswith("/roll"):
- roll_msg = handle_dice(message)
- if roll_msg:
- message += '