2018-06-16 18:52:46 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Database tools and functions.
|
|
|
|
"""
|
|
|
|
import MySQLdb
|
|
|
|
from flask import session
|
|
|
|
from passlib.hash import argon2
|
|
|
|
|
|
|
|
class Database():
|
|
|
|
"""
|
|
|
|
An interface to interact with the database.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Initalizes the database.
|
|
|
|
"""
|
|
|
|
with open("db_key", "r") as file:
|
|
|
|
# TODO: encrypt this
|
|
|
|
self.user, self.db, self.key = file.read().strip().split()
|
|
|
|
try:
|
|
|
|
self.execute("SELECT * FROM `users`").fetchone()
|
|
|
|
except MySQLdb.ProgrammingError: # database not initialized
|
|
|
|
with open("anonkun.sql", "r") as file:
|
|
|
|
commands = file.read().split(";")
|
|
|
|
for cmd in commands:
|
|
|
|
cmd = cmd.strip()
|
|
|
|
if not cmd:
|
|
|
|
continue
|
|
|
|
self.execute(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def execute(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Opens a connection to the app's database and executes the SQL
|
|
|
|
statements passed to this function.
|
|
|
|
"""
|
2018-06-27 10:17:05 -04:00
|
|
|
with MySQLdb.connect(
|
|
|
|
user=self.user,
|
|
|
|
passwd=self.key,
|
|
|
|
db=self.db,
|
|
|
|
charset='utf8mb4') as cur:
|
2018-06-16 18:52:46 -04:00
|
|
|
cur.execute(*args, **kwargs)
|
|
|
|
return cur
|
|
|
|
|
|
|
|
_DB = Database()
|
|
|
|
|
|
|
|
|
2018-06-21 11:00:24 -04:00
|
|
|
def add_user(username, password, timestamp):
|
2018-06-16 18:52:46 -04:00
|
|
|
"""
|
|
|
|
Adds a user to the database.
|
|
|
|
"""
|
|
|
|
if verify_username(username): # username taken
|
|
|
|
return "username_taken"
|
|
|
|
elif len(username) > 20:
|
|
|
|
return "username_too_long"
|
|
|
|
|
|
|
|
pw_hash = argon2.hash(password)
|
|
|
|
_DB.execute(
|
2018-06-21 11:00:24 -04:00
|
|
|
"INSERT INTO `users` (`username`, `password_hash`, `signup_date`) " \
|
|
|
|
+ "VALUES (%s, %s, %s)", (username, pw_hash, timestamp))
|
2018-06-16 18:52:46 -04:00
|
|
|
return "success"
|
|
|
|
|
|
|
|
|
|
|
|
def verify_password(username, password):
|
|
|
|
"""
|
|
|
|
Verifies a user's password.
|
|
|
|
"""
|
|
|
|
user = verify_username(username)
|
|
|
|
if not user:
|
|
|
|
return False
|
|
|
|
|
2018-06-21 20:44:43 -04:00
|
|
|
user_id, _, pw_hash, _ = user
|
2018-06-16 18:52:46 -04:00
|
|
|
|
|
|
|
if argon2.verify(password, pw_hash):
|
|
|
|
session["user_id"] = user_id
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def verify_username(username):
|
|
|
|
"""
|
|
|
|
Checks to see if the given username is in the database.
|
|
|
|
"""
|
|
|
|
user = _DB.execute("SELECT * FROM `users` WHERE `username` = %s",
|
|
|
|
(username,)).fetchone()
|
|
|
|
if user:
|
|
|
|
return user
|
|
|
|
else:
|
|
|
|
return False
|
2018-06-17 21:10:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
def log_chat_message(data):
|
|
|
|
"""
|
|
|
|
Logs chat messages into the database.
|
|
|
|
'data' should be a dict containing: message, date, room_id, name, and
|
|
|
|
user_id (optional).
|
|
|
|
"""
|
|
|
|
message = data["message"]
|
|
|
|
date = data["date"]
|
2018-07-06 02:09:55 -04:00
|
|
|
room_id = data["room"]
|
2018-06-17 21:10:41 -04:00
|
|
|
user_id = data.get("user_id")
|
|
|
|
_DB.execute(
|
|
|
|
"INSERT INTO `chat_messages` (" \
|
2018-07-07 19:18:08 -04:00
|
|
|
+ "`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
|
2018-06-17 21:10:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_chat_messages(room_id):
|
2018-07-07 13:43:58 -04:00
|
|
|
"""Retrieves all chat messages for the provided room_id."""
|
2018-06-17 21:10:41 -04:00
|
|
|
res = _DB.execute(
|
|
|
|
"SELECT * FROM `chat_messages` WHERE `room_id` = %s " \
|
|
|
|
+ "ORDER BY `date` ASC", (room_id,)).fetchall()
|
|
|
|
return res
|
2018-06-20 13:07:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
def insert_quest(canon_title, ident_title, owner_id):
|
2018-07-07 13:43:58 -04:00
|
|
|
"""Creates a new quest entry."""
|
2018-06-20 13:07:11 -04:00
|
|
|
_DB.execute(
|
|
|
|
"INSERT INTO `quest_meta` (`canon_title`, `ident_title`, `owner_id`) "\
|
|
|
|
+ "VALUES (%s, %s, %s)", (canon_title, ident_title, owner_id))
|
2018-07-07 19:18:08 -04:00
|
|
|
|
2018-06-20 13:07:11 -04:00
|
|
|
quest_id = _DB.execute(
|
2018-07-07 19:18:08 -04:00
|
|
|
"SELECT `quest_id` FROM `quest_meta` WHERE `ident_title` = %s" \
|
|
|
|
+ "ORDER BY `quest_id` DESC",
|
2018-06-20 13:07:11 -04:00
|
|
|
(ident_title,)).fetchone()[0]
|
|
|
|
return quest_id
|
|
|
|
|
|
|
|
|
2018-07-07 13:43:58 -04:00
|
|
|
def insert_quest_post(quest_id, post_type, post, timestamp):
|
|
|
|
"""Insers a new quest post."""
|
2018-06-20 13:07:11 -04:00
|
|
|
_DB.execute(
|
2018-07-07 13:43:58 -04:00
|
|
|
"INSERT INTO `quest_data`" \
|
|
|
|
+ "(`quest_id`, `post_type`, `post`, `timestamp`) " \
|
|
|
|
+ "VALUES (%s, %s, %s, %s)", (quest_id, post_type, post, timestamp))
|
2018-07-04 17:20:47 -04:00
|
|
|
post_id = _DB.execute(
|
|
|
|
"SELECT `post_id` FROM `quest_data` WHERE `quest_id` = %s " \
|
|
|
|
+ "ORDER BY `post_id` DESC", (quest_id,)).fetchone()[0]
|
|
|
|
return post_id
|
2018-06-20 13:07:11 -04:00
|
|
|
|
|
|
|
|
2018-06-21 20:44:43 -04:00
|
|
|
def get_quest_meta(quest_id=None, ident_title=None):
|
2018-06-20 13:07:11 -04:00
|
|
|
"""
|
2018-06-21 20:44:43 -04:00
|
|
|
Retrieves all meta info about a quest. Allows searching by either
|
|
|
|
quest_id or ident_title.
|
2018-06-20 13:07:11 -04:00
|
|
|
"""
|
2018-06-21 20:44:43 -04:00
|
|
|
statement = "SELECT * FROM `quest_meta` WHERE "
|
|
|
|
if quest_id:
|
|
|
|
statement += "`quest_id` = %s"
|
|
|
|
data = _DB.execute(statement, (quest_id,)).fetchone()
|
|
|
|
elif ident_title:
|
|
|
|
statement += "`ident_title` = %s"
|
|
|
|
data = _DB.execute(statement, (ident_title,)).fetchone()
|
|
|
|
else:
|
|
|
|
return
|
2018-06-20 13:07:11 -04:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def get_quest_data(quest_id):
|
2018-07-07 13:43:58 -04:00
|
|
|
"""Retrieves all quest posts."""
|
|
|
|
data = _DB.execute(
|
|
|
|
"SELECT * FROM `quest_data` WHERE `quest_id` = %s " \
|
|
|
|
+ "ORDER BY `post_id` ASC",
|
2018-06-20 13:07:11 -04:00
|
|
|
(quest_id,)).fetchall()
|
|
|
|
return data
|
2018-06-21 11:00:24 -04:00
|
|
|
|
|
|
|
|
2018-07-07 13:43:58 -04:00
|
|
|
def get_quest_post(post_id=None, quest_id=None):
|
2018-06-21 11:00:24 -04:00
|
|
|
"""
|
2018-07-07 13:43:58 -04:00
|
|
|
Retrieves the post data for the given post_id. If no post_id is given,
|
|
|
|
it returns the most recent post.
|
2018-06-21 11:00:24 -04:00
|
|
|
"""
|
2018-07-07 13:43:58 -04:00
|
|
|
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."""
|
2018-06-21 11:00:24 -04:00
|
|
|
data = _DB.execute(
|
|
|
|
"SELECT `user_id`, `signup_date` FROM `users` WHERE `username` = %s",
|
|
|
|
(username,)).fetchone()
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_quests(user_id):
|
2018-07-07 13:43:58 -04:00
|
|
|
"""Retrieves all quests ran by a particular user_id."""
|
2018-06-21 11:00:24 -04:00
|
|
|
data = _DB.execute(
|
|
|
|
"SELECT * FROM `quest_meta` WHERE `owner_id` = %s",
|
|
|
|
(user_id,)).fetchall()
|
|
|
|
return data
|
2018-06-21 20:44:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
def update_quest_post(post_id, new_post):
|
2018-07-07 13:43:58 -04:00
|
|
|
"""Updates a quest post."""
|
2018-06-21 20:44:43 -04:00
|
|
|
_DB.execute(
|
|
|
|
"UPDATE `quest_data` SET `post` = %s WHERE `post_id` = %s",
|
|
|
|
(new_post, post_id))
|
2018-07-07 13:43:58 -04:00
|
|
|
|
|
|
|
|
2018-07-07 19:18:08 -04:00
|
|
|
def set_dice_call_open(post_id, quest_id, new_call=None):
|
2018-07-07 13:43:58 -04:00
|
|
|
"""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))
|
|
|
|
|
2018-07-07 19:18:08 -04:00
|
|
|
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))
|
|
|
|
|
2018-07-07 13:43:58 -04:00
|
|
|
|
|
|
|
def set_dice_call_closed(quest_id):
|
|
|
|
"""Closes a quest's dice call."""
|
|
|
|
_DB.execute(
|
2018-07-08 17:50:54 -04:00
|
|
|
"UPDATE `quest_meta` SET `dice_call` = NULL WHERE `quest_id` = %s",
|
2018-07-07 13:43:58 -04:00
|
|
|
(quest_id,))
|
|
|
|
|
|
|
|
|
2018-07-08 17:50:54 -04:00
|
|
|
def get_dice_call(post_id):
|
2018-07-07 13:43:58 -04:00
|
|
|
"""Retrives the currently open dice call, if there is one."""
|
|
|
|
data = _DB.execute(
|
2018-07-08 17:50:54 -04:00
|
|
|
"SELECT * FROM `quest_dice_calls` WHERE `post_id` = %s",
|
|
|
|
(post_id,)).fetchone()
|
2018-07-07 13:43:58 -04:00
|
|
|
return data
|
2018-07-07 19:18:08 -04:00
|
|
|
|
|
|
|
|
2018-07-09 09:18:58 -04:00
|
|
|
def insert_quest_roll(message_id, quest_id, post_id, roll_data):
|
2018-07-07 19:18:08 -04:00
|
|
|
"""Inserts a user roll into the `quest_rolls` table."""
|
2018-07-09 09:18:58 -04:00
|
|
|
ins = (message_id, quest_id, post_id) + roll_data
|
2018-07-07 19:18:08 -04:00
|
|
|
_DB.execute(
|
|
|
|
"INSERT INTO `quest_rolls`" \
|
2018-07-09 09:18:58 -04:00
|
|
|
+ "(`message_id`, `quest_id`, `post_id`, " \
|
|
|
|
+ "`roll_dice`, `roll_results`, `roll_total`)" \
|
|
|
|
+ "VALUES (%s, %s, %s, %s, %s, %s)",
|
|
|
|
(ins))
|
2018-07-07 19:18:08 -04:00
|
|
|
|
|
|
|
|
2018-07-09 07:45:35 -04:00
|
|
|
def get_quest_rolls(quest_id=None, post_id=None):
|
2018-07-07 19:18:08 -04:00
|
|
|
"""Gets all rolls for the given quest."""
|
2018-07-09 07:45:35 -04:00
|
|
|
if quest_id:
|
|
|
|
sql = "SELECT * FROM `quest_rolls` WHERE `quest_id` = %s "
|
|
|
|
ins = quest_id
|
|
|
|
elif post_id:
|
|
|
|
sql = "SELECT * FROM `quest_rolls` WHERE `post_id` = %s "
|
|
|
|
ins = post_id
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
sql += "ORDER BY `message_id` ASC"
|
|
|
|
data = _DB.execute(sql, (ins,)).fetchall()
|
2018-07-07 19:18:08 -04:00
|
|
|
return data
|