make winner setable by setter
This commit is contained in:
parent
369b279470
commit
df588af0c6
@ -24,6 +24,9 @@ export const Team = Object.freeze({
|
|||||||
* only implements the raising mechanics, and no actual game play.
|
* only implements the raising mechanics, and no actual game play.
|
||||||
*/
|
*/
|
||||||
export class Round extends EventTarget {
|
export class Round extends EventTarget {
|
||||||
|
/** The event triggered when the round is won. */
|
||||||
|
static winEvent= "roundWon";
|
||||||
|
|
||||||
/** 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. */
|
||||||
@ -115,25 +118,23 @@ export class Round extends EventTarget {
|
|||||||
return this.#winner;
|
return this.#winner;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check whether the round has been decided. */
|
/** Set the winner of the round.
|
||||||
get decided() {
|
|
||||||
return this.#winner !== null;
|
|
||||||
}
|
|
||||||
|
|
||||||
static victoryEvent = "roundWon";
|
|
||||||
|
|
||||||
/** A team has won the round.
|
|
||||||
*
|
*
|
||||||
* @param {Team} team The team that won the round.
|
* @param {Team} team The team that won the round.
|
||||||
*/
|
*/
|
||||||
won(team) {
|
set winner(team) {
|
||||||
if (team !== Team.We && team !== Team.They)
|
if (team !== Team.We && team !== Team.They)
|
||||||
throw new TypeError("only actual teams can win");
|
throw new TypeError("only actual teams can win");
|
||||||
if (this.decided)
|
if (this.decided)
|
||||||
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));
|
this.dispatchEvent(new CustomEvent(Round.winEvent));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check whether the round has been decided. */
|
||||||
|
get decided() {
|
||||||
|
return this.#winner !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check whether a team can raise.
|
/** Check whether a team can raise.
|
||||||
@ -164,12 +165,12 @@ export class Round extends EventTarget {
|
|||||||
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.won(Team.They);
|
this.winner = Team.They;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (team === Team.They && this.points >= this.#theyLimit) {
|
if (team === Team.They && this.points >= this.#theyLimit) {
|
||||||
this.won(Team.We);
|
this.winner = Team.We;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,7 @@ QUnit.module("models", function() {
|
|||||||
|
|
||||||
QUnit.test("immediate victory", function(assert) {
|
QUnit.test("immediate victory", function(assert) {
|
||||||
let round = new Round();
|
let round = new Round();
|
||||||
round.won(Team.We);
|
round.winner = Team.We;
|
||||||
assert.strictEqual(round.points, 2, "initial points");
|
assert.strictEqual(round.points, 2, "initial points");
|
||||||
assert.true(round.decided, "there is a winner");
|
assert.true(round.decided, "there is a winner");
|
||||||
assert.strictEqual(round.winner, Team.We, "correct winner");
|
assert.strictEqual(round.winner, Team.We, "correct winner");
|
||||||
@ -25,9 +25,9 @@ QUnit.module("models", function() {
|
|||||||
|
|
||||||
QUnit.test("multiple victories", function(assert) {
|
QUnit.test("multiple victories", function(assert) {
|
||||||
let round = new Round();
|
let round = new Round();
|
||||||
round.won(Team.They);
|
round.winner = Team.They;
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
round.won(Team.We);
|
round.winner = Team.We;
|
||||||
}, "victory cannot be stolen");
|
}, "victory cannot be stolen");
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -161,10 +161,10 @@ QUnit.module("models", function() {
|
|||||||
|
|
||||||
QUnit.test("victory causes event", function(assert) {
|
QUnit.test("victory causes event", function(assert) {
|
||||||
let round = new Round();
|
let round = new Round();
|
||||||
round.addEventListener(Round.victoryEvent, function() {
|
round.addEventListener(Round.winEvent, function() {
|
||||||
assert.step("event");
|
assert.step("event");
|
||||||
});
|
});
|
||||||
round.won(Team.We);
|
round.winner = Team.We;
|
||||||
assert.verifySteps(["event"], "event was triggered");
|
assert.verifySteps(["event"], "event was triggered");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user