2022-09-20 13:49:23 +02:00
|
|
|
import mysql.connector
|
|
|
|
|
2022-10-03 19:35:06 +02:00
|
|
|
class Database:
|
|
|
|
def __init__(self, host, user, password, db):
|
|
|
|
self.con = mysql.connector.connect(
|
|
|
|
host=host,
|
|
|
|
user=user,
|
|
|
|
password=password,
|
|
|
|
database=db,
|
|
|
|
)
|
2022-09-20 13:49:23 +02:00
|
|
|
|
2022-10-03 19:35:06 +02:00
|
|
|
def init_test_database(self):
|
|
|
|
cursor = self.con.cursor()
|
|
|
|
cursor.execute("CREATE TABLE discord_link ( admin_id int(10) UNSIGNED NOT NULL, discord_id bigint(20) UNSIGNED NOT NULL, steamid varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL);")
|
|
|
|
cursor.execute("CREATE TABLE sm_admins ( id int(10) UNSIGNED NOT NULL, authtype enum('steam', 'name', 'ip') COLLATE utf8mb4_unicode_ci NOT NULL, identity varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, password varchar(65) COLLATE utf8mb4_unicode_ci DEFAULT NULL, flags varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, name varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, immunity int(10) UNSIGNED NOT NULL);")
|
|
|
|
cursor.execute("ALTER TABLE discord_link ADD PRIMARY KEY (admin_id), ADD UNIQUE KEY admin_id (admin_id), ADD UNIQUE KEY discord_id (discord_id), ADD UNIQUE KEY steamid (steamid);")
|
|
|
|
cursor.execute("ALTER TABLE sm_admins ADD PRIMARY KEY (id), ADD UNIQUE KEY identity (identity), ADD UNIQUE KEY id (id);")
|
|
|
|
cursor.execute("ALTER TABLE sm_admins MODIFY id int(10) UNSIGNED NOT NULL AUTO_INCREMENT;")
|
|
|
|
cursor.execute("ALTER TABLE discord_link ADD CONSTRAINT discord_link_ibfk_1 FOREIGN KEY (admin_id) REFERENCES sm_admins (id);")
|
2022-09-20 13:49:23 +02:00
|
|
|
|
2022-10-03 19:35:06 +02:00
|
|
|
def create_reserved_slot(self, discord_user, steam_id):
|
|
|
|
cursor = self.con.cursor()
|
2022-09-20 13:49:23 +02:00
|
|
|
|
2022-10-03 19:35:06 +02:00
|
|
|
try:
|
|
|
|
sql_sm = "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES (%s, %s, %s, %s, %s, %s)"
|
|
|
|
val_sm = ("steam", steam_id, "", "a", discord_user.username, "5")
|
|
|
|
cursor.execute(sql_sm, val_sm)
|
|
|
|
self.con.commit()
|
|
|
|
|
|
|
|
id = cursor.lastrowid
|
2022-09-20 13:49:23 +02:00
|
|
|
|
2022-10-03 19:35:06 +02:00
|
|
|
sql_discord = "INSERT INTO discord_link (admin_id, discord_id, steamid) VALUES (%s, %s, %s)"
|
|
|
|
val_discord = (id, str(discord_user.id), steam_id)
|
|
|
|
cursor.execute(sql_discord, val_discord)
|
|
|
|
self.con.commit()
|
|
|
|
except mysql.connector.IntegrityError:
|
|
|
|
raise Exception("Duplicate entrys")
|