make round finishing trigger an event
This commit is contained in:
parent
5089bef0f7
commit
6971a2b20c
@ -3,7 +3,19 @@
|
|||||||
import { Round, Team } from "./round.js";
|
import { Round, Team } from "./round.js";
|
||||||
import RoundResult from "./round_result.js";
|
import RoundResult from "./round_result.js";
|
||||||
|
|
||||||
export default class Game {
|
/** A single game of watten.
|
||||||
|
*
|
||||||
|
* A game consists of several rounds, and continues until either team reaches
|
||||||
|
* a points goal.
|
||||||
|
*
|
||||||
|
* This class keeps track of individual rounds and their results, and sets up
|
||||||
|
* new ones until the game is finished. It also has a `results` property, that
|
||||||
|
* calculates who won and how many points they earned.
|
||||||
|
*/
|
||||||
|
export default class Game extends EventTarget {
|
||||||
|
/** The event triggered when the game is finished. */
|
||||||
|
static finishedEvent = "gameFinished";
|
||||||
|
|
||||||
/** The finished rounds.
|
/** The finished rounds.
|
||||||
* @type {RoundResult[]}
|
* @type {RoundResult[]}
|
||||||
*/
|
*/
|
||||||
@ -36,6 +48,7 @@ export default class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor(value) {
|
constructor(value) {
|
||||||
|
super();
|
||||||
if (value === undefined || typeof value === "number") {
|
if (value === undefined || typeof value === "number") {
|
||||||
if (typeof value === "number")
|
if (typeof value === "number")
|
||||||
this.#goal = value;
|
this.#goal = value;
|
||||||
@ -131,6 +144,8 @@ export default class Game {
|
|||||||
Math.max(this.#goal - result.theirPoints, 2));
|
Math.max(this.#goal - result.theirPoints, 2));
|
||||||
this.#currentRound.addEventListener(
|
this.#currentRound.addEventListener(
|
||||||
Round.winEvent, this.#boundRoundFinishedHandler);
|
Round.winEvent, this.#boundRoundFinishedHandler);
|
||||||
|
} else {
|
||||||
|
this.dispatchEvent(new CustomEvent(Game.finishedEvent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -333,5 +333,14 @@ QUnit.module("models", function() {
|
|||||||
deso.currentRound = null;
|
deso.currentRound = null;
|
||||||
new Game(deso);
|
new Game(deso);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test("finished event", function(assert) {
|
||||||
|
let game = new Game(2);
|
||||||
|
game.addEventListener(Game.finishedEvent, function() {
|
||||||
|
assert.step("event");
|
||||||
|
});
|
||||||
|
game.currentRound.winner = Team.They;
|
||||||
|
assert.verifySteps(["event"], "event was triggered");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user