Compare commits

...

1 Commits

Author SHA1 Message Date
513841bd89 same as last commit but completely different 2018-07-07 19:20:28 -04:00
7 changed files with 126 additions and 34 deletions

View File

@ -84,6 +84,16 @@ def unix2string(unix):
return time.strftime(form, t) 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() init()
if __name__ == "__main__": if __name__ == "__main__":
app.run(host='0.0.0.0', port=5050) app.run(host='0.0.0.0', port=5050)

View File

@ -16,7 +16,7 @@ CREATE TABLE `quest_meta` (
) 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` MEDIUMINT 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_type` ENUM('text', 'dice', 'poll') NOT NULL,
`post` MEDIUMTEXT NOT NULL, `post` MEDIUMTEXT NOT NULL,
@ -24,10 +24,29 @@ CREATE TABLE `quest_data` (
PRIMARY KEY (`post_id`) PRIMARY KEY (`post_id`)
) ENGINE=InnoDB CHARSET utf8mb4; ) 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` ( CREATE TABLE `chat_messages` (
`room_id` SMALLINT UNSIGNED NOT NULL, `message_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL, `room_id` MEDIUMINT UNSIGNED NOT NULL,
`name_id` SMALLINT UNSIGNED DEFAULT NULL, `name_id` SMALLINT UNSIGNED DEFAULT NULL,
`date` INT UNSIGNED NOT NULL, `date` INT UNSIGNED NOT NULL,
`message` TEXT NOT NULL `message` TEXT NOT NULL,
PRIMARY KEY (`message_id`)
) ENGINE=InnoDB CHARSET utf8mb4; ) ENGINE=InnoDB CHARSET utf8mb4;

View File

@ -99,12 +99,18 @@ def log_chat_message(data):
message = data["message"] message = data["message"]
date = data["date"] date = data["date"]
room_id = data["room"] room_id = data["room"]
name = data["name"]
user_id = data.get("user_id") user_id = data.get("user_id")
_DB.execute( _DB.execute(
"INSERT INTO `chat_messages` (" \ "INSERT INTO `chat_messages` (" \
+ "`message`, `room_id`, `date`, `name`, `name_id`) VALUES (" \ + "`message`, `room_id`, `date`, `name_id`) VALUES (" \
+ "%s, %s, %s, %s, %s)", (message, room_id, date, name, user_id)) + "%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): def get_chat_messages(room_id):
@ -120,8 +126,10 @@ def insert_quest(canon_title, ident_title, owner_id):
_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))
quest_id = _DB.execute( 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] (ident_title,)).fetchone()[0]
return quest_id return quest_id
@ -206,17 +214,27 @@ def update_quest_post(post_id, new_post):
(new_post, post_id)) (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.""" """Sets an open dice call for the given quest."""
_DB.execute( _DB.execute(
"UPDATE `quest_meta` SET `dice_call` = %s WHERE `quest_id` = %s", "UPDATE `quest_meta` SET `dice_call` = %s WHERE `quest_id` = %s",
(post_id, quest_id)) (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): def set_dice_call_closed(quest_id):
"""Closes a quest's dice call.""" """Closes a quest's dice call."""
_DB.execute( _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,)) (quest_id,))
@ -226,3 +244,21 @@ def get_dice_call(quest_id):
"SELECT `dice_call` FROM `quest_meta` WHERE `quest_id` = %s", "SELECT `dice_call` FROM `quest_meta` WHERE `quest_id` = %s",
(quest_id,)).fetchone() (quest_id,)).fetchone()
return data 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

View File

@ -54,14 +54,31 @@ def message(data):
message = "<br />".join(lines) message = "<br />".join(lines)
message = tools.handle_img(message) message = tools.handle_img(message)
data["message"] = message data["message"] = message
roll_msg = ""
if message.startswith("/dice") or message.startswith("/roll"): if message.startswith("/dice") or message.startswith("/roll"):
roll_msg = handle_dice(data) roll_msg = handle_dice(data)
if roll_msg: if roll_msg:
data["message"] += '<hr class="msgSrvHr" />' + roll_msg data["message"] += '<hr class="msgSrvHr" />' + roll_msg
db.log_chat_message(data) message_id = db.log_chat_message(data)
emit("message", data, room=room) emit("message", data, room=room)
if roll_msg:
dice_call = db.get_dice_call(data["room"])[0]
if dice_call:
dice_roll = re.search(r"(\d+d\d+(?:[+-]\d+)?)", message).group(1)
roll_results = re.search(r"Rolled (.+)</b>", roll_msg).group(1)
db.insert_quest_roll(
message_id, room, dice_call, dice_roll, roll_results)
room = data["room"]
data = {}
data["post"] = roll_msg + " (" + dice_roll + ")"
data["post_type"] = "dice"
data["post_id"] = dice_call
emit("update_post", data, room=room)
def handle_dice(data): def handle_dice(data):
""" """
@ -85,22 +102,10 @@ def handle_dice(data):
msg = f"<b>Rolled {', '.join(map(str, dice))}" msg = f"<b>Rolled {', '.join(map(str, dice))}"
if diceMod: if diceMod:
if diceMod > 0: if diceMod > 0:
msg += " +" msg += " +" + str(diceMod)
msg += " " + str(diceMod) else:
msg += " - " + str(diceMod)[1:]
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
@ -143,6 +148,7 @@ def update_post(data):
if session.get("user_id") != res[3]: if session.get("user_id") != res[3]:
return return
# TODO: enforce only update text posts
post = data["post"] post = data["post"]
post = post.strip().replace("<br>", "<br />") post = post.strip().replace("<br>", "<br />")
post = tools.handle_img(post) post = tools.handle_img(post)
@ -153,6 +159,7 @@ def update_post(data):
data = {} data = {}
data["post"] = post data["post"] = post
data["post_id"] = post_id data["post_id"] = post_id
data["post_type"] = "text"
emit("update_post", data, room=room) emit("update_post", data, room=room)
@ -183,12 +190,15 @@ def dice_post(data):
assert 0 <= diceRolls < 100 assert 0 <= diceRolls < 100
except (ValueError, AssertionError): except (ValueError, AssertionError):
return return
diceStrict = bool(data.get("diceStrict"))
post = f"<h3>Roll {data['diceNum']}d{data['diceSides']}" dice_roll = f"{data['diceNum']}d{data['diceSides']}"
if diceMod: if diceMod:
if diceMod > 0: if diceMod > 0:
post += "+" dice_roll += "+"
post += str(diceMod) dice_roll += str(diceMod)
post = "<h3>Roll " + dice_roll
if diceChal: if diceChal:
post += " vs DC" + str(diceChal) post += " vs DC" + str(diceChal)
post += "</h3>" post += "</h3>"
@ -200,5 +210,6 @@ def dice_post(data):
post_id = db.insert_quest_post(room, "dice", post, data["date"]) post_id = db.insert_quest_post(room, "dice", post, data["date"])
data["post_id"] = post_id data["post_id"] = post_id
db.set_dice_call_open(room, post_id) new_call = (dice_roll, diceStrict, diceChal, diceRolls)
db.set_dice_call_open(post_id, room, new_call)
emit("new_post", data, room=room) emit("new_post", data, room=room)

View File

@ -4,7 +4,8 @@ img {
} }
h3 { h3 {
margin: 0px; margin-top: 0px;
margin-bottom: 0.5em;
} }
.header { .header {

View File

@ -37,7 +37,13 @@
}); });
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; let post = document.getElementById('questPostData-' + post_id);
if (data.post_type == 'text') {
post.innerHTML = data.post;
}
else if (data.post_type == 'dice') {
post.innerHTML += '<b>' + data.post + '</b><br />';
}
}); });
let mtarea = document.getElementById('messageTextArea'); let mtarea = document.getElementById('messageTextArea');
mtarea.addEventListener('keypress', function(event) { mtarea.addEventListener('keypress', function(event) {
@ -140,6 +146,13 @@
{% autoescape false %} {% autoescape false %}
{{ quest_post[3] }} {{ quest_post[3] }}
{% endautoescape %} {% endautoescape %}
{% if quest_post[2] == "dice" %}
{% for quest_roll in quest_rolls %}
{% if quest_roll[2] == quest_post[0] %}
<div id="questRollId-{{ quest_roll[0] }}"><b>Rolled {{ quest_roll[4] }} ({{ quest_roll[3] }})</b></div>
{% endif %}
{% endfor %}
{% endif %}
</div> </div>
</div><br /> </div><br />
{% endfor %} {% endfor %}
@ -189,7 +202,7 @@
{% for message in messages %} {% for message in messages %}
<div class="message"> <div class="message">
<div class="messageHeader"> <div class="messageHeader">
<span class="messageName">{{ message[1] }}</span> <span class="messageDate">{{ message[3] | strftime }}</span> <span class="messageName">Anonymous</span> <span class="messageDate">{{ message[3] | strftime }}</span>
</div> </div>
<div class="messageMessage">{{ message[4] }}</div> <div class="messageMessage">{{ message[4] }}</div>
</div> </div>

View File

@ -55,11 +55,13 @@ def quest(quest_title):
quest_title = data[1] quest_title = data[1]
owner_id = data[3] owner_id = data[3]
quest_posts = db.get_quest_data(quest_id) quest_posts = db.get_quest_data(quest_id)
quest_rolls = db.get_quest_rolls(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',
quest_title=quest_title, quest_title=quest_title,
ident_title=ident_title, ident_title=ident_title,
quest_posts=quest_posts, quest_posts=quest_posts,
quest_rolls=quest_rolls,
owner_id=owner_id, owner_id=owner_id,
room_id=quest_id, room_id=quest_id,
messages=messages) messages=messages)
@ -100,7 +102,7 @@ def create_quest():
timestamp = int(time.time()) timestamp = int(time.time())
quest_id = db.insert_quest(canon_title, ident_title, owner_id) quest_id = db.insert_quest(canon_title, ident_title, owner_id)
db.insert_quest_post(quest_id, quest_body, timestamp) db.insert_quest_post(quest_id, "text", quest_body, timestamp)
return redirect(url_for('.quest', quest_title=ident_title)) return redirect(url_for('.quest', quest_title=ident_title))