Skip to content

Commit 0ff8782

Browse files
committed
feat + fix: Реализовал создание папки под каждого пользователя, заодно убрал проблему общего файла последних игр
1 parent cd76119 commit 0ff8782

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

db.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def login_user(self, login: str, password: str) -> bool:
5757
self._current_user_id = user[0]
5858
self._current_user_login = user[1]
5959
self._save_session()
60+
from utils import data_manager
61+
data_manager.update_current_user(self._current_user_id)
6062
return True
6163
return False
6264
finally:
@@ -91,6 +93,8 @@ def logout(self):
9193
self._current_user_id = None
9294
self._current_user_login = None
9395
self._delete_session()
96+
from utils import data_manager
97+
data_manager.update_current_user(None)
9498

9599
def _save_session(self):
96100
from utils import data_manager

utils.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,40 @@ def clear_session(self):
6262

6363
class GameHistoryManager:
6464
def __init__(self):
65-
self.file_manager = FileManager("last_games.txt")
66-
self.file_manager.ensure_exists()
67-
68-
def get_last_games(self):
65+
self.data_path = get_data_path()
66+
self.current_user_id = None
67+
68+
def _get_user_history_path(self, user_id: int) -> Path:
69+
70+
user_dir = self.data_path / f"user_{user_id}"
71+
user_dir.mkdir(exist_ok=True)
72+
return user_dir / "last_games.txt"
73+
74+
def set_current_user(self, user_id: int | None):
75+
76+
self.current_user_id = user_id
77+
if user_id is not None:
78+
path = self._get_user_history_path(user_id)
79+
if not path.exists():
80+
path.touch()
81+
82+
def get_last_games(self) -> list[str]:
83+
84+
if self.current_user_id is None:
85+
return []
86+
87+
path = self._get_user_history_path(self.current_user_id)
6988
try:
70-
content = self.file_manager._read_content()
71-
return [game for game in content.split("\n") if game.strip()]
89+
with open(path, 'r', encoding='utf-8') as f:
90+
return [game for game in f.read().split("\n") if game.strip()]
7291
except FileNotFoundError:
7392
return []
7493

75-
def add_game(self, game_name):
94+
def add_game(self, game_name: str):
95+
96+
if self.current_user_id is None:
97+
return
98+
7699
games = self.get_last_games()
77100
# Удаляем дубликат и добавляем в начало
78101
games = [game for game in games if game != game_name]
@@ -81,12 +104,24 @@ def add_game(self, game_name):
81104
games = games[:5]
82105
self._save_games(games)
83106

84-
def _save_games(self, games):
107+
def _save_games(self, games: list[str]):
108+
109+
if self.current_user_id is None:
110+
return
111+
112+
path = self._get_user_history_path(self.current_user_id)
85113
content = "\n".join(game for game in games if game.strip())
86-
self.file_manager._write_content(content)
114+
with open(path, 'w', encoding='utf-8') as f:
115+
f.write(content)
87116

88117
def clear_history(self):
89-
self.file_manager._write_content("")
118+
119+
if self.current_user_id is None:
120+
return
121+
122+
path = self._get_user_history_path(self.current_user_id)
123+
with open(path, 'w', encoding='utf-8') as f:
124+
f.write("")
90125

91126

92127
class SettingsManager:
@@ -120,6 +155,9 @@ def __init__(self):
120155
self.history = GameHistoryManager()
121156
self.settings = SettingsManager()
122157
self.session = SessionManager()
158+
159+
user_id, _ = self.session.load_session()
160+
self.history.set_current_user(user_id)
123161

124162
def get_last_games(self):
125163
return self.history.get_last_games()
@@ -138,6 +176,9 @@ def set_theme(self, theme):
138176

139177
def update_setting(self, key, value):
140178
self.settings.update_setting(key, value)
179+
180+
def update_current_user(self, user_id: int | None):
181+
self.history.set_current_user(user_id)
141182

142183

143184
data_manager = DataManager()

0 commit comments

Comments
 (0)