Block fixed, looking for invite in game

This commit is contained in:
2024-01-19 16:48:20 +01:00
parent cdb4dab47a
commit fe47a4d633
9 changed files with 306 additions and 115 deletions

View File

@ -1,6 +1,8 @@
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
from games.models import GameModel
import time
import json
@ -31,19 +33,7 @@ class ChatNoticeConsumer(WebsocketConsumer):
self.accept()
message_time: int = int(time.time() * 1000)
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):
continue
async_to_sync(self.channel_layer.send)(channel, {
'type':"online_users",
'author_id':user.pk,
'targets': targets,
'time':message_time,
'status': 200,
})
self.sync()
def disconnect(self, code):
@ -51,22 +41,15 @@ class ChatNoticeConsumer(WebsocketConsumer):
if (user.is_anonymous or not user.is_authenticated):
return
self.channel_layer.users_channels.pop(user.pk)
del self.channel_layer.users_channels[user.pk]
del self.channel_layer.invite[user.pk]
message_time: int = int(time.time() * 1000)
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()
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):
continue
async_to_sync(self.channel_layer.send)(channel, {
'type':"online_users",
'author_id':user.pk,
'targets': targets,
'time':message_time,
'status': 200,
})
def receive(self, text_data=None, bytes_data=None):
@ -89,50 +72,115 @@ class ChatNoticeConsumer(WebsocketConsumer):
if (self.channel_layer == None):
return
message_time: int = int(time.time() * 1000)
status = 200;
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 = getattr(self, "pre_" + type_notice)(user, targets)
status, result = getattr(self, "pre_" + type_notice)(user, targets)
except AttributeError:
print(f"La fonction pre_{type_notice} n'existe pas.")
status = 200
#print(f"La fonction pre_{type_notice} n'existe pas.")
if targets == "all":
targets = list(self.channel_layer.users_channels.keys())
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,
})
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,
'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,
'content':"notice return",
'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
status = 200
for target in targets:
if (target in self.channel_layer.invite[user.pk]):
status = 409
return status
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):
@ -140,23 +188,48 @@ class ChatNoticeConsumer(WebsocketConsumer):
if (user.is_anonymous or not user.is_authenticated):
return
if (user.pk in self.channel_layer.invite[event["author_id"]]):
return
if (user.pk != event["author_id"]):
self.channel_layer.invite[event["author_id"]].append(user.pk)
invites = self.get_invites(user)
self.send(text_data=json.dumps({
'type':event['type'],
'author_id':event['author_id'],
'content':event['content'],
'invites': invites,
'targets': event['targets'],
'time': event['time'],
'status':event['status'],
}))
def pre_online_users(self, user, targets):
pass
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):