add goal id and other things idk

This commit is contained in:
2024-10-10 19:46:06 +02:00
parent 44866712ca
commit 0b58783565
9 changed files with 38 additions and 27 deletions

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ # # By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/06 03:24:10 by tomoron #+# #+# # # Created: 2024/10/06 03:24:10 by tomoron #+# #+# #
# Updated: 2024/10/10 01:25:42 by tomoron ### ########.fr # # Updated: 2024/10/10 03:31:32 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -20,15 +20,14 @@ class Ball:
self.obstacles = [] self.obstacles = []
def setStartVel(self, inv): def setStartVel(self, inv):
# self.speed = GameSettings.startSpeed self.speed = GameSettings.startSpeed
# self.vel[0] = self.speed * (random.randint(-50, 50) / 100) self.vel[0] = self.speed * (random.randint(-50, 50) / 100)
# self.vel[1] = self.speed - abs(self.vel[0]) self.vel[1] = self.speed - abs(self.vel[0])
# if(inv == 2): if(inv == 2):
# self.vel[1] = -self.vel[1] self.vel[1] = -self.vel[1]
self.vel = [0, -3]
def default(self): def default(self):
self.pos = [1, 5] self.pos = [0, 0]
self.up = False self.up = False
self.vel = [0, 0] self.vel = [0, 0]
self.speed = GameSettings.startSpeed self.speed = GameSettings.startSpeed
@ -70,7 +69,7 @@ class Ball:
return(None) return(None)
wallSide = (GameSettings.wallWidth / 2) + GameSettings.ballRadius wallSide = (GameSettings.wallWidth / 2) + GameSettings.ballRadius
if(self.vel[1] > 0): if(self.pos[1] < 0):
wallSide *= -1 wallSide *= -1
hitPos = (wallSide - offset) / slope hitPos = (wallSide - offset) / slope
relPos = wpos - hitPos relPos = wpos - hitPos

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ # # By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/05 03:54:20 by tomoron #+# #+# # # Created: 2024/10/05 03:54:20 by tomoron #+# #+# #
# Updated: 2024/10/10 02:24:08 by tomoron ### ########.fr # # Updated: 2024/10/10 19:25:56 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -27,6 +27,7 @@ class Bot(Player):
self.lastCalculated = {"pos":0, "up":False} self.lastCalculated = {"pos":0, "up":False}
self.objective = {"pos":0, "up": False} self.objective = {"pos":0, "up": False}
self.skin = 0 self.skin = 0
self.goal = 0
asyncio.create_task(self.updateLoop()) asyncio.create_task(self.updateLoop())
asyncio.create_task(self.goToObjectiveLoop()) asyncio.create_task(self.goToObjectiveLoop())
print("I am a bot, boop boop beep boop") print("I am a bot, boop boop beep boop")

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ # # By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/08 07:33:29 by tomoron #+# #+# # # Created: 2024/10/08 07:33:29 by tomoron #+# #+# #
# Updated: 2024/10/09 07:03:46 by tomoron ### ########.fr # # Updated: 2024/10/10 19:25:49 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

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/09 07:06:00 by tomoron ### ########.fr # # Updated: 2024/10/10 19:24:02 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -25,7 +25,7 @@ import math
class Game: class Game:
waitingForPlayer = None waitingForPlayer = None
def __init__(self, socket, withBot, skinId = 0, opponent = None): def __init__(self, socket, withBot, skinId = 0, goalId = 0, opponent = None):
self.p1 = None self.p1 = None
self.p2 = None self.p2 = None
self.started = False self.started = False
@ -34,6 +34,7 @@ class Game:
self.winner = None self.winner = None
self.gameStart = 0 self.gameStart = 0
self.gameTime = 0 self.gameTime = 0
self.withBot = withBot
self.ball = Ball() self.ball = Ball()
self.speed = GameSettings.startSpeed self.speed = GameSettings.startSpeed
@ -44,7 +45,7 @@ class Game:
self.opponentLock = opponent self.opponentLock = opponent
if(1 or withBot): if(1 or withBot):
self.p1 = Bot(self) self.p1 = Bot(self)
self.join(socket, skinId) self.join(socket, skinId, goalId)
elif(opponent != None): elif(opponent != None):
if(opponent not in socket.onlinePlayers): if(opponent not in socket.onlinePlayers):
return return
@ -52,15 +53,15 @@ class Game:
if (opponentGame != None and opponentGame.opponentLock != None and opponentGame.opponentLock == socket.id): if (opponentGame != None and opponentGame.opponentLock != None and opponentGame.opponentLock == socket.id):
socket.onlinePlayers[opponent].game.join(socket, skinId) socket.onlinePlayers[opponent].game.join(socket, skinId)
else: else:
self.join(socket, skinId) self.join(socket, skinId, goalId)
socket.onlinePlayers[opponent].sync_send({"type":"invitation","content":{"invitor":socket.id, "username":socket.username}}) socket.onlinePlayers[opponent].sync_send({"type":"invitation","content":{"invitor":socket.id, "username":socket.username}})
else: else:
if(Game.waitingForPlayer == None): if(Game.waitingForPlayer == None):
Game.waitingForPlayer = self Game.waitingForPlayer = self
socket.sync_send({"type":"game","content":{"action":0}}) socket.sync_send({"type":"game","content":{"action":0}})
self.join(socket, skinId) self.join(socket, skinId, goalId)
else: else:
Game.waitingForPlayer.join(socket, skinId) Game.waitingForPlayer.join(socket, skinId, goalId)
Game.waitingForPlayer = None Game.waitingForPlayer = None
def __del__(self): def __del__(self):
@ -95,12 +96,13 @@ class Game:
self.p2.socket.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() self.obstaclesInvLength()
def join(self, socket, skin = 0): def join(self, socket, skin = 0, goal = 0):
try: try:
if(self.p1 == None): if(self.p1 == None):
print("game created, set as player 1") print("game created, set as player 1")
self.p1 = Player(socket, self) self.p1 = Player(socket, self)
self.p1.skin = skin self.p1.skin = skin
self.p1.goal = goal
else: else:
if(self.opponentLock != None and self.opponentLock != socket.id): if(self.opponentLock != None and self.opponentLock != socket.id):
socket.sendError("You are not invited to this game", 9103) socket.sendError("You are not invited to this game", 9103)
@ -108,10 +110,11 @@ class Game:
print("joined game, set as player 2") print("joined game, set as player 2")
self.p2 = Player(socket, self) self.p2 = Player(socket, self)
self.p2.skin = skin self.p2.skin = skin
self.p2.goal = goal
if(self.p2 != None and self.p1 != None): if(self.p2 != None and self.p1 != None):
print("both players here, send opponent to both players") print("both players here, send opponent to both players")
self.p1.socket.sync_send({"type":"game", "content":{"action":1,"id":self.p2.socket.id,"username":self.p2.socket.username, "skin":self.p2.skin, 'pfpOpponent':self.p2.socket.pfp, 'pfpSelf':self.p1.socket.pfp}}) self.p1.socket.sync_send({"type":"game", "content":{"action":1,"id":self.p2.socket.id,"username":self.p2.socket.username, "skin":self.p2.skin, "goal":self.p2.goal, 'pfpOpponent':self.p2.socket.pfp, 'pfpSelf':self.p1.socket.pfp}})
self.p2.socket.sync_send({"type":"game", "content":{"action":1,"id":self.p1.socket.id,"username":self.p1.socket.username, "skin":self.p1.skin, 'pfpOpponent':self.p1.socket.pfp, 'pfpSelf':self.p2.socket.pfp}}) self.p2.socket.sync_send({"type":"game", "content":{"action":1,"id":self.p1.socket.id,"username":self.p1.socket.username, "skin":self.p1.skin, "goal":self.p1.goal, 'pfpOpponent':self.p1.socket.pfp, 'pfpSelf':self.p2.socket.pfp}})
except Exception as e: except Exception as e:
socket.sendError("invalid request", 9005, e) socket.sendError("invalid request", 9005, e)
@ -238,7 +241,8 @@ class Game:
self.p1.socket.game = None self.p1.socket.game = None
if(self.p2.socket.game == self): if(self.p2.socket.game == self):
self.p2.socket.game = None self.p2.socket.game = None
await self.saveResults() if(not self.withBot):
await self.saveResults()
del self del self
@sync_to_async @sync_to_async

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ # # By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/06 16:33:56 by tomoron #+# #+# # # Created: 2024/10/06 16:33:56 by tomoron #+# #+# #
# Updated: 2024/10/09 07:03:19 by tomoron ### ########.fr # # Updated: 2024/10/10 03:48:00 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -48,6 +48,7 @@ class GameSettings:
{id: 6, 'color': None, 'texture': '/static/img/skin/3.jpg'}, {id: 6, 'color': None, 'texture': '/static/img/skin/3.jpg'},
{id: 7, 'color': None, 'texture': '/static/img/skin/4.jpg'}, {id: 7, 'color': None, 'texture': '/static/img/skin/4.jpg'},
] ]
nbGoals = 4
wallLength = 1 wallLength = 1
wallWidth = 0.05 wallWidth = 0.05
bounceSpeedIncrease = 0.2 bounceSpeedIncrease = 0.2

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ # # By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/05 03:22:32 by tomoron #+# #+# # # Created: 2024/10/05 03:22:32 by tomoron #+# #+# #
# Updated: 2024/10/08 07:47:31 by tomoron ### ########.fr # # Updated: 2024/10/10 03:52:40 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -17,6 +17,7 @@ class Player():
self.ready = False self.ready = False
self.pos = {"pos":0, "up": False} self.pos = {"pos":0, "up": False}
self.skin = 0 self.skin = 0
self.goal = 0
def __del__(self): def __del__(self):
print("player destroy") print("player destroy")

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/05 04:31:00 by tomoron ### ########.fr # # Updated: 2024/10/10 06:12:28 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -36,7 +36,7 @@ class Tournament:
nbIter = 0 nbIter = 0
while(self.code in Tournament.currentTournaments): while(self.code in Tournament.currentTournaments):
if(nbIter == 100): if(nbIter == 100):
nbInter = 0 nbIter = 0
nbChar += 1 nbChar += 1
self.code = genString(nbChar, string.ascii_uppercase) self.code = genString(nbChar, string.ascii_uppercase)
nbIter += 1 nbIter += 1

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/06 16:39:52 by tomoron ### ########.fr # # Updated: 2024/10/10 03:52:54 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -25,5 +25,9 @@ async def start(socket, content):
if(skinId < 0 or skinId >= len(GameSettings.skins)): if(skinId < 0 or skinId >= len(GameSettings.skins)):
socket.sendError("Skin id out of range", 9033) socket.sendError("Skin id out of range", 9033)
return; return;
Game(socket, content.get("with_bot", False),skinId ,opponent) goalId = content.get("goalId",0)
if(goalId < 0 or goalId >= GameSettings.nbGoals):
socket.sendError("Goal id out of range", 9039)
return;
Game(socket, False,skinId, goalId,opponent)

View File

@ -40,6 +40,7 @@
- 9036 : already in a tournament - 9036 : already in a tournament
- 9037 : you're not in a tournament - 9037 : you're not in a tournament
- 9038 : missing message field - 9038 : missing message field
- 9039 : Goal id out of range
- 9100 : Action out of range - 9100 : Action out of range
- 9101 : No game started - 9101 : No game started