2023-03-22 14:49:08 +00:00
|
|
|
# Copyright (C) 2023 by Animath
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2023-03-25 19:38:58 +00:00
|
|
|
|
2024-03-31 13:30:17 +00:00
|
|
|
import os
|
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
from asgiref.sync import sync_to_async
|
2023-03-22 14:49:08 +00:00
|
|
|
from django.conf import settings
|
2023-04-05 15:52:46 +00:00
|
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
2023-03-22 14:49:08 +00:00
|
|
|
from django.db import models
|
2023-04-04 17:52:44 +00:00
|
|
|
from django.db.models import QuerySet
|
2023-04-03 16:10:52 +00:00
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.utils.text import format_lazy, slugify
|
2023-03-22 14:49:08 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-04-05 15:52:46 +00:00
|
|
|
from participation.models import Participation, Passage, Pool as PPool, Tournament
|
2023-03-22 14:49:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Draw(models.Model):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
|
|
|
A draw instance is linked to a :model:`participation.Tournament` and contains all information
|
|
|
|
about a draw.
|
|
|
|
"""
|
|
|
|
|
2023-03-22 14:49:08 +00:00
|
|
|
tournament = models.OneToOneField(
|
|
|
|
Tournament,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
verbose_name=_('tournament'),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The associated tournament.")
|
2023-03-22 14:49:08 +00:00
|
|
|
)
|
|
|
|
|
2023-03-22 15:35:59 +00:00
|
|
|
current_round = models.ForeignKey(
|
|
|
|
'Round',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
related_name='+',
|
|
|
|
verbose_name=_('current round'),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The current round where teams select their problems."),
|
2023-03-22 15:35:59 +00:00
|
|
|
)
|
|
|
|
|
2023-03-24 10:50:10 +00:00
|
|
|
last_message = models.TextField(
|
|
|
|
blank=True,
|
|
|
|
default="",
|
|
|
|
verbose_name=_("last message"),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The last message that is displayed on the drawing interface.")
|
2023-03-24 10:50:10 +00:00
|
|
|
)
|
|
|
|
|
2023-04-03 16:10:52 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse_lazy('draw:index') + f'#{slugify(self.tournament.name)}'
|
|
|
|
|
2023-03-25 19:38:58 +00:00
|
|
|
@property
|
2023-04-04 13:10:28 +00:00
|
|
|
def exportable(self) -> bool:
|
|
|
|
"""
|
|
|
|
True if any pool of the draw is exportable, ie. can be exported to the tournament interface.
|
|
|
|
This operation is synchronous.
|
|
|
|
"""
|
2023-03-25 19:38:58 +00:00
|
|
|
return any(pool.exportable for r in self.round_set.all() for pool in r.pool_set.all())
|
|
|
|
|
2023-04-04 13:10:28 +00:00
|
|
|
async def is_exportable(self) -> bool:
|
|
|
|
"""
|
|
|
|
True if any pool of the draw is exportable, ie. can be exported to the tournament interface.
|
|
|
|
This operation is asynchronous.
|
|
|
|
"""
|
|
|
|
return any([await pool.is_exportable() async for r in self.round_set.all() async for pool in r.pool_set.all()])
|
|
|
|
|
|
|
|
def get_state(self) -> str:
|
|
|
|
"""
|
|
|
|
The current state of the draw.
|
|
|
|
Can be:
|
|
|
|
|
|
|
|
* **DICE_SELECT_POULES** if we are waiting for teams to launch their dice to determine pools and passage order ;
|
|
|
|
* **DICE_ORDER_POULE** if we are waiting for teams to launch their dice to determine the problem draw order ;
|
|
|
|
* **WAITING_DRAW_PROBLEM** if we are waiting for a team to draw a problem ;
|
|
|
|
* **WAITING_CHOOSE_PROBLEM** if we are waiting for a team to accept or reject a problem ;
|
|
|
|
* **WAITING_FINAL** if this is the final tournament and we are between the two rounds ;
|
|
|
|
* **DRAW_ENDED** if the draw is ended.
|
|
|
|
|
|
|
|
Warning: the current round and the current team must be prefetched in an async context.
|
|
|
|
"""
|
2023-03-23 15:17:29 +00:00
|
|
|
if self.current_round.current_pool is None:
|
|
|
|
return 'DICE_SELECT_POULES'
|
|
|
|
elif self.current_round.current_pool.current_team is None:
|
|
|
|
return 'DICE_ORDER_POULE'
|
2023-03-25 05:21:39 +00:00
|
|
|
elif self.current_round.current_pool.current_team.accepted is not None:
|
2023-03-26 09:08:03 +00:00
|
|
|
if self.current_round.number == 1:
|
2023-04-04 17:52:44 +00:00
|
|
|
# The last step can be the last problem acceptation after the first round
|
|
|
|
# only for the final between the two rounds
|
2023-03-26 09:08:03 +00:00
|
|
|
return 'WAITING_FINAL'
|
|
|
|
else:
|
|
|
|
return 'DRAW_ENDED'
|
2023-03-23 15:17:29 +00:00
|
|
|
elif self.current_round.current_pool.current_team.purposed is None:
|
|
|
|
return 'WAITING_DRAW_PROBLEM'
|
|
|
|
else:
|
2023-03-25 05:21:39 +00:00
|
|
|
return 'WAITING_CHOOSE_PROBLEM'
|
2024-02-23 20:43:44 +00:00
|
|
|
get_state.short_description = _('State')
|
2023-03-23 15:17:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def information(self):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
|
|
|
The information header on the draw interface, which is defined according to the
|
|
|
|
current state.
|
|
|
|
|
|
|
|
Warning: this property is synchronous.
|
|
|
|
"""
|
2023-03-23 15:17:29 +00:00
|
|
|
s = ""
|
2023-03-24 10:50:10 +00:00
|
|
|
if self.last_message:
|
|
|
|
s += self.last_message + "<br><br>"
|
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
match self.get_state():
|
|
|
|
case 'DICE_SELECT_POULES':
|
2023-04-04 17:52:44 +00:00
|
|
|
# Waiting for dices to determine pools and passage order
|
2023-03-23 15:17:29 +00:00
|
|
|
if self.current_round.number == 1:
|
2023-04-04 17:52:44 +00:00
|
|
|
# Specific information for the first round
|
2023-03-23 15:17:29 +00:00
|
|
|
s += """Nous allons commencer le tirage des problèmes.<br>
|
|
|
|
Vous pouvez à tout moment poser toute question si quelque chose
|
|
|
|
n'est pas clair ou ne va pas.<br><br>
|
|
|
|
Nous allons d'abord tirer les poules et l'ordre de passage
|
|
|
|
pour le premier tour avec toutes les équipes puis pour chaque poule,
|
|
|
|
nous tirerons l'ordre de tirage pour le tour et les problèmes.<br><br>"""
|
|
|
|
s += """
|
|
|
|
Les capitaines, vous pouvez désormais toustes lancer un dé 100,
|
|
|
|
en cliquant sur le gros bouton. Les poules et l'ordre de passage
|
|
|
|
lors du premier tour sera l'ordre croissant des dés, c'est-à-dire
|
|
|
|
que le plus petit lancer sera le premier à passer dans la poule A."""
|
|
|
|
case 'DICE_ORDER_POULE':
|
2023-04-04 17:52:44 +00:00
|
|
|
# Waiting for dices to determine the choice order
|
2023-03-23 15:17:29 +00:00
|
|
|
s += f"""Nous passons au tirage des problèmes pour la poule
|
|
|
|
<strong>{self.current_round.current_pool}</strong>, entre les équipes
|
|
|
|
<strong>{', '.join(td.participation.team.trigram
|
|
|
|
for td in self.current_round.current_pool.teamdraw_set.all())}</strong>.
|
|
|
|
Les capitaines peuvent lancer un dé 100 en cliquant sur le gros bouton
|
|
|
|
pour déterminer l'ordre de tirage. L'équipe réalisant le plus gros score pourra
|
|
|
|
tirer en premier."""
|
2023-03-24 10:50:10 +00:00
|
|
|
case 'WAITING_DRAW_PROBLEM':
|
2023-04-04 17:52:44 +00:00
|
|
|
# Waiting for a problem draw
|
2023-03-24 10:50:10 +00:00
|
|
|
td = self.current_round.current_pool.current_team
|
|
|
|
s += f"""C'est au tour de l'équipe <strong>{td.participation.team.trigram}</strong>
|
|
|
|
de choisir son problème. Cliquez sur l'urne au milieu pour tirer un problème au sort."""
|
|
|
|
case 'WAITING_CHOOSE_PROBLEM':
|
2023-04-04 17:52:44 +00:00
|
|
|
# Waiting for the team that can accept or reject the problem
|
2023-03-24 10:50:10 +00:00
|
|
|
td = self.current_round.current_pool.current_team
|
|
|
|
s += f"""L'équipe <strong>{td.participation.team.trigram}</strong> a tiré le problème
|
2023-04-04 17:52:44 +00:00
|
|
|
<strong>{td.purposed} : {settings.PROBLEMS[td.purposed - 1]}</strong>. """
|
2023-03-24 10:50:10 +00:00
|
|
|
if td.purposed in td.rejected:
|
2023-04-04 17:52:44 +00:00
|
|
|
# The problem was previously rejected
|
2023-03-24 10:50:10 +00:00
|
|
|
s += """Elle a déjà refusé ce problème auparavant, elle peut donc le refuser sans pénalité et
|
|
|
|
tirer un nouveau problème immédiatement, ou bien revenir sur son choix."""
|
|
|
|
else:
|
2023-04-04 17:52:44 +00:00
|
|
|
# The problem can be rejected
|
2023-03-24 10:50:10 +00:00
|
|
|
s += "Elle peut décider d'accepter ou de refuser ce problème. "
|
2024-06-07 12:18:25 +00:00
|
|
|
if len(td.rejected) >= len(settings.PROBLEMS) - settings.RECOMMENDED_SOLUTIONS_COUNT:
|
2024-04-19 17:02:11 +00:00
|
|
|
s += "Refuser ce problème ajoutera une nouvelle pénalité de 25 % sur le coefficient de l'oral de la défense."
|
2023-03-24 10:50:10 +00:00
|
|
|
else:
|
2024-06-07 12:18:25 +00:00
|
|
|
s += f"Il reste {len(settings.PROBLEMS) - settings.RECOMMENDED_SOLUTIONS_COUNT - len(td.rejected)} refus sans pénalité."
|
2023-03-26 09:08:03 +00:00
|
|
|
case 'WAITING_FINAL':
|
2023-04-04 17:52:44 +00:00
|
|
|
# We are between the two rounds of the final tournament
|
2023-03-26 09:08:03 +00:00
|
|
|
s += "Le tirage au sort pour le tour 2 aura lieu à la fin du premier tour. Bon courage !"
|
2023-03-25 05:21:39 +00:00
|
|
|
case 'DRAW_ENDED':
|
2023-04-04 17:52:44 +00:00
|
|
|
# The draw is ended
|
2023-03-25 05:21:39 +00:00
|
|
|
s += "Le tirage au sort est terminé. Les solutions des autres équipes peuvent être trouvées dans l'onglet « Ma participation »."
|
2023-03-23 15:17:29 +00:00
|
|
|
|
2023-03-24 10:10:07 +00:00
|
|
|
s += "<br><br>" if s else ""
|
|
|
|
s += """Pour plus de détails sur le déroulement du tirage au sort,
|
2023-03-23 15:17:29 +00:00
|
|
|
le règlement est accessible sur
|
|
|
|
<a class="alert-link" href="https://tfjm.org/reglement">https://tfjm.org/reglement</a>."""
|
|
|
|
return s
|
|
|
|
|
2023-04-04 17:52:44 +00:00
|
|
|
async def ainformation(self) -> str:
|
|
|
|
"""
|
|
|
|
Asynchronous version to get the information header content.
|
|
|
|
"""
|
2023-03-23 15:17:29 +00:00
|
|
|
return await sync_to_async(lambda: self.information)()
|
|
|
|
|
2023-04-03 16:10:52 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(format_lazy(_("Draw of tournament {tournament}"), tournament=self.tournament.name))
|
|
|
|
|
2023-03-22 14:49:08 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('draw')
|
|
|
|
verbose_name_plural = _('draws')
|
|
|
|
|
|
|
|
|
|
|
|
class Round(models.Model):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
|
|
|
This model is attached to a :model:`draw.Draw` and represents the draw
|
|
|
|
for one round of the :model:`participation.Tournament`.
|
|
|
|
"""
|
2023-03-22 14:49:08 +00:00
|
|
|
draw = models.ForeignKey(
|
|
|
|
Draw,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
verbose_name=_('draw'),
|
|
|
|
)
|
|
|
|
|
2023-03-22 17:44:49 +00:00
|
|
|
number = models.PositiveSmallIntegerField(
|
2023-03-22 14:49:08 +00:00
|
|
|
choices=[
|
|
|
|
(1, _('Round 1')),
|
|
|
|
(2, _('Round 2')),
|
2024-06-07 12:18:25 +00:00
|
|
|
] + ([] if settings.NB_ROUNDS == 2 else [(3, _('Round 3'))]),
|
2023-03-22 14:49:08 +00:00
|
|
|
verbose_name=_('number'),
|
2024-06-07 12:18:25 +00:00
|
|
|
help_text=_("The number of the round, 1 or 2 (or 3 for ETEAM)"),
|
|
|
|
validators=[MinValueValidator(1), MaxValueValidator(settings.NB_ROUNDS)],
|
2023-03-22 14:49:08 +00:00
|
|
|
)
|
|
|
|
|
2023-03-22 15:35:59 +00:00
|
|
|
current_pool = models.ForeignKey(
|
|
|
|
'Pool',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
related_name='+',
|
|
|
|
verbose_name=_('current pool'),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The current pool where teams select their problems."),
|
2023-03-22 15:35:59 +00:00
|
|
|
)
|
|
|
|
|
2023-04-08 22:50:47 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse_lazy('draw:index') + f'#{slugify(self.draw.tournament.name)}'
|
|
|
|
|
2023-03-24 10:10:07 +00:00
|
|
|
@property
|
2023-04-04 17:52:44 +00:00
|
|
|
def team_draws(self) -> QuerySet["TeamDraw"]:
|
|
|
|
"""
|
|
|
|
Returns a query set ordered by pool and by passage index of all team draws.
|
|
|
|
"""
|
2023-03-24 10:10:07 +00:00
|
|
|
return self.teamdraw_set.order_by('pool__letter', 'passage_index').all()
|
|
|
|
|
2023-03-25 05:21:39 +00:00
|
|
|
async def next_pool(self):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
|
|
|
Returns the next pool of the round.
|
|
|
|
For example, after the pool A, we have the pool B.
|
|
|
|
"""
|
2023-04-04 13:10:28 +00:00
|
|
|
pool = self.current_pool
|
2023-03-25 05:21:39 +00:00
|
|
|
return await self.pool_set.aget(letter=pool.letter + 1)
|
|
|
|
|
2023-03-22 14:49:08 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.get_number_display()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('round')
|
|
|
|
verbose_name_plural = _('rounds')
|
2023-04-03 16:10:52 +00:00
|
|
|
ordering = ('draw__tournament__name', 'number',)
|
2023-03-22 14:49:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Pool(models.Model):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
|
|
|
A Pool is a collection of teams in a :model:`draw.Round` of a `draw.Draw`.
|
|
|
|
It has a letter (eg. A, B, C or D) and a size, between 3 and 5.
|
|
|
|
After the draw, the pool can be exported in a `participation.Pool` instance.
|
|
|
|
"""
|
2023-03-22 14:49:08 +00:00
|
|
|
round = models.ForeignKey(
|
|
|
|
Round,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
|
2023-03-22 17:44:49 +00:00
|
|
|
letter = models.PositiveSmallIntegerField(
|
2023-03-22 14:49:08 +00:00
|
|
|
choices=[
|
2023-03-22 17:44:49 +00:00
|
|
|
(1, 'A'),
|
|
|
|
(2, 'B'),
|
|
|
|
(3, 'C'),
|
2023-03-31 15:23:40 +00:00
|
|
|
(4, 'D'),
|
2023-03-22 14:49:08 +00:00
|
|
|
],
|
|
|
|
verbose_name=_('letter'),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The letter of the pool: A, B, C or D."),
|
2023-03-22 14:49:08 +00:00
|
|
|
)
|
|
|
|
|
2023-03-22 17:44:49 +00:00
|
|
|
size = models.PositiveSmallIntegerField(
|
|
|
|
verbose_name=_('size'),
|
2023-04-04 17:52:44 +00:00
|
|
|
validators=[MinValueValidator(3), MaxValueValidator(5)],
|
|
|
|
help_text=_("The number of teams in this pool, between 3 and 5."),
|
2023-03-22 17:44:49 +00:00
|
|
|
)
|
|
|
|
|
2023-03-22 15:35:59 +00:00
|
|
|
current_team = models.ForeignKey(
|
|
|
|
'TeamDraw',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
related_name='+',
|
|
|
|
verbose_name=_('current team'),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The current team that is selecting its problem."),
|
2023-03-22 15:35:59 +00:00
|
|
|
)
|
|
|
|
|
2023-03-25 19:38:58 +00:00
|
|
|
associated_pool = models.OneToOneField(
|
|
|
|
'participation.Pool',
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
related_name='draw_pool',
|
|
|
|
verbose_name=_("associated pool"),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The full pool instance."),
|
2023-03-25 19:38:58 +00:00
|
|
|
)
|
|
|
|
|
2023-04-08 22:50:47 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse_lazy('draw:index') + f'#{slugify(self.round.draw.tournament.name)}'
|
|
|
|
|
2023-03-24 10:10:07 +00:00
|
|
|
@property
|
2023-04-04 17:52:44 +00:00
|
|
|
def team_draws(self) -> QuerySet["TeamDraw"]:
|
|
|
|
"""
|
|
|
|
Returns a query set ordered by passage index of all team draws in this pool.
|
|
|
|
"""
|
2024-04-22 21:36:52 +00:00
|
|
|
return self.teamdraw_set.all()
|
2023-03-24 10:10:07 +00:00
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
@property
|
2023-04-04 17:52:44 +00:00
|
|
|
def trigrams(self) -> list[str]:
|
|
|
|
"""
|
|
|
|
Returns a list of trigrams of the teams in this pool ordered by passage index.
|
|
|
|
This property is synchronous.
|
|
|
|
"""
|
2023-04-05 15:52:46 +00:00
|
|
|
return [td.participation.team.trigram for td in self.teamdraw_set.order_by('passage_index')
|
|
|
|
.prefetch_related('participation__team').all()]
|
2023-03-24 10:10:07 +00:00
|
|
|
|
2023-04-04 17:52:44 +00:00
|
|
|
async def atrigrams(self) -> list[str]:
|
|
|
|
"""
|
|
|
|
Returns a list of trigrams of the teams in this pool ordered by passage index.
|
|
|
|
This property is asynchronous.
|
|
|
|
"""
|
2023-04-05 15:52:46 +00:00
|
|
|
return [td.participation.team.trigram async for td in self.teamdraw_set.order_by('passage_index')
|
|
|
|
.prefetch_related('participation__team').all()]
|
2023-03-23 15:17:29 +00:00
|
|
|
|
2023-04-04 17:52:44 +00:00
|
|
|
async def next_td(self) -> "TeamDraw":
|
|
|
|
"""
|
|
|
|
Returns the next team draw after the current one, to know who should draw a new problem.
|
|
|
|
"""
|
2023-04-04 13:10:28 +00:00
|
|
|
td = self.current_team
|
2023-03-25 05:21:39 +00:00
|
|
|
current_index = (td.choose_index + 1) % self.size
|
2023-04-04 13:10:28 +00:00
|
|
|
td = await self.teamdraw_set.prefetch_related('participation__team').aget(choose_index=current_index)
|
2023-03-25 05:21:39 +00:00
|
|
|
while td.accepted:
|
2023-04-04 17:52:44 +00:00
|
|
|
# Ignore if the next team already accepted its problem
|
2023-03-25 05:21:39 +00:00
|
|
|
current_index += 1
|
|
|
|
current_index %= self.size
|
2023-04-04 13:10:28 +00:00
|
|
|
td = await self.teamdraw_set.prefetch_related('participation__team').aget(choose_index=current_index)
|
2023-03-25 05:21:39 +00:00
|
|
|
return td
|
|
|
|
|
2023-03-25 19:38:58 +00:00
|
|
|
@property
|
2023-04-04 17:52:44 +00:00
|
|
|
def exportable(self) -> bool:
|
|
|
|
"""
|
|
|
|
True if this pool is exportable, ie. can be exported to the tournament interface. That means that
|
|
|
|
each team selected its problem.
|
|
|
|
This operation is synchronous.
|
|
|
|
"""
|
2023-04-04 13:10:28 +00:00
|
|
|
return self.associated_pool_id is None and self.teamdraw_set.exists() \
|
2023-03-26 09:08:03 +00:00
|
|
|
and all(td.accepted is not None for td in self.teamdraw_set.all())
|
2023-03-25 19:38:58 +00:00
|
|
|
|
2023-04-04 17:52:44 +00:00
|
|
|
async def is_exportable(self) -> bool:
|
|
|
|
"""
|
|
|
|
True if this pool is exportable, ie. can be exported to the tournament interface. That means that
|
|
|
|
each team selected its problem.
|
|
|
|
This operation is asynchronous.
|
|
|
|
"""
|
2023-04-04 13:10:28 +00:00
|
|
|
return self.associated_pool_id is None and await self.teamdraw_set.aexists() \
|
|
|
|
and all([td.accepted is not None async for td in self.teamdraw_set.all()])
|
|
|
|
|
2023-04-04 17:52:44 +00:00
|
|
|
async def export(self) -> PPool:
|
|
|
|
"""
|
|
|
|
Translates this Pool instance in a :model:`participation.Pool` instance, with the passage orders.
|
|
|
|
"""
|
|
|
|
# Create the pool
|
2024-03-30 21:25:08 +00:00
|
|
|
self.associated_pool, _created = await PPool.objects.aget_or_create(
|
2023-04-04 13:10:28 +00:00
|
|
|
tournament=self.round.draw.tournament,
|
|
|
|
round=self.round.number,
|
|
|
|
letter=self.letter,
|
|
|
|
)
|
2023-04-04 17:52:44 +00:00
|
|
|
|
|
|
|
# Define the participations of the pool
|
2023-04-04 13:10:28 +00:00
|
|
|
tds = [td async for td in self.team_draws.prefetch_related('participation')]
|
2023-04-05 15:52:46 +00:00
|
|
|
await self.associated_pool.participations.aset([td.participation async for td in self.team_draws
|
|
|
|
.prefetch_related('participation')])
|
2023-04-04 13:10:28 +00:00
|
|
|
await self.asave()
|
|
|
|
|
2024-04-18 12:53:58 +00:00
|
|
|
pool2 = None
|
|
|
|
if self.size == 5:
|
|
|
|
pool2, _created = await PPool.objects.aget_or_create(
|
|
|
|
tournament=self.round.draw.tournament,
|
|
|
|
round=self.round.number,
|
|
|
|
letter=self.letter,
|
|
|
|
room=2,
|
|
|
|
)
|
|
|
|
await pool2.participations.aset([td.participation async for td in self.team_draws
|
|
|
|
.prefetch_related('participation')])
|
|
|
|
|
2023-04-04 17:52:44 +00:00
|
|
|
# Define the passage matrix according to the number of teams
|
2023-04-07 10:10:25 +00:00
|
|
|
table = []
|
2023-04-04 17:52:44 +00:00
|
|
|
if self.size == 3:
|
2023-04-04 13:10:28 +00:00
|
|
|
table = [
|
|
|
|
[0, 1, 2],
|
|
|
|
[1, 2, 0],
|
|
|
|
[2, 0, 1],
|
|
|
|
]
|
2023-04-04 17:52:44 +00:00
|
|
|
elif self.size == 4:
|
2023-04-04 13:10:28 +00:00
|
|
|
table = [
|
2023-04-07 10:10:25 +00:00
|
|
|
[0, 1, 2, 3],
|
|
|
|
[1, 2, 3, 0],
|
|
|
|
[2, 3, 0, 1],
|
|
|
|
[3, 0, 1, 2],
|
2023-04-04 13:10:28 +00:00
|
|
|
]
|
2023-04-04 17:52:44 +00:00
|
|
|
elif self.size == 5:
|
2023-04-04 13:10:28 +00:00
|
|
|
table = [
|
2024-04-17 19:56:46 +00:00
|
|
|
[0, 2, 3],
|
|
|
|
[1, 3, 4],
|
2024-04-18 12:53:58 +00:00
|
|
|
[2, 4, 0],
|
2024-04-17 19:56:46 +00:00
|
|
|
[3, 0, 1],
|
|
|
|
[4, 1, 2],
|
2023-04-04 13:10:28 +00:00
|
|
|
]
|
|
|
|
|
2023-04-06 22:05:56 +00:00
|
|
|
for i, line in enumerate(table):
|
2024-04-18 12:53:58 +00:00
|
|
|
passage_pool = self.associated_pool
|
|
|
|
passage_position = i + 1
|
|
|
|
if self.size == 5:
|
|
|
|
# In 5-teams pools, we may create some passages in the second room
|
|
|
|
if i % 2 == 1:
|
|
|
|
passage_pool = pool2
|
|
|
|
passage_position = 1 + i // 2
|
|
|
|
|
2023-04-04 17:52:44 +00:00
|
|
|
# Create the passage
|
2024-04-16 22:02:48 +00:00
|
|
|
await Passage.objects.acreate(
|
2024-04-18 12:53:58 +00:00
|
|
|
pool=passage_pool,
|
|
|
|
position=passage_position,
|
2023-04-04 13:10:28 +00:00
|
|
|
solution_number=tds[line[0]].accepted,
|
|
|
|
defender=tds[line[0]].participation,
|
|
|
|
opponent=tds[line[1]].participation,
|
|
|
|
reporter=tds[line[2]].participation,
|
|
|
|
defender_penalties=tds[line[0]].penalty_int,
|
2023-03-25 19:38:58 +00:00
|
|
|
)
|
|
|
|
|
2024-03-31 13:30:17 +00:00
|
|
|
# Update Google Sheets
|
|
|
|
if os.getenv('GOOGLE_PRIVATE_KEY_ID', None):
|
|
|
|
await sync_to_async(self.associated_pool.update_spreadsheet)()
|
2024-03-31 11:38:20 +00:00
|
|
|
|
2023-04-04 17:52:44 +00:00
|
|
|
return self.associated_pool
|
|
|
|
|
2023-03-22 14:49:08 +00:00
|
|
|
def __str__(self):
|
2023-04-03 16:10:52 +00:00
|
|
|
return str(format_lazy(_("Pool {letter}{number}"), letter=self.get_letter_display(), number=self.round.number))
|
2023-03-22 14:49:08 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('pool')
|
|
|
|
verbose_name_plural = _('pools')
|
2023-04-03 16:10:52 +00:00
|
|
|
ordering = ('round__draw__tournament__name', 'round__number', 'letter',)
|
2023-03-22 14:49:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TeamDraw(models.Model):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
|
|
|
This model represents the state of the draw for a given team, including
|
|
|
|
its accepted problem or their rejected ones.
|
|
|
|
"""
|
2023-03-22 14:49:08 +00:00
|
|
|
participation = models.ForeignKey(
|
|
|
|
Participation,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
verbose_name=_('participation'),
|
|
|
|
)
|
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
round = models.ForeignKey(
|
|
|
|
Round,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
verbose_name=_('round'),
|
|
|
|
)
|
|
|
|
|
2023-03-22 15:35:59 +00:00
|
|
|
pool = models.ForeignKey(
|
|
|
|
Pool,
|
|
|
|
on_delete=models.CASCADE,
|
2023-03-22 17:44:49 +00:00
|
|
|
null=True,
|
|
|
|
default=None,
|
2023-03-22 15:35:59 +00:00
|
|
|
verbose_name=_('pool'),
|
|
|
|
)
|
|
|
|
|
2023-03-23 15:17:29 +00:00
|
|
|
passage_index = models.PositiveSmallIntegerField(
|
2023-04-04 17:52:44 +00:00
|
|
|
choices=zip(range(0, 5), range(0, 5)),
|
2023-03-23 15:17:29 +00:00
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
verbose_name=_('passage index'),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The passage order in the pool, between 0 and the size of the pool minus 1."),
|
|
|
|
validators=[MinValueValidator(0), MaxValueValidator(4)],
|
2023-03-23 15:17:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
choose_index = models.PositiveSmallIntegerField(
|
2023-04-04 17:52:44 +00:00
|
|
|
choices=zip(range(0, 5), range(0, 5)),
|
2023-03-22 17:44:49 +00:00
|
|
|
null=True,
|
|
|
|
default=None,
|
2023-03-23 15:17:29 +00:00
|
|
|
verbose_name=_('choose index'),
|
2023-04-04 17:52:44 +00:00
|
|
|
help_text=_("The choice order in the pool, between 0 and the size of the pool minus 1."),
|
|
|
|
validators=[MinValueValidator(0), MaxValueValidator(4)],
|
2023-03-22 15:35:59 +00:00
|
|
|
)
|
|
|
|
|
2023-03-22 17:44:49 +00:00
|
|
|
accepted = models.PositiveSmallIntegerField(
|
2023-03-22 14:49:08 +00:00
|
|
|
choices=[
|
2023-04-04 09:56:13 +00:00
|
|
|
(i, format_lazy(_("Problem #{problem}"), problem=i)) for i in range(1, len(settings.PROBLEMS) + 1)
|
2023-03-22 14:49:08 +00:00
|
|
|
],
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
verbose_name=_("accepted problem"),
|
|
|
|
)
|
|
|
|
|
2023-03-28 19:56:18 +00:00
|
|
|
passage_dice = models.PositiveSmallIntegerField(
|
2023-03-22 15:35:59 +00:00
|
|
|
choices=zip(range(1, 101), range(1, 101)),
|
2023-03-22 17:44:49 +00:00
|
|
|
null=True,
|
|
|
|
default=None,
|
2023-03-28 19:56:18 +00:00
|
|
|
verbose_name=_("passage dice"),
|
|
|
|
)
|
|
|
|
|
|
|
|
choice_dice = models.PositiveSmallIntegerField(
|
|
|
|
choices=zip(range(1, 101), range(1, 101)),
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
verbose_name=_("choice dice"),
|
2023-03-22 15:35:59 +00:00
|
|
|
)
|
|
|
|
|
2023-03-22 17:44:49 +00:00
|
|
|
purposed = models.PositiveSmallIntegerField(
|
2023-03-22 14:49:08 +00:00
|
|
|
choices=[
|
2023-04-04 09:56:13 +00:00
|
|
|
(i, format_lazy(_("Problem #{problem}"), problem=i)) for i in range(1, len(settings.PROBLEMS) + 1)
|
2023-03-22 14:49:08 +00:00
|
|
|
],
|
|
|
|
null=True,
|
|
|
|
default=None,
|
2023-05-20 09:45:21 +00:00
|
|
|
verbose_name=_("purposed problem"),
|
2023-03-22 14:49:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
rejected = models.JSONField(
|
2023-03-22 15:35:59 +00:00
|
|
|
default=list,
|
2023-03-22 14:49:08 +00:00
|
|
|
verbose_name=_('rejected problems'),
|
|
|
|
)
|
|
|
|
|
2023-04-08 22:50:47 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse_lazy('draw:index') + f'#{slugify(self.round.draw.tournament.name)}'
|
|
|
|
|
2023-03-28 19:56:18 +00:00
|
|
|
@property
|
|
|
|
def last_dice(self):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
|
|
|
The last dice that was thrown.
|
|
|
|
"""
|
2023-03-28 19:56:18 +00:00
|
|
|
return self.passage_dice if self.round.draw.get_state() == 'DICE_SELECT_POULES' else self.choice_dice
|
|
|
|
|
2023-03-25 19:38:58 +00:00
|
|
|
@property
|
|
|
|
def penalty_int(self):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
2024-06-07 12:18:25 +00:00
|
|
|
The number of penalties, which is the number of rejected problems after the P - 5 free rejects
|
|
|
|
(P - 6 for ETEAM), where P is the number of problems.
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
2024-06-07 12:18:25 +00:00
|
|
|
return max(0, len(self.rejected) - (len(settings.PROBLEMS) - settings.RECOMMENDED_SOLUTIONS_COUNT))
|
2023-03-25 19:38:58 +00:00
|
|
|
|
2023-03-25 05:21:39 +00:00
|
|
|
@property
|
|
|
|
def penalty(self):
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
2024-04-19 17:02:11 +00:00
|
|
|
The penalty multiplier on the defender oral, in percentage, which is a malus of 25% for each penalty.
|
2023-04-04 17:52:44 +00:00
|
|
|
"""
|
2024-04-19 17:02:11 +00:00
|
|
|
return 25 * self.penalty_int
|
2023-03-23 15:17:29 +00:00
|
|
|
|
2023-04-03 16:10:52 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(format_lazy(_("Draw of the team {trigram} for the pool {letter}{number}"),
|
|
|
|
trigram=self.participation.team.trigram,
|
|
|
|
letter=self.pool.get_letter_display() if self.pool else "",
|
|
|
|
number=self.round.number))
|
|
|
|
|
2023-03-22 14:49:08 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('team draw')
|
|
|
|
verbose_name_plural = _('team draws')
|
2024-04-22 21:36:52 +00:00
|
|
|
ordering = ('round__draw__tournament__name', 'round__number', 'pool__letter', 'passage_index',
|
|
|
|
'choice_dice', 'passage_dice',)
|