From 01fea03112064326e63a29727b0c9626f8d15e46 Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Wed, 14 Dec 2022 13:07:04 +0100
Subject: [PATCH] Allow saving and restoring save games

---
 packages/game/src/js/Game.js    | 28 ++++++++++++++++++++++++++++
 packages/game/src/js/types.d.ts | 16 ++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/packages/game/src/js/Game.js b/packages/game/src/js/Game.js
index 3a36f34..7b36579 100644
--- a/packages/game/src/js/Game.js
+++ b/packages/game/src/js/Game.js
@@ -73,6 +73,34 @@ export class Game
         return this.state.board;
     }
 
+    /**
+     * Dumps the state of the game.
+     *
+     * @returns {IState}
+     * The JSON string representing the state.
+     */
+    dump()
+    {
+        return {
+            turnCount: this.state.turnCount,
+            board: {
+                ...this.state.board
+            }
+        };
+    }
+
+    /**
+     * Loads the game from the specified {@link data `data`}.
+     *
+     * @param {IState} data
+     * The data to load.
+     */
+    load(data)
+    {
+        this.state.turnCount = data.turnCount;
+        Object.assign(this.state.board, data.board);
+    }
+
     /**
      * Initializes the game.
      */
diff --git a/packages/game/src/js/types.d.ts b/packages/game/src/js/types.d.ts
index 3633da2..a2a4c52 100644
--- a/packages/game/src/js/types.d.ts
+++ b/packages/game/src/js/types.d.ts
@@ -12,3 +12,19 @@ type Row = CellOwner[];
  * Represents a game board.
  */
 type Board = Row[];
+
+/**
+ * Represents the state of a game.
+ */
+interface IState
+{
+    /**
+     * The number of rounds that have been played.
+     */
+    turnCount: number;
+
+    /**
+     * The board of the game.
+     */
+    board: Board;
+}