add invitation request and possibility to join a game when invited, fix jumperpos does not exist, changeSettings request can now change multiple settings at the same time and changes are now in functions
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
|
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
|
||||||
# Updated: 2024/09/27 13:59:03 by edbernar ### ########.fr #
|
# Updated: 2024/09/27 17:17:23 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -87,8 +87,14 @@ class Game:
|
|||||||
if(withBot):
|
if(withBot):
|
||||||
self.join(socket, skinId)
|
self.join(socket, skinId)
|
||||||
elif(opponent != None):
|
elif(opponent != None):
|
||||||
self.join(socket, skinId)
|
if(opponent not in socket.onlinePlayers):
|
||||||
#send invite to oponnent
|
return;
|
||||||
|
opponentGame = socket.onlinePlayers[opponent].game
|
||||||
|
if (opponentGame != None and opponentGame.opponentLock != None and opponentGame.opponentLock == socket.id):
|
||||||
|
socket.onlinePlayers[opponent].game.join(socket, skinId)
|
||||||
|
else:
|
||||||
|
self.join(socket, skinId)
|
||||||
|
socket.onlinePlayers[opponent].sync_send({"type":"invitation","content":{"invitor":socket.id}})
|
||||||
else:
|
else:
|
||||||
while(Game.waitingForPlayerLock):
|
while(Game.waitingForPlayerLock):
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
@ -123,9 +129,9 @@ class Game:
|
|||||||
up = True
|
up = True
|
||||||
i+=2
|
i+=2
|
||||||
if not down:
|
if not down:
|
||||||
self.obstacles.append(Game.jumperPos[i])
|
self.obstacles.append(Game.jumpersPos[i])
|
||||||
if not up:
|
if not up:
|
||||||
self.obstacles.append(Game.jumperPos[i + 1])
|
self.obstacles.append(Game.jumpersPos[i + 1])
|
||||||
self.p1.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
|
self.p1.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
|
||||||
self.obstaclesInvLength()
|
self.obstaclesInvLength()
|
||||||
self.p2.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
|
self.p2.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
|
||||||
@ -134,16 +140,19 @@ class Game:
|
|||||||
def join(self, socket, skin):
|
def join(self, socket, skin):
|
||||||
try:
|
try:
|
||||||
if(self.p1 == None):
|
if(self.p1 == None):
|
||||||
|
print("game created, set as player 1")
|
||||||
self.p1 = socket
|
self.p1 = socket
|
||||||
self.p1Skin = skin
|
self.p1Skin = skin
|
||||||
else:
|
else:
|
||||||
if(self.opponentLock and self.opponentLock != socket.id):
|
if(self.opponentLock != None and self.opponentLock != socket.id):
|
||||||
socket.sendError("You are not invited to this game", 9103)
|
socket.sendError("You are not invited to this game", 9103)
|
||||||
return;
|
return;
|
||||||
|
print("joined game, set as player 2")
|
||||||
self.p2 = socket
|
self.p2 = socket
|
||||||
self.p2Skin = skin
|
self.p2Skin = skin
|
||||||
socket.game = self
|
socket.game = self
|
||||||
if(self.p2 != None and self.p1 != None):
|
if(self.p2 != None and self.p1 != None):
|
||||||
|
print("both players here, send opponent to both players")
|
||||||
self.p1.sync_send({"type":"game", "content":{"action":1,"id":self.p2.id,"username":self.p2.username}})
|
self.p1.sync_send({"type":"game", "content":{"action":1,"id":self.p2.id,"username":self.p2.username}})
|
||||||
self.p2.sync_send({"type":"game", "content":{"action":1,"id":self.p1.id,"username":self.p1.username}})
|
self.p2.sync_send({"type":"game", "content":{"action":1,"id":self.p1.id,"username":self.p1.username}})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/25 23:28:49 by edbernar #+# #+# #
|
# Created: 2024/09/25 23:28:49 by edbernar #+# #+# #
|
||||||
# Updated: 2024/09/27 10:57:55 by edbernar ### ########.fr #
|
# Updated: 2024/09/27 16:00:05 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -16,49 +16,63 @@ from ..fieldsVerif import usernameValid, passwordValid, discordValid
|
|||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
def changePassword(socket, user, old, new):
|
||||||
|
if(user.id42 == None):
|
||||||
|
socket.sendError("You can't set or change this field if you have a 42 account", 9031)
|
||||||
|
return(False)
|
||||||
|
if (old == None):
|
||||||
|
socket.sendError("You must provide your current password to change it",9030)
|
||||||
|
return(False)
|
||||||
|
if (hashlib.md5((user.mail + old).encode()).hexdigest() != user.password):
|
||||||
|
socket.sendError("Invalid password", 9029)
|
||||||
|
return(False)
|
||||||
|
if (not passwordValid(new, socket)):
|
||||||
|
return(False)
|
||||||
|
user.password = hashlib.md5((user.mail + new).encode()).hexdigest()
|
||||||
|
socket.sync_send(json.dumps({"type": "change_private_info", "content": "Password successfully updated."}))
|
||||||
|
user.save()
|
||||||
|
return(True)
|
||||||
|
|
||||||
|
def changeDiscord(socket, name, user):
|
||||||
|
if (not discordValid(name, socket)):
|
||||||
|
return(False)
|
||||||
|
if (name == ""):
|
||||||
|
user.discord_username = None
|
||||||
|
else:
|
||||||
|
user.discord_username = name
|
||||||
|
user.save()
|
||||||
|
socket.sync_send(json.dumps({"type": "change_private_info", "content": "Discord successfully updated."}))
|
||||||
|
return(True)
|
||||||
|
|
||||||
|
def changeUsername(socket, username, user):
|
||||||
|
if (not usernameValid(username, socket)):
|
||||||
|
return(False)
|
||||||
|
user.username = username
|
||||||
|
socket.username = username
|
||||||
|
socket.scope["session"]['username'] = username
|
||||||
|
user.save()
|
||||||
|
socket.scope["session"].save()
|
||||||
|
socket.sync_send(json.dumps({"type": "change_private_info", "content": "Username successfully updated."}))
|
||||||
|
return(True)
|
||||||
|
|
||||||
|
def deleteAccount(socket,user):
|
||||||
|
user.delete()
|
||||||
|
socket.scope["session"].delete()
|
||||||
|
socket.sync_send(json.dumps({"type": "change_private_info", "content": "Successfully deleted."}))
|
||||||
|
socket.close()
|
||||||
|
|
||||||
@sync_to_async
|
@sync_to_async
|
||||||
def changePrivateInfo(socket, content):
|
def changePrivateInfo(socket, content):
|
||||||
whatChanged = ""
|
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(id=socket.id)
|
user = User.objects.get(id=socket.id)
|
||||||
if ("delete" in content):
|
if ("delete" in content):
|
||||||
whatChanged = "Delete"
|
deleteAccount(socket, user)
|
||||||
user.delete()
|
return
|
||||||
socket.scope["session"].delete()
|
if ("username" in content):
|
||||||
socket.sync_send(json.dumps({"type": "change_private_info", "content": "Successfully deleted."}))
|
changeUsername(socket, content["username"], user)
|
||||||
socket.close()
|
if ("new_password" in content):
|
||||||
return;
|
changePassword(socket, user, content.get("old_password", None), content["new_password"])
|
||||||
elif ("username" in content):
|
if ("discord" in content):
|
||||||
whatChanged = "Username"
|
changeDiscord(socket,content["discord"], user)
|
||||||
if (not usernameValid(content.get("username"), socket)):
|
|
||||||
return
|
|
||||||
user.username = content["username"]
|
|
||||||
socket.username = content["username"]
|
|
||||||
socket.scope["session"]['username'] = content["username"]
|
|
||||||
elif ("new_password" in content):
|
|
||||||
whatChanged = "Password"
|
|
||||||
if (not passwordValid(content.get("new_password"), socket)):
|
|
||||||
return
|
|
||||||
if (not content.get("old_password")):
|
|
||||||
socket.sendError("You must provide your current password to change it",9030)
|
|
||||||
return
|
|
||||||
if (hashlib.md5((user.mail + content["old_password"]).encode()).hexdigest() != user.password):
|
|
||||||
socket.sendError("Invalid password", 9029)
|
|
||||||
return
|
|
||||||
user.password = hashlib.md5((user.mail + content["new_password"]).encode()).hexdigest()
|
|
||||||
elif ("discord" in content):
|
|
||||||
whatChanged = "Discord"
|
|
||||||
if (not discordValid(content.get("discord"), socket)):
|
|
||||||
return
|
|
||||||
if (content["discord"] == ""):
|
|
||||||
user.discord_username = None
|
|
||||||
else:
|
|
||||||
user.discord_username = content["discord"]
|
|
||||||
else:
|
|
||||||
socket.sendError("You must provide a field to update", 9028)
|
|
||||||
return;
|
|
||||||
user.save()
|
|
||||||
socket.scope["session"].save()
|
|
||||||
socket.sync_send(json.dumps({"type": "change_private_info", "content": whatChanged + " successfully updated."}))
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
socket.sendError("An unknown error occured", 9027, e)
|
socket.sendError("An unknown error occured", 9027, e)
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/11 17:07:08 by tomoron #+# #+# #
|
# Created: 2024/09/11 17:07:08 by tomoron #+# #+# #
|
||||||
# Updated: 2024/09/27 02:12:40 by tomoron ### ########.fr #
|
# Updated: 2024/09/27 16:33:32 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -16,5 +16,9 @@ async def start(socket, content):
|
|||||||
if(socket.game != None):
|
if(socket.game != None):
|
||||||
socket.sendError("Game already started", 9102)
|
socket.sendError("Game already started", 9102)
|
||||||
return;
|
return;
|
||||||
Game(socket, content.get("with_bot", False), content.get("skinId", 0), content.get("opponent", None))
|
opponent = content.get("opponent", None)
|
||||||
|
if(opponent != None and opponent not in socket.onlinePlayers):
|
||||||
|
socket.sendError("Your opponent isn't online",9032)
|
||||||
|
return;
|
||||||
|
Game(socket, content.get("with_bot", False), content.get("skinId", 0),opponent)
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/25 22:51:55 by edbernar #+# #+# #
|
# Created: 2024/09/25 22:51:55 by edbernar #+# #+# #
|
||||||
# Updated: 2024/09/25 23:04:18 by edbernar ### ########.fr #
|
# Updated: 2024/09/27 15:58:21 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -16,13 +16,16 @@ import json
|
|||||||
|
|
||||||
@sync_to_async
|
@sync_to_async
|
||||||
def getPrivateInfo(socket, content):
|
def getPrivateInfo(socket, content):
|
||||||
response = {}
|
try:
|
||||||
user = User.objects.filter(id=socket.id).values().first()
|
response = {}
|
||||||
if (user.get('id42') == None):
|
user = User.objects.get(id=socket.id)
|
||||||
response["is42Account"] = False
|
if (user.id42 == None):
|
||||||
else:
|
response["is42Account"] = False
|
||||||
response["is42Account"] = True
|
else:
|
||||||
response["username"] = user.get('username')
|
response["is42Account"] = True
|
||||||
response["mail"] = user.get('mail')
|
response["username"] = user.username
|
||||||
response["discord_username"] = user.get('discord_username')
|
response["mail"] = user.mail
|
||||||
socket.sync_send({"type":"private_info", "content": response})
|
response["discord_username"] = user.discord_username
|
||||||
|
socket.sync_send({"type":"private_info", "content": response})
|
||||||
|
except Exception as e:
|
||||||
|
socket.sendError("Invalid request", 9005, e)
|
||||||
|
@ -32,6 +32,8 @@
|
|||||||
- 9028 : You must provide exactly one field to update
|
- 9028 : You must provide exactly one field to update
|
||||||
- 9029 : Invalid password
|
- 9029 : Invalid password
|
||||||
- 9030 : You must provide your current password to it
|
- 9030 : You must provide your current password to it
|
||||||
|
- 9031 : You can't set or change this field if you have a 42 account
|
||||||
|
- 9032 : Your opponent isn't online
|
||||||
|
|
||||||
- 9100 : Action out of range
|
- 9100 : Action out of range
|
||||||
- 9101 : No game started
|
- 9101 : No game started
|
||||||
|
Reference in New Issue
Block a user