Merge remote-tracking branch 'refs/remotes/origin/main'

This commit is contained in:
Namonay
2024-04-10 19:42:45 +02:00
15 changed files with 57 additions and 51 deletions

View File

@ -12,15 +12,18 @@ from .objects.tictactoe.TicTacToePlayer import TicTacToePlayer
import time
from .objects.pong.PongPlayer import PongPlayer
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .objects.pong.PongSpectator import PongSpectator
from .objects.pong.PongPlayer import PongPlayer
from .objects.pong.PongGame import PongGame
from .objects.tictactoe.TicTacToeGame import TicTacToeGame
from .objects.tictactoe.TicTacToeSpectator import TicTacToeSpectator
from .objects.tictactoe.TicTacToePlayer import TicTacToePlayer
game_manager: GameManager = GameManager()
@ -98,4 +101,5 @@ class PongWebSocket(WebsocketConsumer):
data: dict = json.loads(text_data)
self.member.receive(data)
if (isinstance(self.member, PongPlayer)):
self.member.receive(data)

View File

@ -20,7 +20,6 @@ class Point:
return dist((point.x, point.y), (self.x, self.y))
def copy(self):
return Point(self.x, self.y)
def to_dict(self) -> dict:

View File

@ -47,12 +47,16 @@ class PongPlayer(APlayer):
def update_position(self, data: dict):
new_position: Position = Position()
new_position: Position = Position(None)
new_position.location = data.get("position")
position_dict = data.get("position")
if (position_dict is None):
self.send_error("missing position")
return
if (new_position.position is None):
self.send_error("missing new_position")
new_position.location = position_dict.get("location")
if (new_position.location is None):
self.send_error("missing location")
return
new_position.time = data.get("time")
@ -65,9 +69,9 @@ class PongPlayer(APlayer):
self.game_member.send_error("time error")
return
distance: float = abs(self.position.position - new_position.position)
distance: float = abs(self.position.location - new_position.location)
sign: int = 1 if self.position.position >= new_position.position else -1
sign: int = 1 if self.position.location >= new_position.location else -1
time_difference: float = (new_position.time - self.position.time) / 1000
@ -76,19 +80,19 @@ class PongPlayer(APlayer):
new_position_verified: Position = new_position.copy()
if (distance > max_distance):
new_position_verified.position = self.position.position + max_distance * sign
new_position_verified.location = self.position.location + max_distance * sign
if (not config.PADDLE_POSITION_MIN <= new_position_verified.position <= config.PADDLE_POSITION_MAX):
if (not config.PADDLE_POSITION_MIN <= new_position_verified.location <= config.PADDLE_POSITION_MAX):
new_position_verified.position = max(new_position_verified.position, config.PADDLE_POSITION_MIN)
new_position_verified.position = min(new_position_verified.position, config.PADDLE_POSITION_MAX)
new_position_verified.location = max(new_position_verified.location, config.PADDLE_POSITION_MIN)
new_position_verified.location = min(new_position_verified.location, config.PADDLE_POSITION_MAX)
invalid_pos: bool = new_position.position != new_position_verified.position
invalid_pos: bool = new_position.location != new_position_verified.location
if (new_position != self.position):
if (new_position.location != self.position.location):
self.game.update_player(self)
self.position = new_position
self.position.location = new_position.location
if (invalid_pos):
self.send("update_player", self.to_dict())
@ -116,7 +120,7 @@ class PongPlayer(APlayer):
"username": self.username,
"id": self.user_id,
"position": self.position.to_dict(),
"score": [*self.score],
"score": self.score,
"rail": self.rail.to_dict(),

View File

@ -16,7 +16,7 @@ from .Ball import Ball
class PongSpectator(ASpectator):
def __init__(self, user_id: int, socket: WebsocketConsumer, game: PongGame):
super().__init__(user_id, socket)
super().__init__(user_id, socket, game)
self.game: PongGame = game
def send_paddle(self, player: PongPlayer):

View File

@ -8,7 +8,10 @@ class Position:
self.location: float = location
def copy(self):
return Position(self.location, self.time)
try:
return Position(self.location.copy(), self.time)
except:
return Position(self.location, self.time)
def to_dict(self):

View File

@ -253,7 +253,7 @@ async def update_ball(game: PongGame, impact_data: dict) -> None:
await asyncio.sleep(0.1) # delay to create frontend animation
game.ball.reset()
else:
game.ball.position = impact_data.get("impact")
game.ball.position.location = impact_data.get("impact")
await SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict())
@ -288,8 +288,9 @@ async def render(game: PongGame):
routine_ball.cancel()
routine_players.cancel()
return
await asyncio.sleep(0.3)
await asyncio.sleep(0.05)
def routine(game: PongGame):
asyncio.run(render(game))