Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
commit
e9bc43c650
@ -97,15 +97,9 @@ export class AGame extends AExchangeable
|
|||||||
*/
|
*/
|
||||||
send(data)
|
send(data)
|
||||||
{
|
{
|
||||||
console.log(this._socket)
|
|
||||||
if (this._socket === undefined || this._socket.readyState !== WebSocket.OPEN)
|
if (this._socket === undefined || this._socket.readyState !== WebSocket.OPEN)
|
||||||
{
|
return;
|
||||||
console.log("bozo pas confirme")
|
this._socket.send(data);
|
||||||
return ;
|
|
||||||
}
|
|
||||||
console.log("j'essaye de send")
|
|
||||||
this._socket.send(data);
|
|
||||||
console.log("j'ai passer le send")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async join()
|
async join()
|
||||||
|
@ -2,6 +2,7 @@ import { AExchangeable } from "../../AExchangable.js";
|
|||||||
import { PongGame } from "./PongGame.js";
|
import { PongGame } from "./PongGame.js";
|
||||||
import { renderCube} from "../../../3D/cube.js"
|
import { renderCube} from "../../../3D/cube.js"
|
||||||
import { Position } from "./Position.js";
|
import { Position } from "./Position.js";
|
||||||
|
import { Point } from "./Point.js";
|
||||||
|
|
||||||
export class PongBall extends AExchangeable
|
export class PongBall extends AExchangeable
|
||||||
{
|
{
|
||||||
@ -13,7 +14,7 @@ export class PongBall extends AExchangeable
|
|||||||
* @param {Number} speed
|
* @param {Number} speed
|
||||||
* @param {Number} size
|
* @param {Number} size
|
||||||
*/
|
*/
|
||||||
constructor(game, size, position = new Position(), angle, speed)
|
constructor(game, size, position = new Position(new Point(game.config.CENTER_X, game.config.CENTER_Y), 0), angle, speed)
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
@ -67,12 +67,12 @@ export class PongConfig extends AExchangeable
|
|||||||
/**
|
/**
|
||||||
* @type {Number}
|
* @type {Number}
|
||||||
*/
|
*/
|
||||||
this.CENTER_X;
|
this.MAP_CENTER_X;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Number}
|
* @type {Number}
|
||||||
*/
|
*/
|
||||||
this.CENTER_Y;
|
this.MAP_CENTER_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
async init()
|
async init()
|
||||||
|
@ -79,9 +79,9 @@ export class PongGame extends AGame
|
|||||||
|
|
||||||
let response_data = await response.json();
|
let response_data = await response.json();
|
||||||
|
|
||||||
console.log(response_data.players[0])
|
|
||||||
response_data.players.forEach((player_data) => {
|
response_data.players.forEach((player_data) => {
|
||||||
this.players.push(new PongPlayer(this.client, this));
|
let player = new PongPlayer(this.client, this)
|
||||||
|
this.players.push(player);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.import(response_data);
|
this.import(response_data);
|
||||||
@ -160,7 +160,7 @@ export class PongGame extends AGame
|
|||||||
else if (data.detail === "update_ball")
|
else if (data.detail === "update_ball")
|
||||||
this._updateBall(data);
|
this._updateBall(data);
|
||||||
else if (data.detail === "goal")
|
else if (data.detail === "goal")
|
||||||
await this._receiveGoal(data);
|
await this._goalHandler(data); // TODO fix: extract goal data in separate function
|
||||||
else if (data.detail === "finish")
|
else if (data.detail === "finish")
|
||||||
await this._finishHandler(data);
|
await this._finishHandler(data);
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ export class PongGame extends AGame
|
|||||||
{
|
{
|
||||||
let player = this.players.find((player) => player.id === data.user_id);
|
let player = this.players.find((player) => player.id === data.user_id);
|
||||||
|
|
||||||
player.from_json(data);
|
player.import(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateBall(data)
|
_updateBall(data)
|
||||||
|
@ -13,7 +13,7 @@ export class MyPlayer extends PongPlayer
|
|||||||
* @param {[Number]} score
|
* @param {[Number]} score
|
||||||
* @param {Position} position
|
* @param {Position} position
|
||||||
*/
|
*/
|
||||||
constructor(client, game, score, rail, position = new Position())
|
constructor(client, game, score, rail, position = new Position(0.5))
|
||||||
{
|
{
|
||||||
super(client, game, client.me.id, client.me.username, client.me.avatar, score, rail, position, true);
|
super(client, game, client.me.id, client.me.username, client.me.avatar, score, rail, position, true);
|
||||||
/**
|
/**
|
||||||
@ -62,22 +62,22 @@ export class MyPlayer extends PongPlayer
|
|||||||
*/
|
*/
|
||||||
updatePaddle(keys_pressed)
|
updatePaddle(keys_pressed)
|
||||||
{
|
{
|
||||||
let new_pos = this.position;
|
let new_location = this.position.location;
|
||||||
|
|
||||||
keys_pressed.forEach(key => {
|
keys_pressed.forEach(key => {
|
||||||
if (this.downKeys.includes(key))
|
if (this.downKeys.includes(key))
|
||||||
new_pos += this.game.config.paddle_speed_per_second_max * this.game.time.deltaTimeSecond();
|
new_location += this.game.config.PADDLE_SPEED_PER_SECOND_MAX * this.game.time.deltaTimeSecond();
|
||||||
if (this.upKeys.includes(key))
|
if (this.upKeys.includes(key))
|
||||||
new_pos -= this.game.config.paddle_speed_per_second_max * this.game.time.deltaTimeSecond();
|
new_location -= this.game.config.PADDLE_SPEED_PER_SECOND_MAX * this.game.time.deltaTimeSecond();
|
||||||
});
|
});
|
||||||
|
|
||||||
new_pos = Math.max(0 + this.game.config.paddle_ratio / 2, new_pos);
|
new_location = Math.max(0 + this.game.config.PADDLE_RATIO / 2, new_location);
|
||||||
new_pos = Math.min(1 - this.game.config.paddle_ratio / 2, new_pos);
|
new_location = Math.min(1 - this.game.config.PADDLE_RATIO / 2, new_location);
|
||||||
|
|
||||||
if (this.position === new_pos)
|
if (this.position.location === new_location)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.position = new_pos;
|
this.position.location = new_location;
|
||||||
|
|
||||||
this._sendPaddlePosition();
|
this._sendPaddlePosition();
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ export class PongPlayer extends APlayer
|
|||||||
* @param {String} avatar
|
* @param {String} avatar
|
||||||
* @param {Client} client
|
* @param {Client} client
|
||||||
*/
|
*/
|
||||||
constructor(client, game, id, username, avatar, score = [], rail = new Segment(game), position = new Position(), isConnected)
|
constructor(client, game, id, username, avatar, score = [], rail = new Segment(game), position = new Position(0.5), isConnected)
|
||||||
{
|
{
|
||||||
super(client, game, id, username, avatar, isConnected)
|
super(client, game, id, username, avatar, isConnected)
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ export class PongPlayer extends APlayer
|
|||||||
*
|
*
|
||||||
* @param {Number} new_position
|
* @param {Number} new_position
|
||||||
*/
|
*/
|
||||||
updatePos(new_position, time)
|
updatePos(new_position)
|
||||||
{
|
{
|
||||||
this.position = new_position;
|
this.position = new_position;
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ export class Position extends AExchangeable
|
|||||||
* @param {Point | Number} location
|
* @param {Point | Number} location
|
||||||
* @param {Number} time
|
* @param {Number} time
|
||||||
*/
|
*/
|
||||||
constructor(location, time)
|
constructor(location = new Point(), time)
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
/**
|
/**
|
||||||
|
@ -58,8 +58,8 @@ class Segment extends AExchangeable
|
|||||||
{
|
{
|
||||||
const size = this.game.config.BALL_SIZE * 2;
|
const size = this.game.config.BALL_SIZE * 2;
|
||||||
const sizex = this.len() / 2;
|
const sizex = this.len() / 2;
|
||||||
const posx = (this.start.x - this.game.config.CENTER_X);
|
const posx = (this.start.x - this.game.config.MAP_CENTER_X);
|
||||||
const posy = (this.start.y - this.game.config.CENTER_Y);
|
const posy = (this.start.y - this.game.config.MAP_CENTER_Y);
|
||||||
renderCube(ctx, posx, 0, posy, -this.angle(), sizex, size, size);
|
renderCube(ctx, posx, 0, posy, -this.angle(), sizex, size, size);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -32,8 +32,8 @@ export class Wall extends Segment
|
|||||||
{
|
{
|
||||||
const size = this.game.config.BALL_SIZE * 2;
|
const size = this.game.config.BALL_SIZE * 2;
|
||||||
const sizeX = this.len() / 2;
|
const sizeX = this.len() / 2;
|
||||||
const posX = (this.start.x - this.game.config.CENTER_X);
|
const posX = (this.start.x - this.game.config.MAP_CENTER_X);
|
||||||
const posY = (this.start.y - this.game.config.CENTER_Y);
|
const posY = (this.start.y - this.game.config.MAP_CENTER_Y);
|
||||||
renderCube(ctx, posX, 0, posY, -this.angle(), sizeX, size, size);
|
renderCube(ctx, posX, 0, posY, -this.angle(), sizeX, size, size);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -12,15 +12,18 @@ from .objects.tictactoe.TicTacToePlayer import TicTacToePlayer
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from .objects.pong.PongPlayer import PongPlayer
|
||||||
|
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .objects.pong.PongSpectator import PongSpectator
|
from .objects.pong.PongSpectator import PongSpectator
|
||||||
from .objects.pong.PongPlayer import PongPlayer
|
|
||||||
from .objects.pong.PongGame import PongGame
|
from .objects.pong.PongGame import PongGame
|
||||||
|
|
||||||
from .objects.tictactoe.TicTacToeGame import TicTacToeGame
|
from .objects.tictactoe.TicTacToeGame import TicTacToeGame
|
||||||
from .objects.tictactoe.TicTacToeSpectator import TicTacToeSpectator
|
from .objects.tictactoe.TicTacToeSpectator import TicTacToeSpectator
|
||||||
|
from .objects.tictactoe.TicTacToePlayer import TicTacToePlayer
|
||||||
|
|
||||||
game_manager: GameManager = GameManager()
|
game_manager: GameManager = GameManager()
|
||||||
|
|
||||||
@ -98,4 +101,5 @@ class PongWebSocket(WebsocketConsumer):
|
|||||||
|
|
||||||
data: dict = json.loads(text_data)
|
data: dict = json.loads(text_data)
|
||||||
|
|
||||||
self.member.receive(data)
|
if (isinstance(self.member, PongPlayer)):
|
||||||
|
self.member.receive(data)
|
@ -20,7 +20,6 @@ class Point:
|
|||||||
return dist((point.x, point.y), (self.x, self.y))
|
return dist((point.x, point.y), (self.x, self.y))
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
|
|
||||||
return Point(self.x, self.y)
|
return Point(self.x, self.y)
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
|
@ -47,12 +47,16 @@ class PongPlayer(APlayer):
|
|||||||
|
|
||||||
def update_position(self, data: dict):
|
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):
|
new_position.location = position_dict.get("location")
|
||||||
self.send_error("missing new_position")
|
if (new_position.location is None):
|
||||||
|
self.send_error("missing location")
|
||||||
return
|
return
|
||||||
|
|
||||||
new_position.time = data.get("time")
|
new_position.time = data.get("time")
|
||||||
@ -65,9 +69,9 @@ class PongPlayer(APlayer):
|
|||||||
self.game_member.send_error("time error")
|
self.game_member.send_error("time error")
|
||||||
return
|
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
|
time_difference: float = (new_position.time - self.position.time) / 1000
|
||||||
|
|
||||||
@ -76,19 +80,19 @@ class PongPlayer(APlayer):
|
|||||||
new_position_verified: Position = new_position.copy()
|
new_position_verified: Position = new_position.copy()
|
||||||
|
|
||||||
if (distance > max_distance):
|
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.location = max(new_position_verified.location, config.PADDLE_POSITION_MIN)
|
||||||
new_position_verified.position = min(new_position_verified.position, config.PADDLE_POSITION_MAX)
|
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.game.update_player(self)
|
||||||
|
|
||||||
self.position = new_position
|
self.position.location = new_position.location
|
||||||
|
|
||||||
if (invalid_pos):
|
if (invalid_pos):
|
||||||
self.send("update_player", self.to_dict())
|
self.send("update_player", self.to_dict())
|
||||||
@ -116,7 +120,7 @@ class PongPlayer(APlayer):
|
|||||||
"username": self.username,
|
"username": self.username,
|
||||||
"id": self.user_id,
|
"id": self.user_id,
|
||||||
"position": self.position.to_dict(),
|
"position": self.position.to_dict(),
|
||||||
"score": [*self.score],
|
"score": self.score,
|
||||||
|
|
||||||
"rail": self.rail.to_dict(),
|
"rail": self.rail.to_dict(),
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ from .Ball import Ball
|
|||||||
class PongSpectator(ASpectator):
|
class PongSpectator(ASpectator):
|
||||||
|
|
||||||
def __init__(self, user_id: int, socket: WebsocketConsumer, game: PongGame):
|
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
|
self.game: PongGame = game
|
||||||
|
|
||||||
def send_paddle(self, player: PongPlayer):
|
def send_paddle(self, player: PongPlayer):
|
||||||
|
@ -8,7 +8,10 @@ class Position:
|
|||||||
self.location: float = location
|
self.location: float = location
|
||||||
|
|
||||||
def copy(self):
|
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):
|
def to_dict(self):
|
||||||
|
|
||||||
|
@ -253,7 +253,7 @@ async def update_ball(game: PongGame, impact_data: dict) -> None:
|
|||||||
await asyncio.sleep(0.1) # delay to create frontend animation
|
await asyncio.sleep(0.1) # delay to create frontend animation
|
||||||
game.ball.reset()
|
game.ball.reset()
|
||||||
else:
|
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())
|
await SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict())
|
||||||
|
|
||||||
@ -288,8 +288,9 @@ async def render(game: PongGame):
|
|||||||
routine_ball.cancel()
|
routine_ball.cancel()
|
||||||
routine_players.cancel()
|
routine_players.cancel()
|
||||||
return
|
return
|
||||||
await asyncio.sleep(0.3)
|
await asyncio.sleep(0.05)
|
||||||
|
|
||||||
def routine(game: PongGame):
|
def routine(game: PongGame):
|
||||||
|
|
||||||
asyncio.run(render(game))
|
asyncio.run(render(game))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user