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. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/09 08:08:00 by edbernar #+# #+# #
|
# 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()
|
password = hashlib.md5((content["mail"] + content["password"]).encode()).hexdigest()
|
||||||
new_user = User.objects.create(username=content["username"], mail=content["mail"], password=password)
|
new_user = User.objects.create(username=content["username"], mail=content["mail"], password=password)
|
||||||
new_user.save()
|
new_user.save()
|
||||||
socket.scope["session"]["logged_in"] = True
|
if(socket.login(new_user.id, content["username"])):
|
||||||
socket.scope["session"]["username"] = content["username"]
|
socket.send(text_data=json.dumps({"type": "create_account", "content": "Account created"}))
|
||||||
socket.scope["session"]["id"] = new_user.id
|
else:
|
||||||
socket.scope["session"].save()
|
socket.sendError("Already logged in", 9012)
|
||||||
socket.send(text_data=json.dumps({"type": "create_account", "content": "Account created"}))
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
socket.sendError("Error create account", 9005, e)
|
socket.sendError("Error create account", 9005, e)
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/03 15:10:23 by edbernar #+# #+# #
|
# 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)
|
# Si user existe pas, faire ça : socket.sendError("User not found", 9008)
|
||||||
id = socket.scope["session"].get("id", 0)
|
id = socket.scope["session"].get("id", 0)
|
||||||
request = """
|
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
|
FROM server_user
|
||||||
LEFT JOIN server_message AS sended ON sended.to_id=server_user.id
|
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
|
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])
|
res = User.objects.raw(request,[id,id])
|
||||||
data = []
|
data = []
|
||||||
for x in res:
|
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}))
|
socket.send(text_data=json.dumps({"type": "private_list_user", "content": data}))
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/03 08:10:38 by edbernar #+# #+# #
|
# 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):
|
def loginByPass(socket, content):
|
||||||
password_hash = hashlib.md5((content["mail"] + content["password"]).encode()).hexdigest()
|
password_hash = hashlib.md5((content["mail"] + content["password"]).encode()).hexdigest()
|
||||||
user = User.objects.filter(mail=content["mail"], password=password_hash)
|
user = User.objects.filter(mail=content["mail"], password=password_hash)
|
||||||
if(len(user)):
|
if(user.exists()):
|
||||||
jsonVar = {"type": "login", "content": {"username": user[0].username, "id": user[0].id}}
|
if(socket.login(user[0].id, user[0].username)):
|
||||||
socket.scope["session"]["logged_in"] = True
|
socket.send(text_data=json.dumps({"type":"logged_in", "content":{
|
||||||
socket.scope["session"]["username"] = jsonVar["content"]["username"]
|
"status":True,
|
||||||
socket.scope["session"]["id"] = user[0].id
|
"username":user[0].username,
|
||||||
socket.scope["session"].save()
|
"id": user[0].id,
|
||||||
socket.send(text_data=json.dumps({"type":"logged_in", "content":{
|
}}))
|
||||||
"status":True,
|
else:
|
||||||
"username":jsonVar["content"]["username"],
|
socket.sendError("Already logged in", 9012)
|
||||||
"id": user[0].id,
|
else:
|
||||||
}}))
|
socket.send(text_data=json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
|
||||||
return
|
|
||||||
socket.send(text_data=json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/04 13:44:11 by edbernar #+# #+# #
|
# 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"],
|
"content": content["content"],
|
||||||
"date": new_msg.date.strftime("%H:%M:%S %d/%m/%Y")
|
"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))
|
socket.send(text_data=json.dumps(jsonVar))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
socket.sendError("Invalid message sent", 9009, e)
|
socket.sendError("Invalid message sent", 9009, e)
|
||||||
|
@ -21,6 +21,33 @@ from random import randint
|
|||||||
class WebsocketHandler(WebsocketConsumer):
|
class WebsocketHandler(WebsocketConsumer):
|
||||||
debugMode = True
|
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):
|
def connect(self):
|
||||||
self.accept()
|
self.accept()
|
||||||
self.send(text_data=json.dumps({"type":"logged_in", "content":{
|
self.send(text_data=json.dumps({"type":"logged_in", "content":{
|
||||||
@ -28,10 +55,17 @@ class WebsocketHandler(WebsocketConsumer):
|
|||||||
"username":self.scope["session"].get("username",None),
|
"username":self.scope["session"].get("username",None),
|
||||||
"id":self.scope["session"].get("id",0)
|
"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")
|
print("new client")
|
||||||
|
|
||||||
def disconnect(self, close_code):
|
def disconnect(self, close_code):
|
||||||
print("you can go, i am not mad, we never wanted you anyway")
|
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):
|
def receive(self, text_data):
|
||||||
print("someone is talking")
|
print("someone is talking")
|
||||||
@ -58,7 +92,6 @@ class WebsocketHandler(WebsocketConsumer):
|
|||||||
self.sendError("Invalid type", 9004)
|
self.sendError("Invalid type", 9004)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.sendError("Invalid request", 9005, e)
|
self.sendError("Invalid request", 9005, e)
|
||||||
print(e)
|
|
||||||
|
|
||||||
def sendError(self, message, code, error=None):
|
def sendError(self, message, code, error=None):
|
||||||
try:
|
try:
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
INSERT INTO "server_user" ("username", "mail", "password", "id42", "pfp") VALUES
|
INSERT INTO "server_user" ("username", "mail", "password", "id42", "pfp") VALUES
|
||||||
('tomoron', 'a@a.com', 'a5f1efc32bcc4dc53e471f9620b78ca9', NULL, '/static/default_pfp.jpg'),
|
('tomoron', 'a@a.com', 'a5f1efc32bcc4dc53e471f9620b78ca9', NULL, '/static/img/default_pfp.jpg'),
|
||||||
('patate', 'b@b.com', '5e547979d3f20f43c87e5e0e8406f267', NULL, '/static/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
|
INSERT INTO "server_message" ("date", "sender_id", "to_id", "content") VALUES
|
||||||
('2024-08-25 18:10:54.27476+00', 1, 2, 'coucou'),
|
('2024-08-25 18:10:54.27476+00', 1, 2, 'coucou'),
|
||||||
|
@ -12,4 +12,5 @@
|
|||||||
- 9008 : User not found
|
- 9008 : User not found
|
||||||
- 9009 : Invalid message sent
|
- 9009 : Invalid message sent
|
||||||
- 9010 : Invalid token 42
|
- 9010 : Invalid token 42
|
||||||
- 9011 : Not user registered with this 42 account
|
- 9011 : Not user registered with this 42 account
|
||||||
|
- 9012 : Already logged in
|
||||||
|
Reference in New Issue
Block a user