extend session with ID
This commit is contained in:
parent
384aaf2fe7
commit
b2ab89a151
@ -4,6 +4,9 @@ import Game from "./game.js";
|
|||||||
import { Team } from "./round.js";
|
import { Team } from "./round.js";
|
||||||
|
|
||||||
export default class Session {
|
export default class Session {
|
||||||
|
/** The ID of this session. */
|
||||||
|
id = null;
|
||||||
|
|
||||||
/** The amout of points at which individual games are won.
|
/** The amout of points at which individual games are won.
|
||||||
*
|
*
|
||||||
* Only applies to new games.
|
* Only applies to new games.
|
||||||
@ -115,14 +118,19 @@ export default class Session {
|
|||||||
* 2. It can be stored using IndexedDB.
|
* 2. It can be stored using IndexedDB.
|
||||||
*/
|
*/
|
||||||
toStruct() {
|
toStruct() {
|
||||||
return {
|
let res = {
|
||||||
goal: this.#goal,
|
goal: this.#goal,
|
||||||
ourTeam: this.ourTeam,
|
ourTeam: this.ourTeam,
|
||||||
theirTeam: this.theirTeam,
|
theirTeam: this.theirTeam,
|
||||||
games: this.#games.map((g) => g.toStruct()),
|
games: this.#games.map((g) => g.toStruct()),
|
||||||
currentGame:
|
currentGame:
|
||||||
this.#currentGame !== null ? this.#currentGame.toStruct() : null,
|
this.#currentGame !== null ? this.#currentGame.toStruct() : null,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
if (this.id !== null)
|
||||||
|
res.id = this.id;
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read in an object created by `Session.toStruct` */
|
/** Read in an object created by `Session.toStruct` */
|
||||||
@ -130,6 +138,12 @@ export default class Session {
|
|||||||
if (typeof value !== "object")
|
if (typeof value !== "object")
|
||||||
throw new TypeError("struct must be an object");
|
throw new TypeError("struct must be an object");
|
||||||
|
|
||||||
|
if ("id" in value) {
|
||||||
|
if (typeof value.id !== "number")
|
||||||
|
throw new TypeError("if struct contains id, then it must be a number");
|
||||||
|
this.id = value.id;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof value.goal !== "number")
|
if (typeof value.goal !== "number")
|
||||||
throw new TypError("struct must contain goal as number");
|
throw new TypError("struct must contain goal as number");
|
||||||
if (!Number.isInteger(value.goal) || value.goal < 1)
|
if (!Number.isInteger(value.goal) || value.goal < 1)
|
||||||
|
|||||||
@ -123,6 +123,7 @@ export default function() {
|
|||||||
session.currentGame.currentRound.winner = Team.They;
|
session.currentGame.currentRound.winner = Team.They;
|
||||||
session.anotherGame();
|
session.anotherGame();
|
||||||
session.currentGame.currentRound.winner = Team.We;
|
session.currentGame.currentRound.winner = Team.We;
|
||||||
|
session.id = 15;
|
||||||
session.ourTeam = "This is us!";
|
session.ourTeam = "This is us!";
|
||||||
session.theirTeam = "This is them!";
|
session.theirTeam = "This is them!";
|
||||||
let struct = session.toStruct();
|
let struct = session.toStruct();
|
||||||
@ -133,6 +134,7 @@ export default function() {
|
|||||||
let unfinished = new Game(3);
|
let unfinished = new Game(3);
|
||||||
unfinished.currentRound.winner = Team.We;
|
unfinished.currentRound.winner = Team.We;
|
||||||
let expected = {
|
let expected = {
|
||||||
|
id: 15,
|
||||||
goal: 3,
|
goal: 3,
|
||||||
ourTeam: "This is us!",
|
ourTeam: "This is us!",
|
||||||
theirTeam: "This is them!",
|
theirTeam: "This is them!",
|
||||||
@ -150,6 +152,8 @@ export default function() {
|
|||||||
orig.theirTeam = "This is them!";
|
orig.theirTeam = "This is them!";
|
||||||
|
|
||||||
let copy = new Session(orig.toStruct());
|
let copy = new Session(orig.toStruct());
|
||||||
|
assert.strictEqual(copy.id, orig.id, "IDs match");
|
||||||
|
assert.strictEqual(copy.id, null, "copy ID is null");
|
||||||
assert.strictEqual(copy.goal, orig.goal, "goals match");
|
assert.strictEqual(copy.goal, orig.goal, "goals match");
|
||||||
assert.strictEqual(copy.ourTeam, orig.ourTeam, "our teams match");
|
assert.strictEqual(copy.ourTeam, orig.ourTeam, "our teams match");
|
||||||
assert.strictEqual(copy.theirTeam, orig.theirTeam, "their teams match");
|
assert.strictEqual(copy.theirTeam, orig.theirTeam, "their teams match");
|
||||||
@ -160,12 +164,15 @@ export default function() {
|
|||||||
assert.deepEqual(copy.result, orig.result, "results match");
|
assert.deepEqual(copy.result, orig.result, "results match");
|
||||||
|
|
||||||
orig.anotherGame();
|
orig.anotherGame();
|
||||||
|
orig.id = 15;
|
||||||
orig.currentGame.currentRound.raise(Team.They);
|
orig.currentGame.currentRound.raise(Team.They);
|
||||||
orig.currentGame.currentRound.winner = Team.We;
|
orig.currentGame.currentRound.winner = Team.We;
|
||||||
orig.anotherGame();
|
orig.anotherGame();
|
||||||
orig.currentGame.currentRound.winner = Team.They;
|
orig.currentGame.currentRound.winner = Team.They;
|
||||||
|
|
||||||
copy = new Session(orig.toStruct());
|
copy = new Session(orig.toStruct());
|
||||||
|
assert.strictEqual(copy.id, orig.id, "IDs match");
|
||||||
|
assert.strictEqual(copy.id, 15, "copy ID is correct");
|
||||||
assert.strictEqual(copy.games.length, 1, "single past game");
|
assert.strictEqual(copy.games.length, 1, "single past game");
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
copy.games.length, orig.games.length, "amount of past games");
|
copy.games.length, orig.games.length, "amount of past games");
|
||||||
@ -293,5 +300,55 @@ export default function() {
|
|||||||
};
|
};
|
||||||
assert.deepEqual(session.toStruct(), expected, "reexport matches");
|
assert.deepEqual(session.toStruct(), expected, "reexport matches");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test("fromStruct - v2 - new session", function(assert) {
|
||||||
|
let struct = {
|
||||||
|
id: 23,
|
||||||
|
goal: 3,
|
||||||
|
ourTeam: "",
|
||||||
|
theirTeam: "",
|
||||||
|
games: [],
|
||||||
|
currentGame: null,
|
||||||
|
};
|
||||||
|
let session = new Session(struct);
|
||||||
|
|
||||||
|
let expected = {
|
||||||
|
id: 23,
|
||||||
|
goal: 3,
|
||||||
|
ourTeam: "",
|
||||||
|
theirTeam: "",
|
||||||
|
games: [],
|
||||||
|
currentGame: null,
|
||||||
|
};
|
||||||
|
assert.deepEqual(session.toStruct(), expected, "reexport matches");
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test("fromStruct - v2 - finished & unfinished", function(assert) {
|
||||||
|
let finished = new Game(3);
|
||||||
|
finished.currentRound.raise(Team.We);
|
||||||
|
finished.currentRound.winner = Team.They;
|
||||||
|
let unfinished = new Game(3);
|
||||||
|
unfinished.currentRound.winner = Team.We;
|
||||||
|
|
||||||
|
let struct = {
|
||||||
|
id: 17,
|
||||||
|
goal: 3,
|
||||||
|
ourTeam: "This is us!",
|
||||||
|
theirTeam: "This is them!",
|
||||||
|
games: [ finished.toStruct() ],
|
||||||
|
currentGame: unfinished.toStruct(),
|
||||||
|
};
|
||||||
|
let session = new Session(struct);
|
||||||
|
|
||||||
|
let expected = {
|
||||||
|
id: 17,
|
||||||
|
goal: 3,
|
||||||
|
ourTeam: "This is us!",
|
||||||
|
theirTeam: "This is them!",
|
||||||
|
games: [ finished.toStruct() ],
|
||||||
|
currentGame: unfinished.toStruct(),
|
||||||
|
};
|
||||||
|
assert.deepEqual(session.toStruct(), expected, "reexport matches");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user