328 lines
13 KiB
Python
Executable File
328 lines
13 KiB
Python
Executable File
import mysql.connector
|
|
|
|
class WrongMinecraftName(Exception):
|
|
pass
|
|
class SQLInsertError(Exception):
|
|
pass
|
|
class UnLinkError(Exception):
|
|
pass
|
|
class PlayerNotLinked(Exception):
|
|
pass
|
|
class QuestionNotFound(Exception):
|
|
pass
|
|
class WordNotFound(Exception):
|
|
pass
|
|
class PlayerNotFound(Exception):
|
|
pass
|
|
|
|
class DatabaseConnection:
|
|
# databases -> worldcraft and worldcraft_discord
|
|
def __init__(self, database="worldcraft_discord"):
|
|
if database == "worldcraft_discord":
|
|
self.mydb = mysql.connector.connect(
|
|
host="192.168.1.251",
|
|
user="worldcraft_discord",
|
|
password="aquev5vcwhLwTdRt",
|
|
database=database
|
|
)
|
|
elif database == "s13_worldcraft":
|
|
self.mydb = mysql.connector.connect(
|
|
host="192.168.1.251",
|
|
user="worldcraft",
|
|
port="3308",
|
|
password="uJpa9VOW2P0bh0p1",
|
|
database=database
|
|
)
|
|
elif database == "s13_ecobridge":
|
|
self.mydb = mysql.connector.connect(
|
|
host="192.168.1.251",
|
|
user="u13_H9QOWK3I5x",
|
|
password="^K2HjsLeTtPTl9+Ek.Y21p7S",
|
|
database=database
|
|
)
|
|
|
|
self.cursor = self.mydb.cursor()
|
|
|
|
def get_db(self):
|
|
return self.mydb
|
|
|
|
def get_cursor(self, dictionary=False):
|
|
return self.mydb.cursor(dictionary=dictionary)
|
|
|
|
def close(self):
|
|
self.mydb.close()
|
|
|
|
class QuizPlayersDB:
|
|
def __init__(self, conn=None):
|
|
self.tablename = "quizplayers"
|
|
if (conn is not None):
|
|
self.discorddbconn = conn
|
|
else:
|
|
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("create table IF NOT EXISTS {} (discordid tinytext NOT NULL, wins int DEFAULT 0, rewards int DEFAULT 0, toclaim int DEFAULT 0, cur_streak int DEFAULT 0, max_streak int DEFAULT 0, streak_count int DEFAULT 0)".format(self.tablename))
|
|
|
|
def player_exists(self, discordid):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
sql = f"SELECT count(*) FROM {self.tablename} WHERE discordid={str(discordid)}"
|
|
cursor.execute(sql)
|
|
res = cursor.fetchone()
|
|
return res[0] == 1
|
|
|
|
# give the user with discordid his reward + start streak
|
|
def player_won(self, discordid, reward):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
if self.player_exists(discordid):
|
|
sql = f"UPDATE {self.tablename} SET wins = wins + 1, rewards = rewards + {str(reward)}, toclaim = toclaim + {str(reward)} WHERE discordid = {str(discordid)}"
|
|
cursor.execute(sql)
|
|
else:
|
|
sql = f"INSERT INTO {self.tablename} (discordid, wins, rewards, toclaim) VALUES ({str(discordid)}, 1, {reward}, {reward}, 0, 0)"
|
|
cursor.execute(sql)
|
|
|
|
self.increment_cur_streak(discordid)
|
|
self.discorddbconn.get_db().commit()
|
|
|
|
|
|
# get discord id of user with current streak
|
|
def get_current_streak(self):
|
|
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
|
sql = f"SELECT discordid, cur_streak FROM {self.tablename} WHERE cur_streak > 0"
|
|
cursor.execute(sql)
|
|
res = cursor.fetchone()
|
|
return res
|
|
|
|
# set current streak to 0 from discorduser
|
|
def reset_streak(self, discordid):
|
|
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
|
sql = f"UPDATE {self.tablename} SET cur_streak = 0 WHERE discordid = {str(discordid)}"
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
|
|
def get_top_players(self, limit = 20):
|
|
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
|
sql = f"SELECT discordid, wins as stat FROM {self.tablename} ORDER BY wins DESC LIMIT {str(limit)}"
|
|
cursor.execute(sql)
|
|
return list(cursor.fetchall())
|
|
|
|
# increment the current streak of user with discordid (+ checks to increment max_streak and streak_count)
|
|
def increment_cur_streak(self, discordid):
|
|
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
|
streaks = self.get_streaks(discordid)
|
|
if streaks["cur_streak"] == streaks["max_streak"]:
|
|
if streaks["cur_streak"] == 1:
|
|
sql = f"UPDATE {self.tablename} SET cur_streak = cur_streak + 1, max_streak = max_streak + 1, streak_count = streak_count + 1 WHERE discordid = {str(discordid)}"
|
|
else:
|
|
sql = f"UPDATE {self.tablename} SET cur_streak = cur_streak + 1, max_streak = max_streak + 1 WHERE discordid = {str(discordid)}"
|
|
else:
|
|
if streaks["cur_streak"] == 1:
|
|
sql = f"UPDATE {self.tablename} SET cur_streak = cur_streak + 1, streak_count = streak_count + 1 WHERE discordid = {str(discordid)}"
|
|
else:
|
|
sql = f"UPDATE {self.tablename} SET cur_streak = cur_streak + 1 WHERE discordid = {str(discordid)}"
|
|
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
|
|
# get the streak information from user with discordid
|
|
def get_streaks(self, discordid):
|
|
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
|
sql = f"SELECT cur_streak, max_streak, streak_count FROM {self.tablename} WHERE discordid={str(discordid)}"
|
|
cursor.execute(sql)
|
|
return cursor.fetchone()
|
|
|
|
def close(self):
|
|
self.discorddbconn.close()
|
|
|
|
class WordGameDB:
|
|
def __init__(self):
|
|
self.tablename = "wordGame"
|
|
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("create table IF NOT EXISTS {} (id int NOT NULL PRIMARY KEY AUTO_INCREMENT, word text NOT NULL, reward int NOT NULL, asked int DEFAULT 0)".format(self.tablename))
|
|
|
|
def add_word(self, word, reward):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
sql = f"insert into {self.tablename} (word, reward) VALUES ('{word}', {str(reward)})"
|
|
try:
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
return cursor.lastrowid
|
|
except:
|
|
raise SQLInsertError()
|
|
|
|
def rm_word(self, id):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("select count(*) from {} where id={}".format(self.tablename, str(id)))
|
|
res = cursor.fetchone()
|
|
if res[0] == 1:
|
|
sql = f"DELETE FROM {self.tablename} WHERE id = {str(id)}"
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
else:
|
|
raise QuestionNotFound()
|
|
|
|
def get_words(self):
|
|
# word, used, reward
|
|
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
|
sql = f"SELECT * FROM {self.tablename}"
|
|
cursor.execute(sql)
|
|
return list(cursor.fetchall())
|
|
|
|
def word_asked_increment(self, w_id):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
sql = f"UPDATE {self.tablename} SET asked = asked + 1 WHERE id = {str(w_id)}"
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
|
|
def count_words(self):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("select count(*) from {}".format(self.tablename))
|
|
res = cursor.fetchone()
|
|
return res[0]
|
|
|
|
def get_random_word(self):
|
|
if self.count_words() != 0:
|
|
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
|
sql = f"SELECT * FROM {self.tablename} ORDER BY RAND() LIMIT 1"
|
|
cursor.execute(sql)
|
|
return cursor.fetchone()
|
|
else:
|
|
raise WordNotFound()
|
|
|
|
def close(self):
|
|
self.discorddbconn.close()
|
|
|
|
|
|
|
|
class QuizDB:
|
|
def __init__(self):
|
|
self.tablename = "questions"
|
|
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("create table IF NOT EXISTS {} (id int NOT NULL PRIMARY KEY AUTO_INCREMENT, question text NOT NULL, answer text NOT NULL, reward int NOT NULL, asked int DEFAULT 0)".format(self.tablename))
|
|
|
|
def add_question(self, question, answer, reward):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
sql = f"insert into {self.tablename} (question, answer, reward) VALUES ('{question}', '{answer}', {str(reward)})"
|
|
try:
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
return cursor.lastrowid
|
|
except:
|
|
raise SQLInsertError()
|
|
|
|
def rm_question(self, id):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("select count(*) from {} where id={}".format(self.tablename, str(id)))
|
|
res = cursor.fetchone()
|
|
if res[0] == 1:
|
|
sql = f"DELETE FROM {self.tablename} WHERE id = {str(id)}"
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
else:
|
|
raise QuestionNotFound()
|
|
|
|
def get_questions(self):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
sql = f"SELECT * FROM {self.tablename}"
|
|
cursor.execute(sql)
|
|
return list(cursor.fetchall())
|
|
|
|
def count_questions(self):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("select count(*) from {}".format(self.tablename))
|
|
res = cursor.fetchone()
|
|
return res[0]
|
|
|
|
def question_asked_increment(self, q_id):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
sql = f"UPDATE {self.tablename} SET asked = asked + 1 WHERE id = {str(q_id)}"
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
|
|
|
|
def get_random_question(self):
|
|
if self.count_questions() != 0:
|
|
cursor = self.discorddbconn.get_cursor()
|
|
sql = f"SELECT * FROM {self.tablename} ORDER BY RAND() LIMIT 1"
|
|
cursor.execute(sql)
|
|
return list(cursor.fetchone())
|
|
else:
|
|
raise QuestionNotFound()
|
|
|
|
def close(self):
|
|
self.discorddbconn.close()
|
|
|
|
# this will save the uuid of the player and discord id
|
|
class PlayerDBLinker:
|
|
def __init__(self):
|
|
self.tablename = "playerlinks"
|
|
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
|
|
self.serverdbconn = DatabaseConnection(database="s13_worldcraft")
|
|
# check if table exists
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("create table IF NOT EXISTS {} (id int NOT NULL PRIMARY KEY AUTO_INCREMENT, minecraftUUID varchar(36) NOT NULL, discordid TINYTEXT NOT NULL)".format(self.tablename))
|
|
|
|
def checkPlayerLink(self, minecraftUUID, discordid):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("select count(*) from {} where minecraftUUID='{}' AND discordid='{}'".format(self.tablename, minecraftUUID, discordid))
|
|
res = cursor.fetchone()
|
|
return res[0] == 1
|
|
|
|
def minecraftUUIDused(self, minecraftUUID):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("select count(*) from {} where minecraftUUID='{}'".format(self.tablename, minecraftUUID))
|
|
res = cursor.fetchone()
|
|
return res[0] >= 1
|
|
|
|
def discordidused(self, discordid):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("select count(*) from {} where discordid='{}'".format(self.tablename, discordid))
|
|
res = cursor.fetchone()
|
|
return res[0] >= 1
|
|
|
|
def get_minecraftUUID(self, discordid):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
cursor.execute("select minecraftUUID from {} where discordid='{}'".format(self.tablename, discordid))
|
|
res = cursor.fetchone()
|
|
return res[0]
|
|
|
|
def linkPlayer(self, minecraftUUID, discordid):
|
|
cursor = self.discorddbconn.get_cursor()
|
|
sql = "insert into {} (minecraftUUID, discordid) VALUES (%s, %s)".format(self.tablename)
|
|
val = (minecraftUUID, discordid)
|
|
|
|
try:
|
|
cursor.execute(sql, val)
|
|
self.discorddbconn.get_db().commit()
|
|
|
|
except:
|
|
raise SQLInsertError()
|
|
|
|
def unlinkPlayer(self, discordid):
|
|
# get uuid from server database -> check if in linkerdatabase
|
|
discorddb_cursor = self.discorddbconn.get_cursor()
|
|
if self.discordidused(discordid):
|
|
sql2 = f"DELETE FROM {self.tablename} WHERE discordid = '{discordid}'"
|
|
discorddb_cursor.execute(sql2)
|
|
res2 = self.discorddbconn.get_db().commit()
|
|
else:
|
|
raise PlayerNotLinked()
|
|
|
|
#Broken
|
|
#Deaths
|
|
#kills
|
|
#level
|
|
#placed
|
|
#playedtime/onlinetime
|
|
#sailed
|
|
#walked
|
|
def get_stats(self, categorie, limit = 20):
|
|
serverdb_cursor = self.serverdbconn.get_cursor(dictionary=True)
|
|
sql = f"SELECT playerName, {categorie} as stat FROM playerprofiles ORDER BY {categorie} DESC LIMIT {str(limit)}"
|
|
serverdb_cursor.execute(sql)
|
|
return list(serverdb_cursor.fetchall())
|
|
|
|
def close(self):
|
|
self.discorddbconn.close()
|
|
self.serverdbconn.close()
|