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:
2024-09-27 17:25:43 +02:00
parent 12e30b254e
commit 07c35c20fc
5 changed files with 91 additions and 59 deletions

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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):
self.join(socket, skinId)
elif(opponent != None):
self.join(socket, skinId)
#send invite to oponnent
if(opponent not in socket.onlinePlayers):
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:
while(Game.waitingForPlayerLock):
time.sleep(0.05)
@ -123,9 +129,9 @@ class Game:
up = True
i+=2
if not down:
self.obstacles.append(Game.jumperPos[i])
self.obstacles.append(Game.jumpersPos[i])
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.obstaclesInvLength()
self.p2.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
@ -134,16 +140,19 @@ class Game:
def join(self, socket, skin):
try:
if(self.p1 == None):
print("game created, set as player 1")
self.p1 = socket
self.p1Skin = skin
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)
return;
print("joined game, set as player 2")
self.p2 = socket
self.p2Skin = skin
socket.game = self
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.p2.sync_send({"type":"game", "content":{"action":1,"id":self.p1.id,"username":self.p1.username}})
except Exception as e:

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 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
def changePrivateInfo(socket, content):
whatChanged = ""
try:
user = User.objects.get(id=socket.id)
if ("delete" in content):
whatChanged = "Delete"
user.delete()
socket.scope["session"].delete()
socket.sync_send(json.dumps({"type": "change_private_info", "content": "Successfully deleted."}))
socket.close()
return;
elif ("username" in content):
whatChanged = "Username"
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."}))
deleteAccount(socket, user)
return
if ("username" in content):
changeUsername(socket, content["username"], user)
if ("new_password" in content):
changePassword(socket, user, content.get("old_password", None), content["new_password"])
if ("discord" in content):
changeDiscord(socket,content["discord"], user)
except Exception as e:
socket.sendError("An unknown error occured", 9027, e)

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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):
socket.sendError("Game already started", 9102)
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)

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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
def getPrivateInfo(socket, content):
response = {}
user = User.objects.filter(id=socket.id).values().first()
if (user.get('id42') == None):
response["is42Account"] = False
else:
response["is42Account"] = True
response["username"] = user.get('username')
response["mail"] = user.get('mail')
response["discord_username"] = user.get('discord_username')
socket.sync_send({"type":"private_info", "content": response})
try:
response = {}
user = User.objects.get(id=socket.id)
if (user.id42 == None):
response["is42Account"] = False
else:
response["is42Account"] = True
response["username"] = user.username
response["mail"] = user.mail
response["discord_username"] = user.discord_username
socket.sync_send({"type":"private_info", "content": response})
except Exception as e:
socket.sendError("Invalid request", 9005, e)

View File

@ -32,6 +32,8 @@
- 9028 : You must provide exactly one field to update
- 9029 : Invalid password
- 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
- 9101 : No game started