- Added user authentication UI in the base template for navbar. - Expanded `league.dj.html` to include a new "Draft Sessions" tab showing active drafts. - Refactored Django views and models to support `DraftSession` with participants and movies. - Replaced deprecated models like `DraftParticipant` and `DraftMoviePool` with a new schema using `DraftSessionParticipant`. - Introduced WebSocket consumers (`DraftAdminConsumer`, `DraftParticipantConsumer`) with structured phase logic and caching. - Added `DraftStateManager` for managing draft state in Django cache. - Created frontend UI components in React for draft admin and participants, including phase control and WebSocket message logging. - Updated SCSS styles for improved UI structure and messaging area.
276 lines
8.9 KiB
Python
276 lines
8.9 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
|
|
|
|
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.channel_layer.group_send(
|
|
self.group_names.session,
|
|
{
|
|
"type": DraftMessage.INFORM_PHASE,
|
|
"phase": str(self.draft_state.phase)
|
|
}
|
|
)
|
|
|
|
async def should_accept_user(self)->bool:
|
|
return self.user.is_authenticated
|
|
|
|
async def receive_json(self, content):
|
|
event_type = content.get("type")
|
|
|
|
async def inform_leave_participant(self,event):
|
|
await self.send_json(
|
|
{
|
|
"type": event["type"],
|
|
"user": event["user"],
|
|
"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"],
|
|
"user": event["user"],
|
|
"participants": [user.username for user in self.draft_participants],
|
|
"connected_participants": self.draft_state.connected_users
|
|
}
|
|
)
|
|
|
|
async def reject_join_participant(self,event):
|
|
await self.send_json(
|
|
{
|
|
"type": event["type"],
|
|
"user": event["user"],
|
|
"participants": [user.username for user in self.draft_participants],
|
|
"connected_participants": self.draft_state.connected_users
|
|
}
|
|
)
|
|
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 send_draft_summary(self): ...
|
|
|
|
# === Broadcast handlers ===
|
|
async def draft_status(self, event):
|
|
await self.send_json(
|
|
{
|
|
"type": "draft.status",
|
|
"status": event["status"],
|
|
}
|
|
)
|
|
|
|
# === 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")
|
|
user = self.scope["user"]
|
|
destination = DraftPhase(content.get("destination"))
|
|
if (
|
|
event_type == DraftMessage.REQUEST_PHASE_CHANGE
|
|
and destination == DraftPhase.DETERMINE_ORDER
|
|
):
|
|
await self.determine_draft_order()
|
|
|
|
def should_accept_user(self):
|
|
return super().should_accept_user() and self.user.is_staff
|
|
|
|
# === Draft logic ===
|
|
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 ===
|
|
|
|
async def confirm_phase_change(self, event):
|
|
await self.send_json({
|
|
"type": event["type"],
|
|
"payload": event["payload"]
|
|
})
|
|
|
|
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},
|
|
)
|