2023-03-23 15:17:29 +00:00
|
|
|
# Copyright (C) 2023 by Animath
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from random import randint
|
2023-03-22 14:24:15 +00:00
|
|
|
|
|
|
|
from asgiref.sync import sync_to_async
|
|
|
|
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
2023-03-22 17:44:49 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-03-22 14:24:15 +00:00
|
|
|
|
2023-03-22 17:44:49 +00:00
|
|
|
from draw.models import Draw, Round, Pool, TeamDraw
|
|
|
|
from participation.models import Tournament, Participation
|
|
|
|
from registration.models import Registration
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_orga(f):
|
|
|
|
async def func(self, *args, **kwargs):
|
|
|
|
reg = self.registration
|
|
|
|
if reg.is_volunteer and not reg.is_admin and self.tournament not in reg.interesting_tournaments \
|
|
|
|
or not reg.is_volunteer:
|
|
|
|
return await self.alert(_("You are not an organizer."), 'danger')
|
|
|
|
|
|
|
|
return await f(self, *args, **kwargs)
|
|
|
|
|
|
|
|
return func
|
2023-03-22 14:24:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DrawConsumer(AsyncJsonWebsocketConsumer):
|
|
|
|
async def connect(self):
|
|
|
|
tournament_id = self.scope['url_route']['kwargs']['tournament_id']
|
2023-03-23 15:17:29 +00:00
|
|
|
self.tournament = await Tournament.objects.filter(pk=tournament_id)\
|
|
|
|
.prefetch_related('draw__current_round__current_pool__current_team').aget()
|
2023-03-22 14:24:15 +00:00
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
self.participations = []
|
|
|
|
async for participation in Participation.objects.filter(tournament=self.tournament, valid=True)\
|
|
|
|
.prefetch_related('team'):
|
|
|
|
self.participations.append(participation)
|
2023-03-22 17:44:49 +00:00
|
|
|
|
2023-03-22 14:24:15 +00:00
|
|
|
user = self.scope['user']
|
2023-03-23 15:17:29 +00:00
|
|
|
reg = await Registration.objects.aget(user=user)
|
2023-03-22 17:44:49 +00:00
|
|
|
self.registration = reg
|
2023-03-22 14:24:15 +00:00
|
|
|
if reg.is_volunteer and not reg.is_admin and self.tournament not in reg.interesting_tournaments \
|
|
|
|
or not reg.is_volunteer and reg.team.participation.tournament != self.tournament:
|
|
|
|
# This user may not have access to the drawing session
|
|
|
|
await self.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
await self.accept()
|
|
|
|
await self.channel_layer.group_add(f"tournament-{self.tournament.id}", self.channel_name)
|
2023-03-23 15:17:29 +00:00
|
|
|
if not self.registration.is_volunteer:
|
|
|
|
await self.channel_layer.group_add(f"team-{self.registration.team.trigram}", self.channel_name)
|
|
|
|
else:
|
|
|
|
await self.channel_layer.group_add(f"volunteer-{self.tournament.id}", self.channel_name)
|
2023-03-22 14:24:15 +00:00
|
|
|
|
|
|
|
async def disconnect(self, close_code):
|
|
|
|
await self.channel_layer.group_discard(f"tournament-{self.tournament.id}", self.channel_name)
|
2023-03-23 15:17:29 +00:00
|
|
|
if not self.registration.is_volunteer:
|
|
|
|
await self.channel_layer.group_discard(f"team-{self.registration.team.trigram}", self.channel_name)
|
|
|
|
else:
|
|
|
|
await self.channel_layer.group_discard(f"volunteer-{self.tournament.id}", self.channel_name)
|
2023-03-22 14:24:15 +00:00
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
async def alert(self, message: str, alert_type: str = 'info', **kwargs):
|
2023-03-22 17:44:49 +00:00
|
|
|
return await self.send_json({'type': 'alert', 'alert_type': alert_type, 'message': str(message)})
|
|
|
|
|
2023-03-22 14:24:15 +00:00
|
|
|
async def receive_json(self, content, **kwargs):
|
2023-03-22 17:44:49 +00:00
|
|
|
print(content)
|
|
|
|
|
|
|
|
match content['type']:
|
|
|
|
case 'start_draw':
|
|
|
|
await self.start_draw(**content)
|
2023-03-23 15:17:29 +00:00
|
|
|
case 'dice':
|
|
|
|
await self.process_dice(**content)
|
2023-03-22 17:44:49 +00:00
|
|
|
|
|
|
|
@ensure_orga
|
|
|
|
async def start_draw(self, fmt, **kwargs):
|
|
|
|
print(fmt, kwargs)
|
|
|
|
try:
|
|
|
|
fmt = list(map(int, fmt.split('+')))
|
|
|
|
except ValueError as e:
|
|
|
|
return await self.alert(_("Invalid format"), 'danger')
|
|
|
|
|
|
|
|
print(fmt, sum(fmt), len(self.participations))
|
|
|
|
|
|
|
|
if sum(fmt) != len(self.participations):
|
|
|
|
return await self.alert(
|
|
|
|
_("The sum must be equal to the number of teams: expected {len}, got {sum}")\
|
|
|
|
.format(len=len(self.participations), sum=sum(fmt)), 'danger')
|
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
draw = await Draw.objects.acreate(tournament=self.tournament)
|
|
|
|
r1 = None
|
2023-03-22 19:41:16 +00:00
|
|
|
for i in [1, 2]:
|
2023-03-23 15:17:29 +00:00
|
|
|
r = await Round.objects.acreate(draw=draw, number=i)
|
|
|
|
if i == 1:
|
|
|
|
r1 = r
|
|
|
|
|
2023-03-22 19:41:16 +00:00
|
|
|
for j, f in enumerate(fmt):
|
2023-03-23 15:17:29 +00:00
|
|
|
await Pool.objects.acreate(round=r, letter=j + 1, size=f)
|
2023-03-22 19:41:16 +00:00
|
|
|
for participation in self.participations:
|
2023-03-23 15:17:29 +00:00
|
|
|
await TeamDraw.objects.acreate(participation=participation, round=r)
|
2023-03-24 10:10:07 +00:00
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.send_poules', 'round': r})
|
2023-03-23 15:17:29 +00:00
|
|
|
|
|
|
|
draw.current_round = r1
|
|
|
|
await sync_to_async(draw.save)()
|
2023-03-22 17:44:49 +00:00
|
|
|
|
|
|
|
await self.alert(_("Draw started!"), 'success')
|
|
|
|
|
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
2023-03-22 19:41:16 +00:00
|
|
|
{'type': 'draw.start', 'fmt': fmt, 'draw': draw})
|
2023-03-23 15:17:29 +00:00
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.set_info', 'draw': draw})
|
2023-03-24 10:10:07 +00:00
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.set_active', 'draw': self.tournament.draw})
|
2023-03-22 17:44:49 +00:00
|
|
|
|
|
|
|
async def draw_start(self, content):
|
|
|
|
await self.alert(_("The draw for the tournament {tournament} will start.")\
|
|
|
|
.format(tournament=self.tournament.name), 'warning')
|
|
|
|
await self.send_json({'type': 'draw_start', 'fmt': content['fmt'],
|
|
|
|
'trigrams': [p.team.trigram for p in self.participations]})
|
2023-03-23 15:17:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def process_dice(self, trigram: str | None = None, **kwargs):
|
|
|
|
if self.registration.is_volunteer:
|
|
|
|
participation = await Participation.objects.filter(team__trigram=trigram).prefetch_related('team').aget()
|
|
|
|
else:
|
|
|
|
participation = await Participation.objects.filter(team__participants=self.registration)\
|
|
|
|
.prefetch_related('team').aget()
|
|
|
|
trigram = participation.team.trigram
|
|
|
|
|
|
|
|
team_draw = await TeamDraw.objects.filter(participation=participation,
|
|
|
|
round_id=self.tournament.draw.current_round_id).aget()
|
|
|
|
|
|
|
|
state = await sync_to_async(self.tournament.draw.get_state)()
|
|
|
|
match state:
|
|
|
|
case 'DICE_SELECT_POULES':
|
|
|
|
if team_draw.last_dice is not None:
|
|
|
|
return await self.alert(_("You've already launched the dice."), 'danger')
|
|
|
|
case 'DICE_ORDER_POULE':
|
|
|
|
if team_draw.last_dice is not None:
|
|
|
|
return await self.alert(_("You've already launched the dice."), 'danger')
|
|
|
|
if not await self.tournament.draw.current_round.current_pool.teamdraw_set\
|
|
|
|
.filter(participation=participation).aexists():
|
|
|
|
return await self.alert(_("It is not your turn."), 'danger')
|
|
|
|
case _:
|
|
|
|
return await self.alert(_("This is not the time for this."), 'danger')
|
|
|
|
|
|
|
|
res = randint(1, 100)
|
|
|
|
team_draw.last_dice = res
|
|
|
|
await sync_to_async(team_draw.save)()
|
|
|
|
|
|
|
|
await self.channel_layer.group_send(
|
|
|
|
f"tournament-{self.tournament.id}", {'type': 'draw.dice', 'team': trigram, 'result': res})
|
|
|
|
|
|
|
|
if state == 'DICE_SELECT_POULES' and \
|
|
|
|
not await TeamDraw.objects.filter(round_id=self.tournament.draw.current_round_id,
|
|
|
|
last_dice__isnull=True).aexists():
|
|
|
|
tds = []
|
|
|
|
async for td in TeamDraw.objects.filter(round_id=self.tournament.draw.current_round_id)\
|
|
|
|
.prefetch_related('participation__team'):
|
|
|
|
tds.append(td)
|
|
|
|
|
|
|
|
dices = {td: td.last_dice for td in tds}
|
|
|
|
values = list(dices.values())
|
|
|
|
error = False
|
|
|
|
for v in set(values):
|
|
|
|
if values.count(v) > 1:
|
|
|
|
dups = [td for td in tds if td.last_dice == v]
|
|
|
|
|
|
|
|
for dup in dups:
|
|
|
|
dup.last_dice = None
|
|
|
|
await sync_to_async(dup.save)()
|
|
|
|
await self.channel_layer.group_send(
|
|
|
|
f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.dice', 'team': dup.participation.team.trigram, 'result': None})
|
|
|
|
await self.channel_layer.group_send(
|
|
|
|
f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.alert',
|
|
|
|
'message': _('Dices from teams {teams} are identical. Please relaunch your dices.').format(
|
|
|
|
teams=', '.join(td.participation.team.trigram for td in dups)),
|
|
|
|
'alert_type': 'warning'})
|
|
|
|
error = True
|
|
|
|
|
|
|
|
if error:
|
|
|
|
return
|
|
|
|
|
|
|
|
tds.sort(key=lambda td: td.last_dice)
|
|
|
|
tds_copy = tds.copy()
|
|
|
|
|
|
|
|
async for p in Pool.objects.filter(round_id=self.tournament.draw.current_round_id).order_by('letter').all():
|
|
|
|
while (c := await TeamDraw.objects.filter(pool=p).acount()) < p.size:
|
|
|
|
td = tds_copy.pop(0)
|
|
|
|
td.pool = p
|
|
|
|
td.passage_index = c
|
|
|
|
await sync_to_async(td.save)()
|
|
|
|
|
|
|
|
if self.tournament.draw.current_round.number == 2 \
|
|
|
|
and await self.tournament.draw.current_round.pool_set.acount() >= 2:
|
|
|
|
# Check that we don't have a same pool as the first day
|
|
|
|
async for p1 in Pool.objects.filter(round__draw=self.tournament.draw, number=1).all():
|
|
|
|
async for p2 in Pool.objects.filter(round_id=self.tournament.draw.current_round_id).all():
|
|
|
|
if set(await p1.teamdraw_set.avalues('id')) \
|
|
|
|
== set(await p2.teamdraw_set.avalues('id')):
|
|
|
|
await TeamDraw.objects.filter(round=self.tournament.draw.current_round)\
|
|
|
|
.aupdate(last_dice=None, pool=None, passage_index=None)
|
|
|
|
for td in tds:
|
|
|
|
await self.channel_layer.group_send(
|
|
|
|
f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.dice', 'team': td.participation.team.trigram, 'result': None})
|
|
|
|
await self.channel_layer.group_send(
|
|
|
|
f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.alert',
|
|
|
|
'message': _('Two pools are identical. Please relaunch your dices.'),
|
|
|
|
'alert_type': 'warning'})
|
|
|
|
return
|
|
|
|
|
|
|
|
pool = await Pool.objects.filter(round=self.tournament.draw.current_round, letter=1).aget()
|
|
|
|
self.tournament.draw.current_round.current_pool = pool
|
|
|
|
await sync_to_async(self.tournament.draw.current_round.save)()
|
|
|
|
|
2023-03-24 10:50:10 +00:00
|
|
|
msg = "Les résultats des dés sont les suivants : "
|
|
|
|
msg += await sync_to_async(lambda: ", ".join(
|
|
|
|
f"<strong>{td.participation.team.trigram}</strong> ({td.last_dice})"
|
|
|
|
for td in self.tournament.draw.current_round.team_draws))()
|
|
|
|
msg += ". L'ordre de passage et les compositions des différentes poules sont affiché⋅es sur le côté."
|
|
|
|
self.tournament.draw.last_message = msg
|
|
|
|
await sync_to_async(self.tournament.draw.save)()
|
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
await TeamDraw.objects.filter(round=self.tournament.draw.current_round).aupdate(last_dice=None)
|
|
|
|
for td in tds:
|
|
|
|
await self.channel_layer.group_send(
|
|
|
|
f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.dice', 'team': td.participation.team.trigram, 'result': None})
|
|
|
|
|
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.dice_visibility', 'visible': False})
|
|
|
|
|
|
|
|
async for td in pool.teamdraw_set.prefetch_related('participation__team').all():
|
|
|
|
await self.channel_layer.group_send(f"team-{td.participation.team.trigram}",
|
|
|
|
{'type': 'draw.dice_visibility', 'visible': True})
|
|
|
|
|
2023-03-24 10:10:07 +00:00
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.send_poules',
|
|
|
|
'round': self.tournament.draw.current_round})
|
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.set_info', 'draw': self.tournament.draw})
|
2023-03-24 10:10:07 +00:00
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.set_active', 'draw': self.tournament.draw})
|
2023-03-23 15:17:29 +00:00
|
|
|
elif state == 'DICE_ORDER_POULE' and \
|
|
|
|
not await TeamDraw.objects.filter(pool=self.tournament.draw.current_round.current_pool,
|
|
|
|
last_dice__isnull=True).aexists():
|
|
|
|
pool = self.tournament.draw.current_round.current_pool
|
|
|
|
|
|
|
|
tds = []
|
|
|
|
async for td in TeamDraw.objects.filter(pool=pool)\
|
|
|
|
.prefetch_related('participation__team'):
|
|
|
|
tds.append(td)
|
|
|
|
|
|
|
|
dices = {td: td.last_dice for td in tds}
|
2023-03-24 10:10:07 +00:00
|
|
|
values = list(dices.values())
|
2023-03-23 15:17:29 +00:00
|
|
|
error = False
|
|
|
|
for v in set(values):
|
|
|
|
if values.count(v) > 1:
|
|
|
|
dups = [td for td in tds if td.last_dice == v]
|
|
|
|
|
|
|
|
for dup in dups:
|
|
|
|
dup.last_dice = None
|
|
|
|
await sync_to_async(dup.save)()
|
|
|
|
await self.channel_layer.group_send(
|
|
|
|
f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.dice', 'team': dup.participation.team.trigram, 'result': None})
|
|
|
|
await self.channel_layer.group_send(
|
|
|
|
f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.alert',
|
|
|
|
'message': _('Dices from teams {teams} are identical. Please relaunch your dices.').format(
|
|
|
|
teams=', '.join(td.participation.team.trigram for td in dups)),
|
|
|
|
'alert_type': 'warning'})
|
|
|
|
error = True
|
|
|
|
|
|
|
|
if error:
|
|
|
|
return
|
|
|
|
|
|
|
|
tds.sort(key=lambda x: -x.last_dice)
|
|
|
|
for i, td in enumerate(tds):
|
|
|
|
td.choose_index = i
|
|
|
|
await sync_to_async(td.save)()
|
|
|
|
|
|
|
|
pool.current_team = tds[0]
|
|
|
|
await sync_to_async(pool.save)()
|
|
|
|
|
2023-03-24 10:50:10 +00:00
|
|
|
self.tournament.draw.last_message = ""
|
|
|
|
await sync_to_async(self.tournament.draw.save)()
|
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.set_info', 'draw': self.tournament.draw})
|
2023-03-24 10:10:07 +00:00
|
|
|
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
|
|
|
|
{'type': 'draw.set_active', 'draw': self.tournament.draw})
|
2023-03-23 15:17:29 +00:00
|
|
|
|
|
|
|
async def draw_alert(self, content):
|
|
|
|
return await self.alert(**content)
|
|
|
|
|
|
|
|
async def draw_notify(self, content):
|
|
|
|
await self.send_json({'type': 'notification', 'title': content['title'], 'body': content['body']})
|
|
|
|
|
|
|
|
async def draw_set_info(self, content):
|
|
|
|
await self.send_json({'type': 'set_info', 'information': await content['draw'].ainformation()})
|
|
|
|
|
|
|
|
async def draw_dice(self, content):
|
|
|
|
await self.send_json({'type': 'dice', 'team': content['team'], 'result': content['result']})
|
|
|
|
|
|
|
|
async def draw_dice_visibility(self, content):
|
|
|
|
await self.send_json({'type': 'dice_visibility', 'visible': content['visible']})
|
2023-03-24 10:10:07 +00:00
|
|
|
|
|
|
|
async def draw_send_poules(self, content):
|
|
|
|
await self.send_json({'type': 'set_poules', 'round': content['round'].number,
|
|
|
|
'poules': [{'letter': pool.get_letter_display(), 'teams': await pool.atrigrams()}
|
|
|
|
async for pool in content['round'].pool_set.order_by('letter').all()]})
|
|
|
|
|
|
|
|
async def draw_set_active(self, content):
|
|
|
|
r = content['draw'].current_round
|
|
|
|
await self.send_json(
|
|
|
|
await sync_to_async(lambda: {
|
|
|
|
'type': 'set_active',
|
|
|
|
'round': r.number,
|
|
|
|
'poule': r.current_pool.get_letter_display() if r.current_pool else None,
|
|
|
|
'team': r.current_pool.current_team.participation.team.trigram \
|
|
|
|
if r.current_pool and r.current_pool.current_team else None,
|
|
|
|
})())
|