1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-24 11:48:50 +02:00

Use separate fields for the two dices

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2023-03-28 21:56:18 +02:00
parent 7f7d921c53
commit cf8892ee1a
4 changed files with 311 additions and 53 deletions

View File

@ -163,11 +163,11 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
if state == 'DICE_ORDER_POULE':
participation = await Participation.objects\
.filter(teamdraw__pool=self.tournament.draw.current_round.current_pool,
teamdraw__last_dice__isnull=True).prefetch_related('team').afirst()
teamdraw__choice_dice__isnull=True).prefetch_related('team').afirst()
else:
participation = await Participation.objects\
.filter(teamdraw__round=self.tournament.draw.current_round,
teamdraw__last_dice__isnull=True).prefetch_related('team').afirst()
teamdraw__passage_dice__isnull=True).prefetch_related('team').afirst()
trigram = participation.team.trigram
else:
participation = await Participation.objects.filter(team__participants=self.registration)\
@ -179,10 +179,10 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
match state:
case 'DICE_SELECT_POULES':
if team_draw.last_dice is not None:
if team_draw.passage_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:
if team_draw.choice_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():
@ -191,7 +191,10 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
return await self.alert(_("This is not the time for this."), 'danger')
res = randint(1, 100)
team_draw.last_dice = res
if state == 'DICE_SELECT_POULES':
team_draw.passage_dice = res
else:
team_draw.choice_dice = res
await sync_to_async(team_draw.save)()
await self.channel_layer.group_send(
@ -199,21 +202,21 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
if state == 'DICE_SELECT_POULES' and \
not await TeamDraw.objects.filter(round_id=self.tournament.draw.current_round_id,
last_dice__isnull=True).aexists():
passage_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}
dices = {td: td.passage_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]
dups = [td for td in tds if td.passage_dice == v]
for dup in dups:
dup.last_dice = None
dup.passage_dice = None
await sync_to_async(dup.save)()
await self.channel_layer.group_send(
f"tournament-{self.tournament.id}",
@ -229,7 +232,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
if error:
return
tds.sort(key=lambda td: td.last_dice)
tds.sort(key=lambda td: td.passage_dice)
tds_copy = tds.copy()
async for p in Pool.objects.filter(round_id=self.tournament.draw.current_round_id).order_by('letter').all():
@ -247,7 +250,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
if await sync_to_async(lambda: set(td['id'] for td in p1.teamdraw_set.values('id')))() \
== await sync_to_async(lambda:set(td['id'] for td in p2.teamdraw_set.values('id')))():
await TeamDraw.objects.filter(round=self.tournament.draw.current_round)\
.aupdate(last_dice=None, pool=None, passage_index=None)
.aupdate(passage_dice=None, pool=None, passage_index=None)
for td in tds:
await self.channel_layer.group_send(
f"tournament-{self.tournament.id}",
@ -265,13 +268,12 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
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})"
f"<strong>{td.participation.team.trigram}</strong> ({td.passage_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)()
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}",
@ -296,7 +298,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
{'type': 'draw.set_active', 'draw': self.tournament.draw})
elif state == 'DICE_ORDER_POULE' and \
not await TeamDraw.objects.filter(pool=self.tournament.draw.current_round.current_pool,
last_dice__isnull=True).aexists():
choice_dice__isnull=True).aexists():
pool = self.tournament.draw.current_round.current_pool
tds = []
@ -304,15 +306,15 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
.prefetch_related('participation__team'):
tds.append(td)
dices = {td: td.last_dice for td in tds}
dices = {td: td.choice_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]
dups = [td for td in tds if td.choice_dice == v]
for dup in dups:
dup.last_dice = None
dup.choice_dice = None
await sync_to_async(dup.save)()
await self.channel_layer.group_send(
f"tournament-{self.tournament.id}",
@ -328,7 +330,7 @@ class DrawConsumer(AsyncJsonWebsocketConsumer):
if error:
return
tds.sort(key=lambda x: -x.last_dice)
tds.sort(key=lambda x: -x.choice_dice)
for i, td in enumerate(tds):
td.choose_index = i
await sync_to_async(td.save)()