mail are now sent in a thread to not block serveur execution, bots are now limited by the map limits

This commit is contained in:
2024-10-22 16:38:20 +02:00
parent 4f0a0ac3aa
commit 648c9e12fa
6 changed files with 32 additions and 19 deletions

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ # # By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/19 18:29:36 by tomoron #+# #+# # # Created: 2024/10/19 18:29:36 by tomoron #+# #+# #
# Updated: 2024/10/22 14:39:52 by tomoron ### ########.fr # # Updated: 2024/10/22 16:35:59 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -73,6 +73,13 @@ class Bot(Player):
self.objective = self.genRandomBallDirection(0, 0, True) self.objective = self.genRandomBallDirection(0, 0, True)
return return
self.objective = self.genRandomBallDirection(tempBall.pos[0], tempBall.up, False) self.objective = self.genRandomBallDirection(tempBall.pos[0], tempBall.up, False)
leftLimit = GameSettings.mapLimits["left"] + (GameSettings.playerLength / 2)
rightLimit = GameSettings.mapLimits["right"] - (GameSettings.playerLength / 2)
if(self.objective["pos"] < leftLimit or self.objective["pos"] > rightLimit):
print("objective out of bound , set objective to limit")
print("prev objective : ", self.objective["pos"])
self.objective["pos"] = leftLimit if self.objective["pos"] < 0 else rightLimit
print("new objective : ", self.objective["pos"])
def isEnd(self): def isEnd(self):
if(self.tournament != None): if(self.tournament != None):

View File

@ -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/10/22 15:34:32 by tomoron ### ########.fr # # Updated: 2024/10/22 16:28:36 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -45,8 +45,11 @@ class Game:
print("game created with ", p1.socket.username, "vs", p2.socket.username) print("game created with ", p1.socket.username, "vs", p2.socket.username)
@multimethod @multimethod
def __init__(self, socket, withBot : bool, skinId = 0, goalId = 0 , opponent = None): def __init__(self, socket, withBot : bool, skinId : int, goalId :int , ranked = False, opponent = None):
self.initAttributes() self.initAttributes()
if(ranked):
self.rankedInnit()
return;
self.withBot = withBot self.withBot = withBot
self.opponentLock = opponent self.opponentLock = opponent
@ -197,7 +200,7 @@ class Game:
else: else:
self.p2.pos["pos"] = self.p2.checkMovement(-pos) self.p2.pos["pos"] = self.p2.checkMovement(-pos)
if(self.p2.pos["pos"] != -pos): if(self.p2.pos["pos"] != -pos):
self.p2.socket.sync_send("game",{"action":3, "pos":self.p2.pos["pos"], "up":up, "is_opponent":False}) self.p2.socket.sync_send("game",{"action":3, "pos":-self.p2.pos["pos"], "up":up, "is_opponent":False})
self.p2.pos["up"] = up self.p2.pos["up"] = up
if(opponent != None): if(opponent != None):
opponent.sync_send({"type":"game","content":{"action":3, "pos":-pos, "up":up, "is_opponent":True}}) opponent.sync_send({"type":"game","content":{"action":3, "pos":-pos, "up":up, "is_opponent":True}})

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/06 16:33:56 by tomoron #+# #+# # # Created: 2024/10/06 16:33:56 by tomoron #+# #+# #
# Updated: 2024/10/22 15:15:02 by tomoron ### ########.fr # # Updated: 2024/10/22 16:33:39 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/04 17:17:07 by tomoron #+# #+# # # Created: 2024/10/04 17:17:07 by tomoron #+# #+# #
# Updated: 2024/10/22 01:46:59 by tomoron ### ########.fr # # Updated: 2024/10/22 16:32:35 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -138,4 +138,7 @@ class Tournament:
nbLoop += 1 nbLoop += 1
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
self.broadcast({"action":7, "winnerId" : self.players.index(self.finalGame.winner)}) self.broadcast({"action":7, "winnerId" : self.players.index(self.finalGame.winner)})
for x in self.players:
x.socket.tournament = None
self.players = []
print("tournament done, winner is ", self.finalGame.winner.socket.username) print("tournament done, winner is ", self.finalGame.winner.socket.username)

View File

@ -6,7 +6,7 @@
# By: marvin <marvin@student.42.fr> +#+ +:+ +#+ # # By: marvin <marvin@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/08/09 08:08:00 by edbernar #+# #+# # # Created: 2024/08/09 08:08:00 by edbernar #+# #+# #
# Updated: 2024/09/30 19:41:51 by tomoron ### ########.fr # # Updated: 2024/10/22 15:57:27 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -24,6 +24,7 @@ import random
import re import re
import json import json
import hashlib import hashlib
from threading import Thread
URLMAIL = SERVER_URL + "/verify?token=" URLMAIL = SERVER_URL + "/verify?token="
@ -47,9 +48,8 @@ def createAccount(socket, content):
MailVerify.objects.create(uid=new_user, token=verif_str).save() MailVerify.objects.create(uid=new_user, token=verif_str).save()
print("send") print("send")
socket.sync_send(json.dumps({"type": "create_account", "content": "Account created"})) socket.sync_send(json.dumps({"type": "create_account", "content": "Account created"}))
if(not sendVerifMail(verif_str, content["mail"], content["username"])): thread = Thread(target = sendVerifMail, args = (verif_str, content["mail"], content["username"]))
print("mail error") thread.start()
socket.sendError("An error occured while sending the email, glhf", 9026)
except Exception as e: except Exception as e:
print("error") print("error")
socket.sendError("An error occured while creating the account", 9024, e) socket.sendError("An error occured while creating the account", 9024, e)
@ -139,13 +139,13 @@ def sendVerifMail(verif_str, mail, username):
</html> </html>
''', 'html')) ''', 'html'))
try: try:
serveur = smtplib.SMTP('smtp.mail.me.com', 587) server = smtplib.SMTP('smtp.mail.me.com', 587)
serveur.ehlo() server.ehlo()
serveur.starttls() server.starttls()
serveur.ehlo() server.ehlo()
serveur.login(ICLOUD_USER, ICLOUD_PASS) server.login(ICLOUD_USER, ICLOUD_PASS)
serveur.sendmail(ICLOUD_USER, mail, msg.as_string()) server.sendmail(ICLOUD_USER, mail, msg.as_string())
serveur.quit() server.quit()
print("E-mail envoyé avec succès !") print("E-mail envoyé avec succès !")
return(74725) return(74725)
except Exception as e: except Exception as e:

View File

@ -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/10/14 20:04:28 by tomoron ### ########.fr # # Updated: 2024/10/22 16:02:51 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -29,5 +29,5 @@ async def start(socket, content):
if(goalId < 0 or goalId >= GameSettings.nbGoals): if(goalId < 0 or goalId >= GameSettings.nbGoals):
socket.sendError("Goal id out of range", 9039) socket.sendError("Goal id out of range", 9039)
return; return;
Game(socket, content.get("with_bot", False), skinId, goalId, opponent) Game(socket, content.get("with_bot", False), skinId, goalId, False, opponent)