Reorganize the cancel step code in order to make it more readable

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2023-04-06 18:15:14 +02:00
parent 8778f58fe4
commit ae62e3daf7
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 325 additions and 273 deletions

View File

@ -942,18 +942,34 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
if not await Draw.objects.filter(tournament=self.tournament).aexists(): if not await Draw.objects.filter(tournament=self.tournament).aexists():
return await self.alert(_("The draw has not started yet."), 'danger') return await self.alert(_("The draw has not started yet."), 'danger')
content_type = await ContentType.objects.aget(app_label=TeamDraw._meta.app_label,
model=TeamDraw._meta.model_name)
state = self.tournament.draw.get_state() state = self.tournament.draw.get_state()
self.tournament.draw.last_message = "" self.tournament.draw.last_message = ""
await self.tournament.draw.asave() await self.tournament.draw.asave()
r = self.tournament.draw.current_round
if state == 'DRAW_ENDED' or state == 'WAITING_FINAL': if state == 'DRAW_ENDED' or state == 'WAITING_FINAL':
td = self.tournament.draw.current_round.current_pool.current_team await self.undo_end_draw()
elif state == 'WAITING_CHOOSE_PROBLEM':
await self.undo_draw_problem()
elif state == 'WAITING_DRAW_PROBLEM':
await self.undo_process_problem()
elif state == 'DICE_ORDER_POULE':
await self.undo_pool_dice()
elif state == 'DICE_SELECT_POULES':
await self.undo_order_dice()
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.set_info', 'draw': self.tournament.draw})
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.set_active', 'draw': self.tournament.draw})
async def undo_end_draw(self) -> None:
"""
If the draw is ended, or if we are between the two rounds of the final,
then we cancel the last problem that was accepted.
"""
r = self.tournament.draw.current_round
td = r.current_pool.current_team
td.purposed = td.accepted td.purposed = td.accepted
td.accepted = None td.accepted = None
await td.asave() await td.asave()
@ -971,7 +987,13 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
'round': r.number, 'round': r.number,
'team': td.participation.team.trigram, 'team': td.participation.team.trigram,
'problem': td.accepted}) 'problem': td.accepted})
elif state == 'WAITING_CHOOSE_PROBLEM':
async def undo_draw_problem(self):
"""
A problem was drawn and we wait for the current team to accept or reject the problem.
Then, we just reset the problem draw.
:return:
"""
td = self.tournament.draw.current_round.current_pool.current_team td = self.tournament.draw.current_round.current_pool.current_team
td.purposed = None td.purposed = None
await td.asave() await td.asave()
@ -984,7 +1006,24 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
{'type': 'draw.box_visibility', 'visible': True}) {'type': 'draw.box_visibility', 'visible': True})
await self.channel_layer.group_send(f"volunteer-{self.tournament.id}", await self.channel_layer.group_send(f"volunteer-{self.tournament.id}",
{'type': 'draw.box_visibility', 'visible': True}) {'type': 'draw.box_visibility', 'visible': True})
elif state == 'WAITING_DRAW_PROBLEM':
async def undo_process_problem(self):
"""
Now, a team must draw a new problem. Multiple cases are possible:
* In the same pool, a previous team accepted a problem ;
* In the same pool, a previous team rejected a problem ;
* The current team rejected a problem that was previously rejected ;
* The last team drawn its dice to choose the draw order.
In the two first cases, we explore the database history to fetch what team accepted or rejected
its problem at last.
The third case is ignored, because too hard and too useless to manage.
For the last case, we cancel the last dice.
"""
content_type = await ContentType.objects.aget(app_label=TeamDraw._meta.app_label,
model=TeamDraw._meta.model_name)
r = self.tournament.draw.current_round
p = r.current_pool p = r.current_pool
accepted_tds = {td.id: td async for td in p.team_draws.filter(accepted__isnull=False) accepted_tds = {td.id: td async for td in p.team_draws.filter(accepted__isnull=False)
.prefetch_related('participation__team')} .prefetch_related('participation__team')}
@ -1082,7 +1121,17 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
await self.channel_layer.group_send(f"tournament-{self.tournament.id}", await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.box_visibility', 'visible': False}) {'type': 'draw.box_visibility', 'visible': False})
elif state == 'DICE_ORDER_POULE':
async def undo_pool_dice(self):
"""
Teams of a pool are launching their dices to define the draw order.
We reset the last dice if possible, or we go to the last pool, or the last round,
or the passage dices.
"""
content_type = await ContentType.objects.aget(app_label=TeamDraw._meta.app_label,
model=TeamDraw._meta.model_name)
r = self.tournament.draw.current_round
p = r.current_pool p = r.current_pool
already_launched_tds = {td.id: td async for td in p.team_draws.filter(choice_dice__isnull=False) already_launched_tds = {td.id: td async for td in p.team_draws.filter(choice_dice__isnull=False)
.prefetch_related('participation__team')} .prefetch_related('participation__team')}
@ -1114,7 +1163,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
# Go to the previous pool if possible # Go to the previous pool if possible
if p.letter > 1: if p.letter > 1:
# Go to the previous pool # Go to the previous pool
previous_pool = await r.pool_set.prefetch_related('current_team__participation__team')\ previous_pool = await r.pool_set.prefetch_related('current_team__participation__team') \
.aget(letter=p.letter - 1) .aget(letter=p.letter - 1)
r.current_pool = previous_pool r.current_pool = previous_pool
await r.asave() await r.asave()
@ -1140,7 +1189,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
elif r.number == 2: elif r.number == 2:
if not self.tournament.final: if not self.tournament.final:
# Go to the previous round # Go to the previous round
r1 = await self.tournament.draw.round_set\ r1 = await self.tournament.draw.round_set \
.prefetch_related('current_pool__current_team__participation__team').aget(number=1) .prefetch_related('current_pool__current_team__participation__team').aget(number=1)
self.tournament.draw.current_round = r1 self.tournament.draw.current_round = r1
await self.tournament.draw.asave() await self.tournament.draw.asave()
@ -1243,7 +1292,15 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
await self.channel_layer.group_send(f"tournament-{self.tournament.id}", await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.dice_visibility', 'visible': True}) {'type': 'draw.dice_visibility', 'visible': True})
elif state == 'DICE_SELECT_POULES':
async def undo_order_dice(self):
"""
Undo the last dice for the passage order, or abort the draw if we are at the beginning.
"""
content_type = await ContentType.objects.aget(app_label=TeamDraw._meta.app_label,
model=TeamDraw._meta.model_name)
r = self.tournament.draw.current_round
already_launched_tds = {td.id: td async for td in r.team_draws.filter(passage_dice__isnull=False) already_launched_tds = {td.id: td async for td in r.team_draws.filter(passage_dice__isnull=False)
.prefetch_related('participation__team')} .prefetch_related('participation__team')}
@ -1273,11 +1330,6 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
else: else:
await self.abort() await self.abort()
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.set_info', 'draw': self.tournament.draw})
await self.channel_layer.group_send(f"tournament-{self.tournament.id}",
{'type': 'draw.set_active', 'draw': self.tournament.draw})
async def draw_alert(self, content): async def draw_alert(self, content):
""" """
Send alert to the current user. Send alert to the current user.