|
|
@ -1,22 +1,26 @@ |
|
|
|
import sqlite3 |
|
|
|
import sqlite3 |
|
|
|
import random |
|
|
|
import random |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: grab this from a config file instead |
|
|
|
DB_PATH = "./gacha_game.db" # Adjust if needed |
|
|
|
DB_PATH = "./gacha_game.db" # Adjust if needed |
|
|
|
|
|
|
|
|
|
|
|
def get_db_connection(): |
|
|
|
def get_db_connection(): |
|
|
|
|
|
|
|
'''Creates a connection to the database''' |
|
|
|
conn = sqlite3.connect(DB_PATH) |
|
|
|
conn = sqlite3.connect(DB_PATH) |
|
|
|
conn.row_factory = sqlite3.Row |
|
|
|
conn.row_factory = sqlite3.Row |
|
|
|
return conn |
|
|
|
return conn |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
created.''' |
|
|
|
conn = get_db_connection() |
|
|
|
conn = get_db_connection() |
|
|
|
conn.row_factory = sqlite3.Row |
|
|
|
conn.row_factory = sqlite3.Row |
|
|
|
cur = conn.cursor() |
|
|
|
cur = conn.cursor() |
|
|
|
cur.execute('SELECT * FROM users WHERE username = ?', (username,)) |
|
|
|
cur.execute('SELECT id FROM users WHERE username = ?', (username,)) |
|
|
|
user = cur.fetchone() |
|
|
|
user = cur.fetchone() |
|
|
|
if user: |
|
|
|
if user: |
|
|
|
conn.close() |
|
|
|
conn.close() |
|
|
|
return user['id'] |
|
|
|
return user |
|
|
|
|
|
|
|
|
|
|
|
# New user starts with has_rolled = False |
|
|
|
# New user starts with has_rolled = False |
|
|
|
cur.execute( |
|
|
|
cur.execute( |
|
|
@ -29,6 +33,7 @@ def get_or_create_user(username): |
|
|
|
return user_id |
|
|
|
return user_id |
|
|
|
|
|
|
|
|
|
|
|
def add_pull(user_id, character_id): |
|
|
|
def add_pull(user_id, character_id): |
|
|
|
|
|
|
|
'''Creates a pull in the database''' |
|
|
|
conn = get_db_connection() |
|
|
|
conn = get_db_connection() |
|
|
|
cur = conn.cursor() |
|
|
|
cur = conn.cursor() |
|
|
|
cur.execute('INSERT INTO pulls (user_id, character_id) VALUES (?, ?)', (user_id, character_id)) |
|
|
|
cur.execute('INSERT INTO pulls (user_id, character_id) VALUES (?, ?)', (user_id, character_id)) |
|
|
@ -36,6 +41,7 @@ def add_pull(user_id, character_id): |
|
|
|
conn.close() |
|
|
|
conn.close() |
|
|
|
|
|
|
|
|
|
|
|
def get_config(key): |
|
|
|
def get_config(key): |
|
|
|
|
|
|
|
'''Reads the value for a specified config key from the db''' |
|
|
|
conn = get_db_connection() |
|
|
|
conn = get_db_connection() |
|
|
|
cur = conn.cursor() |
|
|
|
cur = conn.cursor() |
|
|
|
cur.execute("SELECT value FROM config WHERE key = ?", (key,)) |
|
|
|
cur.execute("SELECT value FROM config WHERE key = ?", (key,)) |
|
|
@ -44,8 +50,9 @@ def get_config(key): |
|
|
|
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''' |
|
|
|
conn = get_db_connection() |
|
|
|
conn = get_db_connection() |
|
|
|
cur = conn.cursor() |
|
|
|
cur = conn.cursor() |
|
|
|
cur.execute("INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)", (key, value)) |
|
|
|
cur.execute("INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)", (key, value)) |
|
|
|
conn.commit() |
|
|
|
conn.commit() |
|
|
|
conn.close() |
|
|
|
conn.close() |
|
|
|