game: add: defeated by forfeit
This commit is contained in:
@ -10,9 +10,10 @@ class Game
|
||||
{
|
||||
/**
|
||||
* @param {Client} client
|
||||
* @param {CallableFunction} update_goal
|
||||
* @param {CallableFunction} goal_handler
|
||||
* @param {CallableFunction} finish_handler
|
||||
*/
|
||||
constructor(client, id, update_goal)
|
||||
constructor(client, id, goal_handler, finish_handler)
|
||||
{
|
||||
/**
|
||||
* @type {Client}
|
||||
@ -27,7 +28,12 @@ class Game
|
||||
/**
|
||||
* @type {CallableFunction}
|
||||
*/
|
||||
this.update_goal = update_goal;
|
||||
this.goal_handler = goal_handler;
|
||||
|
||||
/**
|
||||
* @type {CallableFunction}
|
||||
*/
|
||||
this.finish_handler = finish_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -161,6 +167,16 @@ class Game
|
||||
this.ball.from_json(data);
|
||||
}
|
||||
|
||||
_receive_finish(data)
|
||||
{
|
||||
this.finish_handler(data)
|
||||
}
|
||||
|
||||
_receive_goal(data)
|
||||
{
|
||||
this.goal_handler(data)
|
||||
}
|
||||
|
||||
_receive(data)
|
||||
{
|
||||
if (data.detail === "update_paddle")
|
||||
@ -170,7 +186,9 @@ class Game
|
||||
else if (data.detail === "init_game")
|
||||
this._init_game(data);
|
||||
else if (data.detail === "goal")
|
||||
this.update_goal(data);
|
||||
this._receive_goal(data);
|
||||
else if (data.detail === "finish")
|
||||
this._receive_finish(data)
|
||||
}
|
||||
|
||||
_init_game(data)
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { client } from "../index.js";
|
||||
import { client, reloadView } from "../index.js";
|
||||
import { Game } from "../api/game/Game.js";
|
||||
import AbstractView from "./abstracts/AbstractView.js";
|
||||
import { MyPlayer } from "../api/game/MyPlayer.js";
|
||||
@ -9,11 +9,17 @@ export default class extends AbstractView
|
||||
constructor(params)
|
||||
{
|
||||
super(params, "Game");
|
||||
this.game = new Game(client, params.id, this.update_goal);
|
||||
this.game = new Game(client, params.id, this.on_goal, this.on_finish);
|
||||
this.keys_pressed = [];
|
||||
this.my_player = undefined;
|
||||
}
|
||||
|
||||
on_finish(data)
|
||||
{
|
||||
console.log(data)
|
||||
reloadView()
|
||||
}
|
||||
|
||||
keyReleaseHandler(event)
|
||||
{
|
||||
const idx = this.keys_pressed.indexOf(event.key);
|
||||
@ -51,7 +57,7 @@ export default class extends AbstractView
|
||||
/**
|
||||
* @param {Object} data
|
||||
*/
|
||||
update_goal(data)
|
||||
on_goal(data)
|
||||
{
|
||||
document.getElementById(`goal-${data.player}`).innerText = data.nb_goal;
|
||||
}
|
||||
|
Reference in New Issue
Block a user