- Created new `api` Django app with serializers, viewsets, and routers to expose draft sessions, participants, and movie data. - Registered `api` app in settings and updated root URL configuration. - Extended WebSocket consumers with `inform.draft_status` / `request.draft_status` to allow fetching current draft state. - Updated `DraftSession` and related models to support reverse lookups for draft picks. - Enhanced draft state manager to include `draft_order` in summaries. - Added React WebSocket context provider, connection status component, and new admin/participant panels with phase and participant tracking. - Updated SCSS for participant lists, phase indicators, and status badges. - Modified Django templates to mount new React roots for admin and participant views. - Updated Webpack dev server config to proxy WebSocket connections.
295 lines
9.5 KiB
Python
295 lines
9.5 KiB
Python
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
|
from channels.db import database_sync_to_async
|
|
from django.core.exceptions import PermissionDenied
|
|
from boxofficefantasy.models import League, Season
|
|
from boxofficefantasy.views import parse_season_slug
|
|
from draft.models import DraftSession, DraftSessionParticipant
|
|
from django.core.cache import cache
|
|
import asyncio
|
|
from django.contrib.auth.models import User
|
|
from draft.constants import (
|
|
DraftMessage,
|
|
DraftPhase,
|
|
DraftGroupChannelNames,
|
|
)
|
|
from draft.state import DraftCacheKeys, DraftStateManager
|
|
from typing import Any
|
|
|
|
import random
|
|
|
|
|
|
class DraftConsumerBase(AsyncJsonWebsocketConsumer):
|
|
group_names: DraftGroupChannelNames
|
|
cache_keys: DraftCacheKeys
|
|
draft_state: DraftStateManager
|
|
user: User
|
|
|
|
async def connect(self):
|
|
draft_hashid = self.scope["url_route"]["kwargs"].get("draft_session_id_hashed")
|
|
|
|
self.draft_session = await self.get_draft_session(
|
|
draft_session_id_hashed=draft_hashid,
|
|
)
|
|
self.draft_participants = await self.get_draft_participants(
|
|
session=self.draft_session
|
|
)
|
|
|
|
self.group_names = DraftGroupChannelNames(draft_hashid)
|
|
self.cache_keys = DraftCacheKeys(draft_hashid)
|
|
self.draft_state = DraftStateManager(draft_hashid)
|
|
|
|
self.user = self.scope["user"]
|
|
if not self.should_accept_user():
|
|
await self.send_json(
|
|
{
|
|
"type": DraftMessage.REJECT_JOIN_PARTICIPANT,
|
|
"user": self.user.username,
|
|
}
|
|
)
|
|
await self.close()
|
|
await self.channel_layer.group_send(
|
|
self.group_names.session,
|
|
{
|
|
"type": DraftMessage.REJECT_JOIN_PARTICIPANT,
|
|
"user": self.user.username,
|
|
},
|
|
)
|
|
return
|
|
else:
|
|
await self.accept()
|
|
await self.channel_layer.group_add(
|
|
self.group_names.session, self.channel_name
|
|
)
|
|
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,
|
|
"payload": self.get_draft_status(),
|
|
},
|
|
)
|
|
|
|
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:
|
|
await self.send_json(
|
|
{
|
|
"type": DraftMessage.INFORM_DRAFT_STATUS,
|
|
"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,
|
|
},
|
|
}
|
|
)
|
|
|
|
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 inform_draft_status(self, event):
|
|
await self.send_json(
|
|
{"type": event["type"], "payload": self.get_draft_status()}
|
|
)
|
|
|
|
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): ...
|
|
|
|
def get_draft_status(self) -> dict[str, Any]:
|
|
return {
|
|
**self.draft_state.get_summary(),
|
|
"user": self.user.username,
|
|
"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:
|
|
draft_session_id = DraftSession.decode_id(draft_session_id_hashed)
|
|
if draft_session_id:
|
|
draft_session = DraftSession.objects.select_related(
|
|
"season", "season__league"
|
|
).get(pk=draft_session_id)
|
|
else:
|
|
raise Exception()
|
|
|
|
return draft_session
|
|
|
|
@database_sync_to_async
|
|
def get_draft_participants(self, session) -> list[DraftSessionParticipant]:
|
|
participants = session.participants.all()
|
|
return list(participants.all())
|
|
|
|
|
|
class DraftAdminConsumer(DraftConsumerBase):
|
|
async def connect(self):
|
|
await super().connect()
|
|
if not self.user.is_staff:
|
|
await self.close()
|
|
return
|
|
|
|
await self.channel_layer.group_add(self.group_names.admin, self.channel_name)
|
|
|
|
async def receive_json(self, content):
|
|
await super().receive_json(content)
|
|
event_type = content.get("type")
|
|
if (
|
|
event_type == DraftMessage.REQUEST_PHASE_CHANGE
|
|
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
|
|
):
|
|
await self.start_nominate();
|
|
|
|
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.channel_layer.group_send(
|
|
self.group_names.session,
|
|
{
|
|
"type": DraftMessage.CONFIRM_PHASE_CHANGE,
|
|
"payload": {"phase": self.draft_state.phase},
|
|
},
|
|
)
|
|
|
|
async def determine_draft_order(self):
|
|
draft_order = random.sample(
|
|
self.draft_participants, len(self.draft_participants)
|
|
)
|
|
self.draft_state.draft_order = [p.username for p in draft_order]
|
|
await self.set_draft_phase(DraftPhase.DETERMINE_ORDER)
|
|
|
|
await self.channel_layer.group_send(
|
|
self.group_names.session,
|
|
{
|
|
"type": DraftMessage.CONFIRM_DETERMINE_DRAFT_ORDER,
|
|
"payload": {"draft_order": self.draft_state.draft_order},
|
|
},
|
|
)
|
|
|
|
async def set_draft_phase(self, destination: DraftPhase):
|
|
self.draft_state.phase = destination
|
|
await self.channel_layer.group_send(
|
|
self.group_names.session,
|
|
{
|
|
"type": DraftMessage.CONFIRM_PHASE_CHANGE,
|
|
"payload": {"phase": self.draft_state.phase},
|
|
},
|
|
)
|
|
|
|
# === Broadcast Handlers ===
|
|
|
|
|
|
class DraftParticipantConsumer(DraftConsumerBase):
|
|
async def connect(self):
|
|
await super().connect()
|
|
|
|
self.draft_state.connect_user(self.user.username)
|
|
|
|
await self.channel_layer.group_add(
|
|
self.group_names.participant, self.channel_name
|
|
)
|
|
|
|
async def disconnect(self, close_code):
|
|
await self.channel_layer.group_send(
|
|
self.group_names.session,
|
|
{"type": DraftMessage.INFORM_LEAVE_PARTICIPANT, "user": self.user.username},
|
|
)
|
|
await super().disconnect(close_code)
|
|
self.draft_state.disconnect_user(self.user.username)
|
|
await self.channel_layer.group_discard(
|
|
self.group_names.session, self.channel_name
|
|
)
|
|
|
|
def should_accept_user(self):
|
|
return super().should_accept_user() and self.user in self.draft_participants
|
|
|
|
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"],
|
|
}
|
|
)
|
|
|
|
# === Draft ===
|
|
|
|
async def nominate(self, movie_title): ...
|
|
|
|
async def place_bid(self, amount, user): ...
|
|
|
|
# === Example DB Access ===
|
|
|
|
@database_sync_to_async
|
|
def add_draft_participant(self):
|
|
self.participant, _ = DraftSessionParticipant.objects.get_or_create(
|
|
user=self.user,
|
|
draft=self.draft_session,
|
|
defaults={"budget": self.draft_session.settings.starting_budget},
|
|
)
|