websocket consumer to async for game infinite loop. game requests now work like stated in comment
This commit is contained in:
@ -6,37 +6,40 @@
|
|||||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
|
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
|
||||||
# Updated: 2024/09/14 12:01:35 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 21:30:54 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
|
import asyncio
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
waitingForPlayerLock = False
|
waitingForPlayerLock = False
|
||||||
waitingForPlayer = None
|
waitingForPlayer = None
|
||||||
def __init__(self, socket, withBot):
|
def __init__(self, socket, withBot):
|
||||||
self.p1 = None;
|
self.p1 = None
|
||||||
self.p2 = None
|
self.p2 = None
|
||||||
self.p1Ready = False
|
self.p1Ready = False
|
||||||
self.p2Ready = False
|
self.p2Ready = False
|
||||||
self.bot = withBot
|
self.bot = withBot
|
||||||
print("new game start")
|
self.started = False
|
||||||
|
self.playerLeft = False
|
||||||
|
self.end = False
|
||||||
|
|
||||||
if(not withBot):
|
if(withBot):
|
||||||
print("no bot")
|
self.join(socket)
|
||||||
|
else:
|
||||||
while(Game.waitingForPlayerLock):
|
while(Game.waitingForPlayerLock):
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
Game.waitingForPlayerLock = True
|
Game.waitingForPlayerLock = True
|
||||||
if(Game.waitingForPlayer == None):
|
if(Game.waitingForPlayer == None):
|
||||||
print("no player waiting")
|
|
||||||
Game.waitingForPlayer = self
|
Game.waitingForPlayer = self
|
||||||
|
socket.sync_send({"type":"game","content":{"action":0}})
|
||||||
self.join(socket)
|
self.join(socket)
|
||||||
else:
|
else:
|
||||||
print("player waiting")
|
|
||||||
Game.waitingForPlayer.join(socket)
|
Game.waitingForPlayer.join(socket)
|
||||||
Game.waitingForPlayer = None;
|
Game.waitingForPlayer = None
|
||||||
Game.waitingForPlayerLock = False
|
Game.waitingForPlayerLock = False
|
||||||
|
|
||||||
def join(self, socket):
|
def join(self, socket):
|
||||||
@ -46,31 +49,59 @@ class Game:
|
|||||||
else:
|
else:
|
||||||
self.p2 = socket
|
self.p2 = socket
|
||||||
socket.game = self
|
socket.game = self
|
||||||
data = json.dumps({"type":"game", "content":{
|
if(self.p2 != None and self.p1 != None):
|
||||||
"action" : 0,
|
data = json.dumps({"type":"game", "content":{
|
||||||
"id": socket.id,
|
"action" : 1,
|
||||||
"username": socket.username
|
"id": socket.id,
|
||||||
}})
|
"username": socket.username
|
||||||
self.p1.send(text_data=data)
|
}})
|
||||||
if(self.p2 != None):
|
self.p1.sync_send(data)
|
||||||
self.p2.send(text_data=data)
|
self.p2.sync_send(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
socket.sendError("invalid request", 9005, e)
|
socket.sendError("invalid request", 9005, e)
|
||||||
|
|
||||||
def setReady(self, socket):
|
async def setReady(self, socket):
|
||||||
print("ready request")
|
|
||||||
if(socket == self.p1):
|
if(socket == self.p1):
|
||||||
self.p1Ready = True
|
self.p1Ready = True
|
||||||
print("p1 ready")
|
|
||||||
elif (socket == self.p2):
|
elif (socket == self.p2):
|
||||||
self.p2Ready = True
|
self.p2Ready = True
|
||||||
print("p2 ready")
|
|
||||||
else:
|
else:
|
||||||
return(0)
|
return(0)
|
||||||
if(self.p1Ready and self.p2Ready):
|
if(self.p1Ready and self.p2Ready):
|
||||||
print("both ready, start game loop")
|
print("both players are ready, starting game")
|
||||||
self.game_loop();
|
asyncio.create_task(self.gameLoop())
|
||||||
return(1)
|
return(1)
|
||||||
|
|
||||||
def game_loop(self):
|
def leave(self, socket):
|
||||||
print("AAAAAAAAAAAAAAAAAAA")
|
socket.game = None
|
||||||
|
if(socket == self.p1):
|
||||||
|
self.p1 = None
|
||||||
|
self.p1Ready =False
|
||||||
|
elif(socket == self.p2):
|
||||||
|
self.p2 = None
|
||||||
|
self.p2Ready =False
|
||||||
|
if(not self.started):
|
||||||
|
while(Game.waitingForPlayerLock):
|
||||||
|
time.sleep(0.05)
|
||||||
|
Game.waitingForPlayerLock = True
|
||||||
|
if(Game.waitingForPlayer == self):
|
||||||
|
Game.waitingForPlayer = None
|
||||||
|
if(self.p1 != None or self.p2 != None):
|
||||||
|
Game.waitingForPlayer = self
|
||||||
|
Game.waitingForPlayerLock = False
|
||||||
|
self.playerLeft = True
|
||||||
|
|
||||||
|
def sendPlayers(self, data):
|
||||||
|
data_raw = json.dumps({"type":"game","content":data})
|
||||||
|
self.p1.sync_send(data_raw)
|
||||||
|
self.p2.sync_send(data_raw)
|
||||||
|
|
||||||
|
async def gameLoop(self):
|
||||||
|
self.started = True
|
||||||
|
self.sendPlayers({"action":2})
|
||||||
|
while(not self.end):
|
||||||
|
print("AAAAAAAAAAAAAAAAAAA")
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
if(self.playerLeft):
|
||||||
|
self.end = True
|
||||||
|
print("A player left, stopping the game. (i know it's a very beautiful and complete game)")
|
||||||
|
@ -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/09/12 16:29:17 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 18:55:01 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -15,6 +15,7 @@ from ..models import User, MailVerify
|
|||||||
from ..data import ICLOUD_USER, ICLOUD_PASS, SERVER_URL
|
from ..data import ICLOUD_USER, ICLOUD_PASS, SERVER_URL
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.mime.image import MIMEImage
|
from email.mime.image import MIMEImage
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
import smtplib
|
import smtplib
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
@ -25,9 +26,12 @@ mail_pattern = "^((?!\\.)[\\w\\-_.]*[^.])(@\\w+)(\\.\\w+(\\.\\w+)?[^.\\W])$"
|
|||||||
password_pattern = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"
|
password_pattern = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"
|
||||||
URLMAIL = SERVER_URL + "/verify?token="
|
URLMAIL = SERVER_URL + "/verify?token="
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
def createAccount(socket, content):
|
def createAccount(socket, content):
|
||||||
|
print("create account")
|
||||||
if (socket.logged_in):
|
if (socket.logged_in):
|
||||||
socket.sendError("Already logged in", 9012)
|
socket.sendError("Already logged in", 9012)
|
||||||
|
return;
|
||||||
try:
|
try:
|
||||||
if (not bool(re.match(mail_pattern, content["mail"]))):
|
if (not bool(re.match(mail_pattern, content["mail"]))):
|
||||||
socket.sendError("Invalid mail", 9014)
|
socket.sendError("Invalid mail", 9014)
|
||||||
@ -64,10 +68,13 @@ def createAccount(socket, content):
|
|||||||
new_user.save()
|
new_user.save()
|
||||||
verif_str = gen_string(200)
|
verif_str = gen_string(200)
|
||||||
MailVerify.objects.create(uid=new_user, token=verif_str).save()
|
MailVerify.objects.create(uid=new_user, token=verif_str).save()
|
||||||
socket.send(text_data=json.dumps({"type": "create_account", "content": "Account created"}))
|
print("send")
|
||||||
|
socket.sync_send(json.dumps({"type": "create_account", "content": "Account created"}))
|
||||||
if(not sendVerifMail(verif_str, content["mail"], content["username"])):
|
if(not sendVerifMail(verif_str, content["mail"], content["username"])):
|
||||||
|
print("mail error")
|
||||||
socket.sendError("An error occured while sending the email, glhf", 2026)
|
socket.sendError("An error occured while sending the email, glhf", 2026)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print("error")
|
||||||
socket.sendError("An error occured while creating the account", 9024, e)
|
socket.sendError("An error occured while creating the account", 9024, e)
|
||||||
|
|
||||||
def sendVerifMail(verif_str, mail, username):
|
def sendVerifMail(verif_str, mail, username):
|
||||||
|
@ -0,0 +1,14 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# leave.py :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/09/14 15:37:49 by tomoron #+# #+# #
|
||||||
|
# Updated: 2024/09/14 21:20:32 by tomoron ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
async def leave(socket, content):
|
||||||
|
socket.game.leave(socket)
|
@ -6,13 +6,13 @@
|
|||||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/13 23:41:12 by tomoron #+# #+# #
|
# Created: 2024/09/13 23:41:12 by tomoron #+# #+# #
|
||||||
# Updated: 2024/09/14 00:21:43 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 19:27:51 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
def ready(socket, content):
|
async def ready(socket, content):
|
||||||
print("ready request")
|
print("ready request")
|
||||||
if(socket.game == None):
|
if(socket.game == None):
|
||||||
socket.sendError("No game started", 9101)
|
socket.sendError("No game started", 9101)
|
||||||
return;
|
return;
|
||||||
socket.game.setReady(socket)
|
await socket.game.setReady(socket)
|
||||||
|
@ -6,11 +6,14 @@
|
|||||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/11 17:07:08 by tomoron #+# #+# #
|
# Created: 2024/09/11 17:07:08 by tomoron #+# #+# #
|
||||||
# Updated: 2024/09/13 23:46:25 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 19:28:30 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
from ...Game import Game
|
from ...Game import Game
|
||||||
|
|
||||||
def start(socket, content):
|
async def start(socket, content):
|
||||||
game = Game(socket, content.get("with_bot", False))
|
if(socket.game != None):
|
||||||
|
socket.sendError("Game already started", 9102)
|
||||||
|
Game(socket, content.get("with_bot", False))
|
||||||
|
|
||||||
|
@ -6,12 +6,13 @@
|
|||||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/09 16:10:26 by tomoron #+# #+# #
|
# Created: 2024/09/09 16:10:26 by tomoron #+# #+# #
|
||||||
# Updated: 2024/09/14 12:43:14 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 19:22:52 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
from .gameActions.start import start
|
from .gameActions.start import start
|
||||||
from .gameActions.ready import ready
|
from .gameActions.ready import ready
|
||||||
|
from .gameActions.leave import leave
|
||||||
|
|
||||||
# game request format : {"type":"game", "content":{"action": 1, ...}}
|
# game request format : {"type":"game", "content":{"action": 1, ...}}
|
||||||
|
|
||||||
@ -19,24 +20,24 @@ from .gameActions.ready import ready
|
|||||||
# 0 : wait : tell the client to wait for an opponent
|
# 0 : wait : tell the client to wait for an opponent
|
||||||
#
|
#
|
||||||
# 1 : opponent : tell the client the name of the opponent
|
# 1 : opponent : tell the client the name of the opponent
|
||||||
# - id
|
# - id : 0 if it's a bot
|
||||||
# - username
|
# - username
|
||||||
#
|
#
|
||||||
# 2 : go : the game started
|
# 2 : go : the game started
|
||||||
|
|
||||||
#client actions (actions sent by the client) :
|
#client actions (actions sent by the client) :
|
||||||
# 0 : start : starts a game
|
# 0 : start : starts a game
|
||||||
# - with_bot : true/false, is the second player a bot
|
# - with_bot : true/false (default : false), is the second player a bot
|
||||||
#
|
#
|
||||||
# 1 : ready : tell the server the game is ready to start
|
# 1 : ready : tell the server the game is ready to start
|
||||||
#
|
#
|
||||||
# 2 : leave : leave the game (or waiting screen)
|
# 2 : leave : leave the game (or waiting screen)
|
||||||
|
|
||||||
action_list = [start, ready]
|
action_list = [start, ready, leave]
|
||||||
def gameRequest(socket, content):
|
async def gameRequest(socket, content):
|
||||||
action = content["action"]
|
action = content["action"]
|
||||||
if(action < 0 or action > len(action_list)):
|
if(action < 0 or action > len(action_list)):
|
||||||
socket.sendError("Action out of range", 9100)
|
socket.sendError("Action out of range", 9100)
|
||||||
return;
|
return;
|
||||||
action_list[action](socket,content)
|
await action_list[action](socket,content)
|
||||||
|
|
||||||
|
@ -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/28 18:22:43 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 18:31:09 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -14,11 +14,13 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from ..models import Message, User
|
from ..models import Message, User
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
def getAllListUser(socket, content=None):
|
def getAllListUser(socket, content=None):
|
||||||
uid = socket.scope["session"].get("uid", 0)
|
uid = socket.id
|
||||||
res = User.objects.filter(~Q(id=uid))
|
res = User.objects.filter(~Q(id=uid))
|
||||||
data = []
|
data = []
|
||||||
for x in res:
|
for x in res:
|
||||||
data.append({"name":x.username, "pfp":x.pfp, "id":x.id})
|
data.append({"name":x.username, "pfp":x.pfp, "id":x.id})
|
||||||
socket.send(text_data=json.dumps({"type": "all_list_user", "content": data}))
|
socket.sync_send(json.dumps({"type": "all_list_user", "content": data}))
|
||||||
|
@ -6,62 +6,29 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/03 22:53:14 by edbernar #+# #+# #
|
# Created: 2024/08/03 22:53:14 by edbernar #+# #+# #
|
||||||
# Updated: 2024/08/29 20:56:36 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 18:31:09 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
import random
|
import random
|
||||||
import json
|
import json
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from ..models import User, Message
|
from ..models import User, Message
|
||||||
|
|
||||||
listMessage = {
|
@sync_to_async
|
||||||
"type": "private_list_message",
|
|
||||||
"content": [
|
|
||||||
{
|
|
||||||
"from": 0,
|
|
||||||
"content": "",
|
|
||||||
"date": "10:05 31/07/2024"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"from": 0,
|
|
||||||
"content": "",
|
|
||||||
"date": "10:05 31/07/2024"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"from": 0,
|
|
||||||
"content": "",
|
|
||||||
"date": "10:06 31/07/2024"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"from": 0,
|
|
||||||
"content": "",
|
|
||||||
"date": "10:06 31/07/2024"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"from": 0,
|
|
||||||
"content": "",
|
|
||||||
"date": "10:45 31/07/2024"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"from": 0,
|
|
||||||
"content": "",
|
|
||||||
"date": "10:46 31/07/2024"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
def getPrivateListMessage(socket, content):
|
def getPrivateListMessage(socket, content):
|
||||||
# |TOM| Requete pour avoir la liste des messages privés grace à l'id de l'utilisateur
|
# |TOM| Requete pour avoir la liste des messages privés grace à l'id de l'utilisateur
|
||||||
if(not User.objects.filter(id=content["id"]).exists()):
|
if(not User.objects.filter(id=content["id"]).exists()):
|
||||||
socket.sendError("User not found", 9008)
|
socket.sendError("User not found", 9008)
|
||||||
return;
|
return;
|
||||||
id = socket.scope["session"].get("id", 0)
|
id = socket.id
|
||||||
other_id = content["id"]
|
other_id = content["id"]
|
||||||
messages = Message.objects.filter((Q(to=id) & Q(sender=other_id)) | (Q(to=other_id) & Q(sender=id))).order_by('date')
|
messages = Message.objects.filter((Q(to=id) & Q(sender=other_id)) | (Q(to=other_id) & Q(sender=id))).order_by('date')
|
||||||
result = []
|
result = []
|
||||||
for x in messages:
|
for x in messages:
|
||||||
result.append({"from":x.sender.id, "content": x.content, "date" : x.date.strftime("%H:%M:%S %d/%m/%Y")})
|
result.append({"from":x.sender.id, "content": x.content, "date" : x.date.strftime("%H:%M:%S %d/%m/%Y")})
|
||||||
socket.send(text_data=json.dumps({"type":"private_list_message","content":result}))
|
socket.sync_send(json.dumps({"type":"private_list_message","content":result}))
|
||||||
|
|
||||||
|
@ -6,59 +6,22 @@
|
|||||||
# 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/27 23:57:44 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 18:31:45 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
from ..models import Message, User
|
from ..models import Message, User
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
|
|
||||||
#data = [
|
@sync_to_async
|
||||||
# {
|
|
||||||
# "name": "Nessundorma",
|
|
||||||
# "status": "online",
|
|
||||||
# "pfp": "https://wallpapers-clan.com/wp-content/uploads/2023/05/cool-pfp-02.jpg",
|
|
||||||
# "id": 145564
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "name": "Succotash",
|
|
||||||
# "status": "offline",
|
|
||||||
# "pfp": "https://i.pinimg.com/200x/28/75/96/287596f98304bf1adc2c411619ae8fef.jpg",
|
|
||||||
# "id": 256981
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "name": "Astropower",
|
|
||||||
# "status": "online",
|
|
||||||
# "pfp": "https://ashisheditz.com/wp-content/uploads/2024/03/cool-anime-pfp-demon-slayer-HD.jpg",
|
|
||||||
# "id": 301547
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "name": "Assaultive",
|
|
||||||
# "status": "offline",
|
|
||||||
# "pfp": "https://i1.sndcdn.com/artworks-1Li0JIJrQGlojD3y-AEiNkw-t500x500.jpg",
|
|
||||||
# "id": 432448
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "name": "Redshock",
|
|
||||||
# "status": "offline",
|
|
||||||
# "pfp": "https://cdn.pfps.gg/pfps/7094-boy-pfp.png",
|
|
||||||
# "id": 543211
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "name": "Parley",
|
|
||||||
# "status": "offline",
|
|
||||||
# "pfp": "https://pbs.twimg.com/media/EscE6ckU0AA-Uhe.png",
|
|
||||||
# "id": 654123
|
|
||||||
# }
|
|
||||||
#]
|
|
||||||
|
|
||||||
def getPrivateListUser(socket, content=None):
|
def getPrivateListUser(socket, content=None):
|
||||||
# |TOM| Faire une requête à la base de données pour récupérer la liste des
|
# |TOM| Faire une requête à la base de données pour récupérer la liste des
|
||||||
# utilisateurs qui doivent apparaitre dans la liste du chat privé
|
# utilisateurs qui doivent apparaitre dans la liste du chat privé
|
||||||
# (ceux qui ont eu conversation avec l'utilisateur)
|
# (ceux qui ont eu conversation avec l'utilisateur)
|
||||||
# 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.id
|
||||||
request = """
|
request = """
|
||||||
SELECT DISTINCT server_user.id AS id, username, pfp
|
SELECT DISTINCT server_user.id AS id, username, pfp
|
||||||
FROM server_user
|
FROM server_user
|
||||||
@ -71,4 +34,4 @@ def getPrivateListUser(socket, content=None):
|
|||||||
for x in res:
|
for x in res:
|
||||||
status = "online" if x.id in socket.onlinePlayers else "offline"
|
status = "online" if x.id in socket.onlinePlayers else "offline"
|
||||||
data.append({"name":x.username, "status": status, "pfp":x.pfp, "id":x.id})
|
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.sync_send(json.dumps({"type": "private_list_user", "content": data}))
|
||||||
|
@ -6,10 +6,11 @@
|
|||||||
# 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/09/10 13:28:38 by edbernar ### ########.fr #
|
# Updated: 2024/09/14 18:54:47 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
from ..models import User
|
from ..models import User
|
||||||
import hashlib
|
import hashlib
|
||||||
import requests
|
import requests
|
||||||
@ -24,14 +25,15 @@ def loginByPass(socket, content):
|
|||||||
socket.sendError("Account not verified, please verify your account before logging in",9025)
|
socket.sendError("Account not verified, please verify your account before logging in",9025)
|
||||||
return
|
return
|
||||||
if(socket.login(user[0].id, user[0].username)):
|
if(socket.login(user[0].id, user[0].username)):
|
||||||
socket.send(text_data=json.dumps({"type":"logged_in", "content":{
|
socket.sync_send(json.dumps({"type":"logged_in", "content":{
|
||||||
"status":True,
|
"status":True,
|
||||||
"username":user[0].username,
|
"username":user[0].username,
|
||||||
"id": user[0].id,
|
"id": user[0].id,
|
||||||
}}))
|
}}))
|
||||||
else:
|
else:
|
||||||
socket.send(text_data=json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
|
socket.sync_send(json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
def login(socket, content):
|
def login(socket, content):
|
||||||
try:
|
try:
|
||||||
if (content["type"] == "byPass"):
|
if (content["type"] == "byPass"):
|
||||||
|
@ -6,14 +6,16 @@
|
|||||||
# 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/09/09 14:16:01 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 18:32:14 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
from ..models import User, Message
|
from ..models import User, Message
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
def sendPrivateMessage(socket, content):
|
def sendPrivateMessage(socket, content):
|
||||||
# |Tom| Requete pour vérifier si l'user existe
|
# |Tom| Requete pour vérifier si l'user existe
|
||||||
# Si user existe pas, faire ça : socket.sendError("User not found", 9008)
|
# Si user existe pas, faire ça : socket.sendError("User not found", 9008)
|
||||||
@ -27,7 +29,7 @@ def sendPrivateMessage(socket, content):
|
|||||||
if(not dest.exists()):
|
if(not dest.exists()):
|
||||||
socket.sendError("User not found", 9008)
|
socket.sendError("User not found", 9008)
|
||||||
return
|
return
|
||||||
user = User.objects.filter(id=socket.scope["session"]["id"])
|
user = User.objects.filter(id=socket.id)
|
||||||
if(int(content["to"]) == user[0].id):
|
if(int(content["to"]) == user[0].id):
|
||||||
socket.sendError("Invalid message sent", 9009)
|
socket.sendError("Invalid message sent", 9009)
|
||||||
new_msg = Message.objects.create(sender=user[0], to=dest[0], content=content["content"])
|
new_msg = Message.objects.create(sender=user[0], to=dest[0], content=content["content"])
|
||||||
@ -41,7 +43,7 @@ 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")
|
||||||
}}
|
}}
|
||||||
socket.send(text_data=json.dumps(jsonVar))
|
socket.sync_send(json.dumps(jsonVar))
|
||||||
if(content["to"] in socket.onlinePlayers):
|
if(content["to"] in socket.onlinePlayers):
|
||||||
jsonVar = {"type": "new_private_message", "content": {
|
jsonVar = {"type": "new_private_message", "content": {
|
||||||
"from": new_msg.sender.id,
|
"from": new_msg.sender.id,
|
||||||
@ -49,6 +51,6 @@ 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")
|
||||||
}}
|
}}
|
||||||
socket.onlinePlayers[content["to"]].send(text_data=json.dumps(jsonVar))
|
socket.onlinePlayers[content["to"]].sync_send(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)
|
||||||
|
@ -6,12 +6,15 @@
|
|||||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/09 14:31:30 by tomoron #+# #+# #
|
# Created: 2024/09/09 14:31:30 by tomoron #+# #+# #
|
||||||
# Updated: 2024/09/14 00:30:59 by tomoron ### ########.fr #
|
# Updated: 2024/09/14 21:21:19 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
from channels.generic.websocket import WebsocketConsumer
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
|
from typing import Union
|
||||||
import json
|
import json
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import django
|
import django
|
||||||
django.setup()
|
django.setup()
|
||||||
@ -31,15 +34,27 @@ functionRequest = [login, getPrivateListUser, getPrivateListMessage,
|
|||||||
|
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
class WebsocketHandler(WebsocketConsumer):
|
class WebsocketHandler(AsyncWebsocketConsumer):
|
||||||
debugMode = True
|
debugMode = True
|
||||||
|
|
||||||
# format : {id : socket, ...}
|
# format : {id : socket, ...}
|
||||||
onlinePlayers = {}
|
onlinePlayers = {}
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
|
def session_get(self, key, default=None):
|
||||||
|
return(self.scope["session"].get(key, default))
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
|
def session_set(self, key, value):
|
||||||
|
self.scope["session"][key] = value
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
|
def session_save(self):
|
||||||
|
self.scope["session"].save()
|
||||||
|
|
||||||
def add_to_online(self, uid):
|
def add_to_online(self, uid):
|
||||||
if(not uid):
|
if(not uid):
|
||||||
return
|
return(0)
|
||||||
if(uid not in self.onlinePlayers):
|
if(uid not in self.onlinePlayers):
|
||||||
self.onlinePlayers[uid] = self
|
self.onlinePlayers[uid] = self
|
||||||
return(1)
|
return(1)
|
||||||
@ -47,56 +62,53 @@ class WebsocketHandler(WebsocketConsumer):
|
|||||||
return(0)
|
return(0)
|
||||||
|
|
||||||
def login(self, uid: int, username: str) -> int:
|
def login(self, uid: int, username: str) -> int:
|
||||||
if(self.scope["session"].get("logged_in", False)):
|
if(self.session_get("logged_in", False)):
|
||||||
return(0)
|
return(0)
|
||||||
if(not self.add_to_online(uid)):
|
if(not self.add_to_online(uid)):
|
||||||
socket.sendError("Already logged in", 9012)
|
socket.sendError("Already logged in", 9012)
|
||||||
return(0)
|
return(0)
|
||||||
self.scope["session"]["logged_in"] = True
|
self.session_set("logged_in",True)
|
||||||
self.scope["session"]["id"] = uid
|
self.session_set("id",uid)
|
||||||
self.scope["session"]["username"] = username
|
self.session_set("username",username)
|
||||||
self.scope["session"].save()
|
self.session_save()
|
||||||
self.logged_in = True
|
self.logged_in = True
|
||||||
self.id = uid
|
self.id = uid
|
||||||
self.username = username
|
self.username = username
|
||||||
return(1)
|
return(1)
|
||||||
|
|
||||||
def connect(self):
|
async def connect(self):
|
||||||
self.logged_in = False
|
self.logged_in = False
|
||||||
self.game = None
|
self.game = None
|
||||||
self.id = 0
|
self.id = 0
|
||||||
self.username = None
|
self.username = None
|
||||||
self.accept()
|
await self.accept()
|
||||||
if(self.scope["session"].get("logged_in", False)):
|
if(await self.session_get("logged_in", False)):
|
||||||
if(not self.add_to_online(self.scope["session"].get("id", 0))):
|
if(not self.add_to_online(await self.session_get("id", 0))):
|
||||||
self.sendError("User already connected", 9013)
|
jsonVar = {"type": "error", "content": "User already connected", "code": 9013}
|
||||||
self.close()
|
await self.send(text_data=json.dumps(jsonVar))
|
||||||
|
await self.close()
|
||||||
return;
|
return;
|
||||||
self.id = self.scope["session"].get("id",0)
|
self.id = await self.session_get("id",0)
|
||||||
self.username = self.scope["session"].get("username", None)
|
self.username = await self.session_get("username", None)
|
||||||
self.send(text_data=json.dumps({"type":"logged_in", "content":{
|
self.logged_in = True
|
||||||
"status":self.scope["session"].get("logged_in",False),
|
await self.send(text_data=json.dumps({"type":"logged_in", "content":{
|
||||||
"username":self.scope["session"].get("username",None),
|
"status":await self.session_get("logged_in",False),
|
||||||
"id":self.scope["session"].get("id",0)
|
"username":await self.session_get("username",None),
|
||||||
|
"id":await self.session_get("id",0)
|
||||||
}}))
|
}}))
|
||||||
self.logged_in = self.scope["session"].get("logged_in", False)
|
|
||||||
print("new client")
|
print("new client")
|
||||||
|
|
||||||
def disconnect(self, close_code):
|
async 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")
|
||||||
if(not self.logged_in):
|
if(not self.logged_in):
|
||||||
return ;
|
return ;
|
||||||
uid = self.scope["session"].get("id", 0)
|
uid = await self.session_get("id", 0)
|
||||||
if(uid in self.onlinePlayers):
|
if(uid in self.onlinePlayers):
|
||||||
del self.onlinePlayers[uid]
|
del self.onlinePlayers[uid]
|
||||||
|
if(self.game !=None):
|
||||||
|
self.game.leave(self)
|
||||||
|
|
||||||
def receive(self, text_data):
|
async def receive(self, text_data):
|
||||||
print("someone is talking")
|
|
||||||
print("username is ", self.scope["session"].get("username"))
|
|
||||||
if(self.scope["session"].get("logged_in", False)):
|
|
||||||
print("user is logged in")
|
|
||||||
else:
|
|
||||||
print("user is not logged in")
|
|
||||||
try:
|
try:
|
||||||
jsonRequest = json.loads(text_data)
|
jsonRequest = json.loads(text_data)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
@ -106,21 +118,37 @@ class WebsocketHandler(WebsocketConsumer):
|
|||||||
self.printDebug(jsonRequest, 0)
|
self.printDebug(jsonRequest, 0)
|
||||||
if (jsonRequest["type"] in typeRequest):
|
if (jsonRequest["type"] in typeRequest):
|
||||||
if (jsonRequest["type"] == "login" or jsonRequest["type"] == "create_account"):
|
if (jsonRequest["type"] == "login" or jsonRequest["type"] == "create_account"):
|
||||||
functionRequest[typeRequest.index(jsonRequest["type"])](self, jsonRequest["content"])
|
await functionRequest[typeRequest.index(jsonRequest["type"])](self, jsonRequest["content"])
|
||||||
else:
|
else:
|
||||||
if (not self.scope["session"].get("logged_in", False)):
|
if (not await self.session_get("logged_in", False)):
|
||||||
return;
|
return;
|
||||||
functionRequest[typeRequest.index(jsonRequest["type"])](self, jsonRequest["content"])
|
await functionRequest[typeRequest.index(jsonRequest["type"])](self, jsonRequest["content"])
|
||||||
else:
|
else:
|
||||||
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)
|
||||||
|
|
||||||
|
def sync_send(self, data: Union[dict,str]):
|
||||||
|
txt_data = None
|
||||||
|
if(type(data) is dict):
|
||||||
|
txt_data = json.dumps(data)
|
||||||
|
else:
|
||||||
|
txt_data = data
|
||||||
|
|
||||||
|
try:
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
except RuntimeError:
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
|
asyncio.set_event_loop(loop)
|
||||||
|
loop.run_until_complete(self.send(text_data=txt_data))
|
||||||
|
else:
|
||||||
|
loop.create_task(self.send(text_data=txt_data))
|
||||||
|
|
||||||
def sendError(self, message, code, error=None):
|
def sendError(self, message, code, error=None):
|
||||||
try:
|
try:
|
||||||
jsonVar = {"type": "error", "content": message, "code": code}
|
jsonVar = {"type": "error", "content": message, "code": code}
|
||||||
self.printDebug(jsonVar, 2, error)
|
self.printDebug(jsonVar, 2, error)
|
||||||
self.send(text_data=json.dumps(jsonVar))
|
self.sync_send(jsonVar)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if (self.debugMode):
|
if (self.debugMode):
|
||||||
print("\033[0;31m|------ Error in sendError ------|\033[0;0m")
|
print("\033[0;31m|------ Error in sendError ------|\033[0;0m")
|
||||||
|
@ -31,3 +31,4 @@
|
|||||||
|
|
||||||
- 9100 : Action out of range
|
- 9100 : Action out of range
|
||||||
- 9101 : No game started
|
- 9101 : No game started
|
||||||
|
- 9102 : Game already started
|
||||||
|
Reference in New Issue
Block a user