switch game over to a general change event
This commit is contained in:
parent
3bf6cc52ab
commit
685359ebd9
@ -15,8 +15,8 @@ import RoundResult from "./round_result.js";
|
|||||||
* Note that game points are punitive, players want to avoid earning them.
|
* Note that game points are punitive, players want to avoid earning them.
|
||||||
*/
|
*/
|
||||||
export default class Game extends EventTarget {
|
export default class Game extends EventTarget {
|
||||||
/** The event triggered when the game is finished. */
|
/** The event triggered when something about the game changes. */
|
||||||
static finishedEvent = "gameFinished";
|
static get EVENT_CHANGE() { return "wb:game:change"; }
|
||||||
|
|
||||||
/** The finished rounds.
|
/** The finished rounds.
|
||||||
* @type {RoundResult[]}
|
* @type {RoundResult[]}
|
||||||
@ -68,6 +68,11 @@ export default class Game extends EventTarget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Check whether the game is finished. */
|
||||||
|
get decided() {
|
||||||
|
return this.#currentRound === null;
|
||||||
|
}
|
||||||
|
|
||||||
/** Get the results of the game. */
|
/** Get the results of the game. */
|
||||||
get result() {
|
get result() {
|
||||||
let ourPoints = 0;
|
let ourPoints = 0;
|
||||||
@ -136,12 +141,10 @@ export default class Game extends EventTarget {
|
|||||||
Math.max(this.#goal - result.theirPoints, 2));
|
Math.max(this.#goal - result.theirPoints, 2));
|
||||||
this.#currentRound.addEventListener(
|
this.#currentRound.addEventListener(
|
||||||
Round.EVENT_CHANGE, this.#boundHandleRoundChange);
|
Round.EVENT_CHANGE, this.#boundHandleRoundChange);
|
||||||
} else {
|
|
||||||
this.dispatchEvent(new CustomEvent(Game.finishedEvent));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.dispatchEvent(new CustomEvent(Game.EVENT_CHANGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** #handleRoundChange, but bound to this instance. */
|
/** #handleRoundChange, but bound to this instance. */
|
||||||
|
|||||||
@ -58,6 +58,7 @@ export default function() {
|
|||||||
game.currentRound.winner = Team.We;
|
game.currentRound.winner = Team.We;
|
||||||
|
|
||||||
assert.equal(game.rounds.length, 1, "one round played");
|
assert.equal(game.rounds.length, 1, "one round played");
|
||||||
|
assert.false(game.decided, "game is not decided");
|
||||||
assert.strictEqual(game.rounds[0].points, 2, "first round points");
|
assert.strictEqual(game.rounds[0].points, 2, "first round points");
|
||||||
assert.strictEqual(game.rounds[0].winner, Team.We, "first round winner");
|
assert.strictEqual(game.rounds[0].winner, Team.We, "first round winner");
|
||||||
assert.notStrictEqual(game.currentRound, null, "current round there");
|
assert.notStrictEqual(game.currentRound, null, "current round there");
|
||||||
@ -81,6 +82,7 @@ export default function() {
|
|||||||
game.currentRound.winner = Team.They;
|
game.currentRound.winner = Team.They;
|
||||||
|
|
||||||
assert.equal(game.rounds.length, 2, "two round played");
|
assert.equal(game.rounds.length, 2, "two round played");
|
||||||
|
assert.false(game.decided, "game is not decided");
|
||||||
assert.strictEqual(game.rounds[1].points, 3, "second round points");
|
assert.strictEqual(game.rounds[1].points, 3, "second round points");
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
game.rounds[1].winner, Team.They, "second round winner");
|
game.rounds[1].winner, Team.They, "second round winner");
|
||||||
@ -110,6 +112,31 @@ export default function() {
|
|||||||
|
|
||||||
assert.equal(game.rounds.length, 7, "seven rounds played");
|
assert.equal(game.rounds.length, 7, "seven rounds played");
|
||||||
assert.strictEqual(game.currentRound, null, "no further rounds");
|
assert.strictEqual(game.currentRound, null, "no further rounds");
|
||||||
|
assert.true(game.decided, "game is decided");
|
||||||
|
assert.deepEqual(
|
||||||
|
game.result,
|
||||||
|
{
|
||||||
|
winner: Team.We,
|
||||||
|
points: 1,
|
||||||
|
ourPoints: 12,
|
||||||
|
theirPoints: 2,
|
||||||
|
},
|
||||||
|
"final results",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test("regular victory after near tailor", function(assert) {
|
||||||
|
let game = new Game();
|
||||||
|
game.currentRound.winner = Team.We; // 2
|
||||||
|
game.currentRound.winner = Team.We; // 4
|
||||||
|
game.currentRound.winner = Team.We; // 6
|
||||||
|
game.currentRound.winner = Team.We; // 8
|
||||||
|
game.currentRound.winner = Team.We; // 10
|
||||||
|
game.currentRound.winner = Team.They; // 2
|
||||||
|
game.currentRound.winner = Team.We; // 12
|
||||||
|
|
||||||
|
assert.equal(game.rounds.length, 7, "seven rounds played");
|
||||||
|
assert.true(game.decided, "no further rounds");
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
game.result,
|
game.result,
|
||||||
{
|
{
|
||||||
@ -193,13 +220,14 @@ export default function() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("finished event", function(assert) {
|
QUnit.test("round change triggers event", function(assert) {
|
||||||
let game = new Game(2);
|
let game = new Game(3);
|
||||||
game.addEventListener(Game.finishedEvent, function() {
|
game.addEventListener(Game.EVENT_CHANGE, function() {
|
||||||
assert.step("event");
|
assert.step("event");
|
||||||
});
|
});
|
||||||
|
game.currentRound.raise(Team.We);
|
||||||
game.currentRound.winner = Team.They;
|
game.currentRound.winner = Team.They;
|
||||||
assert.verifySteps(["event"], "event was triggered");
|
assert.verifySteps(["event", "event"], "events were triggered");
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("toStruct - unfinished", function(assert) {
|
QUnit.test("toStruct - unfinished", function(assert) {
|
||||||
|
|||||||
@ -74,7 +74,7 @@ export default class Session {
|
|||||||
if (this.#currentGame === null) {
|
if (this.#currentGame === null) {
|
||||||
this.#currentGame = new Game(this.goal);
|
this.#currentGame = new Game(this.goal);
|
||||||
this.#currentGame.addEventListener(
|
this.#currentGame.addEventListener(
|
||||||
Game.finishedEvent, this.#boundGameFinishedHandler);
|
Game.EVENT_CHANGE, this.#boundHandleGameChange);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,16 +98,18 @@ export default class Session {
|
|||||||
return { ourPoints, theirPoints };
|
return { ourPoints, theirPoints };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handle it when the current game is finished. */
|
/** Handle changes to the current game. */
|
||||||
#gameFinishedHandler() {
|
#handleGameChange() {
|
||||||
this.#currentGame.removeEventListener(
|
if (this.#currentGame.decided) {
|
||||||
Game.finishedEvent, this.#boundGameFinishedHandler);
|
this.#currentGame.removeEventListener(
|
||||||
this.#games.push(this.#currentGame);
|
Game.EVENT_CHANGE, this.#boundHandleGameChange);
|
||||||
this.#currentGame = null;
|
this.#games.push(this.#currentGame);
|
||||||
|
this.#currentGame = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** #gameFinishedHandler, but bound to this instance. */
|
/** #handleGameChange, but bound to this instance. */
|
||||||
#boundGameFinishedHandler = this.#gameFinishedHandler.bind(this);
|
#boundHandleGameChange = this.#handleGameChange.bind(this);
|
||||||
|
|
||||||
constructor(value) {
|
constructor(value) {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user