Add auto-cleanup of stale game rooms after 5 minutes of inactivity

Rooms that sit idle (no player actions or CPU turns) for longer than
ROOM_IDLE_TIMEOUT_SECONDS (default 300s) are now automatically cleaned
up: CPU tasks cancelled, players notified with room_expired, WebSockets
closed, and room removed from memory.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
adlee-was-taken
2026-02-25 12:17:57 -05:00
parent 7001232658
commit 82aa3dfb3e
4 changed files with 91 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ A Room contains:
import asyncio
import random
import string
import time
from dataclasses import dataclass, field
from typing import Optional
@@ -70,6 +71,11 @@ class Room:
game_log_id: Optional[str] = None
game_lock: asyncio.Lock = field(default_factory=asyncio.Lock)
cpu_turn_task: Optional[asyncio.Task] = None
last_activity: float = field(default_factory=time.time)
def touch(self) -> None:
"""Update last_activity timestamp to mark room as active."""
self.last_activity = time.time()
def add_player(
self,