1
0

make victory trigger an event

This commit is contained in:
Adrian Wannenmacher 2026-02-09 00:28:15 +01:00
parent c57b5c2ac0
commit 369b279470
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5
2 changed files with 17 additions and 3 deletions

View File

@ -23,13 +23,15 @@ export const Team = Object.freeze({
* the aim is to create a convenient score keeping system. Therefore this class * the aim is to create a convenient score keeping system. Therefore this class
* only implements the raising mechanics, and no actual game play. * only implements the raising mechanics, and no actual game play.
*/ */
export class Round { export class Round extends EventTarget {
/** The maximum the "we" team may raise to. */ /** The maximum the "we" team may raise to. */
#weLimit = 11; #weLimit = 11;
/** The maximum the "they" team may raise to. */ /** The maximum the "they" team may raise to. */
#theyLimit = 11; #theyLimit = 11;
constructor(value, theyLimit) { constructor(value, theyLimit) {
super();
if (value === undefined && theyLimit === undefined) { if (value === undefined && theyLimit === undefined) {
} else if (typeof value === "number" && typeof theyLimit === "number") { } else if (typeof value === "number" && typeof theyLimit === "number") {
if (value < this.#points) if (value < this.#points)
@ -118,6 +120,8 @@ export class Round {
return this.#winner !== null; return this.#winner !== null;
} }
static victoryEvent = "roundWon";
/** A team has won the round. /** A team has won the round.
* *
* @param {Team} team The team that won the round. * @param {Team} team The team that won the round.
@ -129,6 +133,7 @@ export class Round {
throw new Error("decided round cannot be won again"); throw new Error("decided round cannot be won again");
this.#winner = team; this.#winner = team;
this.dispatchEvent(new CustomEvent(Round.victoryEvent));
} }
/** Check whether a team can raise. /** Check whether a team can raise.
@ -159,12 +164,12 @@ export class Round {
if (!this.canRaise(team)) return; if (!this.canRaise(team)) return;
if (team === Team.We && this.points >= this.#weLimit) { if (team === Team.We && this.points >= this.#weLimit) {
this.#winner = Team.They; this.won(Team.They);
return; return;
} }
if (team === Team.They && this.points >= this.#theyLimit) { if (team === Team.They && this.points >= this.#theyLimit) {
this.#winner = Team.We; this.won(Team.We);
return; return;
} }

View File

@ -158,5 +158,14 @@ QUnit.module("models", function() {
deso.theyLimit = 11; deso.theyLimit = 11;
new Round(deso); new Round(deso);
}); });
QUnit.test("victory causes event", function(assert) {
let round = new Round();
round.addEventListener(Round.victoryEvent, function() {
assert.step("event");
});
round.won(Team.We);
assert.verifySteps(["event"], "event was triggered");
});
}); });
}); });