Исправление хранения токенов: токены теперь в user_gamers, а не в gamers
- Добавлено поле token в таблицу user_gamers - Токены теперь привязываются к паре пользователь-игрок, а не глобально - Обновлены методы работы с токенами - Теперь каждый пользователь может иметь свой токен для одного игрока
This commit is contained in:
parent
a08fc8c962
commit
2c87dc60f7
2 changed files with 35 additions and 24 deletions
|
|
@ -42,6 +42,7 @@ class Database:
|
|||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
gamer_id INTEGER NOT NULL,
|
||||
token TEXT,
|
||||
is_active BOOLEAN DEFAULT FALSE,
|
||||
period_minutes INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
|
|
@ -51,6 +52,13 @@ class Database:
|
|||
)
|
||||
''')
|
||||
|
||||
# Add token column to user_gamers if it doesn't exist
|
||||
try:
|
||||
cursor.execute("ALTER TABLE user_gamers ADD COLUMN token TEXT")
|
||||
except sqlite3.OperationalError:
|
||||
# Column already exists
|
||||
pass
|
||||
|
||||
conn.commit()
|
||||
logger.info("Database initialized successfully")
|
||||
|
||||
|
|
@ -72,7 +80,7 @@ class Database:
|
|||
return True
|
||||
return False
|
||||
|
||||
def add_gamer(self, username: str, token: Optional[str] = None) -> int:
|
||||
def add_gamer(self, username: str) -> int:
|
||||
"""Add a new gamer to the database (return gamer_id)"""
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
|
|
@ -82,35 +90,38 @@ class Database:
|
|||
existing = cursor.fetchone()
|
||||
|
||||
if existing:
|
||||
# Update existing gamer token if provided
|
||||
if token:
|
||||
cursor.execute("UPDATE gamers SET token = ? WHERE username = ?", (token, username))
|
||||
gamer_id = existing[0]
|
||||
else:
|
||||
# Add new gamer
|
||||
# Add new gamer (without token - tokens are stored in user_gamers)
|
||||
cursor.execute(
|
||||
"INSERT INTO gamers (username, token) VALUES (?, ?)",
|
||||
(username, token)
|
||||
"INSERT INTO gamers (username) VALUES (?)",
|
||||
(username,)
|
||||
)
|
||||
gamer_id = cursor.lastrowid
|
||||
|
||||
conn.commit()
|
||||
return gamer_id
|
||||
|
||||
def add_user_gamer(self, user_id: int, gamer_id: int) -> bool:
|
||||
"""Add relationship between user and gamer"""
|
||||
def add_user_gamer(self, user_id: int, gamer_id: int, token: Optional[str] = None) -> bool:
|
||||
"""Add relationship between user and gamer with optional token"""
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
try:
|
||||
cursor.execute(
|
||||
"INSERT INTO user_gamers (user_id, gamer_id) VALUES (?, ?)",
|
||||
(user_id, gamer_id)
|
||||
"INSERT INTO user_gamers (user_id, gamer_id, token) VALUES (?, ?, ?)",
|
||||
(user_id, gamer_id, token)
|
||||
)
|
||||
conn.commit()
|
||||
return True
|
||||
except sqlite3.IntegrityError:
|
||||
# Already exists
|
||||
# Already exists - update token if provided
|
||||
if token:
|
||||
cursor.execute(
|
||||
"UPDATE user_gamers SET token = ? WHERE user_id = ? AND gamer_id = ?",
|
||||
(token, user_id, gamer_id)
|
||||
)
|
||||
conn.commit()
|
||||
return False
|
||||
|
||||
def get_user_gamers(self, user_id: int) -> List[Dict[str, Any]]:
|
||||
|
|
@ -118,7 +129,7 @@ class Database:
|
|||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
SELECT g.id, g.username, g.token, ug.is_active, ug.period_minutes
|
||||
SELECT g.id, g.username, ug.token, ug.is_active, ug.period_minutes
|
||||
FROM user_gamers ug
|
||||
JOIN gamers g ON ug.gamer_id = g.id
|
||||
WHERE ug.user_id = ?
|
||||
|
|
@ -130,7 +141,7 @@ class Database:
|
|||
gamers.append({
|
||||
'id': row[0],
|
||||
'username': row[1],
|
||||
'token': row[2],
|
||||
'token': row[2], # Token from user_gamers, not gamers
|
||||
'is_active': bool(row[3]),
|
||||
'period_minutes': row[4]
|
||||
})
|
||||
|
|
@ -142,7 +153,7 @@ class Database:
|
|||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
SELECT g.id, g.username, g.token
|
||||
SELECT g.id, g.username, ug.token
|
||||
FROM user_gamers ug
|
||||
JOIN gamers g ON ug.gamer_id = g.id
|
||||
WHERE ug.user_id = ? AND ug.is_active = TRUE
|
||||
|
|
@ -154,7 +165,7 @@ class Database:
|
|||
return {
|
||||
'id': row[0],
|
||||
'username': row[1],
|
||||
'token': row[2]
|
||||
'token': row[2] # Token from user_gamers
|
||||
}
|
||||
return None
|
||||
|
||||
|
|
@ -192,7 +203,7 @@ class Database:
|
|||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
SELECT g.id, g.username, g.token, ug.period_minutes
|
||||
SELECT g.id, g.username, ug.token, ug.period_minutes
|
||||
FROM user_gamers ug
|
||||
JOIN gamers g ON ug.gamer_id = g.id
|
||||
WHERE ug.user_id = ? AND ug.period_minutes > 0
|
||||
|
|
@ -203,7 +214,7 @@ class Database:
|
|||
gamers.append({
|
||||
'id': row[0],
|
||||
'username': row[1],
|
||||
'token': row[2],
|
||||
'token': row[2], # Token from user_gamers
|
||||
'period_minutes': row[3]
|
||||
})
|
||||
|
||||
|
|
@ -214,7 +225,7 @@ class Database:
|
|||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
SELECT ug.user_id, g.id, g.username, g.token, ug.period_minutes
|
||||
SELECT ug.user_id, g.id, g.username, ug.token, ug.period_minutes
|
||||
FROM user_gamers ug
|
||||
JOIN gamers g ON ug.gamer_id = g.id
|
||||
WHERE ug.period_minutes > 0
|
||||
|
|
@ -226,7 +237,7 @@ class Database:
|
|||
'user_id': row[0],
|
||||
'id': row[1],
|
||||
'username': row[2],
|
||||
'token': row[3],
|
||||
'token': row[3], # Token from user_gamers
|
||||
'period_minutes': row[4]
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue