game: add: pladdle can move online with ping

This commit is contained in:
starnakin 2024-01-18 14:40:31 +01:00
parent 251e9b032a
commit b0861b0ec0
2 changed files with 21 additions and 11 deletions

View File

@ -118,23 +118,28 @@ class Game
if (index === -1)
return
this.players.splice(index, 1);
}
_receive_paddle_position(data)
_receive_update_paddle(data)
{
data.players.forEach((player_data) => {
let player = this.players.find((player) => player.id === player_data.id);
if (player === null)
return
player.update_pos(player_data.position, player_data.time);
})
console.log(data)
let player = this.players.find((player) => player.id === data.user_id);
if (player === null)
{
this._receive_player_join(data);
return;
}
player.update_pos(data.position.position, data.position.time);
}
_receive(data)
{
if (data.detail === "paddle_position")
this._receive_paddle_position(data);
if (data.detail === "update_paddle")
this._receive_update_paddle(data);
else if (data.detail === "update_ball")
this._update_ball(data);
else if (data.detail === "init_game")

View File

@ -11,13 +11,16 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ...transcendence.abstract.AbstractRoomMember import AbstractRoomMember
from .Game import Game
class Player(Spectator):
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:
def __init__(self, game: Game, 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.game: Game = game
self.position: Position = Position(0.5, 0)
self.nb_goal: int = 0
@ -93,7 +96,9 @@ class Player(Spectator):
self.position = new_position
if (invalid_pos):
self.game_member.send("update_paddle", self.to_dict())
self.send("update_paddle", self.to_dict())
self.game.broadcast("update_paddle", self.to_dict(), [self])
def to_dict(self):