game: add: goal statistic

This commit is contained in:
2024-02-26 17:11:31 +01:00
parent 8f1979bd21
commit 066a2a9c81
15 changed files with 335 additions and 82 deletions

View File

@ -50,9 +50,18 @@ class Game
let response_data = await response.json();
/**
* @type {[Number]}
* @type {[Player]}
*/
this.players_id = response_data.players_id;
this.players = []
response_data.players.forEach(player_data => {
this.players.push(new Player(this,
player_data.player_id,
player_data.username,
player_data.score
)
);
});
/**
* @type {String}
@ -74,6 +83,16 @@ class Game
*/
this.winner_id = this.finished ? response_data.winner_id : undefined;
/**
* @type {Number}
*/
this.start_timestamp = response_data.start_timestamp;
/**
* @type {Number}
*/
this.stop_timestamp = response_data.stop_timestamp;
if (this.finished === true)
return 0;
@ -121,9 +140,8 @@ class Game
render(ctx)
{
if(ctx instanceof CanvasRenderingContext2D)
{
ctx.clearRect(0, 0, this.config.size_x, this.config.size_y);
}
this.draw_sides(ctx);
this.ball.render(ctx);
@ -167,17 +185,32 @@ class Game
this.ball.from_json(data);
}
_receive_finish(data)
async _receive_finish(data)
{
this.finish_handler(data)
await this.finish_handler(data)
}
_receive_goal(data)
async _receive_goal(data)
{
this.goal_handler(data)
/**
* @type { Player }
*/
let player_found;
this.players.forEach(player => {
if (data.player_id === player.id)
{
player_found = player
return;
}
})
player_found.score.push(data.timestamp);
await this.goal_handler(player_found);
}
_receive(data)
async _receive(data)
{
if (data.detail === "update_paddle")
this._receive_update_paddle(data);
@ -186,9 +219,9 @@ class Game
else if (data.detail === "init_game")
this._init_game(data);
else if (data.detail === "goal")
this._receive_goal(data);
await this._receive_goal(data);
else if (data.detail === "finish")
this._receive_finish(data)
await this._receive_finish(data)
}
_init_game(data)
@ -210,11 +243,10 @@ class Game
/**
* @type {[Player]}
*/
this.players = [];
const players_data = data.players;
players_data.forEach((player_data) => {
this.players.push(new Player(this).from_json(player_data));
});
for (let index = 0; index < players_data.length; index++) {
this.players[index].from_json(players_data[index]);
}
this._inited = true;
}
@ -237,9 +269,9 @@ class Game
this._socket = new WebSocket(url);
this._socket.onmessage = (event) => {
this._socket.onmessage = async (event) => {
const data = JSON.parse(event.data);
this._receive(data);
await this._receive(data);
};
return this.wait_init();

View File

@ -82,6 +82,11 @@ class GameConfig
*/
this.stroke_thickness = response_data.STROKE_THICKNESS;
/**
* @type {Number}
*/
this.game_max_score = response_data.GAME_MAX_SCORE;
return 0;
}
}

View File

@ -9,12 +9,12 @@ class MyPlayer extends Player
* @param {Client} client
* @param {Game} game
* @param {Segment} rail
* @param {Number} nb_goal
* @param {[Number]} score
* @param {Number} position
*/
constructor(client, game, rail, nb_goal, position)
constructor(client, game, score, rail, position)
{
super(game, client.me.id, client.me.username, rail, nb_goal, position, true);
super(game, client.me.id, client.me.username, score, rail, position, true);
/**
* @type {Client}
*/

View File

@ -8,12 +8,12 @@ class Player
* @param {Number} id
* @param {Game} game
* @param {Segment} rail
* @param {Number} nb_goal
* @param {[Number]} score
* @param {Number} position
* @param {Boolean} is_connected
* @param {String} username
*/
constructor(game, id, username, rail, nb_goal, position, is_connected)
constructor(game, id, username, score, rail, position, is_connected)
{
/**
* @type {Game}
@ -36,9 +36,9 @@ class Player
this.position = position;
/**
* @type {Number}
* @type {[Number]}
*/
this.nb_goal = nb_goal;
this.score = score;
/**
* @type {Segment}
@ -102,7 +102,7 @@ class Player
this.is_connected = data.is_connected;
this.id = data.user_id;
this.position = data.position.position ;
this.nb_goal = data.nb_goal;
this.score = data.score;
this.rail = this.rail.from_json(data.rail);
this.username = data.username;