npc0 commited on
Commit
cd92569
·
verified ·
1 Parent(s): 533a8cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -45
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
- with self.lock, self.get_connection() as conn:
170
- # Cleanup old rooms before creating new one
171
- self.cleanup_old_rooms()
172
-
173
- try:
174
- conn.execute("""
175
- INSERT INTO rooms (room_id, total_rounds, created_at, status)
176
- VALUES (?, ?, ?, 'waiting')
177
- """, (room_id, total_rounds, datetime.now().isoformat()))
178
- conn.commit()
179
- print(f" Created room {room_id}")
180
- return True
181
- except sqlite3.IntegrityError:
182
- return False
 
 
 
 
 
 
 
 
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
- with self.lock, self.get_connection() as conn:
258
- # Using ISO 8601 format for comparisons
259
- stale_cutoff = (datetime.now() - timedelta(hours=24)).isoformat()
260
- empty_cutoff = (datetime.now() - timedelta(hours=1)).isoformat()
 
261
 
262
- # Find rooms waiting for more than 24 hours
263
- stale_rooms_q = conn.execute(
264
- "SELECT room_id FROM rooms WHERE status = 'waiting' AND created_at < ?",
265
- (stale_cutoff,)
266
- )
267
- stale_ids = {row['room_id'] for row in stale_rooms_q.fetchall()}
268
 
269
- # Find rooms waiting for more than 1 hour with no players
270
- empty_rooms_q = conn.execute(
271
- """
272
- SELECT room_id FROM rooms
273
- WHERE status = 'waiting'
274
- AND created_at < ?
275
- AND room_id NOT IN (SELECT DISTINCT room_id FROM players WHERE room_id IS NOT NULL)
276
- """,
277
- (empty_cutoff,)
278
- )
279
- empty_ids = {row['room_id'] for row in empty_rooms_q.fetchall()}
280
 
281
- rooms_to_delete = stale_ids.union(empty_ids)
282
 
283
- if not rooms_to_delete:
284
- return 0
285
 
286
- for room_id in rooms_to_delete:
287
- conn.execute("DELETE FROM rooms WHERE room_id = ?", (room_id,))
288
- conn.execute("DELETE FROM players WHERE room_id = ?", (room_id,))
289
- conn.execute("DELETE FROM spectators WHERE room_id = ?", (room_id,))
290
- print(f"🧹 Cleaned up stale/empty room: {room_id}")
291
 
292
- conn.commit()
293
- return len(rooms_to_delete)
 
 
 
 
 
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"""