diff --git a/danding_bot/plugins/group_horse_racing/room_store.py b/danding_bot/plugins/group_horse_racing/room_store.py index c62fc4d..df2d403 100644 --- a/danding_bot/plugins/group_horse_racing/room_store.py +++ b/danding_bot/plugins/group_horse_racing/room_store.py @@ -13,7 +13,6 @@ class RoomStore: self.config = config self.rooms: dict[str, Room] = {} self._locks: dict[str, asyncio.Lock] = {} - self._last_horse_names: dict[str, str] = {} self.db_path = Path(config.RACE_DB_FILE) self.db_path.parent.mkdir(parents=True, exist_ok=True) self._init_db() @@ -48,6 +47,13 @@ class RoomStore: ) """) + cursor.execute(""" + CREATE TABLE IF NOT EXISTS user_horse_names ( + user_id TEXT PRIMARY KEY, + horse_name TEXT NOT NULL + ) + """) + conn.commit() conn.close() @@ -74,10 +80,22 @@ class RoomStore: del self.rooms[scope] def get_last_horse_name(self, user_id: str) -> Optional[str]: - return self._last_horse_names.get(user_id) + conn = sqlite3.connect(self.db_path) + cursor = conn.cursor() + cursor.execute("SELECT horse_name FROM user_horse_names WHERE user_id = ?", (user_id,)) + row = cursor.fetchone() + conn.close() + return row[0] if row else None def set_last_horse_name(self, user_id: str, horse_name: str): - self._last_horse_names[user_id] = horse_name + conn = sqlite3.connect(self.db_path) + cursor = conn.cursor() + cursor.execute( + "INSERT OR REPLACE INTO user_horse_names (user_id, horse_name) VALUES (?, ?)", + (user_id, horse_name), + ) + conn.commit() + conn.close() def _save_snapshot(self, room: Room): """Save room snapshot to database."""