game now stops when someone reach the score of 5. server sends a action 10. server now sends username of the person who invited to a game.

This commit is contained in:
2024-09-28 02:16:47 +02:00
parent 97176cd3ae
commit b2bc0f2190
3 changed files with 37 additions and 8 deletions

View File

@ -6,10 +6,11 @@
# 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/09/27 18:21:08 by tomoron ### ########.fr # # Updated: 2024/09/28 02:15:53 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
from asgiref.sync import sync_to_async
import time import time
import json import json
import asyncio import asyncio
@ -59,6 +60,7 @@ class Game:
wallLength = 1 wallLength = 1
wallWidth = 0.05 wallWidth = 0.05
bounceSpeedIncrease = 0.2 bounceSpeedIncrease = 0.2
maxScore = 5
def __init__(self, socket, withBot, skinId = 0, opponent = None): def __init__(self, socket, withBot, skinId = 0, opponent = None):
self.p1 = None self.p1 = None
@ -68,6 +70,7 @@ class Game:
self.bot = withBot self.bot = withBot
self.started = False self.started = False
self.end = False self.end = False
slef.left = None
self.p1Pos = {"pos":0, "up": False} self.p1Pos = {"pos":0, "up": False}
self.p2Pos = {"pos":0, "up": False} self.p2Pos = {"pos":0, "up": False}
@ -94,7 +97,7 @@ class Game:
socket.onlinePlayers[opponent].game.join(socket, skinId) socket.onlinePlayers[opponent].game.join(socket, skinId)
else: else:
self.join(socket, skinId) self.join(socket, skinId)
socket.onlinePlayers[opponent].sync_send({"type":"invitation","content":{"invitor":socket.id}}) socket.onlinePlayers[opponent].sync_send({"type":"invitation","content":{"invitor":socket.id, "username":socket.username}})
else: else:
while(Game.waitingForPlayerLock): while(Game.waitingForPlayerLock):
time.sleep(0.05) time.sleep(0.05)
@ -175,6 +178,7 @@ class Game:
socket.game = None socket.game = None
if (socket == self.p1): if (socket == self.p1):
self.p1 = None self.p1 = None
self.left = 1
else: else:
self.p2 = None self.p2 = None
if(self.p1 != None): if(self.p1 != None):
@ -322,14 +326,26 @@ class Game:
def getPlayerDistance(self, player, ballPos): def getPlayerDistance(self, player, ballPos):
playerPos = player["pos"] playerPos = player["pos"]
return(playerPos - ballPos[0]) return(playerPos - ballPos[0])
def checkGameEndGoal(self):
if(self.score[0] < Game.maxScore and self.score[1] < Game.maxScore):
return(False)
winner = 1 if self.score[0] == Game.maxScore else 2
self.p1.sync_send({"action":"game","content":{"action":10,"won":winner==1}})
self.p2.sync_send({"action":"game","content":{"action":10,"won":winner==2}})
self.end = True
return(True)
async def scoreGoal(self, player): async def scoreGoal(self, player):
self.lastWin = player
print("a player suffured from a major skill issue") print("a player suffured from a major skill issue")
self.score[player-1] += 1 self.score[player-1] += 1
print("new score :", self.score) print("new score :", self.score)
self.p1.sync_send({"type":"game","content":{"action":6, "is_opponent": player == 2}}) 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.p2.sync_send({"type":"game","content":{"action":6, "is_opponent": player == 1}})
await asyncio.sleep(4.5) await asyncio.sleep(4.5)
if(self.checkGameEndGoal()):
return
self.prepareGame(True) self.prepareGame(True)
await asyncio.sleep(3) await asyncio.sleep(3)
self.prepareGame() self.prepareGame()
@ -385,7 +401,6 @@ class Game:
if(newBallPos[1] > 0): if(newBallPos[1] > 0):
velZ = -velZ velZ = -velZ
else: else:
self.lastWin = 1 if newBallPos[1] < 0 else 2
await self.scoreGoal(1 if newBallPos[1] < 0 else 2) await self.scoreGoal(1 if newBallPos[1] < 0 else 2)
return; return;
elif(newBallPos[0] <= Game.limits["left"] or newBallPos[0] >= Game.limits["right"]): elif(newBallPos[0] <= Game.limits["left"] or newBallPos[0] >= Game.limits["right"]):
@ -421,7 +436,14 @@ class Game:
self.prepareGame() self.prepareGame()
while(not self.end): while(not self.end):
await self.updateBall() await self.updateBall()
if(self.end):
return;
sleep_time = self.getSleepTime() sleep_time = self.getSleepTime()
print("sleep time : " , sleep_time) print("sleep time : " , sleep_time)
await asyncio.sleep(sleep_time) await asyncio.sleep(sleep_time)
print("game end") print("game end")
@sync_to_async
def saveGame(self, winner):
print("nope")

File diff suppressed because one or more lines are too long

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ # # By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/09/09 16:10:26 by tomoron #+# #+# # # Created: 2024/09/09 16:10:26 by tomoron #+# #+# #
# Updated: 2024/09/27 23:25:13 by tomoron ### ########.fr # # Updated: 2024/09/28 00:43:45 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -50,6 +50,9 @@ from .gameActions.ping import ping
# name : name of the jumper # name : name of the jumper
# #
# 9: pong # 9: pong
#
# 10: game end
# - won
#client actions (actions sent by the client) : #client actions (actions sent by the client) :
# 0 : start : starts a game # 0 : start : starts a game