from channels.generic.websocket import WebsocketConsumer from asgiref.sync import async_to_sync from games.models import GameModel import time import json class ChatNoticeConsumer(WebsocketConsumer): def connect(self): user = self.scope["user"] #if (user.is_anonymous or not user.is_authenticated): #return if (self.channel_layer == None): return self.room_group_name = f'chatNotice{user.pk}' if (not hasattr(self.channel_layer, "users_channels")): self.channel_layer.users_channels = {} self.channel_layer.users_channels[user.pk] = self.channel_name if (not hasattr(self.channel_layer, "invite")): self.channel_layer.invite = {} self.channel_layer.invite[user.pk] = []; async_to_sync(self.channel_layer.group_add)( self.room_group_name, self.channel_name ) self.accept() self.sync() def disconnect(self, code): user = self.scope["user"] if (user.is_anonymous or not user.is_authenticated): return del self.channel_layer.users_channels[user.pk] del self.channel_layer.invite[user.pk] for inviter_id, inviteds_id in self.channel_layer.invite.items(): if (user.pk in inviteds_id): self.channel_layer.invite[inviter_id].remove(user.pk) self.sync() def receive(self, text_data=None, bytes_data=None): if text_data == None: return user = self.scope["user"] #if (user.is_anonymous or not user.is_authenticated): #return text_data_json = json.loads(text_data) type_notice = text_data_json.get('type') targets : list = text_data_json.get('targets') content : dict = text_data_json.get('content') if (type_notice == None or targets == None): return if (self.channel_layer == None): return message_time: int = int(text_data_json.get('time')) print(message_time) print(time.time()) print(time.time() * 1000 - message_time) if (message_time == None): message_time: int = int(time.time() * 1000) #print("receive" + str(user.pk)) result = None try: status, result = getattr(self, "pre_" + type_notice)(user, targets) except AttributeError: status = 200 #print(f"La fonction pre_{type_notice} n'existe pas.") if (status < 300): if targets == "all": targets = list(self.channel_layer.users_channels.keys()) for target in targets: channel = self.channel_layer.users_channels.get(target) if (channel == None or target == user.pk): if (channel == None): status = 404 continue async_to_sync(self.channel_layer.send)(channel, { 'type':type_notice, 'author_id':user.pk, 'content':content, 'result':result, 'targets': targets, 'time':message_time, 'status': 200, }) async_to_sync(self.channel_layer.send)(self.channel_layer.users_channels.get(user.pk), { 'type':type_notice, 'author_id':user.pk, 'result':result, 'targets': targets, 'time':message_time, 'status':status, }) def sync(self, user = None, level = None): sendToUser = True if (user == None): user = self.scope["user"] sendToUser = False if (level == None): level = 0 message_time: int = int(time.time() * 1000) if (sendToUser): targets = [user.pk] else: targets = list(self.channel_layer.users_channels.keys()) for target in targets: channel = self.channel_layer.users_channels.get(target) if (channel == None or (not sendToUser and target == user.pk)): continue async_to_sync(self.channel_layer.send)(channel, { 'type':"online_users", 'author_id':user.pk, 'targets': targets, 'time':message_time, 'status': 200, }) if (level >= 1): async_to_sync(self.channel_layer.send)(channel, { 'type':"invite", 'author_id':user.pk, 'targets': targets, 'time':message_time, 'status': 200, }) def pre_invite(self, user, targets): status = 200 for target in targets: if (target in self.channel_layer.invite[user.pk]): status = 409 continue channel = self.channel_layer.users_channels.get(target) if (channel == None): status = 404 continue # Add the invited in "self.channel_layer.invite" if (user.pk != target): self.channel_layer.invite[user.pk].append(target) return status, None def get_invites(self, user): invites = [] for inviter_id, inviteds_id in self.channel_layer.invite.items(): if (user.pk in inviteds_id and user.pk != inviter_id): invites.append(inviter_id) def invite(self, event): user = self.scope["user"] if (user.is_anonymous or not user.is_authenticated): return invites = self.get_invites(user) self.send(text_data=json.dumps({ 'type':event['type'], 'author_id':event['author_id'], 'invites': invites, 'targets': event['targets'], 'time': event['time'], 'status':event['status'], })) def pre_accept_invite(self, user, targets): if (user.pk not in self.channel_layer.invite[targets[0]]): return 400, None self.channel_layer.invite[targets[0]].remove(user.pk) if (targets[0] in self.channel_layer.invite[user.pk]): self.channel_layer.invite[user.pk].remove(targets[0]) id_game = GameModel().create([user.pk, targets[0]]); return 200, id_game def accept_invite(self, event): user = self.scope["user"] if (user.is_anonymous or not user.is_authenticated): return invites = self.get_invites(user) self.send(text_data=json.dumps({ 'type':event['type'], 'author_id':event['author_id'], 'result': event['result'], 'invites': invites, 'time': event['time'], 'status':event['status'], })) def online_users(self, event): user = self.scope["user"] #if (user.is_anonymous or not user.is_authenticated): #return #print("online_users" + str(user.pk)) event['content'] = self.channel_layer.users_channels self.send(text_data=json.dumps({ 'type':event['type'], 'author_id':event['author_id'], 'content':event['content'], 'time': event['time'], 'status':event['status'], }))