diff --git a/anonkun.sql b/anonkun.sql index 37ff321..41a1145 100644 --- a/anonkun.sql +++ b/anonkun.sql @@ -1,17 +1,22 @@ CREATE TABLE `users` ( - `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, + `user_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(20) NOT NULL, `password_hash` CHAR(73) NOT NULL, - PRIMARY KEY (`id`) + PRIMARY KEY (`user_id`) ) ENGINE=InnoDB CHARSET utf8mb4; -CREATE TABLE `quests` ( - `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, +CREATE TABLE `quest_meta` ( + `quest_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, `canon_title` VARCHAR(300) DEFAULT NULL, `ident_title` VARCHAR(300) DEFAULT NULL, `owner_id` SMALLINT UNSIGNED DEFAULT NULL, - `quest_data` MEDIUMTEXT DEFAULT NULL, - PRIMARY KEY (`id`) + PRIMARY KEY (`quest_id`) +) ENGINE=InnoDB CHARSET utf8mb4; + +CREATE TABLE `quest_data` ( + `quest_id` SMALLINT UNSIGNED NOT NULL, + `post` MEDIUMTEXT NOT NULL, + `timestamp` INT UNSIGNED NOT NULL ) ENGINE=InnoDB CHARSET utf8mb4; CREATE TABLE `chat_messages` ( diff --git a/database.py b/database.py index b66b6ef..c725cb1 100644 --- a/database.py +++ b/database.py @@ -111,3 +111,43 @@ def get_chat_messages(room_id): "SELECT * FROM `chat_messages` WHERE `room_id` = %s " \ + "ORDER BY `date` ASC", (room_id,)).fetchall() return res + + +def insert_quest(canon_title, ident_title, owner_id): + """ + 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)) + quest_id = _DB.execute( + "SELECT `quest_id` FROM `quest_meta` WHERE `ident_title` = %s", + (ident_title,)).fetchone()[0] + return quest_id + + +def insert_quest_post(quest_id, 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)) + + +def get_quest_meta(ident_title): + """ + Retrieves all meta info about a quest. + """ + data = _DB.execute("SELECT * FROM `quest_meta` WHERE `ident_title` = %s", + (ident_title,)).fetchone() + return data + + +def get_quest_data(quest_id): + """ + Retrieves all quest posts. + """ + data = _DB.execute("SELECT * FROM `quest_data` WHERE `quest_id` = %s", + (quest_id,)).fetchall() + return data diff --git a/events.py b/events.py index 5263896..25756c3 100644 --- a/events.py +++ b/events.py @@ -11,7 +11,7 @@ import database as db socketio = SocketIO() -@socketio.on('joined', namespace="/chat") +@socketio.on('joined') def joined(data): """ Sent by clients when they enter a room. @@ -20,8 +20,8 @@ def joined(data): join_room(room) -@socketio.on('message', namespace="/chat") -def text(data): +@socketio.on('message') +def message(data): """ Sent by a client when the user entered a new message. """ @@ -46,3 +46,19 @@ def text(data): db.log_chat_message(data) emit("message", data, room=room) + + +@socketio.on("new_post") +def new_post(data): + """ + Called when the QM makes a new post. + """ + room = data["room"] + post = data["post"] + + post = bleach.clean(post.strip()) + post = post.replace("\n", "
") + data["post"] = [post] + db.insert_quest_post(int(room), post, int(time.time())) + + emit("new_post", data, room=room) diff --git a/templates/base.html b/templates/base.html index f8e7011..e0199f3 100644 --- a/templates/base.html +++ b/templates/base.html @@ -21,9 +21,7 @@