parent
0690ac5212
commit
bdd2f20b84
@ -1,68 +1,79 @@ |
|||||||
|
from random import choices |
||||||
import sqlite3 |
import sqlite3 |
||||||
import config |
import config |
||||||
|
|
||||||
DB_PATH = config.DB_PATH |
DB_PATH = config.DB_PATH |
||||||
|
CONNECTION: sqlite3.Connection |
||||||
|
CURSOR: sqlite3.Cursor |
||||||
|
|
||||||
def get_db_connection(): |
def connect() -> None: |
||||||
'''Creates a connection to the database''' |
'''Creates a connection to the database''' |
||||||
conn = sqlite3.connect(DB_PATH) |
print('Connecting to the database...') |
||||||
conn.row_factory = sqlite3.Row |
global CONNECTION |
||||||
return conn |
global CURSOR |
||||||
|
CONNECTION = sqlite3.connect(DB_PATH, autocommit=True) |
||||||
|
CONNECTION.row_factory = sqlite3.Row |
||||||
|
CURSOR = CONNECTION.cursor() |
||||||
|
|
||||||
|
def get_random_character(): |
||||||
|
''' Gets a random character from the database''' |
||||||
|
CURSOR.execute('SELECT * FROM characters') |
||||||
|
characters = CURSOR.fetchall() |
||||||
|
|
||||||
|
if not characters: |
||||||
|
return None, None, None, None |
||||||
|
|
||||||
|
weights = [c['weight'] for c in characters] |
||||||
|
chosen = choices(characters, weights=weights, k=1)[0] |
||||||
|
|
||||||
|
return chosen['id'], chosen['name'], chosen['file_id'], chosen['rarity'] |
||||||
|
|
||||||
def get_or_create_user(username): |
def get_or_create_user(username): |
||||||
'''Retrieves an ID for a given user, if the user does not exist, it will be |
'''Retrieves an ID for a given user, if the user does not exist, it will be |
||||||
created.''' |
created.''' |
||||||
conn = get_db_connection() |
CURSOR.execute('SELECT id FROM users WHERE username = ?', (username,)) |
||||||
conn.row_factory = sqlite3.Row |
user = CURSOR.fetchone() |
||||||
cur = conn.cursor() |
|
||||||
cur.execute('SELECT id FROM users WHERE username = ?', (username,)) |
|
||||||
user = cur.fetchone() |
|
||||||
if user: |
if user: |
||||||
conn.close() |
|
||||||
return user[0] |
return user[0] |
||||||
|
|
||||||
# New user starts with has_rolled = False |
# New user starts with has_rolled = False |
||||||
cur.execute( |
CURSOR.execute( |
||||||
'INSERT INTO users (username, has_rolled) VALUES (?, ?)', |
'INSERT INTO users (username, has_rolled) VALUES (?, ?)', |
||||||
(username, False) |
(username, False) |
||||||
) |
) |
||||||
conn.commit() |
user_id = CURSOR.lastrowid |
||||||
user_id = cur.lastrowid |
|
||||||
conn.close() |
|
||||||
return user_id |
return user_id |
||||||
|
|
||||||
def add_pull(user_id, character_id): |
def insert_character(name: str, rarity: int, weight: float, file_id: str) -> int: |
||||||
|
'''Inserts a character''' |
||||||
|
CURSOR.execute( |
||||||
|
'INSERT INTO characters (name, rarity, weight, file_id) VALUES (?, ?, ?, ?)', |
||||||
|
(name, rarity, weight, file_id) |
||||||
|
) |
||||||
|
character_id = CURSOR.lastrowid |
||||||
|
return character_id if character_id else 0 |
||||||
|
|
||||||
|
def insert_pull(user_id, character_id): |
||||||
'''Creates a pull in the database''' |
'''Creates a pull in the database''' |
||||||
conn = get_db_connection() |
CURSOR.execute( |
||||||
cur = conn.cursor() |
'INSERT INTO pulls (user_id, character_id) VALUES (?, ?)', |
||||||
cur.execute('INSERT INTO pulls (user_id, character_id) VALUES (?, ?)', (user_id, character_id)) |
(user_id, character_id) |
||||||
conn.commit() |
) |
||||||
conn.close() |
|
||||||
|
|
||||||
def get_last_rolled_at(user_id): |
def get_last_rolled_at(user_id): |
||||||
'''Gets the timestamp when the user last rolled''' |
'''Gets the timestamp when the user last rolled''' |
||||||
conn = get_db_connection() |
CURSOR.execute("SELECT timestamp FROM pulls WHERE user_id = ? ORDER BY timestamp DESC", \ |
||||||
cur = conn.cursor() |
|
||||||
cur.execute("SELECT timestamp FROM pulls WHERE user_id = ? ORDER BY timestamp DESC", \ |
|
||||||
(user_id,)) |
(user_id,)) |
||||||
row = cur.fetchone() |
row = CURSOR.fetchone() |
||||||
conn.close() |
|
||||||
return row[0] if row else None |
return row[0] if row else None |
||||||
|
|
||||||
|
|
||||||
def get_config(key): |
def get_config(key): |
||||||
'''Reads the value for a specified config key from the db''' |
'''Reads the value for a specified config key from the db''' |
||||||
conn = get_db_connection() |
CURSOR.execute("SELECT value FROM config WHERE key = ?", (key,)) |
||||||
cur = conn.cursor() |
row = CURSOR.fetchone() |
||||||
cur.execute("SELECT value FROM config WHERE key = ?", (key,)) |
|
||||||
row = cur.fetchone() |
|
||||||
conn.close() |
|
||||||
return row[0] if row else None |
return row[0] if row else None |
||||||
|
|
||||||
def set_config(key, value): |
def set_config(key, value): |
||||||
'''Writes the value for a specified config key to the db''' |
'''Writes the value for a specified config key to the db''' |
||||||
conn = get_db_connection() |
CURSOR.execute("INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)", (key, value)) |
||||||
cur = conn.cursor() |
|
||||||
cur.execute("INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)", (key, value)) |
|
||||||
conn.commit() |
|
||||||
conn.close() |
|
||||||
|
Loading…
Reference in new issue