game: core: recreate classes see other paddle
This commit is contained in:
@ -1,22 +1,106 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .. import config
|
||||
|
||||
class Player:
|
||||
from channels.generic.websocket import WebsocketConsumer
|
||||
|
||||
from .Position import Position
|
||||
from .Spectator import Spectator
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ...transcendence.abstract.AbstractRoomMember import AbstractRoomMember
|
||||
|
||||
class Player(Spectator):
|
||||
|
||||
def __init__(self, user_id, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y) -> None:
|
||||
self.user_id = user_id
|
||||
self.position = 0.5
|
||||
self.nb_goal = 0
|
||||
def __init__(self, user_id: int, socket: WebsocketConsumer, rail_start_x: float, rail_start_y: float, rail_stop_x: float, rail_stop_y: float) -> None:
|
||||
|
||||
super().__init__(user_id, socket)
|
||||
|
||||
self.position: Position = Position(0.5, 0)
|
||||
|
||||
self.nb_goal: int = 0
|
||||
|
||||
self.rail_start_x: float = rail_start_x
|
||||
self.rail_start_y: float = rail_start_y
|
||||
self.rail_stop_x: float = rail_stop_x
|
||||
self.rail_stop_y: float = rail_stop_y
|
||||
|
||||
def receive(self, data: dict):
|
||||
|
||||
detail: str = data.get("detail")
|
||||
|
||||
if (detail is None):
|
||||
return
|
||||
|
||||
if (detail == "update_my_paddle_pos"):
|
||||
self.update_position(data)
|
||||
|
||||
def send_error(self, error_message: str, error_data = {}):
|
||||
|
||||
data: dict = {
|
||||
"error_message": error_message
|
||||
}
|
||||
|
||||
data.update(error_data)
|
||||
|
||||
self.send("error", data)
|
||||
|
||||
def update_position(self, data: dict):
|
||||
|
||||
print(data)
|
||||
|
||||
new_position: Position = Position()
|
||||
|
||||
new_position.position: float = data.get("position")
|
||||
|
||||
if (new_position.position is None):
|
||||
self.send_error("missing new_position")
|
||||
return
|
||||
|
||||
new_position.time: float = data.get("time")
|
||||
|
||||
if (new_position.time is None):
|
||||
self.game_member.send_error("missing time")
|
||||
return
|
||||
|
||||
if (self.position.time > new_position.time):
|
||||
self.game_member.send_error("time error")
|
||||
return
|
||||
|
||||
distance: float = abs(self.position.position - new_position.position)
|
||||
|
||||
sign: int = 1 if self.position.position >= new_position.position else -1
|
||||
|
||||
time_difference: float = (new_position.time - self.position.time) / 1000
|
||||
|
||||
max_distance: float = config.PADDLE_SPEED_PER_SECOND_MAX * (time_difference) * config.PADDLE_SPEED_PER_SECOND_TOLERANCE
|
||||
|
||||
new_position_verified: Position = new_position.copy()
|
||||
|
||||
if (distance > max_distance):
|
||||
print(max_distance, distance, time_difference, self.position.position, new_position.position)
|
||||
new_position_verified.position = self.position.position + max_distance * sign
|
||||
|
||||
if (not config.PADDLE_POSITION_MIN <= new_position_verified.position <= 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)
|
||||
|
||||
invalid_pos: bool = new_position.position != new_position_verified.position
|
||||
|
||||
self.position = new_position
|
||||
|
||||
if (invalid_pos):
|
||||
self.game_member.send("update_paddle", self.to_dict())
|
||||
|
||||
self.rail_start_x = rail_start_x
|
||||
self.rail_start_y = rail_start_y
|
||||
self.rail_stop_x = rail_stop_x
|
||||
self.rail_stop_y = rail_stop_y
|
||||
|
||||
def to_dict(self):
|
||||
|
||||
data = {
|
||||
"user_id": self.user_id,
|
||||
"position": self.position,
|
||||
"position": self.position.to_dict(),
|
||||
"nb_goal": self.nb_goal,
|
||||
|
||||
"rail_start_x": self.rail_start_x,
|
||||
|
Reference in New Issue
Block a user