diff --git a/anonkun.py b/anonkun.py
index 43730fe..06c296f 100644
--- a/anonkun.py
+++ b/anonkun.py
@@ -84,6 +84,16 @@ 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)
diff --git a/anonkun.sql b/anonkun.sql
index 60067e4..d2f05cc 100644
--- a/anonkun.sql
+++ b/anonkun.sql
@@ -16,7 +16,7 @@ CREATE TABLE `quest_meta` (
) ENGINE=InnoDB CHARSET utf8mb4;
CREATE TABLE `quest_data` (
- `post_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
+ `post_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
`quest_id` SMALLINT UNSIGNED NOT NULL,
`post_type` ENUM('text', 'dice', 'poll') NOT NULL,
`post` MEDIUMTEXT NOT NULL,
@@ -24,10 +24,29 @@ CREATE TABLE `quest_data` (
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB CHARSET utf8mb4;
+CREATE TABLE `quest_dice_calls` (
+ `post_id` MEDIUMINT UNSIGNED NOT NULL,
+ `dice_roll` TEXT NOT NULL,
+ `strict` BOOLEAN DEFAULT FALSE,
+ `dice_challenge` SMALLINT UNSIGNED,
+ `rolls_taken` TINYINT UNSIGNED,
+ PRIMARY KEY (`post_id`)
+) ENGINE=InnoDB CHARSET utf8mb4;
+
+CREATE TABLE `quest_rolls` (
+ `message_id` MEDIUMINT UNSIGNED NOT NULL,
+ `quest_id` SMALLINT UNSIGNED NOT NULL,
+ `post_id` MEDIUMINT UNSIGNED NOT NULL,
+ `roll_dice` TEXT NOT NULL,
+ `roll_results` TEXT NOT NULL,
+ PRIMARY KEY (`message_id`)
+) ENGINE=InnoDB CHARSET utf8mb4;
+
CREATE TABLE `chat_messages` (
- `room_id` SMALLINT UNSIGNED NOT NULL,
- `name` VARCHAR(20) NOT NULL,
+ `message_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
+ `room_id` MEDIUMINT UNSIGNED NOT NULL,
`name_id` SMALLINT UNSIGNED DEFAULT NULL,
`date` INT UNSIGNED NOT NULL,
- `message` TEXT NOT NULL
+ `message` TEXT NOT NULL,
+ PRIMARY KEY (`message_id`)
) ENGINE=InnoDB CHARSET utf8mb4;
diff --git a/database.py b/database.py
index ff712f2..117eedc 100644
--- a/database.py
+++ b/database.py
@@ -99,12 +99,18 @@ def log_chat_message(data):
message = data["message"]
date = data["date"]
room_id = data["room"]
- name = data["name"]
user_id = data.get("user_id")
_DB.execute(
"INSERT INTO `chat_messages` (" \
- + "`message`, `room_id`, `date`, `name`, `name_id`) VALUES (" \
- + "%s, %s, %s, %s, %s)", (message, room_id, date, name, user_id))
+ + "`message`, `room_id`, `date`, `name_id`) VALUES (" \
+ + "%s, %s, %s, %s)",
+ (message, room_id, date, user_id))
+
+ post_id = _DB.execute(
+ "SELECT `message_id` FROM `chat_messages` WHERE `room_id` = %s " \
+ + "ORDER BY `message_id` DESC",
+ (room_id,)).fetchone()[0]
+ return post_id
def get_chat_messages(room_id):
@@ -120,8 +126,10 @@ def insert_quest(canon_title, ident_title, owner_id):
_DB.execute(
"INSERT INTO `quest_meta` (`canon_title`, `ident_title`, `owner_id`) "\
+ "VALUES (%s, %s, %s)", (canon_title, ident_title, owner_id))
+
quest_id = _DB.execute(
- "SELECT `quest_id` FROM `quest_meta` WHERE `ident_title` = %s",
+ "SELECT `quest_id` FROM `quest_meta` WHERE `ident_title` = %s" \
+ + "ORDER BY `quest_id` DESC",
(ident_title,)).fetchone()[0]
return quest_id
@@ -206,17 +214,27 @@ def update_quest_post(post_id, new_post):
(new_post, post_id))
-def set_dice_call_open(quest_id, post_id):
+def set_dice_call_open(post_id, quest_id, new_call=None):
"""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))
+ if new_call:
+ dice_roll, strict, dice_challence, rolls_taken = new_call
+ _DB.execute(
+ "INSERT INTO `quest_dice_calls`" \
+ + "(`post_id`, `dice_roll`, `strict`, " \
+ + "`dice_challenge`, `rolls_taken`)" \
+ + "VALUES (%s, %s, %s, %s, %s)",
+ (post_id, dice_roll, strict, dice_challence, rolls_taken))
+
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",
+ "UPDATE `quest_meta` SET `dice_call` = NULL, `dice_rolls` = NULL, " \
+ "`dice_strct` = NULL WHERE `quest_id` = %s",
(quest_id,))
@@ -226,3 +244,21 @@ def get_dice_call(quest_id):
"SELECT `dice_call` FROM `quest_meta` WHERE `quest_id` = %s",
(quest_id,)).fetchone()
return data
+
+
+def insert_quest_roll(message_id, quest_id, post_id, roll_dice, roll_results):
+ """Inserts a user roll into the `quest_rolls` table."""
+ _DB.execute(
+ "INSERT INTO `quest_rolls`" \
+ + "(`message_id`, `quest_id`, `post_id`, `roll_dice`, `roll_results`)"\
+ + "VALUES (%s, %s, %s, %s, %s)",
+ (message_id, quest_id, post_id, roll_dice, roll_results))
+
+
+def get_quest_rolls(quest_id):
+ """Gets all rolls for the given quest."""
+ data = _DB.execute(
+ "SELECT * FROM `quest_rolls` WHERE `quest_id` = %s " \
+ + "ORDER BY `message_id` ASC",
+ (quest_id,)).fetchall()
+ return data
diff --git a/events.py b/events.py
index b224ab3..85a1c9f 100644
--- a/events.py
+++ b/events.py
@@ -54,14 +54,31 @@ def message(data):
message = "
".join(lines)
message = tools.handle_img(message)
data["message"] = message
+
+ roll_msg = ""
if message.startswith("/dice") or message.startswith("/roll"):
roll_msg = handle_dice(data)
if roll_msg:
data["message"] += '