game: core: change player to player.isconnected

befort: if a player is disconnected he doesn't have an object
after: he have un object with a socket == None
This commit is contained in:
2024-01-19 15:38:04 +01:00
parent 2f4496c9bc
commit 7cf13640a1
11 changed files with 132 additions and 79 deletions

View File

@ -96,35 +96,26 @@ class Game
{
let index = this.players.indexOf((player) => player.id === player_data.user_id);
if (index !== -1)
this.players.slice(index, 1);
this.players.push(new Player(player_data.user_id,
this,
player_data.rail_start_x,
player_data.rail_start_y,
player_data.rail_stop_x,
player_data.rail_stop_y,
player_data.nb_goal,
player_data.position.position
));
this.players[index].is_connected = true;
}
_receive_player_leave(player_data)
{
const user_id = player_data.user_id;
const index = this.players.indexOf(this.players.find(player => player.id === user_id));
let index = this.players.indexOf((player) => player.id === player_data.user_id);
if (index === -1)
return
this.players[index].is_connected = false;
}
this.players.splice(index, 1);
_receive_update_ball(data)
{
this.ball.position_x = data.position_x
this.ball.position_y = data.position_y
this.ball.position_x = data.position_x
this.ball.position_x = data.position_x
}
_receive_update_paddle(data)
{
console.log(data)
let player = this.players.find((player) => player.id === data.user_id);
if (player === null)
@ -132,16 +123,17 @@ class Game
this._receive_player_join(data);
return;
}
player.is_connected = data.is_connected;
player.update_pos(data.position.position, data.position.time);
}
_receive(data)
{
console.log(data)
if (data.detail === "update_paddle")
this._receive_update_paddle(data);
else if (data.detail === "update_ball")
this._update_ball(data);
this._receive_update_ball(data);
else if (data.detail === "init_game")
this._init_game(data)
else if (data.detail === "player_join")
@ -167,7 +159,17 @@ class Game
this.players = []
const players_data = data.players;
players_data.forEach((player_data) => {
this._receive_player_join(player_data)
this.players.push(new Player(player_data.user_id,
this,
player_data.rail_start_x,
player_data.rail_start_y,
player_data.rail_stop_x,
player_data.rail_stop_y,
player_data.nb_goal,
player_data.position.position,
player_data.is_connected,
));
});
this._inited = true;

View File

@ -5,7 +5,7 @@ class MyPlayer extends Player
{
constructor(client, game, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y, nb_goal, positon)
{
super(client.me.id, game, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y, nb_goal, positon);
super(client.me.id, game, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y, nb_goal, positon, true);
this.client = client;
}

View File

@ -1,8 +1,9 @@
class Player
{
constructor(id, game, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y, nb_goal, positon)
constructor(id, game, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y, nb_goal, positon, is_connected)
{
this.is_connected = is_connected;
this.id = id;
this.positon = positon;
this.nb_goal = nb_goal;
@ -27,7 +28,13 @@ class Player
}
draw(ctx)
{
{
if (this.is_connected === false)
{
ctx.moveTo(this.rail_start_x, this.rail_start_y);
ctx.lineTo(this.rail_stop_x, this.rail_stop_y);
return;
}
let paddle_pos_x = this.rail_start_x + this.diff_x * this.positon,
paddle_pos_y = this.rail_start_y + this.diff_y * this.positon;