aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/web/src/gui/utils/saveGameToDisk.js
blob: aae69a43bb460ad9ac71d7a2c6ddb0d2651f65e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.`);
    }
}