- Add responsive :
     - Lobby page
     - Fix bug profil/login/settings'
This commit is contained in:
Mathis Degryck
2024-10-05 23:50:34 +02:00
26 changed files with 561 additions and 156 deletions

View File

@ -0,0 +1,15 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Bot.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/05 03:54:20 by tomoron #+# #+# #
# Updated: 2024/10/05 03:55:31 by tomoron ### ########.fr #
# #
# **************************************************************************** #
class Bot(Player):
def __init__(self):
print("I am a bot boop boop beep boop")

View File

@ -6,11 +6,12 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
# Updated: 2024/10/03 23:54:53 by tomoron ### ########.fr #
# Updated: 2024/10/05 03:50:25 by tomoron ### ########.fr #
# #
# **************************************************************************** #
from asgiref.sync import sync_to_async
from .Player import Player
from .models import GameResults, User
import time
import json
@ -64,33 +65,22 @@ class Game:
maxScore = 5
def __init__(self, socket, withBot, skinId = 0, opponent = None):
self.p1 = None
self.p2 = None
self.p1Ready = False
self.p2Ready = False
self.bot = withBot
self.p1 = Player()
self.p2 = Player()
self.started = False
self.end = False
self.left = None
self.winner = None
self.gameStart = 0
self.expSleepTime = 0
self.p1Pos = {"pos":0, "up": False}
self.p2Pos = {"pos":0, "up": False}
self.ballPos = {"pos":(0, 0), "up": False}
self.ballVel = (0, 0)
self.speed = Game.startSpeed
self.ballVel = (self.speed, 0)
self.score = [0, 0]
self.obstacles = []
self.lastWin = 2
self.p1Skin = 0
self.p2Skin = 0
self.opponentLock = opponent
if(withBot):
self.join(socket, skinId)
elif(opponent != None):
@ -138,40 +128,40 @@ class Game:
else:
self.obstacles.append(Game.jumpersPos[i + 1])
self.p1.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
self.p1.socket.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
self.obstaclesInvLength()
self.p2.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
self.p2.socket.sync_send({"type":"game", "content":{"action":7, "content":self.obstacles}})
self.obstaclesInvLength()
def join(self, socket, skin = 0):
try:
if(self.p1 == None):
if(self.p1.socket == None):
print("game created, set as player 1")
self.p1 = socket
self.p1Skin = skin
self.p1.socket = socket
self.p1.skin = skin
else:
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
self.p2.socket = socket
self.p2.skin = skin
socket.game = self
if(self.p2 != None and self.p1 != None):
if(self.p2.socket != None and self.p1.socket != 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, "skin":self.p2Skin}})
self.p2.sync_send({"type":"game", "content":{"action":1,"id":self.p1.id,"username":self.p1.username, "skin":self.p1Skin}})
self.p1.socket.sync_send({"type":"game", "content":{"action":1,"id":self.p2.socket.id,"username":self.p2.socket.username, "skin":self.p2.skin}})
self.p2.socket.sync_send({"type":"game", "content":{"action":1,"id":self.p1.socket.id,"username":self.p1.socket.username, "skin":self.p1.skin}})
except Exception as e:
socket.sendError("invalid request", 9005, e)
async def setReady(self, socket):
if(socket == self.p1):
self.p1Ready = True
elif (socket == self.p2):
self.p2Ready = True
if(socket == self.p1.socket):
self.p1.ready = True
elif (socket == self.p2.socket):
self.p2.ready = True
else:
return(0)
if(self.p1Ready and self.p2Ready):
if(self.p1.ready and self.p2.ready):
print("both players are ready, starting game")
self.genObstacles()
print("obstacles generated :", self.obstacles)
@ -181,14 +171,14 @@ class Game:
def endGame(self, winner):
if(self.end):
return
self.p1.sync_send({"type":"game","content":{"action":10,"won":winner==1, "opponentLeft":self.left == 2}})
self.p2.sync_send({"type":"game","content":{"action":10,"won":winner==2, "opponentLeft":self.left == 1}})
self.p1.socket.sync_send({"type":"game","content":{"action":10,"won":winner==1, "opponentLeft":self.left == 2}})
self.p2.socket.sync_send({"type":"game","content":{"action":10,"won":winner==2, "opponentLeft":self.left == 1}})
self.winner = winner
self.end = True
def leave(self, socket):
socket.game = None
if (socket == self.p1):
if (socket == self.p1.socket):
self.left = 1
else:
self.left = 2
@ -204,17 +194,17 @@ class Game:
def sendPlayers(self, data):
data_raw = json.dumps({"type":"game","content":data})
self.p1.sync_send(data_raw)
self.p2.sync_send(data_raw)
self.p1.socket.sync_send(data_raw)
self.p2.socket.sync_send(data_raw)
def move(self, socket, pos, up):
opponent = self.p1 if socket != self.p1 else self.p2
if(socket == self.p1):
self.p1Pos["pos"] = pos
self.p1Pos["up"] = up;
opponent = self.p1.socket if socket != self.p1.socket else self.p2.socket
if(socket == self.p1.socket):
self.p1.pos["pos"] = pos
self.p1.pos["up"] = up;
else:
self.p2Pos["pos"] = -pos;
self.p2Pos["up"] = up
self.p2.pos["pos"] = -pos;
self.p2.pos["up"] = up
if(opponent != None):
opponent.sync_send({"type":"game","content":{"action":3, "pos":-pos, "up":up, "is_opponent":True}})
@ -225,14 +215,14 @@ class Game:
self.gameStart = time.time() * 1000
else:
gameTime = (time.time() * 1000) - self.gameStart
if(self.p1):
self.p1.sync_send({"type":"game", "content":{"action":5,
if(self.p1.socket):
self.p1.socket.sync_send({"type":"game", "content":{"action":5,
"pos" : [self.ballPos["pos"][0],self.ballPos["pos"][1]],
"velocity":[self.ballVel[0], self.ballVel[1]],
"game_time":gameTime
}})
if(self.p2):
self.p2.sync_send({"type":"game","content":{"action":5,
if(self.p2.socket):
self.p2.socket.sync_send({"type":"game","content":{"action":5,
"pos" : [-self.ballPos["pos"][0],-self.ballPos["pos"][1]],
"velocity":[-self.ballVel[0], -self.ballVel[1]],
"game_time":gameTime
@ -355,8 +345,8 @@ class Game:
print("a player suffured from a major skill issue")
self.score[player-1] += 1
print("new score :", self.score)
self.p1.sync_send({"type":"game","content":{"action":6, "is_opponent": player == 2}})
self.p2.sync_send({"type":"game","content":{"action":6, "is_opponent": player == 1}})
self.p1.socket.sync_send({"type":"game","content":{"action":6, "is_opponent": player == 2}})
self.p2.socket.sync_send({"type":"game","content":{"action":6, "is_opponent": player == 1}})
await asyncio.sleep(4.5)
if(self.checkGameEndGoal()):
return
@ -375,8 +365,8 @@ class Game:
if(self.obstacles[i]["isUp"] != self.ballPos["up"]):
continue
if(self.twoPointsDistance((self.obstacles[i]["pos"]["x"], self.obstacles[i]["pos"]["z"]), ballPos) < Game.jumperRadius):
self.p1.sync_send({"type":"game", "content":{"action":8,"name":self.obstacles[i]["name"]}})
self.p2.sync_send({"type":"game", "content":{"action":8,"name":self.obstacles[i]["name"]}})
self.p1.socket.sync_send({"type":"game", "content":{"action":8,"name":self.obstacles[i]["name"]}})
self.p2.socket.sync_send({"type":"game", "content":{"action":8,"name":self.obstacles[i]["name"]}})
self.ballPos["up"] = not self.ballPos["up"]
def checkWallsColision(self, ballPos):
@ -402,22 +392,21 @@ class Game:
now = time.time()
delta = now - self.lastUpdate
print("delta :", delta)
print("\033[31msleep time diff :", (delta - self.expSleepTime) * 1000, "ms")
currentBallPos = self.ballPos["pos"]
velX = self.ballVel[0]
velZ = self.ballVel[1]
newBallPos = (round(currentBallPos[0] + (delta * velX), 5),
round(currentBallPos[1] + (delta * velZ), 5))
if(newBallPos[1] <= Game.limits["back"] or newBallPos[1] >= Game.limits["front"]):
player = self.p2Pos if newBallPos[1] < 0 else self.p1Pos
player = self.p2.pos if newBallPos[1] < 0 else self.p1.pos
playerDistance = self.getPlayerDistance(player, newBallPos)
if(playerDistance >= -(Game.playerLength / 2) and playerDistance <= Game.playerLength / 2 and player["up"] == self.ballPos["up"]):
velX = -((self.speed * 0.80) * (playerDistance / (Game.playerLength / 2)))
velZ = self.speed - abs(velX)
if(newBallPos[1] > 0):
velZ = -velZ
self.p1.sync_send({"type":"game","content":{"action":4, "is_opponent": newBallPos[1] < 0}})
self.p2.sync_send({"type":"game","content":{"action":4, "is_opponent": newBallPos[1] > 0}})
self.p1.socket.sync_send({"type":"game","content":{"action":4, "is_opponent": newBallPos[1] < 0}})
self.p2.socket.sync_send({"type":"game","content":{"action":4, "is_opponent": newBallPos[1] > 0}})
else:
await self.scoreGoal(1 if newBallPos[1] < 0 else 2)
return;
@ -458,12 +447,11 @@ class Game:
break;
sleep_time = self.getSleepTime()
print("sleep time : " , sleep_time)
self.expSleepTime = sleep_time
await asyncio.sleep(sleep_time)
print("game end")
await self.saveResults()
self.p1.game = None
self.p2.game = None
self.p1.socket.game = None
self.p2.socket.game = None
@sync_to_async
def saveResults(self):
@ -472,8 +460,8 @@ class Game:
print("unkown winner, setting to 1")
self.winner = 1
print("saving results")
p1DbUser = User.objects.get(id=self.p1.id)
p2DbUser = User.objects.get(id=self.p2.id)
p1DbUser = User.objects.get(id=self.p1.socket.id)
p2DbUser = User.objects.get(id=self.p2.socket.id)
results = GameResults.objects.create(
player1 = p1DbUser,
player2 = p2DbUser,
@ -485,5 +473,5 @@ class Game:
results.save()
print("results saved")
except Exception as e:
self.p1.sendError("Couldn't save last game results", 9104, e)
self.p2.sendError("Couldn't save last game results", 9104, e)
self.p1.socket.sendError("Couldn't save last game results", 9104, e)
self.p2.socket.sendError("Couldn't save last game results", 9104, e)

View File

@ -0,0 +1,18 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Player.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/05 03:22:32 by tomoron #+# #+# #
# Updated: 2024/10/05 03:46:24 by tomoron ### ########.fr #
# #
# **************************************************************************** #
class Player():
def __init__(self):
self.socket = None
self.ready = False
self.pos = {"pos":0, "up": False}
self.skin = 0

View File

@ -0,0 +1,78 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Tournament.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/04 17:17:07 by tomoron #+# #+# #
# Updated: 2024/10/05 04:31:00 by tomoron ### ########.fr #
# #
# **************************************************************************** #
import string
import asyncio
from .utils import genString
class Tournament:
currentTournamentsLock = False
currentTournaments = {}
playerLimit = 8
def __init__(self, socket):
self.messages = []
self.players = []
while(Tournament.currentTournamentsLock):
continue;
Tournament.currentTournamentsLock = True
self.genCode()
Tournament.currentTournaments[self.code] = self
Tournament.currentTournamentsLock = False
self.join(socket)
def genCode(self):
nbChar = 4
self.code = genString(nbChar, string.ascii_uppercase)
nbIter = 0
while(self.code in Tournament.currentTournaments):
if(nbIter == 100):
nbInter = 0
nbChar += 1
self.code = genString(nbChar, string.ascii_uppercase)
nbIter += 1
def broadcast(self, content):
for x in self.players:
x.sync_send("tournament",content)
def sendAllInfo(self, socket):
players = []
for x in self.players:
players.append({"id":x.id,"username":x.username, "pfp":x.pfp})
socket.sync_send("tournament",{"action":5, "players":players, "messages" : self.messages})
def sendMessage(self, socket, message):
self.messages.append({"username":socket.username, "message":message})
if(len(self.messages) > 20):
self.messages.pop(0)
self.broadcast({"action":3, "username":socket.username, "message":message})
def leave(self, socket):
if(socket not in self.players):
return;
index = self.players.index(socket)
self.players.pop(index)
socket.tournament = None
self.broadcast({"action":2,"id":socket.id})
def join(self, socket):
if(socket.tournament != None):
socket.sendError("already in a tournament", 9036)
return
if(len(self.players) == Tournament.playerLimit):
socket.sync_send("tournament",{"action":0, "isFull":True})
return
socket.tournament = self
self.players.append(socket)
socket.sync_send("tournament",{"action":0,"isFull":False, "isStarted":False,"exist":True, "code":self.code})
self.broadcast({"action":1, "id":socket.id, "username": socket.username,"pfp":socket.pfp})

View File

@ -193,8 +193,7 @@
</div>
<span class="line"></span>
<p style="text-align: center; margin-bottom: 20px;">Tournament code</p>
<input id="tournamentCode" class="search-input" type="text" placeholder="Enter the tournament code (empty for create one)">
<span class="line" id="tournament-line2"></span>
<input id="tournamentCode" class="search-input" type="text" placeholder="Enter the tournament code (empty to create one)">
</div>
<div class="skin-select">
<div class="barSelection" id="bar2">

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/23 23:35:41 by edbernar #+# #+# #
# Updated: 2024/09/27 03:37:33 by tomoron ### ########.fr #
# Updated: 2024/10/04 21:13:28 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -23,9 +23,12 @@ def changePfp(socket, content):
generate_name = genString(50)
if (not User.objects.filter(pfp=f"/pfp/{generate_name}.jpg").exists()):
break
with open(f"/var/www/djangoserver/pfp/{generate_name}.jpg", "wb") as image_file:
image_file.write(base64.b64decode(content["img"]))
user = User.objects.get(id=socket.id)
user.pfp = f"/pfp/{generate_name}.jpg"
user.save()
with open(f"/var/www/djangoserver/pfp/{generate_name}.jpg", "wb") as image_file:
image_file.write(base64.b64decode(content["img"]))
socket.pfp = user.pfp
socket.scope["session"]["pfp"] = user.pfp
socket.scope["session"].save()
socket.sync_send(json.dumps({"type": "change_pfp", "content": {'pfp': user.pfp}}))

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/09 16:10:26 by tomoron #+# #+# #
# Updated: 2024/10/01 16:35:10 by tomoron ### ########.fr #
# Updated: 2024/10/04 17:10:39 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -73,6 +73,9 @@ from .gameActions.ping import ping
action_list = [start, ready, leave, move, ping]
async def gameRequest(socket, content):
if("action" not in content):
socket.sendError("missing action parameter",9035)
return
action = content["action"]
if(action < 0 or action > len(action_list)):
socket.sendError("Action out of range", 9100)

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/08/03 08:10:38 by edbernar #+# #+# #
# Updated: 2024/09/30 19:43:54 by tomoron ### ########.fr #
# Updated: 2024/10/04 23:35:03 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -25,26 +25,29 @@ def userExists(mail, password):
if(not user.exists()):
return({"found":False})
else:
return({"found":True, "id":user[0].id, "username":user[0].username, "mail_verified":user[0].mail_verified})
return({"found":True, "id":user[0].id, "username":user[0].username, "mail_verified":user[0].mail_verified, "pfp":user[0].pfp})
async def loginByPass(socket, content):
u_info = await userExists(content["mail"],content["password"])
if(u_info["found"]):
if(not u_info["mail_verified"]):
socket.sendError("Account not verified, please verify your account before logging in",9025)
return
if(await socket.login(u_info["id"], u_info["username"])):
await socket.setLastLogin()
socket.sync_send(json.dumps({"type":"logged_in", "content":{
"status":True,
"username":u_info["username"],
"id": u_info["id"],
"haveUnreadMessage": await getUnreadStatus(u_info["id"])
}}))
try:
u_info = await userExists(content["mail"],content["password"])
if(u_info["found"]):
if(not u_info["mail_verified"]):
socket.sendError("Account not verified, please verify your account before logging in",9025)
return
if(await socket.login(u_info["id"], u_info["username"], u_info["pfp"])):
await socket.setLastLogin()
socket.sync_send(json.dumps({"type":"logged_in", "content":{
"status":True,
"username":u_info["username"],
"id": u_info["id"],
"haveUnreadMessage": await getUnreadStatus(u_info["id"])
}}))
else:
socket.sendError("An unknown error occured",9027)
else:
socket.sendError("An unknown error occured",9027)
else:
socket.sync_send(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}))
except Exception as e:
socket.sendError("Invalid request", 9005, e)
async def login(socket, content):
try:

View File

@ -0,0 +1,14 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# fetchAllData.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/05 02:08:12 by tomoron #+# #+# #
# Updated: 2024/10/05 02:08:52 by tomoron ### ########.fr #
# #
# **************************************************************************** #
async def fetchAllData(socket, content):
socket.tournament.sendAllInfo(socket)

View File

@ -0,0 +1,18 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# leave.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/04 18:05:07 by tomoron #+# #+# #
# Updated: 2024/10/05 02:28:42 by tomoron ### ########.fr #
# #
# **************************************************************************** #
async def tournamentLeave(socket, content):
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" * 100)
if(socket.tournament == None):
socket.sendError("you're not in a tournament", 9037)
return;
socket.tournament.leave(socket)

View File

@ -0,0 +1,17 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# sendMessage.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/04 18:05:27 by tomoron #+# #+# #
# Updated: 2024/10/05 03:27:45 by tomoron ### ########.fr #
# #
# **************************************************************************** #
async def sendMessage(socket, content):
if("message" not in content):
socket.sendError("missing message field",9038)
return
socket.tournament.sendMessage(socket, content["message"])

View File

@ -0,0 +1,22 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# start.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/04 17:16:02 by tomoron #+# #+# #
# Updated: 2024/10/05 02:28:50 by tomoron ### ########.fr #
# #
# **************************************************************************** #
from ...Tournament import Tournament
async def tournamentStart(socket, content):
if("code" in content and len(content["code"])):
if(content["code"] in Tournament.currentTournaments):
Tournament.currentTournaments[content["code"]].join(socket)
else:
socket.sync_send("tournament",{"action":0, "exist":False})
else:
Tournament(socket)

View File

@ -6,26 +6,31 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/01 13:16:39 by edbernar #+# #+# #
# Updated: 2024/10/03 01:29:01 by edbernar ### ########.fr #
# Updated: 2024/10/05 02:33:45 by tomoron ### ########.fr #
# #
# **************************************************************************** #
from .tournamentActions.start import tournamentStart
from .tournamentActions.leave import tournamentLeave
from .tournamentActions.sendMessage import sendMessage
from .tournamentActions.fetchAllData import fetchAllData
# tournament request format : {"type":"tournament", "content":{"action": 1, ...}}
#server actions (actions sent by the server):
# 0 : start : tell the client if tournament is full or not exist. If not, tell the client can start the tournament
# - exist : true/false
# - exists : true/false
# - isFull : true/false
# - started : true/false
# - code : code of the tournament
#
# 1 : someoneJoin : tell the client someone join the tournament (considering clients will place selon the order of the join)
# 1 : someoneJoined : tell the client someone joined the tournament (considering clients will place depending on the order of the join)
# - id : id of the player
# - username : name of the player
# - pfp : pfp of the player
#
# 2 : someoneLeave : tell the client someone leave the tournament (if game not started, players will be replaced in the order of the join)
# - id : id of the player who leave
# 2 : someoneLeft : tell the client someone left the tournament (if game not started, players will be replaced in the order of the join)
# - id : id of the player who left
#
# 3 : message : send a message to the tournament chat
# - username : name of the player who send the message
@ -35,11 +40,15 @@
# - id : id of the player
# - username : name of the player
#
# 5 : allData : gives tournament data to the client
# - players : [{id, username, pfp}, ...]
# - messages : [{username, message}]
#client actions (actions sent by the client) :
# 0 : start : start a tournament. if code == "", create a new tournament, else join the tournament with the code
# - code : code of the tournament
#
# 1 : leave : leave the tournament
#
# 2 : message tournament : send a message to the tournament chat
@ -48,4 +57,16 @@
# 3 : fetchAllData : fetch all data of the tournament
# --> server will send all the data of the tournament (players, messages, etc...) with his actions
actionList = [tournamentStart, tournamentLeave, sendMessage, fetchAllData]
async def tournamentRequest(socket, content):
if("action" not in content):
socket.sendError("missing action parameter",9035)
return
action = content["action"]
if(action < 0 or action > len(actionList)):
socket.sendError("Action out of range", 9100)
return;
if(action != 0 and socket.tournament == None):
socket.sendError("you're not in a tournament",9037)
return ;
await actionList[action](socket,content)

View File

@ -25,8 +25,9 @@ urlpatterns = [
path("multiOnlineGamePage", views.multiOnlineGamePage, name='multiOnlineGamePage'),
path("waitingGamePage", views.waitingGamePage, name='waitingGamePage'),
path("profilPage", views.profilPage, name='profilPage'),
path("game", views.game, name='game'),
# path("game", views.game, name='game'),
path("wait_game", views.game, name='wait_game'),
path("tournament", views.tournament, name='tournament'),
path("login42", views.login42, name='login42'),
path("logout", views.logout, name='logout'),
path("verify", views.verify, name='verify'),

View File

@ -6,12 +6,12 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/27 03:36:08 by tomoron #+# #+# #
# Updated: 2024/09/27 11:23:51 by edbernar ### ########.fr #
# Updated: 2024/10/04 18:59:25 by tomoron ### ########.fr #
# #
# **************************************************************************** #
import random
import string
def genString(length):
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
def genString(length, letters=string.ascii_letters+string.digits):
return(''.join(random.choice(letters) for i in range(length)))

View File

@ -122,6 +122,8 @@ def login42(request):
request.session["logged_in"] = True
request.session["username"] = db_user[0].username
request.session["id"] = db_user[0].id
request.session["pfp"] = db_user[0].pfp
request.session.save()
return redirect("/")
def logout(request):
@ -164,4 +166,7 @@ def tournamentPage(request):
return index(request)
if(not request.session.get("logged_in", False)):
return(HttpResponse("you are not logged in",status=403))
return render(request, "tournamentPage.html", {})
return render(request, "tournamentPage.html", {})
def tournament(request):
return redirect("/lobby")

View File

@ -6,12 +6,13 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/09 14:31:30 by tomoron #+# #+# #
# Updated: 2024/09/30 19:42:45 by tomoron ### ########.fr #
# Updated: 2024/10/04 21:06:20 by tomoron ### ########.fr #
# #
# **************************************************************************** #
from channels.generic.websocket import AsyncWebsocketConsumer
from asgiref.sync import sync_to_async
from multimethod import multimethod
from typing import Union
import json
import asyncio
@ -36,16 +37,17 @@ from .typeRequests.changePrivateInfo import changePrivateInfo
from .typeRequests.changePfp import changePfp
from .typeRequests.statusMessage import statusMessage,getUnreadStatus
from .typeRequests.readMessage import readMessage
from .typeRequests.tournamentRequest import tournamentRequest
typeRequest = ["login", "get_private_list_user", "get_private_list_message",
"send_private_message", "create_account", "get_all_list_user", "game",
"search_user", "get_user_info", "change_pfp", "change_banner",
"get_private_info", "change_private_info","status_message", "read_message"
"send_private_message", "create_account", "get_all_list_user",
"game", "search_user", "get_user_info", "change_pfp", "change_banner",
"get_private_info", "change_private_info","status_message", "read_message", "tournament"
]
functionRequest = [login, getPrivateListUser, getPrivateListMessage,
sendPrivateMessage, createAccount, getAllListUser, gameRequest,
searchUser, getUserInfo, changePfp, changeBanner,
getPrivateInfo, changePrivateInfo, statusMessage, readMessage
getPrivateInfo, changePrivateInfo, statusMessage, readMessage, tournamentRequest
]
from random import randint
@ -83,7 +85,7 @@ class WebsocketHandler(AsyncWebsocketConsumer):
print("\033[32monline : ", self.onlinePlayers)
return(0)
async def login(self, uid: int, username: str) -> int:
async def login(self, uid: int, username: str, pfp : str) -> int:
if(await self.session_get("logged_in", False)):
print("already logged in")
return(0)
@ -93,15 +95,18 @@ class WebsocketHandler(AsyncWebsocketConsumer):
await self.session_set("logged_in",True)
await self.session_set("id",uid)
await self.session_set("username",username)
await self.session_set("pfp", pfp)
await self.session_save()
self.logged_in = True
self.id = uid
self.username = username
self.pfp = pfp
return(1)
async def connect(self):
self.logged_in = False
self.game = None
self.tournament = None
self.id = 0
self.username = None
self.online = True
@ -114,6 +119,7 @@ class WebsocketHandler(AsyncWebsocketConsumer):
return;
self.id = await self.session_get("id",0)
self.username = await self.session_get("username", None)
self.pfp = await self.session_get("pfp",None)
self.logged_in = True
await self.send(text_data=json.dumps({"type":"logged_in", "content":{
"status":await self.session_get("logged_in",False),
@ -133,6 +139,8 @@ class WebsocketHandler(AsyncWebsocketConsumer):
del self.onlinePlayers[uid]
if(self.game !=None):
self.game.leave(self)
if(self.tournament !=None):
self.tournament.leave(self)
async def receive(self, text_data):
try:
@ -153,7 +161,12 @@ class WebsocketHandler(AsyncWebsocketConsumer):
self.sendError("Invalid type", 9004)
except Exception as e:
self.sendError("Invalid request", 9005, e)
@multimethod
def sync_send(self, reqType : str, content:dict):
self.sync_send({"type":reqType,"content":content})
@multimethod
def sync_send(self, data: Union[dict,str]):
if(not self.online):
return