From 6971a2b20c311bc7906a74494cc429ecff8bf5ca Mon Sep 17 00:00:00 2001 From: Adrian Wannenmacher Date: Mon, 9 Feb 2026 20:24:17 +0100 Subject: [PATCH] make round finishing trigger an event --- models/game.js | 17 ++++++++++++++++- models/game.test.js | 9 +++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/models/game.js b/models/game.js index 34be225..f42526d 100644 --- a/models/game.js +++ b/models/game.js @@ -3,7 +3,19 @@ import { Round, Team } from "./round.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. * @type {RoundResult[]} */ @@ -36,6 +48,7 @@ export default class Game { } constructor(value) { + super(); if (value === undefined || typeof value === "number") { if (typeof value === "number") this.#goal = value; @@ -131,6 +144,8 @@ export default class Game { Math.max(this.#goal - result.theirPoints, 2)); this.#currentRound.addEventListener( Round.winEvent, this.#boundRoundFinishedHandler); + } else { + this.dispatchEvent(new CustomEvent(Game.finishedEvent)); } } diff --git a/models/game.test.js b/models/game.test.js index 392d79c..aae39ea 100644 --- a/models/game.test.js +++ b/models/game.test.js @@ -333,5 +333,14 @@ QUnit.module("models", function() { deso.currentRound = null; 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"); + }); }); });