add anti cheat on server side

This commit is contained in:
2024-10-22 15:37:57 +02:00
parent 869c00116b
commit 9164f1a350
5 changed files with 58 additions and 21 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 03:31:32 by tomoron ### ########.fr # # Updated: 2024/10/22 14:53:55 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -111,7 +111,7 @@ class Ball:
def getTimeUntilWallColision(self, limitNeg, limitPos, position, velocity): def getTimeUntilWallColision(self, limitNeg, limitPos, position, velocity):
if(not velocity): if(not velocity):
return(-1) return(-1)
limit = GameSettings.limits[limitNeg] if velocity < 0 else GameSettings.limits[limitPos] limit = GameSettings.mapLimits[limitNeg] + GameSettings.ballRadius if velocity < 0 else GameSettings.mapLimits[limitPos] - GameSettings.ballRadius
wallDistance = max(limit, position) - min(limit, position) wallDistance = max(limit, position) - min(limit, position)
colision_time = wallDistance / abs(velocity) colision_time = wallDistance / abs(velocity)
return(colision_time) return(colision_time)
@ -215,13 +215,13 @@ class Ball:
async def update(self, delta, p1, p2, p1Hit = False): async def update(self, delta, p1, p2, p1Hit = False):
self.pos[0] += (delta * self.vel[0]) self.pos[0] += (delta * self.vel[0])
self.pos[1] += (delta * self.vel[1]) self.pos[1] += (delta * self.vel[1])
if(self.pos[1] <= GameSettings.limits["back"] + 0.001 or self.pos[1] >= GameSettings.limits["front"] - 0.001): if(self.pos[1] <= GameSettings.mapLimits["back"] + GameSettings.ballRadius + 0.001 or self.pos[1] >= (GameSettings.mapLimits["front"] - GameSettings.ballRadius) - 0.001):
player = p2.pos if self.pos[1] < 0 else p1.pos player = p2.pos if self.pos[1] < 0 else p1.pos
if(self.pos[1] > 0 and p1Hit): if(self.pos[1] > 0 and p1Hit):
return(1) return(1)
playerDistance = self.getPlayerDistance(player, self.pos) playerDistance = self.getPlayerDistance(player, self.pos)
if(playerDistance >= -(GameSettings.playerLength / 2) and playerDistance <= GameSettings.playerLength / 2 and player["up"] == self.up): if(playerDistance >= -(GameSettings.ballPlayerLength / 2) and playerDistance <= GameSettings.ballPlayerLength / 2 and player["up"] == self.up):
self.vel[0] = -((self.speed * 0.80) * (playerDistance / (GameSettings.playerLength / 2))) self.vel[0] = -((self.speed * 0.80) * (playerDistance / (GameSettings.ballPlayerLength / 2)))
self.vel[1] = self.speed - abs(self.vel[0]) self.vel[1] = self.speed - abs(self.vel[0])
if(self.pos[1] > 0): if(self.pos[1] > 0):
self.vel[1] = -self.vel[1] self.vel[1] = -self.vel[1]
@ -233,7 +233,7 @@ class Ball:
return(1 if self.pos[1] < 0 else 2) return(1 if self.pos[1] < 0 else 2)
else: else:
return(0) return(0)
elif(self.pos[0] <= GameSettings.limits["left"] + 0.001 or self.pos[0] >= GameSettings.limits["right"] - 0.001): elif(self.pos[0] <= GameSettings.mapLimits["left"] + GameSettings.ballRadius + 0.001 or self.pos[0] >= (GameSettings.mapLimits["right"] - GameSettings.ballRadius) - 0.001):
self.vel[0] = -self.vel[0] self.vel[0] = -self.vel[0]
elif(self.checkWallsColision(self.pos)): elif(self.checkWallsColision(self.pos)):
self.vel[1] = -self.vel[1] self.vel[1] = -self.vel[1]

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ # # By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/19 18:29:36 by tomoron #+# #+# # # Created: 2024/10/19 18:29:36 by tomoron #+# #+# #
# Updated: 2024/10/22 01:19:12 by tomoron ### ########.fr # # Updated: 2024/10/22 14:39:52 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -38,6 +38,9 @@ class Bot(Player):
def isTournamentReady(self): def isTournamentReady(self):
return(True); return(True);
def checkMovement(self, newPos):
return(newPos)
def createTempBall(self): def createTempBall(self):
res = Ball() res = Ball()
res.setObstacles(self.game.obstacles) res.setObstacles(self.game.obstacles)
@ -58,7 +61,6 @@ class Bot(Player):
pos += offset * (GameSettings.playerLength / 2) pos += offset * (GameSettings.playerLength / 2)
return({"pos":pos, "up":up}) return({"pos":pos, "up":up})
async def getExpectedPos(self): async def getExpectedPos(self):
tempBall = self.createTempBall() tempBall = self.createTempBall()
hit = 0 hit = 0

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/19 22:27:23 by tomoron ### ########.fr # # Updated: 2024/10/22 15:34:32 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -190,10 +190,14 @@ class Game:
def move(self, socket, pos, up): def move(self, socket, pos, up):
opponent = self.p1.socket if socket != self.p1.socket else self.p2.socket opponent = self.p1.socket if socket != self.p1.socket else self.p2.socket
if(socket == self.p1.socket): if(socket == self.p1.socket):
self.p1.pos["pos"] = pos self.p1.pos["pos"] = self.p1.checkMovement(pos)
if(self.p1.pos["pos"] != pos):
self.p1.socket.sync_send("game",{"action":3, "pos":self.p1.pos["pos"], "up":up, "is_opponent":False})
self.p1.pos["up"] = up self.p1.pos["up"] = up
else: else:
self.p2.pos["pos"] = -pos self.p2.pos["pos"] = self.p2.checkMovement(-pos)
if(self.p2.pos["pos"] != -pos):
self.p2.socket.sync_send("game",{"action":3, "pos":self.p2.pos["pos"], "up":up, "is_opponent":False})
self.p2.pos["up"] = up self.p2.pos["up"] = up
if(opponent != None): if(opponent != None):
opponent.sync_send({"type":"game","content":{"action":3, "pos":-pos, "up":up, "is_opponent":True}}) opponent.sync_send({"type":"game","content":{"action":3, "pos":-pos, "up":up, "is_opponent":True}})

View File

@ -6,18 +6,19 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/06 16:33:56 by tomoron #+# #+# # # Created: 2024/10/06 16:33:56 by tomoron #+# #+# #
# Updated: 2024/10/22 01:30:44 by tomoron ### ########.fr # # Updated: 2024/10/22 15:15:02 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
class GameSettings: class GameSettings:
ballRadius = 0.15 ballRadius = 0.15
playerLength = 1 + (ballRadius * 4) playerLength = 1
limits = { ballPlayerLength = playerLength + (ballRadius * 4)
"left" : -3.5 + ballRadius, mapLimits = {
"right" : 3.5 - ballRadius, "left" : -3.5,
"back" : -6.25 + ballRadius, "right" : 3.5,
"front" : 6.25 - ballRadius "back" : -6.25,
"front" : 6.25
} }
mapLength = 13 mapLength = 13
startSpeed = 6 startSpeed = 6
@ -43,8 +44,11 @@ class GameSettings:
wallLength = 1 wallLength = 1
wallWidth = 0.05 wallWidth = 0.05
bounceSpeedIncrease = 0.2 bounceSpeedIncrease = 0.2
maxScore = 1 maxScore = 3
maxPlayerSpeed = 6 maxPlayerSpeed = 4
playerSpeedTolerance = 0.15
BotMovement = True BotMovement = True
maxTimePlayerWait = 10 maxTimePlayerWait = 10
OOBTolerance = 0.01

View File

@ -6,15 +6,19 @@
# 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/20 15:37:20 by tomoron ### ########.fr # # Updated: 2024/10/22 15:35:59 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
import time
from .GameSettings import GameSettings
class Player(): class Player():
def __init__(self, socket): def __init__(self, socket):
self.socket = socket self.socket = socket
self.ready = False self.ready = False
self.tournamentReady = False; self.tournamentReady = False;
self.lastMovement = time.time()
self.pos = {"pos":0, "up": False} self.pos = {"pos":0, "up": False}
self.skin = 0 self.skin = 0
self.goal = 0 self.goal = 0
@ -25,6 +29,29 @@ class Player():
def isTournamentReady(self): def isTournamentReady(self):
return(self.tournamentReady) return(self.tournamentReady)
def checkMovement(self, newPos):
deltaTime = (time.time() - self.lastMovement)
self.lastMovement = time.time()
leftLimit = GameSettings.mapLimits["left"] + (GameSettings.playerLength / 2)
rightLimit = GameSettings.mapLimits["right"] - (GameSettings.playerLength / 2)
if(newPos < leftLimit - GameSettings.OOBTolerance or newPos > rightLimit + GameSettings.OOBTolerance):
print("\033[31mplayer out of the map")
newPos = leftLimit if newPos < 0 else rightLimit
return(newPos)
print("speed :", abs(newPos - self.pos["pos"]) * (1 / deltaTime))
if(abs(newPos - self.pos["pos"]) * (1 / deltaTime) > GameSettings.maxPlayerSpeed + GameSettings.playerSpeedTolerance):
newMove = GameSettings.maxPlayerSpeed * deltaTime
if(newPos - self.pos["pos"] < 0):
newMove = -newMove
print("\033[31mplayer is too fast")
newPos = self.pos["pos"] + newMove
return(newPos)
print("no problem")
return(newPos)
def setGame(self, game): def setGame(self, game):
self.ready = False; self.ready = False;
self.tournamentReady = False; self.tournamentReady = False;