from datetime import datetime from email.policy import default from pyexpat import model import random import string from django.db import models import pytz from .utils import getNow # Create your models here. def generate_unique_code_room(): length = 6 while True: code = ''.join(random.choices(string.ascii_uppercase, k=length)) if Room.objects.filter(id_code=code).count() == 0: break return code def generate_unique_code_parcours(): length = 6 while True: code = ''.join(random.choices(string.ascii_uppercase, k=length)) if Parcours.objects.filter(id_code=code).count() == 0: break return code def generate_unique_code_corr(): length = 6 while True: code = ''.join(random.choices(string.ascii_uppercase, k=length)) if TempCorrection.objects.filter(id_code=code).count() == 0: break return code class RoomManager(models.Manager): def add_participant(self, room, new_name, clientId, owner, online): room = self.get_queryset().filter(id_code=room)[0] participants = room.participants if new_name in list(map(lambda p: p['nick'], participants)): return None code = None while True: code = ''.join(random.choices(string.ascii_uppercase, k=6)) if code not in list(map(lambda p: p['code'], participants)): break new_participant = {'nick': new_name, 'code': code, 'clientId': clientId, "owner": owner, "online": online} room.participants = [*participants, new_participant] room.save() return new_participant def del_participant(self, room, code): room = self.get_queryset().filter(id_code=room)[0] participants = room.participants room.participants = list( filter(lambda c: c['code'] != code, participants)) print('parcours', room.parcours_set) parcours = room.parcours_set.all() for parc in parcours: challenger = parc.challenger parc.challenger = list( filter(lambda c: c['code'] != code, challenger)) parc.save() room.save() return code def add_waiter(self, room, new_name): room = self.get_queryset().filter(id_code=room)[0] participants = room.participants if new_name in list(map(lambda p: p['nick'], participants)): return None waiters = room.waiters if new_name in list(map(lambda p: p['nick'], waiters)): return None code = None while True: code = ''.join(random.choices(string.ascii_uppercase, k=6)) if code not in list(map(lambda p: p['code'], waiters)): break new_waiter = {'nick': new_name, 'code': code, } room.waiters = [*waiters, new_waiter] room.save() return new_waiter def del_waiter(self, room, code): room = self.get_queryset().filter(id_code=room)[0] waiters = room.waiters room.waiters = list(filter(lambda w: w['code'] != code, waiters)) room.save() return True def disconnect(self, room_code, code): room = self.get_queryset().filter(id_code=room_code)[0] participants = room.participants participant = list( filter(lambda p: p['code'] == code, participants))[0] participant['online'] = False room.participants = [ *list(filter(lambda p: p['code'] != code, participants)), participant] room.save() return True def connect(self, room_code, code): room = self.get_queryset().filter(id_code=room_code)[0] participants = room.participants participant = list( filter(lambda p: p['code'] == code, participants))[0] participant['online'] = True room.participants = [ *list(filter(lambda p: p['code'] != code, participants)), participant] room.save() return True class Room(models.Model): name = models.CharField(max_length=50) id_code = models.CharField(max_length=50, default=generate_unique_code_room) participants = models.JSONField(default=list) waiters = models.JSONField(default=list) created_at = models.DateTimeField(auto_now_add=True) owner = models.JSONField(default=dict) public_result = models.BooleanField(default=False) private = models.BooleanField(default=True) objects = RoomManager() class ParcoursManager(models.Manager): def challenge(self, parcours_code, user_code, result): parcours = self.get_queryset().filter(id_code=parcours_code)[0] challengers = list( filter(lambda c: c['code'] == user_code, parcours.challenger)) challenger = list( filter(lambda p: p['code'] == user_code, parcours.room.participants))[0] validate = None date = getNow() if len(challengers) == 0: condition = parcours.success_condition validate = date if result['note']['value'] * 20 / \ result['note']['total'] >= condition else False code = ''.join(random.choices(string.ascii_uppercase, k=6)) parcours.challenger = [*parcours.challenger, {**challenger, 'exos': [{**result, 'endAt': date, 'code':code}], "validate": validate}] else: validate = False if challengers[0]['validate'] != False: validate = challenger[0]['validate'] else: condition = parcours.success_condition validate = date if result['note']['value'] * 20 / \ result['note']['total'] >= condition else False oldChallenger = challengers[0]['exos'] while True: code = ''.join(random.choices(string.ascii_uppercase, k=6)) if len(list(filter(lambda c:c['code'] == code, oldChallenger))) == 0: break parcours.challenger = [*list(filter(lambda c: c['code'] != challenger['code'], parcours.challenger)), {**challenger, 'exos': [*challengers[0]['exos'], {**result, 'endAt': date, "code": code}], "validate": validate}] parcours.save() return {'validate': validate} class Parcours(models.Model): name = models.CharField(max_length=50) id_code = models.CharField( max_length=50, default=generate_unique_code_parcours) challenger = models.JSONField(default=list) room = models.ForeignKey(Room, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) timer = models.IntegerField(blank=True) exercices = models.JSONField(default=list) # https://docs.djangoproject.com/fr/4.0/ref/validators/ success_condition = models.IntegerField(default=10) objects = ParcoursManager() class TempCorrection(models.Model): correction = models.JSONField(default=list) id_code = models.CharField(max_length=50, default = generate_unique_code_corr)