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