add players status and send message to both users when a message is sent
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/09 08:08:00 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/25 16:42:26 by tomoron ### ########.fr #
|
||||
# Updated: 2024/08/27 23:20:40 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -61,10 +61,9 @@ def createAccount(socket, content):
|
||||
password = hashlib.md5((content["mail"] + content["password"]).encode()).hexdigest()
|
||||
new_user = User.objects.create(username=content["username"], mail=content["mail"], password=password)
|
||||
new_user.save()
|
||||
socket.scope["session"]["logged_in"] = True
|
||||
socket.scope["session"]["username"] = content["username"]
|
||||
socket.scope["session"]["id"] = new_user.id
|
||||
socket.scope["session"].save()
|
||||
socket.send(text_data=json.dumps({"type": "create_account", "content": "Account created"}))
|
||||
if(socket.login(new_user.id, content["username"])):
|
||||
socket.send(text_data=json.dumps({"type": "create_account", "content": "Account created"}))
|
||||
else:
|
||||
socket.sendError("Already logged in", 9012)
|
||||
except Exception as e:
|
||||
socket.sendError("Error create account", 9005, e)
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/03 15:10:23 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/26 20:07:57 by tomoron ### ########.fr #
|
||||
# Updated: 2024/08/27 23:57:44 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -60,7 +60,7 @@ def getPrivateListUser(socket, content=None):
|
||||
# Si user existe pas, faire ça : socket.sendError("User not found", 9008)
|
||||
id = socket.scope["session"].get("id", 0)
|
||||
request = """
|
||||
SELECT DISTINCT server_user.id AS id,'online' AS status, username, pfp
|
||||
SELECT DISTINCT server_user.id AS id, username, pfp
|
||||
FROM server_user
|
||||
LEFT JOIN server_message AS sended ON sended.to_id=server_user.id
|
||||
LEFT JOIN server_message AS received ON received.sender_id=server_user.id
|
||||
@ -69,5 +69,6 @@ def getPrivateListUser(socket, content=None):
|
||||
res = User.objects.raw(request,[id,id])
|
||||
data = []
|
||||
for x in res:
|
||||
data.append({"name":x.username, "status":x.status, "pfp":x.pfp, "id":x.id})
|
||||
status = "online" if x.id in socket.onlinePlayers else "offline"
|
||||
data.append({"name":x.username, "status": status, "pfp":x.pfp, "id":x.id})
|
||||
socket.send(text_data=json.dumps({"type": "private_list_user", "content": data}))
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/03 08:10:38 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/25 17:32:37 by tomoron ### ########.fr #
|
||||
# Updated: 2024/08/27 23:40:35 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -61,19 +61,17 @@ userList = [
|
||||
def loginByPass(socket, content):
|
||||
password_hash = hashlib.md5((content["mail"] + content["password"]).encode()).hexdigest()
|
||||
user = User.objects.filter(mail=content["mail"], password=password_hash)
|
||||
if(len(user)):
|
||||
jsonVar = {"type": "login", "content": {"username": user[0].username, "id": user[0].id}}
|
||||
socket.scope["session"]["logged_in"] = True
|
||||
socket.scope["session"]["username"] = jsonVar["content"]["username"]
|
||||
socket.scope["session"]["id"] = user[0].id
|
||||
socket.scope["session"].save()
|
||||
socket.send(text_data=json.dumps({"type":"logged_in", "content":{
|
||||
"status":True,
|
||||
"username":jsonVar["content"]["username"],
|
||||
"id": user[0].id,
|
||||
}}))
|
||||
return
|
||||
socket.send(text_data=json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
|
||||
if(user.exists()):
|
||||
if(socket.login(user[0].id, user[0].username)):
|
||||
socket.send(text_data=json.dumps({"type":"logged_in", "content":{
|
||||
"status":True,
|
||||
"username":user[0].username,
|
||||
"id": user[0].id,
|
||||
}}))
|
||||
else:
|
||||
socket.sendError("Already logged in", 9012)
|
||||
else:
|
||||
socket.send(text_data=json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
|
||||
|
||||
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/04 13:44:11 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/27 17:09:39 by tomoron ### ########.fr #
|
||||
# Updated: 2024/08/28 00:09:41 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -37,6 +37,8 @@ def sendPrivateMessage(socket, content):
|
||||
"content": content["content"],
|
||||
"date": new_msg.date.strftime("%H:%M:%S %d/%m/%Y")
|
||||
}}
|
||||
if(content["to"] in socket.onlinePlayers):
|
||||
socket.send_to_all(content["to"], json.dumps(jsonVar))
|
||||
socket.send(text_data=json.dumps(jsonVar))
|
||||
except Exception as e:
|
||||
socket.sendError("Invalid message sent", 9009, e)
|
||||
|
@ -21,6 +21,33 @@ from random import randint
|
||||
class WebsocketHandler(WebsocketConsumer):
|
||||
debugMode = True
|
||||
|
||||
# format : {id : [socket,...], ...}
|
||||
onlinePlayers = {}
|
||||
|
||||
def send_to_all(self, uid, text_data):
|
||||
print("\033[32msending", text_data, " to all socket of", uid)
|
||||
for x in self.onlinePlayers[uid]:
|
||||
x.send(text_data=text_data)
|
||||
|
||||
def add_to_online(self, uid):
|
||||
if(not uid):
|
||||
return
|
||||
if(uid not in self.onlinePlayers):
|
||||
self.onlinePlayers[uid] = [self]
|
||||
else:
|
||||
self.onlinePlayers[uid].append(self)
|
||||
print("online : ", self.onlinePlayers)
|
||||
|
||||
def login(self, uid: int, username: str) -> int:
|
||||
if(self.scope["session"].get("logged_in", False)):
|
||||
return(0)
|
||||
self.scope["session"]["logged_in"] = True
|
||||
self.scope["session"]["id"] = uid
|
||||
self.scope["session"]["username"] = username
|
||||
self.scope["session"].save()
|
||||
self.add_to_online(uid)
|
||||
return(1)
|
||||
|
||||
def connect(self):
|
||||
self.accept()
|
||||
self.send(text_data=json.dumps({"type":"logged_in", "content":{
|
||||
@ -28,10 +55,17 @@ class WebsocketHandler(WebsocketConsumer):
|
||||
"username":self.scope["session"].get("username",None),
|
||||
"id":self.scope["session"].get("id",0)
|
||||
}}))
|
||||
if(self.scope["session"].get("logged_in", False)):
|
||||
self.add_to_online(self.scope["session"].get("id", 0))
|
||||
print("new client")
|
||||
|
||||
def disconnect(self, close_code):
|
||||
print("you can go, i am not mad, we never wanted you anyway")
|
||||
uid = self.scope["session"].get("id", 0)
|
||||
if(uid in self.onlinePlayers):
|
||||
self.onlinePlayers[uid].remove(self)
|
||||
if(not len(self.onlinePlayers[uid])):
|
||||
del self.onlinePlayers[uid]
|
||||
|
||||
def receive(self, text_data):
|
||||
print("someone is talking")
|
||||
@ -58,7 +92,6 @@ class WebsocketHandler(WebsocketConsumer):
|
||||
self.sendError("Invalid type", 9004)
|
||||
except Exception as e:
|
||||
self.sendError("Invalid request", 9005, e)
|
||||
print(e)
|
||||
|
||||
def sendError(self, message, code, error=None):
|
||||
try:
|
||||
|
@ -1,6 +1,6 @@
|
||||
INSERT INTO "server_user" ("username", "mail", "password", "id42", "pfp") VALUES
|
||||
('tomoron', 'a@a.com', 'a5f1efc32bcc4dc53e471f9620b78ca9', NULL, '/static/default_pfp.jpg'),
|
||||
('patate', 'b@b.com', '5e547979d3f20f43c87e5e0e8406f267', NULL, '/static/default_pfp.jpg');
|
||||
('tomoron', 'a@a.com', 'a5f1efc32bcc4dc53e471f9620b78ca9', NULL, '/static/img/default_pfp.jpg'),
|
||||
('patate', 'b@b.com', '5e547979d3f20f43c87e5e0e8406f267', NULL, '/static/img/default_pfp.jpg');
|
||||
|
||||
INSERT INTO "server_message" ("date", "sender_id", "to_id", "content") VALUES
|
||||
('2024-08-25 18:10:54.27476+00', 1, 2, 'coucou'),
|
||||
|
Reference in New Issue
Block a user