aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/web/src/gui/utils/saveGameToDisk.js
diff options
context:
space:
mode:
Diffstat (limited to 'diplomacy/web/src/gui/utils/saveGameToDisk.js')
-rw-r--r--diplomacy/web/src/gui/utils/saveGameToDisk.js18
1 files changed, 18 insertions, 0 deletions
diff --git a/diplomacy/web/src/gui/utils/saveGameToDisk.js b/diplomacy/web/src/gui/utils/saveGameToDisk.js
new file mode 100644
index 0000000..aae69a4
--- /dev/null
+++ b/diplomacy/web/src/gui/utils/saveGameToDisk.js
@@ -0,0 +1,18 @@
+export function saveGameToDisk(game, onError) {
+ if (game.client) {
+ game.client.save()
+ .then((savedData) => {
+ const domLink = document.createElement('a');
+ domLink.setAttribute(
+ 'href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(savedData)));
+ domLink.setAttribute('download', `${game.game_id}.json`);
+ domLink.style.display = 'none';
+ document.body.appendChild(domLink);
+ domLink.click();
+ document.body.removeChild(domLink);
+ })
+ .catch(exc => onError(`Error while saving game: ${exc.toString()}`));
+ } else {
+ onError(`Cannot save this game.`);
+ }
+}