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:
@@ -14,6 +14,10 @@ from draft.constants import (
|
||||
)
|
||||
from draft.state import DraftCacheKeys, DraftStateManager
|
||||
from typing import Any
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__) # __name__ = module path
|
||||
|
||||
|
||||
import random
|
||||
|
||||
@@ -40,18 +44,21 @@ class DraftConsumerBase(AsyncJsonWebsocketConsumer):
|
||||
|
||||
self.user = self.scope["user"]
|
||||
if not self.should_accept_user():
|
||||
await self.send_json(
|
||||
await self.channel_layer.send(
|
||||
self.channel_name,
|
||||
{
|
||||
"type": DraftMessage.REJECT_JOIN_PARTICIPANT,
|
||||
"user": self.user.username,
|
||||
"type": "direct.message",
|
||||
"subtype": DraftMessage.PARTICIPANT_JOIN_REJECT,
|
||||
"payload":{"current_user": self.user.username}
|
||||
}
|
||||
)
|
||||
await self.close()
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.session,
|
||||
self.group_names.admin,
|
||||
{
|
||||
"type": DraftMessage.REJECT_JOIN_PARTICIPANT,
|
||||
"user": self.user.username,
|
||||
"type": "broadcast.admin",
|
||||
"subtype": DraftMessage.PARTICIPANT_JOIN_REJECT,
|
||||
"payload":{"user": self.user.username}
|
||||
},
|
||||
)
|
||||
return
|
||||
@@ -62,81 +69,56 @@ class DraftConsumerBase(AsyncJsonWebsocketConsumer):
|
||||
)
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.session,
|
||||
{"type": DraftMessage.INFORM_JOIN_USER, "user": self.user.username},
|
||||
)
|
||||
await self.send_json(
|
||||
|
||||
{
|
||||
"type": DraftMessage.INFORM_DRAFT_STATUS,
|
||||
"type": "broadcast.session",
|
||||
"subtype": DraftMessage.USER_JOIN_INFORM,
|
||||
"payload": {"user": self.user.username},
|
||||
},
|
||||
)
|
||||
await self.channel_layer.send(
|
||||
self.channel_name,
|
||||
{
|
||||
"type": "direct.message",
|
||||
"subtype": DraftMessage.STATUS_SYNC_INFORM,
|
||||
"payload": self.get_draft_status(),
|
||||
},
|
||||
)
|
||||
await self.channel_layer.send(
|
||||
self.channel_name,
|
||||
{
|
||||
"type": "direct.message",
|
||||
"subtype": DraftMessage.USER_IDENTIFICATION_INFORM,
|
||||
"payload": {"user": self.user.username},
|
||||
},
|
||||
)
|
||||
|
||||
async def should_accept_user(self) -> bool:
|
||||
return self.user.is_authenticated
|
||||
|
||||
async def receive_json(self, content):
|
||||
event_type = content.get("type")
|
||||
if event_type == DraftMessage.REQUEST_DRAFT_STATUS:
|
||||
if event_type == DraftMessage.STATUS_SYNC_REQUEST:
|
||||
await self.send_json(
|
||||
{
|
||||
"type": DraftMessage.INFORM_DRAFT_STATUS,
|
||||
"type": DraftMessage.STATUS_SYNC_INFORM,
|
||||
"payload": self.get_draft_status(),
|
||||
}
|
||||
)
|
||||
|
||||
async def inform_leave_participant(self, event):
|
||||
await self.send_json(
|
||||
{
|
||||
"type": event["type"],
|
||||
"user": event["user"],
|
||||
"payload": {
|
||||
"participants": [user.username for user in self.draft_participants],
|
||||
"connected_participants": self.draft_state.connected_users,
|
||||
},
|
||||
}
|
||||
)
|
||||
# Broadcast Handlers
|
||||
async def direct_message(self, event):
|
||||
await self._dispatch_broadcast(event)
|
||||
|
||||
async def inform_join_user(self, event):
|
||||
await self.send_json(
|
||||
{
|
||||
"type": event["type"],
|
||||
"payload": {
|
||||
"user": event["user"],
|
||||
"participants": [user.username for user in self.draft_participants],
|
||||
"connected_participants": self.draft_state.connected_users,
|
||||
},
|
||||
}
|
||||
)
|
||||
async def broadcast_session(self, event):
|
||||
await self._dispatch_broadcast(event)
|
||||
|
||||
async def inform_draft_status(self, event):
|
||||
await self.send_json(
|
||||
{"type": event["type"], "payload": self.get_draft_status()}
|
||||
)
|
||||
async def _dispatch_broadcast(self, event):
|
||||
logger.info(f"dispatching message {event}")
|
||||
subtype = event.get("subtype")
|
||||
payload = event.get("payload", {})
|
||||
await self.send_json({"type": subtype, "payload": payload})
|
||||
|
||||
async def reject_join_participant(self, event):
|
||||
await self.send_json(
|
||||
{
|
||||
"type": event["type"],
|
||||
"user": event["user"],
|
||||
}
|
||||
)
|
||||
|
||||
async def inform_phase(self, event):
|
||||
await self.send_json({"type": event["type"], "phase": event["phase"]})
|
||||
|
||||
async def confirm_determine_draft_order(self, event):
|
||||
await self.send_json(
|
||||
{
|
||||
"type": DraftMessage.CONFIRM_DETERMINE_DRAFT_ORDER,
|
||||
"payload": event["payload"],
|
||||
}
|
||||
)
|
||||
|
||||
async def confirm_phase_change(self, event):
|
||||
await self.send_json({"type": event["type"], "payload": event["payload"]})
|
||||
|
||||
async def send_draft_summary(self): ...
|
||||
# === Methods ===
|
||||
|
||||
def get_draft_status(self) -> dict[str, Any]:
|
||||
return {
|
||||
@@ -145,8 +127,6 @@ class DraftConsumerBase(AsyncJsonWebsocketConsumer):
|
||||
"participants": [user.username for user in self.draft_participants],
|
||||
}
|
||||
|
||||
# === Broadcast handlers ===
|
||||
|
||||
# === DB Access ===
|
||||
@database_sync_to_async
|
||||
def get_draft_session(self, draft_session_id_hashed) -> DraftSession:
|
||||
@@ -177,29 +157,42 @@ class DraftAdminConsumer(DraftConsumerBase):
|
||||
|
||||
async def receive_json(self, content):
|
||||
await super().receive_json(content)
|
||||
logger.info(f"Receive message {content}")
|
||||
event_type = content.get("type")
|
||||
if (
|
||||
event_type == DraftMessage.REQUEST_PHASE_CHANGE
|
||||
event_type == DraftMessage.PHASE_CHANGE_REQUEST
|
||||
and content.get("destination") == DraftPhase.DETERMINE_ORDER
|
||||
):
|
||||
await self.determine_draft_order()
|
||||
|
||||
|
||||
if (
|
||||
event_type == DraftMessage.REQUEST_PHASE_CHANGE
|
||||
and content.get("destination") == DraftPhase.NOMINATION
|
||||
event_type == DraftMessage.PHASE_CHANGE_REQUEST
|
||||
and content.get("destination") == DraftPhase.NOMINATING
|
||||
):
|
||||
await self.start_nominate();
|
||||
await self.start_nominate()
|
||||
|
||||
if event_type == DraftMessage.DRAFT_INDEX_ADVANCE_REQUEST:
|
||||
self.draft_state.draft_index += 1
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.session,
|
||||
{
|
||||
"type": "broadcast.session",
|
||||
"subtype": DraftMessage.DRAFT_INDEX_ADVANCE_CONFIRM,
|
||||
"payload": self.draft_state.get_summary(),
|
||||
},
|
||||
)
|
||||
|
||||
def should_accept_user(self):
|
||||
return super().should_accept_user() and self.user.is_staff
|
||||
|
||||
# === Draft logic ===
|
||||
async def start_nominate(self):
|
||||
await self.set_draft_phase(DraftPhase.NOMINATION)
|
||||
await self.set_draft_phase(DraftPhase.NOMINATING)
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.session,
|
||||
{
|
||||
"type": DraftMessage.CONFIRM_PHASE_CHANGE,
|
||||
"type": "broadcast.session",
|
||||
"subtype": DraftMessage.PHASE_CHANGE_CONFIRM,
|
||||
"payload": {"phase": self.draft_state.phase},
|
||||
},
|
||||
)
|
||||
@@ -214,7 +207,8 @@ class DraftAdminConsumer(DraftConsumerBase):
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.session,
|
||||
{
|
||||
"type": DraftMessage.CONFIRM_DETERMINE_DRAFT_ORDER,
|
||||
"type": "broadcast.session",
|
||||
"subtype": DraftMessage.ORDER_DETERMINE_CONFIRM,
|
||||
"payload": {"draft_order": self.draft_state.draft_order},
|
||||
},
|
||||
)
|
||||
@@ -224,31 +218,55 @@ class DraftAdminConsumer(DraftConsumerBase):
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.session,
|
||||
{
|
||||
"type": DraftMessage.CONFIRM_PHASE_CHANGE,
|
||||
"type": "broadcast.session",
|
||||
"subtype": DraftMessage.PHASE_CHANGE_CONFIRM,
|
||||
"payload": {"phase": self.draft_state.phase},
|
||||
},
|
||||
)
|
||||
|
||||
# === Broadcast Handlers ===
|
||||
|
||||
async def broadcast_admin(self, event):
|
||||
await self._dispatch_broadcast(event)
|
||||
|
||||
|
||||
class DraftParticipantConsumer(DraftConsumerBase):
|
||||
async def connect(self):
|
||||
await super().connect()
|
||||
|
||||
self.draft_state.connect_user(self.user.username)
|
||||
self.draft_state.connect_participant(self.user.username)
|
||||
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.session,
|
||||
{
|
||||
"type": "broadcast.session",
|
||||
"subtype": DraftMessage.PARTICIPANT_JOIN_CONFIRM,
|
||||
"payload": {
|
||||
"user": self.user.username,
|
||||
"connected_participants": self.draft_state.connected_participants,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
await self.channel_layer.group_add(
|
||||
self.group_names.participant, self.channel_name
|
||||
)
|
||||
|
||||
async def disconnect(self, close_code):
|
||||
self.draft_state.disconnect_participant(self.user.username)
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.session,
|
||||
{"type": DraftMessage.INFORM_LEAVE_PARTICIPANT, "user": self.user.username},
|
||||
{
|
||||
"type": "broadcast.session",
|
||||
"subtype": DraftMessage.PARTICIPANT_LEAVE_INFORM,
|
||||
"payload": {
|
||||
"user": self.user.username,
|
||||
"connected_participants": self.draft_state.connected_participants,
|
||||
},
|
||||
},
|
||||
)
|
||||
await super().disconnect(close_code)
|
||||
self.draft_state.disconnect_user(self.user.username)
|
||||
self.draft_state.disconnect_participant(self.user.username)
|
||||
await self.channel_layer.group_discard(
|
||||
self.group_names.session, self.channel_name
|
||||
)
|
||||
@@ -258,24 +276,11 @@ class DraftParticipantConsumer(DraftConsumerBase):
|
||||
|
||||
async def receive_json(self, content):
|
||||
await super().receive_json(content)
|
||||
event_type = content.get("type")
|
||||
user = self.scope["user"]
|
||||
|
||||
if event_type == DraftMessage.REQUEST_JOIN_PARTICIPANT:
|
||||
await self.channel_layer.group_send(
|
||||
self.group_names.admin,
|
||||
{"type": DraftMessage.REQUEST_JOIN_PARTICIPANT, "user": user},
|
||||
)
|
||||
|
||||
# === Broadcast handlers ===
|
||||
|
||||
async def request_join_participant(self, event):
|
||||
await self.send_json(
|
||||
{
|
||||
"type": event["type"],
|
||||
"user": event["user"],
|
||||
}
|
||||
)
|
||||
async def broadcast_participant(self, event):
|
||||
await self._dispatch_broadcast(event)
|
||||
|
||||
# === Draft ===
|
||||
|
||||
|
||||
Reference in New Issue
Block a user