fix pfp , goalId, skinId in tournament game start

This commit is contained in:
2024-10-14 20:34:52 +02:00
parent 86b6fe314f
commit 7c0d0eb9b7
6 changed files with 45 additions and 31 deletions

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/05 03:54:20 by tomoron #+# #+# #
# Updated: 2024/10/13 21:24:19 by tomoron ### ########.fr #
# Updated: 2024/10/14 20:18:58 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -70,8 +70,10 @@ class Bot(Player):
def isEnd(self):
if(self.tournament != None):
return(self.tournament.end)
else:
elif(self.game != None):
return(self.game.end)
else:
return(False)
async def updateLoop(self):
while not self.isEnd():

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
# Updated: 2024/10/14 20:06:12 by tomoron ### ########.fr #
# Updated: 2024/10/14 20:32:47 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -160,19 +160,21 @@ class Game:
def endGame(self, winner):
if(self.end):
return
self.p1.socket.sync_send({"type":"game","content":{"action":10,"won":winner==1, "opponentLeft":self.left == 2}})
if(self.p1 != None):
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.pWinner = self.p1 if winner == 1 else self.p2
self.end = True
def leave(self, socket):
if (socket == self.p1.socket):
if (self.p1 != None and socket == self.p1.socket):
self.left = 1
self.p1.setGame(None)
else:
self.left = 2
self.p2.setGame(None)
if(self.p2 != None):
self.p2.setGame(None)
if(Game.waitingForPlayer == self):
Game.waitingForPlayer = None
if(self.p2 != None):

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/04 17:17:07 by tomoron #+# #+# #
# Updated: 2024/10/14 19:54:30 by tomoron ### ########.fr #
# Updated: 2024/10/14 20:23:18 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -72,7 +72,7 @@ class Tournament:
socket.tournament = None
self.broadcast({"action":2,"id":socket.id})
def join(self, socket, goal=0, skin=0, isBot=False):
def join(self, socket, skin=0, goal=0, isBot=False):
if(not isBot and socket.tournament != None):
socket.sendError("already in a tournament", 9036)
return

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/12 22:49:00 by tomoron #+# #+# #
# Updated: 2024/10/14 20:10:13 by tomoron ### ########.fr #
# Updated: 2024/10/14 20:29:06 by tomoron ### ########.fr #
# #
# **************************************************************************** #
import asyncio
@ -23,29 +23,30 @@ class TournamentGame:
def startGame(self):
l = None
r = None
print("start new game")
if(isinstance(self.left,TournamentGame)):
self.game = Game(self.left.winner, self.right.winner, True)
l = self.left.winner.socket
r = self.right.winner.socket
l = self.left.winner
r = self.right.winner
else:
self.game = Game(self.left, self.right, True)
l = self.left.socket
r = self.right.socket
l.sync_send("tournament", {
l = self.left
r = self.right
l.socket.sync_send("tournament", {
"action":4,
"id": r.id,
"username":r.username,
"id": r.socket.id,
"pfp": r.socket.pfp,
"username":r.socket.username,
"skin" : r.skin,
"goal": r.goal,
"pfp": r.pfp
"goal": r.goal
})
r.sync_send("tournament", {
r.socket.sync_send("tournament", {
"action":4,
"id": l.id,
"username": l.username,
"id": l.socket.id,
"pfp": l.socket.pfp,
"username": l.socket.username,
"skin" : l.skin,
"goal": l.goal,
"pfp": l.pfp
"goal": l.goal
})
async def loop(self):
@ -60,5 +61,6 @@ class TournamentGame:
self.startGame()
else:
if(self.game.winner != None):
print("game ended, winner is", self.game.pWinner.socket.username)
self.winner = self.game.pWinner
await asyncio.sleep(1)

View File

@ -6,16 +6,25 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/04 17:16:02 by tomoron #+# #+# #
# Updated: 2024/10/11 21:14:54 by tomoron ### ########.fr #
# Updated: 2024/10/14 20:25:16 by tomoron ### ########.fr #
# #
# **************************************************************************** #
from ...Tournament import Tournament
from ...GameSettings import GameSettings
async def tournamentStart(socket, content):
skinId = content.get("skinId", 0)
if(skinId < 0 or skinId >= GameSettings.nbSkins):
socket.sendError("Skin id out of range", 9033)
return;
goalId = content.get("goalId",0)
if(goalId < 0 or goalId >= GameSettings.nbGoals):
socket.sendError("Goal id out of range", 9039)
return;
if("code" in content and len(content["code"])):
if(content["code"] in Tournament.currentTournaments):
Tournament.currentTournaments[content["code"]].join(socket)
Tournament.currentTournaments[content["code"]].join(socket, skin, goal)
else:
socket.sync_send("tournament",{"action":0, "exist":False})
else:
@ -23,4 +32,4 @@ async def tournamentStart(socket, content):
if(nbBot < 0 or nbBot > 7):
socket.sendError("invalid number of bots", 9040)
return;
Tournament(socket, nbBot)
Tournament(socket, nbBot, skinId, goalId)

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/09 14:31:30 by tomoron #+# #+# #
# Updated: 2024/10/12 02:43:29 by tomoron ### ########.fr #
# Updated: 2024/10/14 20:33:31 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -148,8 +148,7 @@ class WebsocketHandler(AsyncWebsocketConsumer):
except json.JSONDecodeError:
self.sendError("Invalid JSON", 9002)
return
#try:
if(1):
try:
self.printDebug(jsonRequest, 0)
if (jsonRequest["type"] in typeRequest):
if (jsonRequest["type"] == "login" or jsonRequest["type"] == "create_account"):
@ -160,8 +159,8 @@ class WebsocketHandler(AsyncWebsocketConsumer):
await functionRequest[typeRequest.index(jsonRequest["type"])](self, jsonRequest["content"])
else:
self.sendError("Invalid type", 9004)
# except Exception as e:
# self.sendError("Invalid request", 9005, e)
except Exception as e:
self.sendError("Invalid request", 9005, e)
@multimethod
def sync_send(self, reqType : str, content:dict):