Refactor draft messaging to unified enum-based protocol
- Replaced scattered message strings with `DraftMessage` `StrEnum` and numeric `DraftPhase` `IntEnum` for clear, centralized definitions. - Added Python→JS constants sync via `scripts/generate_js_constants.py` to ensure backend/frontend parity. - Refactored WebSocket consumers to use `broadcast.*` and `direct.message` handlers with `_dispatch_broadcast` for consistent event delivery. - Enhanced `DraftStateManager` to store `draft_index` and explicitly manage `connected_participants`. - Added colored logging config in settings for improved debugging. - Frontend: split UI into `ParticipantList` and `DraftMoviePool`, extracted message handlers (`handleDraftStatusMessages`, `handleUserIdentifyMessages`), and updated components to use new message/phase enums.
This commit is contained in:
@@ -32,6 +32,10 @@ class DraftCacheKeys:
|
||||
@property
|
||||
def draft_order(self):
|
||||
return f"{self.prefix}:draft_order"
|
||||
|
||||
@property
|
||||
def draft_index(self):
|
||||
return f"{self.prefix}:draft_index"
|
||||
|
||||
# @property
|
||||
# def state(self):
|
||||
@@ -64,30 +68,30 @@ class DraftStateManager:
|
||||
self.session_id = session_id
|
||||
self.cache = cache
|
||||
self.keys = DraftCacheKeys(session_id)
|
||||
self._phase = self.cache.get(self.keys.phase, DraftPhase.WAITING)
|
||||
self._initial_phase = self.cache.get(self.keys.phase, DraftPhase.WAITING.value)
|
||||
|
||||
|
||||
# === Phase Management ===
|
||||
@property
|
||||
def phase(self) -> str:
|
||||
return str(self.cache.get(self.keys.phase, self._phase))
|
||||
return str(self.cache.get(self.keys.phase, self._initial_phase))
|
||||
|
||||
@phase.setter
|
||||
def phase(self, new_phase: DraftPhase):
|
||||
self.cache.set(self.keys.phase, new_phase)
|
||||
self.cache.set(self.keys.phase, new_phase.value)
|
||||
|
||||
# === Connected Users ===
|
||||
@property
|
||||
def connected_users(self) -> list[str]:
|
||||
def connected_participants(self) -> list[str]:
|
||||
return json.loads(self.cache.get(self.keys.connected_users) or "[]")
|
||||
|
||||
def connect_user(self, username: str):
|
||||
users = set(self.connected_users)
|
||||
def connect_participant(self, username: str):
|
||||
users = set(self.connected_participants)
|
||||
users.add(username)
|
||||
self.cache.set(self.keys.connected_users, json.dumps(list(users)))
|
||||
|
||||
def disconnect_user(self, username: str):
|
||||
users = set(self.connected_users)
|
||||
def disconnect_participant(self, username: str):
|
||||
users = set(self.connected_participants)
|
||||
users.discard(username)
|
||||
self.cache.set(self.keys.connected_users, json.dumps(list(users)))
|
||||
|
||||
@@ -102,6 +106,14 @@ class DraftStateManager:
|
||||
return
|
||||
self.cache.set(self.keys.draft_order,json.dumps(draft_order))
|
||||
|
||||
@property
|
||||
def draft_index(self):
|
||||
return self.cache.get(self.keys.draft_index,0)
|
||||
|
||||
@draft_index.setter
|
||||
def draft_index(self, draft_index: int):
|
||||
self.cache.set(self.keys.draft_index, int(draft_index))
|
||||
|
||||
# === Current Nomination / Bid ===
|
||||
def start_nomination(self, movie_id: int):
|
||||
self.cache.set(self.keys.current_movie, movie_id)
|
||||
@@ -132,7 +144,8 @@ class DraftStateManager:
|
||||
return {
|
||||
"phase": self.phase,
|
||||
"draft_order": self.draft_order,
|
||||
"connected_users": self.connected_users,
|
||||
"draft_index": self.draft_index,
|
||||
"connected_participants": self.connected_participants,
|
||||
# "current_movie": self.cache.get(self.keys.current_movie),
|
||||
# "bids": self.get_bids(),
|
||||
# "timer_end": self.get_timer_end(),
|
||||
|
||||
Reference in New Issue
Block a user