208 lines
7.9 KiB
Python
Executable File
208 lines
7.9 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 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="u13_f2zpWZpIKY",
|
|
password="@fZQ6Uu3+U^WH1i2JNemgTC7",
|
|
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):
|
|
return self.cursor
|
|
|
|
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)".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
|
|
|
|
|
|
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})"
|
|
cursor.execute(sql)
|
|
self.discorddbconn.get_db().commit()
|
|
|
|
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, minecraftname, discordid):
|
|
# get uuid from server database -> check if in linkerdatabase
|
|
serverdb_cursor = self.serverdbconn.get_cursor()
|
|
discorddb_cursor = self.discorddbconn.get_cursor()
|
|
sql = "SELECT playerUUID FROM playerprofiles WHERE playerName = '{}' LIMIT 1".format(minecraftname)
|
|
serverdb_cursor.execute(sql)
|
|
res = serverdb_cursor.fetchone()
|
|
if res == None:
|
|
raise WrongMinecraftName()
|
|
playeruuid = res[0]
|
|
if self.checkPlayerLink(playeruuid, discordid):
|
|
sql2 = f"DELETE FROM {self.tablename} WHERE minecraftUUID = '{playeruuid}' AND discordid = '{discordid}'"
|
|
discorddb_cursor.execute(sql2)
|
|
res2 = self.discorddbconn.get_db().commit()
|
|
else:
|
|
raise PlayerNotLinked()
|
|
|
|
def close(self):
|
|
self.discorddbconn.close()
|
|
self.serverdbconn.close()
|