game: add: goal statistic
This commit is contained in:
@ -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();
|
||||
|
Reference in New Issue
Block a user