Generateurv2/backend/api/room/consumers.py

332 lines
17 KiB
Python
Raw Normal View History

2022-05-18 10:15:54 +02:00
# chat/consumers.py
import json
from uuid import uuid4
from channels.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async
from .models import Room
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
class RoomConsumer(AsyncWebsocketConsumer):
async def connect(self):
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
self.room_id = self.scope['url_route']['kwargs']['room_id']
self.waiter = None
self.clientId = str(uuid4())
self.waiter = False
self.waiter_code = None
self.user = None
try:
self.room = await self.get_room()
print('Connected')
await self.accept()
await self.send(json.dumps({'type': 'connect', "clientId": self.clientId}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
except:
print('closed')
await self.close()
async def receive(self, text_data):
text_data_json = json.loads(text_data)
type = text_data_json['data']['type']
print(text_data_json)
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
if type == "login":
2022-06-11 23:39:03 +02:00
print('LOF', self.scope['user'])
2022-05-18 10:15:54 +02:00
participants = await self.get_participants()
2022-06-11 23:39:03 +02:00
if self.scope['user'].is_anonymous:
nick = text_data_json['data']['nick']
else:
nick = self.scope['user'].username
if nick in list(map(lambda p: p['nick'], participants)) and self.scope['user'].is_anonymous:
2022-05-18 10:15:54 +02:00
await self.send(json.dumps({'type': 'loginResponse', "error": "USER_INPUT", "description": "Pseudo déjà utilisé"}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
else:
if self.room.private == True:
2022-06-11 23:39:03 +02:00
waiter = await self.add_waiter_db(nick, self.scope['user'].is_anonymous == False, None if self.scope['user'].is_anonymous else self.scope['user'].id_code)
2022-05-18 10:15:54 +02:00
if waiter != None:
self.waiter = True
self.waiter_code = waiter['code']
await self.send(json.dumps({'type': "waitingRoom", "nick": nick, 'id_code': self.room_id}))
await self.channel_layer.group_add(f'waiter__{waiter["code"]}', self.channel_name)
2022-06-11 23:39:03 +02:00
await self.channel_layer.group_send(f'owner__{self.room_id}', {"type": "add_waiter", "code": waiter['code'], "nick": waiter['nick'], "status": waiter['status']})
2022-05-18 10:15:54 +02:00
else:
await self.send(json.dumps({'type': 'loginResponse', "error": "USER_INPUT", "description": "Pseudo déjà utilisé"}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
else:
await self.channel_layer.group_add(self.room_id, self.channel_name)
2022-06-11 23:39:03 +02:00
if self.scope['user'].is_anonymous:
new_participant = await self.add_participant(nick, self.clientId)
self.user = new_participant
else:
new_participant = await self.add_user(nick, self.scope['user'])
self.user = new_participant
2022-05-18 10:15:54 +02:00
await self.send(json.dumps({'type': "roomJoined", "clientId": new_participant.clientId, 'id_code': self.room_id, 'identity': new_participant}))
await self.channel_layer.group_send(self.room_id, {'type': 'join.event', "nick": nick, "owner": False, "online": True, "code": ""})
await self.channel_layer.group_send(f'owner__{self.room_id}', {'type': 'join.event', "nick": nick, "owner": False, "online": True, "code": new_participant['code']})
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
elif type == "acceptParticipant":
code = text_data_json['data']['code']
nick = text_data_json['data']['nick']
2022-06-11 23:39:03 +02:00
status = text_data_json['data']['status']
2022-05-18 10:15:54 +02:00
self.waiter = False
2022-06-11 23:39:03 +02:00
if status == 'anonymous':
new_participant = await self.add_participant(nick, str(uuid4()))
else:
new_participant = await self.add_user(code)
print(new_participant)
2022-05-18 10:15:54 +02:00
await self.del_waiter_db(code)
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
await self.channel_layer.group_send(f'waiter__{code}', {"type": "accept_room", 'id_code': self.room_id, 'code': new_participant['code']})
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
elif type == "refusedParticipant":
code = text_data_json['data']['code']
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
await self.del_waiter_db(code)
await self.channel_layer.group_send(f'waiter__{code}', {"type": "refused_participant"})
elif type == "logAcceptedWaiter":
code = text_data_json['data']['code']
participants = await self.get_participants()
2022-06-11 23:39:03 +02:00
if not self.scope['user'].is_anonymous:
await self.channel_layer.group_discard(f'waiter__{code}', self.channel_name)
await self.channel_layer.group_add(self.room_id, self.channel_name)
self.clientId = self.scope['user'].clientId
self.user = {
'nick': self.scope['user'].username, 'code': self.scope['user'].id_code, 'clientId':self.scope['user'].clientId, "owner": False} #à continuer dsl
self.waiter = False
self.waiter_code = None
await self.send(json.dumps({'type': "roomJoined", "clientId": self.clientId, 'id_code': self.room_id, 'identity': self.user}))
await self.channel_layer.group_send(self.room_id, {"type": "join.event", "nick": self.user['nick'], "owner": False, "online": True, "code": ''})
await self.channel_layer.group_send(f'owner__{self.room_id}', {"type": "join.event", "nick": self.user['nick'], "owner": False, "online": True, "code": self.user['code']})
elif code in list(map(lambda p: p['code'], participants)):
2022-05-18 10:15:54 +02:00
participant = list(
filter(lambda p: p['code'] == code, participants))[0]
await self.channel_layer.group_discard(f'waiter__{code}', self.channel_name)
print('new user', participant)
self.clientId = participant['clientId']
await self.channel_layer.group_add(self.room_id, self.channel_name)
self.user = participant
self.waiter = False
self.waiter_code = None
await self.send(json.dumps({'type': "roomJoined", "clientId": self.clientId, 'id_code': self.room_id, 'identity': participant}))
await self.channel_layer.group_send(self.room_id, {"type": "join.event", "nick": participant['nick'], "owner": False, "online": True, "code": ''})
await self.channel_layer.group_send(f'owner__{self.room_id}', {"type": "join.event", "nick": participant['nick'], "owner": False, "online": True, "code": participant['code']})
elif type == 'relogin':
2022-06-11 23:39:03 +02:00
if not self.scope['user'].is_anonymous:
userInRoom = await self.userInRoom()
if not userInRoom:
await self.send(json.dumps({"type": "reloginError"}))
else:
isOwner = self.scope['user'].clientId == self.room.owner[
'clientId'] and self.scope['user'].id_code == self.room.owner['code']
self.clientId = self.scope['user'].clientId
if isOwner:
await self.channel_layer.group_add(f'owner__{self.room_id}', self.channel_name)
else:
await self.channel_layer.group_add(self.room_id, self.channel_name)
self.user = {
'nick': self.scope['user'].username, 'code': self.scope['user'].id_code, 'clientId': self.scope['user'].clientId, "owner": False}
await self.connect_participant()
await self.channel_layer.group_send(self.room_id, {"type": "reconnect.event", 'nick': self.user['nick']})
await self.channel_layer.group_send(f'owner__{self.room_id}', {"type": "reconnect.event", 'nick': self.user['nick']})
2022-05-18 10:15:54 +02:00
code = text_data_json['data']['code']
participants = await self.get_participants()
if code in list(map(lambda p: p['code'], participants)):
2022-06-11 23:39:03 +02:00
participant = list(
filter(lambda p: p['code'] == code, participants))[0]
2022-05-18 10:15:54 +02:00
self.clientId = participant['clientId']
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
if participant['clientId'] == self.room.owner['clientId'] and participant['code'] == self.room.owner['code']:
await self.channel_layer.group_add(f'owner__{self.room_id}', self.channel_name)
else:
await self.channel_layer.group_add(self.room_id, self.channel_name)
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
participant['online'] = True
self.user = participant
await self.connect_participant()
await self.send(json.dumps({'type': "reloged", "clientId": self.clientId, 'id_code': self.room_id, 'identity': participant}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
await self.channel_layer.group_send(self.room_id, {"type": "reconnect.event", 'nick': self.user['nick']})
await self.channel_layer.group_send(f'owner__{self.room_id}', {"type": "reconnect.event", 'nick': self.user['nick']})
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
else:
await self.send(json.dumps({"type": "reloginError"}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
elif type == "reconnect":
client = text_data_json['data']['clientId']
2022-06-11 23:39:03 +02:00
print(self.scope['user'].is_anonymous)
if not self.scope['user'].is_anonymous:
userInRoom = await self.userInRoom()
if not userInRoom:
await self.send(json.dumps({"type": "reconnectError", 'id_code': self.room_id}))
2022-05-18 10:15:54 +02:00
else:
2022-06-11 23:39:03 +02:00
isOwner = self.scope['user'].clientId == self.room.owner[
'clientId'] and self.scope['user'].id_code == self.room.owner['code']
if isOwner:
await self.channel_layer.group_add(f'owner__{self.room_id}', self.channel_name)
else:
await self.channel_layer.group_add(self.room_id, self.channel_name)
self.user = {
'code': self.scope['user'].id_code, 'nick': self.scope['user'].username, "clientId": self.scope['user'].clientId, 'online': True, 'owner': self.scope['user'].clientId == self.room.owner['clientId'] and self.scope['user'].id_code == self.room.owner['code']}
await self.connect_participant()
self.clientId = self.scope['user'].clientId
await self.send(json.dumps({'type': "reconnected", "clientId": self.scope['user'].clientId, 'id_code': self.room_id, 'identity': self.user}))
await self.channel_layer.group_send(self.room_id, {"type": "reconnect.event", 'nick': self.user['nick']})
await self.channel_layer.group_send(f'owner__{self.room_id}', {"type": "reconnect.event", 'nick': self.user['nick']})
2022-05-18 10:15:54 +02:00
else:
2022-06-11 23:39:03 +02:00
participants = await self.get_participants()
if client in list(map(lambda p: p['clientId'], participants)):
self.clientId = client
participant = list(
filter(lambda p: p['clientId'] == client, participants))[0]
if participant['clientId'] == self.room.owner['clientId'] and participant['code'] == self.room.owner['code']:
print('add in admin')
await self.channel_layer.group_add(f'owner__{self.room_id}', self.channel_name)
else:
print('add in users')
await self.channel_layer.group_add(self.room_id, self.channel_name)
participant['online'] = True
self.user = participant
await self.connect_participant()
await self.send(json.dumps({'type': "reconnected", "clientId": client, 'id_code': self.room_id, 'identity': participant}))
await self.channel_layer.group_send(self.room_id, {"type": "reconnect.event", 'nick': self.user['nick']})
await self.channel_layer.group_send(f'owner__{self.room_id}', {"type": "reconnect.event", 'nick': self.user['nick']})
else:
await self.send(json.dumps({"type": "reconnectError", 'id_code': self.room_id}))
2022-05-18 10:15:54 +02:00
elif type == 'ban':
code = text_data_json['data']["code"]
nick = text_data_json['data']["nick"]
2022-06-11 23:39:03 +02:00
status = text_data_json['data']["status"]
if status == 'anonymous':
await self.del_participant(code)
else:
await self.del_user(code)
2022-05-18 10:15:54 +02:00
await self.channel_layer.group_send(self.room_id, {'type': "ban_participant", "code": code, "nick": nick})
await self.channel_layer.group_send(f'owner__{self.room_id}', {'type': "ban_participant", "code": code, "nick": nick})
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def join_event(self, event):
await self.send(json.dumps({'type': 'joined', 'nick': event['nick'], "owner": event['owner'], "online": event['online'], "code": event["code"]}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def accept_room(self, event):
2022-06-11 23:39:03 +02:00
await self.send(json.dumps({'type': 'accept_room', 'id_code': self.room_id, 'code': event["code"]}))
2022-05-18 10:15:54 +02:00
async def accept_waiter(self, event):
await self.send(json.dumps({'type': 'joined', 'nick': event['nick'], "owner": event['owner'], "online": event['online'], "code": event["code"]}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def add_waiter(self, event):
2022-06-11 23:39:03 +02:00
await self.send(json.dumps({'type': 'add_waiter', 'nick': event['nick'], "code": event["code"], "status": event['status']}))
2022-05-18 10:15:54 +02:00
async def new_parcours(self, event):
await self.send(json.dumps({'type': "add_parcours", "parcours": event['parcours']}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def refused_participant(self, event):
await self.send(json.dumps({"type": "refused"}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def ban_participant(self, event):
await self.send(json.dumps({"type": "banned", 'nick': event['nick'], "code": event["code"]}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
@database_sync_to_async
def get_participants(self):
2022-06-11 23:39:03 +02:00
return Room.objects.filter(id_code=self.room_id)[0].anonymousMembers
2022-05-18 10:15:54 +02:00
@database_sync_to_async
def get_room(self):
2022-06-11 23:39:03 +02:00
return Room.objects.filter(id_code=self.room_id)[0]
2022-05-18 10:15:54 +02:00
@database_sync_to_async
def add_participant(self, new, id):
return Room.objects.add_participant(self.room_id, new, id, False, True)
2022-06-11 23:39:03 +02:00
@database_sync_to_async
def add_user(self, code):
return Room.objects.add_user(self.room_id, code, False, True)
2022-05-18 10:15:54 +02:00
@database_sync_to_async
def del_participant(self, code):
return Room.objects.del_participant(self.room_id, code)
@database_sync_to_async
2022-06-11 23:39:03 +02:00
def del_user(self, code):
return Room.objects.del_user(self.room_id, code)
@database_sync_to_async
def add_waiter_db(self, new, isUser, code):
return Room.objects.add_waiter(self.room_id, new, isUser, code)
2022-05-18 10:15:54 +02:00
@database_sync_to_async
def del_waiter_db(self, code):
return Room.objects.del_waiter(self.room_id, code)
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
@database_sync_to_async
def disconnect_participant(self):
2022-06-11 23:39:03 +02:00
if self.scope['user'].is_anonymous:
return Room.objects.disconnect(self.room_id, self.user['code'])
else:
return Room.objects.disconnect(self.room_id, self.scope['user'].id_code)
2022-05-18 10:15:54 +02:00
@database_sync_to_async
def connect_participant(self):
return Room.objects.connect(self.room_id, self.user['code'])
2022-06-11 23:39:03 +02:00
@database_sync_to_async
def userInRoom(self):
if self.scope['user'].is_anonymous:
return None
return len(self.scope['user'].room_set.filter(
id_code=self.room.id_code)) != 0
2022-05-18 10:15:54 +02:00
async def disconnect(self, close_code):
if self.waiter == False and self.user != None:
await self.disconnect_participant()
await self.channel_layer.group_discard(self.room_id, self.channel_name)
await self.channel_layer.group_send(self.room_id, {"type": "disconnect.event", "nick": self.user['nick']})
await self.channel_layer.group_send(f'owner__{self.room_id}', {"type": "disconnect.event", "nick": self.user['nick']})
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
elif self.waiter == True and self.waiter_code != None:
await self.del_waiter_db(self.waiter_code)
await self.channel_layer.group_send(f'owner__{self.room_id}', {'type': "del_waiter", "code": self.waiter_code})
await self.channel_layer.group_discard(f'waiter__{self.waiter_code}', self.channel_name)
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def disconnect_event(self, event):
await self.send(json.dumps({'type': 'disconnect_participant', "nick": event['nick']}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def reconnect_event(self, event):
await self.send(json.dumps({'type': 'reconnect_participant', "nick": event['nick']}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def del_waiter(self, event):
await self.send(json.dumps({"type": 'del_waiter', "code": event['code']}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def challenge_parcours(self, event):
await self.send(json.dumps({**event}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def delete_parcours(self, event):
await self.send(json.dumps({**event}))
2022-06-11 23:39:03 +02:00
2022-05-18 10:15:54 +02:00
async def edit_parcours(self, event):
await self.send(json.dumps({**event}))