Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -166,20 +166,28 @@ class GameDatabase:
|
|
| 166 |
conn.commit()
|
| 167 |
|
| 168 |
def create_room(self, room_id: str, total_rounds: int = 8) -> bool:
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
def join_room(self, room_id: str, player_id: str, name: str,
|
| 185 |
character_id: str) -> Tuple[bool, Optional[int], str]:
|
|
@@ -254,43 +262,49 @@ class GameDatabase:
|
|
| 254 |
|
| 255 |
def cleanup_old_rooms(self):
|
| 256 |
"""Remove empty or stale rooms."""
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
|
| 281 |
-
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
def get_room_state(self, room_id: str) -> Optional[Dict]:
|
| 296 |
"""Get complete room state including players"""
|
|
|
|
| 166 |
conn.commit()
|
| 167 |
|
| 168 |
def create_room(self, room_id: str, total_rounds: int = 8) -> bool:
|
| 169 |
+
try:
|
| 170 |
+
with self.lock, self.get_connection() as conn:
|
| 171 |
+
# Cleanup old rooms before creating new one (inline to avoid nested locks)
|
| 172 |
+
stale_cutoff = (datetime.now() - timedelta(hours=24)).isoformat()
|
| 173 |
+
conn.execute("DELETE FROM rooms WHERE status = 'waiting' AND created_at < ?", (stale_cutoff,))
|
| 174 |
+
|
| 175 |
+
try:
|
| 176 |
+
conn.execute("""
|
| 177 |
+
INSERT INTO rooms (room_id, total_rounds, created_at, status)
|
| 178 |
+
VALUES (?, ?, ?, 'waiting')
|
| 179 |
+
""", (room_id, total_rounds, datetime.now().isoformat()))
|
| 180 |
+
conn.commit()
|
| 181 |
+
print(f"✨ Created room {room_id}")
|
| 182 |
+
return True
|
| 183 |
+
except sqlite3.IntegrityError as e:
|
| 184 |
+
print(f"IntegrityError creating room: {e}")
|
| 185 |
+
return False
|
| 186 |
+
except Exception as e:
|
| 187 |
+
print(f"Exception in create_room: {e}")
|
| 188 |
+
import traceback
|
| 189 |
+
traceback.print_exc()
|
| 190 |
+
return False
|
| 191 |
|
| 192 |
def join_room(self, room_id: str, player_id: str, name: str,
|
| 193 |
character_id: str) -> Tuple[bool, Optional[int], str]:
|
|
|
|
| 262 |
|
| 263 |
def cleanup_old_rooms(self):
|
| 264 |
"""Remove empty or stale rooms."""
|
| 265 |
+
try:
|
| 266 |
+
with self.lock, self.get_connection() as conn:
|
| 267 |
+
# Using ISO 8601 format for comparisons
|
| 268 |
+
stale_cutoff = (datetime.now() - timedelta(hours=24)).isoformat()
|
| 269 |
+
empty_cutoff = (datetime.now() - timedelta(hours=1)).isoformat()
|
| 270 |
|
| 271 |
+
# Find rooms waiting for more than 24 hours
|
| 272 |
+
stale_rooms_q = conn.execute(
|
| 273 |
+
"SELECT room_id FROM rooms WHERE status = 'waiting' AND created_at < ?",
|
| 274 |
+
(stale_cutoff,)
|
| 275 |
+
)
|
| 276 |
+
stale_ids = {row['room_id'] for row in stale_rooms_q.fetchall()}
|
| 277 |
|
| 278 |
+
# Find rooms waiting for more than 1 hour with no players
|
| 279 |
+
empty_rooms_q = conn.execute(
|
| 280 |
+
"""
|
| 281 |
+
SELECT room_id FROM rooms
|
| 282 |
+
WHERE status = 'waiting'
|
| 283 |
+
AND created_at < ?
|
| 284 |
+
AND room_id NOT IN (SELECT DISTINCT room_id FROM players WHERE room_id IS NOT NULL)
|
| 285 |
+
""",
|
| 286 |
+
(empty_cutoff,)
|
| 287 |
+
)
|
| 288 |
+
empty_ids = {row['room_id'] for row in empty_rooms_q.fetchall()}
|
| 289 |
|
| 290 |
+
rooms_to_delete = stale_ids.union(empty_ids)
|
| 291 |
|
| 292 |
+
if not rooms_to_delete:
|
| 293 |
+
return 0
|
| 294 |
|
| 295 |
+
for room_id in rooms_to_delete:
|
| 296 |
+
conn.execute("DELETE FROM rooms WHERE room_id = ?", (room_id,))
|
| 297 |
+
conn.execute("DELETE FROM players WHERE room_id = ?", (room_id,))
|
| 298 |
+
conn.execute("DELETE FROM spectators WHERE room_id = ?", (room_id,))
|
| 299 |
+
print(f"🧹 Cleaned up stale/empty room: {room_id}")
|
| 300 |
|
| 301 |
+
conn.commit()
|
| 302 |
+
return len(rooms_to_delete)
|
| 303 |
+
except Exception as e:
|
| 304 |
+
print(f"Exception in cleanup_old_rooms: {e}")
|
| 305 |
+
import traceback
|
| 306 |
+
traceback.print_exc()
|
| 307 |
+
return 0
|
| 308 |
|
| 309 |
def get_room_state(self, room_id: str) -> Optional[Dict]:
|
| 310 |
"""Get complete room state including players"""
|