add Game class, users can now create and join games. remove bcypt from dockerfile pip install. remove rm ~/PTME_Data in make fclean
This commit is contained in:
3
Makefile
3
Makefile
@ -6,7 +6,7 @@
|
||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/07/13 16:18:56 by tomoron #+# #+# #
|
||||
# Updated: 2024/08/28 18:27:14 by tomoron ### ########.fr #
|
||||
# Updated: 2024/09/14 00:33:28 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -34,7 +34,6 @@ clean:
|
||||
docker system prune -af --volumes
|
||||
|
||||
fclean:clean
|
||||
sudo rm -rf ~/PTME_data
|
||||
|
||||
re: fclean all
|
||||
|
||||
|
@ -12,7 +12,7 @@ RUN apt install -y python3.12 postgresql-client
|
||||
RUN curl https://bootstrap.pypa.io/get-pip.py -o /root/get-pip.py
|
||||
RUN python3.12 /root/get-pip.py
|
||||
|
||||
RUN pip3 install requests django psycopg "channels[daphne]" bcrypt
|
||||
RUN pip3 install requests django psycopg "channels[daphne]"
|
||||
|
||||
ARG DB_HOST
|
||||
ARG DB_NAME
|
||||
|
@ -0,0 +1,76 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Game.py :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
|
||||
# Updated: 2024/09/14 00:40:30 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
import time
|
||||
import json
|
||||
|
||||
class Game:
|
||||
waitingForPlayerLock = False
|
||||
waitingForPlayer = None
|
||||
def __init__(self, socket, withBot):
|
||||
self.p1 = None;
|
||||
self.p2 = None
|
||||
self.p1Ready = False
|
||||
self.p2Ready = False
|
||||
self.bot = withBot
|
||||
print("new game start")
|
||||
|
||||
if(not withBot):
|
||||
print("no bot")
|
||||
while(Game.WaitingForPlayerLock):
|
||||
time.sleep(0.05)
|
||||
Game.waitingForPlayerLock = True
|
||||
if(Game.waitingForPlayer == None):
|
||||
print("no player waiting")
|
||||
Game.waitingForPlayer = self
|
||||
self.join(socket)
|
||||
else:
|
||||
print("player waiting")
|
||||
Game.waitingForPlayer.join(socket)
|
||||
Game.waitingForPlayer = None;
|
||||
Game.waitingForPlayerLock = False
|
||||
|
||||
def join(self, socket):
|
||||
try:
|
||||
if(self.p1 == None):
|
||||
self.p1 = socket
|
||||
else:
|
||||
self.p2 = socket
|
||||
socket.game = self
|
||||
data = json.dumps({"type":"game", "content":{
|
||||
"action" : 0,
|
||||
"id": socket.id,
|
||||
"username": socket.username
|
||||
}})
|
||||
self.p1.send(text_data=data)
|
||||
if(self.p2 != None):
|
||||
self.p2.send(text_data=data)
|
||||
except Exception as e:
|
||||
socket.sendError("invalid request", 9005, e)
|
||||
|
||||
def setReady(self, socket):
|
||||
print("ready request")
|
||||
if(socket == self.p1):
|
||||
self.p1Ready = True
|
||||
print("p1 ready")
|
||||
elif (socket == self.p2):
|
||||
self.p2Ready = True
|
||||
print("p2 ready")
|
||||
else:
|
||||
return(0)
|
||||
if(self.p1Ready and self.p2Ready):
|
||||
print("both ready, start game loop")
|
||||
self.game_loop();
|
||||
return(1)
|
||||
|
||||
def game_loop(self):
|
||||
print("AAAAAAAAAAAAAAAAAAA")
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/09 08:08:00 by edbernar #+# #+# #
|
||||
# Updated: 2024/09/12 16:27:52 by edbernar ### ########.fr #
|
||||
# Updated: 2024/09/12 16:29:17 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -64,8 +64,9 @@ def createAccount(socket, content):
|
||||
new_user.save()
|
||||
verif_str = gen_string(200)
|
||||
MailVerify.objects.create(uid=new_user, token=verif_str).save()
|
||||
sendVerifMail(verif_str, content["mail"], content["username"])
|
||||
socket.send(text_data=json.dumps({"type": "create_account", "content": "Account created"}))
|
||||
if(not sendVerifMail(verif_str, content["mail"], content["username"])):
|
||||
socket.sendError("An error occured while sending the email, glhf", 2026)
|
||||
except Exception as e:
|
||||
socket.sendError("An error occured while creating the account", 9024, e)
|
||||
|
||||
@ -162,8 +163,10 @@ def sendVerifMail(verif_str, mail, username):
|
||||
serveur.sendmail(ICLOUD_USER, mail, msg.as_string())
|
||||
serveur.quit()
|
||||
print("E-mail envoyé avec succès !")
|
||||
return(74725)
|
||||
except Exception as e:
|
||||
print(f"Erreur lors de l'envoi de l'e-mail : {e}")
|
||||
return(0)
|
||||
|
||||
def gen_string(length):
|
||||
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
|
@ -0,0 +1,18 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# ready.py :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/09/13 23:41:12 by tomoron #+# #+# #
|
||||
# Updated: 2024/09/14 00:21:43 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
def ready(socket, content):
|
||||
print("ready request")
|
||||
if(socket.game == None):
|
||||
socket.sendError("No game started", 9101)
|
||||
return;
|
||||
socket.game.setReady(socket)
|
@ -0,0 +1,16 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# start.py :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/09/11 17:07:08 by tomoron #+# #+# #
|
||||
# Updated: 2024/09/13 23:46:25 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
from ...Game import Game
|
||||
|
||||
def start(socket, content):
|
||||
game = Game(socket, content.get("with_bot", False))
|
@ -6,9 +6,33 @@
|
||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/09/09 16:10:26 by tomoron #+# #+# #
|
||||
# Updated: 2024/09/11 14:03:28 by tomoron ### ########.fr #
|
||||
# Updated: 2024/09/13 23:41:52 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
from .gameActions.start import start
|
||||
from .gameActions.ready import ready
|
||||
|
||||
# game request format : {"type":"game", "content":{"action": 1, ...}}
|
||||
|
||||
#client actions :
|
||||
# 0 : player join : a player joins the game
|
||||
# - id : id of the player who joined
|
||||
# - username : username of the player who joined
|
||||
#
|
||||
# 1 : ...
|
||||
|
||||
#server actions :
|
||||
# 0 : start : starts a game
|
||||
# - with_bot : true/false, is the second player a bot
|
||||
#
|
||||
# 1 : ready : tell the server the game is ready to start (after count down)
|
||||
|
||||
action_list = [start, ready]
|
||||
def gameRequest(socket, content):
|
||||
print("fils de pute")
|
||||
action = content["action"]
|
||||
if(action < 0 or action > len(action_list)):
|
||||
socket.sendError("Action out of range", 9100)
|
||||
return;
|
||||
action_list[action](socket,content)
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/09/09 14:31:30 by tomoron #+# #+# #
|
||||
# Updated: 2024/09/09 16:10:15 by tomoron ### ########.fr #
|
||||
# Updated: 2024/09/14 00:30:59 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -57,16 +57,23 @@ class WebsocketHandler(WebsocketConsumer):
|
||||
self.scope["session"]["username"] = username
|
||||
self.scope["session"].save()
|
||||
self.logged_in = True
|
||||
self.id = uid
|
||||
self.username = username
|
||||
return(1)
|
||||
|
||||
def connect(self):
|
||||
self.logged_in = False
|
||||
self.game = None
|
||||
self.id = 0
|
||||
self.username = None
|
||||
self.accept()
|
||||
if(self.scope["session"].get("logged_in", False)):
|
||||
if(not self.add_to_online(self.scope["session"].get("id", 0))):
|
||||
self.sendError("User already connected", 9013)
|
||||
self.close()
|
||||
return;
|
||||
self.id = self.scope["session"].get("id",0)
|
||||
self.username = self.scope["session"].get("username", None)
|
||||
self.send(text_data=json.dumps({"type":"logged_in", "content":{
|
||||
"status":self.scope["session"].get("logged_in",False),
|
||||
"username":self.scope["session"].get("username",None),
|
||||
|
@ -27,3 +27,7 @@
|
||||
- 9023 : Username already used
|
||||
- 9024 : An error occured while creating the account
|
||||
- 9025 : Account not verified, please verify your account before logging in
|
||||
- 9026 : An error occured while sending the email, glhf
|
||||
|
||||
- 9100 : Action out of range
|
||||
- 9101 : No game started
|
||||
|
Reference in New Issue
Block a user