From a982ccd7043005e22a85b4b49cdb9b9ca2f4804b Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Thu, 7 Jan 2021 12:34:34 -0700 Subject: [PATCH] Implement fromJSON method --- server/src/objects/Game.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/server/src/objects/Game.ts b/server/src/objects/Game.ts index 413b3b2..0ff0ed5 100644 --- a/server/src/objects/Game.ts +++ b/server/src/objects/Game.ts @@ -130,10 +130,31 @@ export class Game { }; }; - public static fromJSON() { + public static fromJSON(host: Player, data: datastoreGame): Game { /** - * Converts a JSON object into a Game object + * Converts a JSON representation into a Game object */ + let game = new this(host, { id: data.id }); + + // Re-create the deck objects + game._questions = Deck.fromJSON(data.decks.questions); + game._objects = Deck.fromJSON(data.decks.objects); + + game.teams = data.teams.map(t => Team.fromJSON(t)); + + // Re-instantiate all the players from the game. + game.players.push(host) + for (var player of data.players) { + if (player.name !== host.name) { + player.host = false; + game.players.push(Player.fromJSON(player)); + }; + }; + + game._objectCard = data.objectCard; + game.object = data.object; + + return game; };