switch round over to a general change event
This commit is contained in:
parent
624063b91e
commit
3bf6cc52ab
@ -60,7 +60,7 @@ export default class Game extends EventTarget {
|
|||||||
|
|
||||||
this.#currentRound = new Round(this.#goal, this.#goal);
|
this.#currentRound = new Round(this.#goal, this.#goal);
|
||||||
this.#currentRound.addEventListener(
|
this.#currentRound.addEventListener(
|
||||||
Round.winEvent, this.#boundRoundFinishedHandler);
|
Round.EVENT_CHANGE, this.#boundHandleRoundChange);
|
||||||
} else if (typeof value === "object") {
|
} else if (typeof value === "object") {
|
||||||
this.#fromStruct(value);
|
this.#fromStruct(value);
|
||||||
} else {
|
} else {
|
||||||
@ -119,10 +119,11 @@ export default class Game extends EventTarget {
|
|||||||
return {winner, points, ourPoints, theirPoints};
|
return {winner, points, ourPoints, theirPoints};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handle it when the current round is finished. */
|
/** Handle changes to the current round. */
|
||||||
#handleRoundFinished() {
|
#handleRoundChange() {
|
||||||
|
if (this.#currentRound.decided) {
|
||||||
this.#currentRound.removeEventListener(
|
this.#currentRound.removeEventListener(
|
||||||
Round.winEvent, this.#boundRoundFinishedHandler);
|
Round.EVENT_CHANGE, this.#boundHandleRoundChange);
|
||||||
this.#rounds.push(
|
this.#rounds.push(
|
||||||
new RoundResult(this.#currentRound.points, this.#currentRound.winner));
|
new RoundResult(this.#currentRound.points, this.#currentRound.winner));
|
||||||
this.#currentRound = null;
|
this.#currentRound = null;
|
||||||
@ -134,14 +135,17 @@ export default class Game extends EventTarget {
|
|||||||
Math.max(this.#goal - result.ourPoints, 2),
|
Math.max(this.#goal - result.ourPoints, 2),
|
||||||
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.EVENT_CHANGE, this.#boundHandleRoundChange);
|
||||||
} else {
|
} else {
|
||||||
this.dispatchEvent(new CustomEvent(Game.finishedEvent));
|
this.dispatchEvent(new CustomEvent(Game.finishedEvent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** #handleRoundFinished, but bound to this instance. */
|
|
||||||
#boundRoundFinishedHandler = this.#handleRoundFinished.bind(this);
|
}
|
||||||
|
|
||||||
|
/** #handleRoundChange, but bound to this instance. */
|
||||||
|
#boundHandleRoundChange = this.#handleRoundChange.bind(this);
|
||||||
|
|
||||||
/** Export the data of this `Game` as a plain JS object with fields.
|
/** Export the data of this `Game` as a plain JS object with fields.
|
||||||
*
|
*
|
||||||
@ -188,8 +192,11 @@ export default class Game extends EventTarget {
|
|||||||
if (value.currentRound === null)
|
if (value.currentRound === null)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"struct of ongoing game must contain current round");
|
"struct of ongoing game must contain current round");
|
||||||
else
|
else {
|
||||||
this.#currentRound = new Round(value.currentRound);
|
this.#currentRound = new Round(value.currentRound);
|
||||||
|
this.#currentRound.addEventListener(
|
||||||
|
Round.EVENT_CHANGE, this.#boundHandleRoundChange);
|
||||||
|
}
|
||||||
} else if (value.currentRound !== null)
|
} else if (value.currentRound !== null)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"struct of finished game must not contain current round");
|
"struct of finished game must not contain current round");
|
||||||
|
|||||||
@ -35,8 +35,8 @@ export const Team = Object.freeze({
|
|||||||
* Note that round points are positive, players want to accumulate them.
|
* Note that round points are positive, players want to accumulate them.
|
||||||
*/
|
*/
|
||||||
export class Round extends EventTarget {
|
export class Round extends EventTarget {
|
||||||
/** The event triggered when the round is won. */
|
/** The event triggered when something about the round changes. */
|
||||||
static winEvent= "roundWon";
|
static get EVENT_CHANGE() { return "wb:round:change"; }
|
||||||
|
|
||||||
/** The maximum the "we" team may raise to. */
|
/** The maximum the "we" team may raise to. */
|
||||||
#ourLimit = 11;
|
#ourLimit = 11;
|
||||||
@ -100,7 +100,7 @@ export class Round extends EventTarget {
|
|||||||
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.winEvent));
|
this.dispatchEvent(new CustomEvent(Round.EVENT_CHANGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check whether the round has been decided. */
|
/** Check whether the round has been decided. */
|
||||||
@ -147,6 +147,7 @@ export class Round extends EventTarget {
|
|||||||
|
|
||||||
this.#raisedLast = team;
|
this.#raisedLast = team;
|
||||||
this.#points += 1;
|
this.#points += 1;
|
||||||
|
this.dispatchEvent(new CustomEvent(Round.EVENT_CHANGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Export the data of this `Round` as a plain JS object with fields.
|
/** Export the data of this `Round` as a plain JS object with fields.
|
||||||
|
|||||||
@ -101,13 +101,32 @@ export default 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.winEvent, function() {
|
round.addEventListener(Round.EVENT_CHANGE, function() {
|
||||||
assert.step("event");
|
assert.step("event");
|
||||||
});
|
});
|
||||||
round.winner = Team.We;
|
round.winner = Team.We;
|
||||||
assert.verifySteps(["event"], "event was triggered");
|
assert.verifySteps(["event"], "event was triggered");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test("raising causes event", function(assert) {
|
||||||
|
let round = new Round();
|
||||||
|
round.addEventListener(Round.EVENT_CHANGE, function() {
|
||||||
|
assert.step("event");
|
||||||
|
});
|
||||||
|
round.raise(Team.We);
|
||||||
|
round.raise(Team.They);
|
||||||
|
assert.verifySteps(["event", "event"], "events were triggered");
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test("winning through raising causes event", function(assert) {
|
||||||
|
let round = new Round(2, 2);
|
||||||
|
round.addEventListener(Round.EVENT_CHANGE, function() {
|
||||||
|
assert.step("event");
|
||||||
|
});
|
||||||
|
round.raise(Team.We);
|
||||||
|
assert.verifySteps(["event"], "event was triggered");
|
||||||
|
});
|
||||||
|
|
||||||
QUnit.test("toStruct - unfinished", function(assert) {
|
QUnit.test("toStruct - unfinished", function(assert) {
|
||||||
let round = new Round();
|
let round = new Round();
|
||||||
let struct = round.toStruct();
|
let struct = round.toStruct();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user