1
0

move game rule cloning to game constructor

The rules not changing after a game has started is a invariant of the
`Game` model, not the `Session` model. As such, it should enforce it
itself, instead of relying on its users.
This commit is contained in:
Adrian Wannenmacher 2026-03-10 19:19:05 +01:00
parent eb20849bd8
commit 0342f6742d
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5
4 changed files with 14 additions and 13 deletions

View File

@ -59,7 +59,7 @@ export default class Game extends EventTarget {
super();
if (value === undefined || value instanceof GameRules) {
if (value instanceof GameRules)
this.#rules = value;
this.#rules = new GameRules(value);
this.#currentRound = new Round(
this.#rules.raisingLimit(0), this.#rules.raisingLimit(0));

View File

@ -53,6 +53,18 @@ export default function() {
);
});
QUnit.test("rules stay static after construction", function(assert) {
let rules = new GameRules();
rules.goal = 15;
let game = new Game(rules);
assert.strictEqual(game.rules.goal, 15, "correct goal");
rules.goal = 17;
assert.strictEqual(game.rules.goal, 15, "games rules didn't change");
assert.notStrictEqual(game.rules.goal, rules.goal, "goals are different");
});
QUnit.test("single round played", function(assert) {
let game = new Game();
game.currentRound.winner = Team.We;

View File

@ -130,7 +130,7 @@ export default class Session extends EventTarget {
/** Add another round if there is no current one. */
anotherGame() {
if (this.#currentGame === null) {
this.#currentGame = new Game(new GameRules(this.#rules));
this.#currentGame = new Game(this.#rules);
this.#currentGame.addEventListener(
Game.EVENT_CHANGE, this.#boundHandleGameChange);
this.#changed();

View File

@ -60,17 +60,6 @@ export default function() {
assert.notStrictEqual(session.currentGame, null, "game in progress");
});
QUnit.test("session rule change doesn't affect games", function(assert) {
let session = new Session();
session.anotherGame();
session.rules.goal = 7;
assert.notStrictEqual(
session.currentGame.rules.goal,
session.rules.goal,
"game rules have been copied",
);
});
QUnit.test("single game finished", function(assert) {
let session = new Session();
session.anotherGame();